| 1 |
/* |
| 2 |
* files.t - test of file I/O operations |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "tads.h" |
| 6 |
#include "t3.h" |
| 7 |
#include "bytearr.h" |
| 8 |
#include "file.h" |
| 9 |
#include "bignum.h" |
| 10 |
|
| 11 |
|
| 12 |
main(args) |
| 13 |
{ |
| 14 |
local fp; |
| 15 |
local i; |
| 16 |
|
| 17 |
/* -------------------------------------------------------------------- */ |
| 18 |
/* |
| 19 |
* text file |
| 20 |
*/ |
| 21 |
|
| 22 |
/* open a file */ |
| 23 |
try |
| 24 |
{ |
| 25 |
fp = File.openTextFile('test.txt', FileAccessWrite, 'asc7dflt'); |
| 26 |
} |
| 27 |
catch (FileException fExc) |
| 28 |
{ |
| 29 |
"Error opening file test.txt for writing: |
| 30 |
<<fExc.displayException()>>\n"; |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
/* write some data */ |
| 35 |
for (i = 0 ; i < 100 ; ++i) |
| 36 |
fp.writeFile('This is line ' + i + '!!!\n'); |
| 37 |
fp.writeFile('Some extended characters: ' |
| 38 |
+ '\u2039 \u2122 \u00A9 \u00AE \u203A\n'); |
| 39 |
|
| 40 |
/* close the file */ |
| 41 |
fp.closeFile(); |
| 42 |
|
| 43 |
/* open the file for reading */ |
| 44 |
try |
| 45 |
{ |
| 46 |
fp = File.openTextFile('test.txt', FileAccessRead, 'asc7dflt'); |
| 47 |
} |
| 48 |
catch (FileException fExc) |
| 49 |
{ |
| 50 |
"Error opening file test.txt for reading: |
| 51 |
<<fExc.displayException()>>\n"; |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
"test.txt: size = <<fp.getFileSize()>>\n"; |
| 56 |
|
| 57 |
/* read the data */ |
| 58 |
for (i = 0 ; ; ++i) |
| 59 |
{ |
| 60 |
local val; |
| 61 |
|
| 62 |
val = fp.readFile(); |
| 63 |
if (val == nil) |
| 64 |
break; |
| 65 |
"<<i>>: <<val>>\n"; |
| 66 |
} |
| 67 |
|
| 68 |
fp.closeFile(); |
| 69 |
|
| 70 |
/* -------------------------------------------------------------------- */ |
| 71 |
/* |
| 72 |
* text file read/write with seeking |
| 73 |
*/ |
| 74 |
try |
| 75 |
{ |
| 76 |
fp = File.openTextFile( |
| 77 |
'test.txt', FileAccessReadWriteKeep, 'us-ascii'); |
| 78 |
} |
| 79 |
catch (FileException fExc) |
| 80 |
{ |
| 81 |
"Error opening file test.txt for read/write/keep: |
| 82 |
<<fExc.displayException()>>\n"; |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
"test.txt: size = <<fp.getFileSize()>>\n"; |
| 87 |
|
| 88 |
/* read back the data */ |
| 89 |
local line50 = nil; |
| 90 |
for (i = 0 ; ; ++i) |
| 91 |
{ |
| 92 |
local sol = fp.getPos(); |
| 93 |
local val = fp.readFile(); |
| 94 |
if (val == nil) |
| 95 |
break; |
| 96 |
else if (val.find('line 50') != nil) |
| 97 |
line50 = sol; |
| 98 |
} |
| 99 |
|
| 100 |
/* seek back to line 50 */ |
| 101 |
"Seeking to <<line50>>\n"; |
| 102 |
fp.setPos(line50); |
| 103 |
|
| 104 |
/* re-write the remainder of the file */ |
| 105 |
for (i = 50 ; i < 75 ; ++i) |
| 106 |
fp.writeFile('This is the NEW line <<i>>!!!\n'); |
| 107 |
|
| 108 |
/* done */ |
| 109 |
fp.closeFile(); |
| 110 |
|
| 111 |
/* -------------------------------------------------------------------- */ |
| 112 |
/* |
| 113 |
* binary file |
| 114 |
*/ |
| 115 |
|
| 116 |
try |
| 117 |
{ |
| 118 |
fp = File.openDataFile('test.bin', FileAccessWrite); |
| 119 |
} |
| 120 |
catch (FileException fExc) |
| 121 |
{ |
| 122 |
"Error opening file test.bin for writing\n"; |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
/* write some data */ |
| 127 |
for (i = 0 ; i <= 100 ; i += 20) |
| 128 |
{ |
| 129 |
fp.writeFile(i); |
| 130 |
fp.writeFile('String ' + i); |
| 131 |
} |
| 132 |
|
| 133 |
/* write a couple of BigNumber values */ |
| 134 |
fp.writeFile(1.2345); |
| 135 |
fp.writeFile('BigNumber 1.2345'); |
| 136 |
|
| 137 |
fp.writeFile(new BigNumber(12345, 10).logE()); |
| 138 |
fp.writeFile('BigNumber ln(12345)'); |
| 139 |
|
| 140 |
/* write a byte array */ |
| 141 |
{ |
| 142 |
local arr = new ByteArray(20); |
| 143 |
|
| 144 |
for (local i = 1 ; i <= 20 ; ++i) |
| 145 |
arr[i] = i*5; |
| 146 |
|
| 147 |
fp.writeFile(arr); |
| 148 |
fp.writeFile('ByteArray(20)'); |
| 149 |
} |
| 150 |
|
| 151 |
/* done with the file for this round */ |
| 152 |
fp.closeFile(); |
| 153 |
|
| 154 |
/* open it for reading */ |
| 155 |
try |
| 156 |
{ |
| 157 |
fp = File.openDataFile('test.bin', FileAccessRead); |
| 158 |
} |
| 159 |
catch (FileException fExc) |
| 160 |
{ |
| 161 |
"Error opening file test.bin for reading\n"; |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
/* read the data back */ |
| 166 |
for (i = 0 ; ; ++i) |
| 167 |
{ |
| 168 |
local ival, sval; |
| 169 |
|
| 170 |
/* read the pair of values */ |
| 171 |
if ((ival = fp.readFile()) == nil || (sval = fp.readFile()) == nil) |
| 172 |
break; |
| 173 |
|
| 174 |
/* show the first value in the pair */ |
| 175 |
if (dataType(ival) == TypeObject && ival.ofKind(ByteArray)) |
| 176 |
{ |
| 177 |
"<<i>>: type ByteArray, value: ["; |
| 178 |
for (local j = 1 ; j <= ival.length() ; ++j) |
| 179 |
{ |
| 180 |
if (j > 1) ", "; |
| 181 |
"<<ival[j]>>"; |
| 182 |
} |
| 183 |
"]"; |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
"<<i>>: type <<dataType(ival)>>, value '<<ival>>'"; |
| 188 |
} |
| 189 |
|
| 190 |
/* show the second of the pair (always a string) */ |
| 191 |
"; type <<dataType(sval)>>, value '<<sval>>'\n"; |
| 192 |
} |
| 193 |
|
| 194 |
/* done with the file */ |
| 195 |
fp.closeFile(); |
| 196 |
|
| 197 |
/* -------------------------------------------------------------------- */ |
| 198 |
/* |
| 199 |
* open a file with a weird name |
| 200 |
*/ |
| 201 |
|
| 202 |
local fname = 'test\u00e4\u00eb\u00ef\u00f6\u00fc\u00ff.dat'; |
| 203 |
"Opening <<fname>> (that's 'test' + 'aeiouy' with umlauts + '.dat')\n"; |
| 204 |
fp = File.openTextFile(fname, FileAccessWrite, 'cp437'); |
| 205 |
fp.writeFile('Hello there!\n'); |
| 206 |
fp.writeFile('Filename = ' + fname + '\n'); |
| 207 |
fp.closeFile(); |
| 208 |
|
| 209 |
|
| 210 |
/* -------------------------------------------------------------------- */ |
| 211 |
/* |
| 212 |
* raw file |
| 213 |
*/ |
| 214 |
|
| 215 |
/* open a raw file for writing */ |
| 216 |
fp = File.openRawFile('test.raw', FileAccessWrite); |
| 217 |
|
| 218 |
/* write some bytes */ |
| 219 |
local arr = new ByteArray(100); |
| 220 |
for (local i = 1 ; i <= arr.length() ; ++i) |
| 221 |
arr[i] = i; |
| 222 |
|
| 223 |
fp.writeBytes(arr, 11, 10); |
| 224 |
fp.writeBytes(arr, 1, 10); |
| 225 |
fp.writeBytes(arr, 21, 10); |
| 226 |
fp.writeBytes(arr, 31); |
| 227 |
|
| 228 |
/* done with the file */ |
| 229 |
fp.closeFile(); |
| 230 |
|
| 231 |
/* -------------------------------------------------------------------- */ |
| 232 |
/* |
| 233 |
* done |
| 234 |
*/ |
| 235 |
"Done!\n"; |
| 236 |
} |
| 237 |
|