diff --git a/src/com/annimon/ownlang/lib/modules/files.java b/src/com/annimon/ownlang/lib/modules/files.java index 68b8c35..475a58a 100644 --- a/src/com/annimon/ownlang/lib/modules/files.java +++ b/src/com/annimon/ownlang/lib/modules/files.java @@ -94,10 +94,11 @@ public final class files implements Module { DataOutputStream dos = null; BufferedWriter writer = null; + final boolean append = mode.contains("+"); if (mode.contains("wb")) { - dos = new DataOutputStream(new FileOutputStream(file)); + dos = new DataOutputStream(new FileOutputStream(file, append)); } else if (mode.contains("w")) { - writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), "UTF-8")); } final int key = files.size(); @@ -115,7 +116,7 @@ public final class files implements Module { try { return execute(files.get(key), args); } catch (IOException ioe) { - return NumberValue.ZERO; + return NumberValue.MINUS_ONE; } } @@ -168,8 +169,6 @@ public final class files implements Module { @Override protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { final File dest = files.get(args[1].asInt()).file; - System.out.println(fileInfo.file); - System.out.println(dest); return NumberValue.fromBoolean(fileInfo.file.renameTo(dest)); } } @@ -367,7 +366,13 @@ public final class files implements Module { private static class writeLong extends FileFunction { @Override protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { - fileInfo.dos.writeLong((long) args[1].asNumber()); + final long value; + if (args[1].type() == Types.NUMBER) { + value = ((NumberValue)args[1]).asLong(); + } else { + value = (long) args[1].asNumber(); + } + fileInfo.dos.writeLong(value); return NumberValue.ONE; } } @@ -375,7 +380,13 @@ public final class files implements Module { private static class writeFloat extends FileFunction { @Override protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { - fileInfo.dos.writeFloat((float) args[1].asNumber()); + final float value; + if (args[1].type() == Types.NUMBER) { + value = ((NumberValue)args[1]).asFloat(); + } else { + value = (float) args[1].asNumber(); + } + fileInfo.dos.writeFloat(value); return NumberValue.ONE; } } diff --git a/tests.own b/tests.own index 620ac4c..862f935 100644 --- a/tests.own +++ b/tests.own @@ -1,6 +1,8 @@ use "ounit" +use "types" use "functional" use "date" +use "files" def testAdditionOnNumbers() { assertEquals(6, 0 + 1 + 2 + 3) @@ -68,13 +70,6 @@ def testFunctionalChain() { assertEquals([8,6,4,2], result) } -def testDateParse() { - d = parseDate("2016/05/10", newFormat("yyyy/MM/dd")) - assertEquals(2016, d.year) - assertEquals(4, d.month) - assertEquals(10, d.day) - assertEquals(0, d.hour) -} // --- Date def testNewDate() { @@ -107,4 +102,30 @@ def testDateParse() { assertEquals(0, d.hour) } +// --- Files +def testFiles() { + // writeLong + f = fopen("test.file", "wb") + writeLong(f, 1002003004005006007) + flush(f) + fclose(f) + + // append & writeFloat + fpNumber = 100200.3004005006007 + f = fopen("test.file", "wb+") + writeFloat(f, fpNumber) + flush(f) + fclose(f) + + f = fopen("test.file", "rb") + assertEquals(1002003004005006007, readLong(f)) + assertEquals(float(fpNumber), readFloat(f)) + assertEquals(-1, readInt(f)) // EOF + fclose(f) + + f = fopen("test.file", "i") + delete(f) + fclose(f) +} + println runTests() \ No newline at end of file