|
| 1 | +package com.annimon.ownlang.lib.modules; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.ArgumentsMismatchException; |
| 4 | +import com.annimon.ownlang.lib.*; |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.BufferedWriter; |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.DataInputStream; |
| 9 | +import java.io.DataOutputStream; |
| 10 | +import java.io.File; |
| 11 | +import java.io.FileInputStream; |
| 12 | +import java.io.FileOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStreamReader; |
| 15 | +import java.io.OutputStreamWriter; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * @author aNNiMON |
| 22 | + */ |
| 23 | +public final class files implements Module { |
| 24 | + |
| 25 | + private static Map<Integer, FileInfo> files; |
| 26 | + |
| 27 | + @Override |
| 28 | + public void init() { |
| 29 | + files = new HashMap<>(); |
| 30 | + |
| 31 | + Functions.set("fopen", new fopen()); |
| 32 | + Functions.set("readBoolean", new readBoolean()); |
| 33 | + Functions.set("readByte", new readByte()); |
| 34 | + Functions.set("readBytes", new readBytes()); |
| 35 | + Functions.set("readAllBytes", new readAllBytes()); |
| 36 | + Functions.set("readChar", new readChar()); |
| 37 | + Functions.set("readShort", new readShort()); |
| 38 | + Functions.set("readInt", new readInt()); |
| 39 | + Functions.set("readLong", new readLong()); |
| 40 | + Functions.set("readFloat", new readFloat()); |
| 41 | + Functions.set("readDouble", new readDouble()); |
| 42 | + Functions.set("readUTF", new readUTF()); |
| 43 | + Functions.set("readLine", new readLine()); |
| 44 | + Functions.set("readText", new readText()); |
| 45 | + Functions.set("writeBoolean", new writeBoolean()); |
| 46 | + Functions.set("writeByte", new writeByte()); |
| 47 | + Functions.set("writeChar", new writeChar()); |
| 48 | + Functions.set("writeShort", new writeShort()); |
| 49 | + Functions.set("writeInt", new writeInt()); |
| 50 | + Functions.set("writeLong", new writeLong()); |
| 51 | + Functions.set("writeFloat", new writeFloat()); |
| 52 | + Functions.set("writeDouble", new writeDouble()); |
| 53 | + Functions.set("writeUTF", new writeUTF()); |
| 54 | + Functions.set("writeLine", new writeLine()); |
| 55 | + Functions.set("flush", new flush()); |
| 56 | + Functions.set("fclose", new fclose()); |
| 57 | + } |
| 58 | + |
| 59 | + private static class fopen implements Function { |
| 60 | + |
| 61 | + @Override |
| 62 | + public Value execute(Value... args) { |
| 63 | + if (args.length < 1) throw new ArgumentsMismatchException("At least one argument expected"); |
| 64 | + |
| 65 | + final File file = new File(args[0].asString()); |
| 66 | + try { |
| 67 | + if (args.length > 1) { |
| 68 | + return process(file, args[1].asString().toLowerCase()); |
| 69 | + } |
| 70 | + return process(file, "r"); |
| 71 | + } catch (IOException ioe) { |
| 72 | + return new NumberValue(-1); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private Value process(File file, String mode) throws IOException { |
| 77 | + DataInputStream dis = null; |
| 78 | + BufferedReader reader = null; |
| 79 | + if (mode.contains("rb")) { |
| 80 | + dis = new DataInputStream(new FileInputStream(file)); |
| 81 | + } else if (mode.contains("r")) { |
| 82 | + reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); |
| 83 | + } |
| 84 | + |
| 85 | + DataOutputStream dos = null; |
| 86 | + BufferedWriter writer = null; |
| 87 | + if (mode.contains("wb")) { |
| 88 | + dos = new DataOutputStream(new FileOutputStream(file)); |
| 89 | + } else if (mode.contains("w")) { |
| 90 | + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); |
| 91 | + } |
| 92 | + |
| 93 | + final int key = files.size(); |
| 94 | + files.put(key, new FileInfo(file, dis, dos, reader, writer)); |
| 95 | + return new NumberValue(key); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static abstract class FileFunction implements Function { |
| 100 | + |
| 101 | + @Override |
| 102 | + public Value execute(Value... args) { |
| 103 | + if (args.length < 1) throw new ArgumentsMismatchException("File descriptor expected"); |
| 104 | + final int key = (int) args[0].asNumber(); |
| 105 | + try { |
| 106 | + return execute(files.get(key), args); |
| 107 | + } catch (IOException ioe) { |
| 108 | + return NumberValue.ZERO; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + protected abstract Value execute(FileInfo fileInfo, Value[] args) throws IOException; |
| 113 | + } |
| 114 | + |
| 115 | + private static class readBoolean extends FileFunction { |
| 116 | + @Override |
| 117 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 118 | + return new NumberValue(fileInfo.dis.readBoolean() ? 1 : 0); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static class readByte extends FileFunction { |
| 123 | + @Override |
| 124 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 125 | + return new NumberValue(fileInfo.dis.readByte()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static class readBytes extends FileFunction { |
| 130 | + @Override |
| 131 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 132 | + final ArrayValue array = (ArrayValue) args[1]; |
| 133 | + int offset = 0, length = array.size(); |
| 134 | + if (args.length > 3) { |
| 135 | + offset = (int) args[2].asNumber(); |
| 136 | + length = (int) args[3].asNumber(); |
| 137 | + } |
| 138 | + |
| 139 | + final byte[] buffer = new byte[length]; |
| 140 | + final int readed = fileInfo.dis.read(buffer, offset, length); |
| 141 | + for (int i = 0; i < readed; i++) { |
| 142 | + array.set(i, new NumberValue(buffer[i])); |
| 143 | + } |
| 144 | + return new NumberValue(readed); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static class readAllBytes extends FileFunction { |
| 149 | + @Override |
| 150 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 151 | + final int bufferSize = 4096; |
| 152 | + final byte[] buffer = new byte[bufferSize]; |
| 153 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 154 | + int readed; |
| 155 | + while ((readed = fileInfo.dis.read(buffer, 0, bufferSize)) != -1) { |
| 156 | + baos.write(buffer, 0, readed); |
| 157 | + } |
| 158 | + baos.flush(); |
| 159 | + baos.close(); |
| 160 | + final byte[] bytes = baos.toByteArray(); |
| 161 | + final int size = bytes.length; |
| 162 | + final ArrayValue result = new ArrayValue(size); |
| 163 | + for (int i = 0; i < size; i++) { |
| 164 | + result.set(i, new NumberValue(bytes[i])); |
| 165 | + } |
| 166 | + return result; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static class readChar extends FileFunction { |
| 171 | + @Override |
| 172 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 173 | + return new NumberValue(fileInfo.dis.readChar()); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private static class readShort extends FileFunction { |
| 178 | + @Override |
| 179 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 180 | + return new NumberValue(fileInfo.dis.readShort()); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private static class readInt extends FileFunction { |
| 185 | + @Override |
| 186 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 187 | + return new NumberValue(fileInfo.dis.readInt()); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private static class readLong extends FileFunction { |
| 192 | + @Override |
| 193 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 194 | + return new NumberValue(fileInfo.dis.readLong()); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private static class readFloat extends FileFunction { |
| 199 | + @Override |
| 200 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 201 | + return new NumberValue(fileInfo.dis.readFloat()); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private static class readDouble extends FileFunction { |
| 206 | + @Override |
| 207 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 208 | + return new NumberValue(fileInfo.dis.readDouble()); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + private static class readUTF extends FileFunction { |
| 213 | + @Override |
| 214 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 215 | + return new StringValue(fileInfo.dis.readUTF()); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private static class readLine extends FileFunction { |
| 220 | + @Override |
| 221 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 222 | + return new StringValue(fileInfo.reader.readLine()); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + private static class readText extends FileFunction { |
| 227 | + @Override |
| 228 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 229 | + final StringBuilder sb = new StringBuilder(); |
| 230 | + int ch; |
| 231 | + while ((ch = fileInfo.reader.read()) != -1) { |
| 232 | + sb.append((char) ch); |
| 233 | + } |
| 234 | + return new StringValue(sb.toString()); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + |
| 239 | + private static class writeBoolean extends FileFunction { |
| 240 | + @Override |
| 241 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 242 | + fileInfo.dos.writeBoolean(args[1].asNumber() != 0); |
| 243 | + return NumberValue.ONE; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private static class writeByte extends FileFunction { |
| 248 | + @Override |
| 249 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 250 | + fileInfo.dos.writeByte((byte) args[1].asNumber()); |
| 251 | + return NumberValue.ONE; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + private static class writeChar extends FileFunction { |
| 256 | + @Override |
| 257 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 258 | + final char ch = (args[1].type() == Types.NUMBER) |
| 259 | + ? ((char) args[1].asNumber()) |
| 260 | + : args[1].asString().charAt(0); |
| 261 | + fileInfo.dos.writeChar(ch); |
| 262 | + return NumberValue.ONE; |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + private static class writeShort extends FileFunction { |
| 267 | + @Override |
| 268 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 269 | + fileInfo.dos.writeShort((short) args[1].asNumber()); |
| 270 | + return NumberValue.ONE; |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + private static class writeInt extends FileFunction { |
| 275 | + @Override |
| 276 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 277 | + fileInfo.dos.writeInt((int) args[1].asNumber()); |
| 278 | + return NumberValue.ONE; |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + private static class writeLong extends FileFunction { |
| 283 | + @Override |
| 284 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 285 | + fileInfo.dos.writeLong((long) args[1].asNumber()); |
| 286 | + return NumberValue.ONE; |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + private static class writeFloat extends FileFunction { |
| 291 | + @Override |
| 292 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 293 | + fileInfo.dos.writeFloat((float) args[1].asNumber()); |
| 294 | + return NumberValue.ONE; |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + private static class writeDouble extends FileFunction { |
| 299 | + @Override |
| 300 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 301 | + fileInfo.dos.writeDouble(args[1].asNumber()); |
| 302 | + return NumberValue.ONE; |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + private static class writeUTF extends FileFunction { |
| 307 | + @Override |
| 308 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 309 | + fileInfo.dos.writeUTF(args[1].asString()); |
| 310 | + return NumberValue.ONE; |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + private static class writeLine extends FileFunction { |
| 315 | + @Override |
| 316 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 317 | + fileInfo.dos.writeDouble(args[1].asNumber()); |
| 318 | + return NumberValue.ONE; |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + private static class flush extends FileFunction { |
| 323 | + @Override |
| 324 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 325 | + if (fileInfo.dos != null) { |
| 326 | + fileInfo.dos.flush(); |
| 327 | + } |
| 328 | + if (fileInfo.writer != null) { |
| 329 | + fileInfo.writer.flush(); |
| 330 | + } |
| 331 | + return NumberValue.ONE; |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + private static class fclose extends FileFunction { |
| 336 | + @Override |
| 337 | + protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { |
| 338 | + if (fileInfo.dis != null) { |
| 339 | + fileInfo.dis.close(); |
| 340 | + } |
| 341 | + if (fileInfo.dos != null) { |
| 342 | + fileInfo.dos.close(); |
| 343 | + } |
| 344 | + if (fileInfo.reader != null) { |
| 345 | + fileInfo.reader.close(); |
| 346 | + } |
| 347 | + if (fileInfo.writer != null) { |
| 348 | + fileInfo.writer.close(); |
| 349 | + } |
| 350 | + return NumberValue.ONE; |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + private static class FileInfo { |
| 355 | + File file; |
| 356 | + DataInputStream dis; |
| 357 | + DataOutputStream dos; |
| 358 | + BufferedReader reader; |
| 359 | + BufferedWriter writer; |
| 360 | + |
| 361 | + public FileInfo(File file, DataInputStream dis, DataOutputStream dos, BufferedReader reader, BufferedWriter writer) { |
| 362 | + this.file = file; |
| 363 | + this.dis = dis; |
| 364 | + this.dos = dos; |
| 365 | + this.reader = reader; |
| 366 | + this.writer = writer; |
| 367 | + } |
| 368 | + } |
| 369 | +} |
0 commit comments