Skip to content

Commit 50866ae

Browse files
committed
Исправления в модуле files.
Исправлены функции writeLong, writeFloat При достижении конца файла теперь возвращается -1 Добавлена возможность дозаписи в файл
1 parent 4236fee commit 50866ae

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/com/annimon/ownlang/lib/modules/files.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ private Value process(File file, String mode) throws IOException {
9494

9595
DataOutputStream dos = null;
9696
BufferedWriter writer = null;
97+
final boolean append = mode.contains("+");
9798
if (mode.contains("wb")) {
98-
dos = new DataOutputStream(new FileOutputStream(file));
99+
dos = new DataOutputStream(new FileOutputStream(file, append));
99100
} else if (mode.contains("w")) {
100-
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
101+
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), "UTF-8"));
101102
}
102103

103104
final int key = files.size();
@@ -115,7 +116,7 @@ public Value execute(Value... args) {
115116
try {
116117
return execute(files.get(key), args);
117118
} catch (IOException ioe) {
118-
return NumberValue.ZERO;
119+
return NumberValue.MINUS_ONE;
119120
}
120121
}
121122

@@ -168,8 +169,6 @@ private static class rename extends FileFunction {
168169
@Override
169170
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
170171
final File dest = files.get(args[1].asInt()).file;
171-
System.out.println(fileInfo.file);
172-
System.out.println(dest);
173172
return NumberValue.fromBoolean(fileInfo.file.renameTo(dest));
174173
}
175174
}
@@ -367,15 +366,27 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
367366
private static class writeLong extends FileFunction {
368367
@Override
369368
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
370-
fileInfo.dos.writeLong((long) args[1].asNumber());
369+
final long value;
370+
if (args[1].type() == Types.NUMBER) {
371+
value = ((NumberValue)args[1]).asLong();
372+
} else {
373+
value = (long) args[1].asNumber();
374+
}
375+
fileInfo.dos.writeLong(value);
371376
return NumberValue.ONE;
372377
}
373378
}
374379

375380
private static class writeFloat extends FileFunction {
376381
@Override
377382
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
378-
fileInfo.dos.writeFloat((float) args[1].asNumber());
383+
final float value;
384+
if (args[1].type() == Types.NUMBER) {
385+
value = ((NumberValue)args[1]).asFloat();
386+
} else {
387+
value = (float) args[1].asNumber();
388+
}
389+
fileInfo.dos.writeFloat(value);
379390
return NumberValue.ONE;
380391
}
381392
}

tests.own

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use "ounit"
2+
use "types"
23
use "functional"
34
use "date"
5+
use "files"
46

57
def testAdditionOnNumbers() {
68
assertEquals(6, 0 + 1 + 2 + 3)
@@ -68,13 +70,6 @@ def testFunctionalChain() {
6870
assertEquals([8,6,4,2], result)
6971
}
7072

71-
def testDateParse() {
72-
d = parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
73-
assertEquals(2016, d.year)
74-
assertEquals(4, d.month)
75-
assertEquals(10, d.day)
76-
assertEquals(0, d.hour)
77-
}
7873

7974
// --- Date
8075
def testNewDate() {
@@ -107,4 +102,30 @@ def testDateParse() {
107102
assertEquals(0, d.hour)
108103
}
109104

105+
// --- Files
106+
def testFiles() {
107+
// writeLong
108+
f = fopen("test.file", "wb")
109+
writeLong(f, 1002003004005006007)
110+
flush(f)
111+
fclose(f)
112+
113+
// append & writeFloat
114+
fpNumber = 100200.3004005006007
115+
f = fopen("test.file", "wb+")
116+
writeFloat(f, fpNumber)
117+
flush(f)
118+
fclose(f)
119+
120+
f = fopen("test.file", "rb")
121+
assertEquals(1002003004005006007, readLong(f))
122+
assertEquals(float(fpNumber), readFloat(f))
123+
assertEquals(-1, readInt(f)) // EOF
124+
fclose(f)
125+
126+
f = fopen("test.file", "i")
127+
delete(f)
128+
fclose(f)
129+
}
130+
110131
println runTests()

0 commit comments

Comments
 (0)