| | 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 | * binary file |
| | 73 | */ |
| | 74 | |
| | 75 | try |
| | 76 | { |
| | 77 | fp = File.openDataFile('test.bin', FileAccessWrite); |
| | 78 | } |
| | 79 | catch (FileException fExc) |
| | 80 | { |
| | 81 | "Error opening file test.bin for writing\n"; |
| | 82 | return; |
| | 83 | } |
| | 84 | |
| | 85 | /* write some data */ |
| | 86 | for (i = 0 ; i <= 100 ; i += 20) |
| | 87 | { |
| | 88 | fp.writeFile(i); |
| | 89 | fp.writeFile('String ' + i); |
| | 90 | } |
| | 91 | |
| | 92 | /* write a couple of BigNumber values */ |
| | 93 | fp.writeFile(1.2345); |
| | 94 | fp.writeFile('BigNumber 1.2345'); |
| | 95 | |
| | 96 | fp.writeFile(new BigNumber(12345, 10).logE()); |
| | 97 | fp.writeFile('BigNumber ln(12345)'); |
| | 98 | |
| | 99 | /* write a byte array */ |
| | 100 | { |
| | 101 | local arr = new ByteArray(20); |
| | 102 | |
| | 103 | for (local i = 1 ; i <= 20 ; ++i) |
| | 104 | arr[i] = i*5; |
| | 105 | |
| | 106 | fp.writeFile(arr); |
| | 107 | fp.writeFile('ByteArray(20)'); |
| | 108 | } |
| | 109 | |
| | 110 | /* done with the file for this round */ |
| | 111 | fp.closeFile(); |
| | 112 | |
| | 113 | /* open it for reading */ |
| | 114 | try |
| | 115 | { |
| | 116 | fp = File.openDataFile('test.bin', FileAccessRead); |
| | 117 | } |
| | 118 | catch (FileException fExc) |
| | 119 | { |
| | 120 | "Error opening file test.bin for reading\n"; |
| | 121 | return; |
| | 122 | } |
| | 123 | |
| | 124 | /* read the data back */ |
| | 125 | for (i = 0 ; ; ++i) |
| | 126 | { |
| | 127 | local ival, sval; |
| | 128 | |
| | 129 | /* read the pair of values */ |
| | 130 | if ((ival = fp.readFile()) == nil || (sval = fp.readFile()) == nil) |
| | 131 | break; |
| | 132 | |
| | 133 | /* show the first value in the pair */ |
| | 134 | if (dataType(ival) == TypeObject && ival.ofKind(ByteArray)) |
| | 135 | { |
| | 136 | "<<i>>: type ByteArray, value: ["; |
| | 137 | for (local j = 1 ; j <= ival.length() ; ++j) |
| | 138 | { |
| | 139 | if (j > 1) ", "; |
| | 140 | "<<ival[j]>>"; |
| | 141 | } |
| | 142 | "]"; |
| | 143 | } |
| | 144 | else |
| | 145 | { |
| | 146 | "<<i>>: type <<dataType(ival)>>, value '<<ival>>'"; |
| | 147 | } |
| | 148 | |
| | 149 | /* show the second of the pair (always a string) */ |
| | 150 | "; type <<dataType(sval)>>, value '<<sval>>'\n"; |
| | 151 | } |
| | 152 | |
| | 153 | /* done with the file */ |
| | 154 | fp.closeFile(); |
| | 155 | |
| | 156 | /* -------------------------------------------------------------------- */ |
| | 157 | /* |
| | 158 | * open a file with a weird name |
| | 159 | */ |
| | 160 | |
| | 161 | local fname = 'test\u00e4\u00eb\u00ef\u00f6\u00fc\u00ff.dat'; |
| | 162 | "Opening <<fname>> (that's 'test' + 'aeiouy' with umlauts + '.dat')\n"; |
| | 163 | fp = File.openTextFile(fname, FileAccessWrite, 'cp437'); |
| | 164 | fp.writeFile('Hello there!\n'); |
| | 165 | fp.writeFile('Filename = ' + fname + '\n'); |
| | 166 | fp.closeFile(); |
| | 167 | |
| | 168 | |
| | 169 | /* -------------------------------------------------------------------- */ |
| | 170 | /* |
| | 171 | * raw file |
| | 172 | */ |
| | 173 | |
| | 174 | /* open a raw file for writing */ |
| | 175 | fp = File.openRawFile('test.raw', FileAccessWrite); |
| | 176 | |
| | 177 | /* write some bytes */ |
| | 178 | local arr = new ByteArray(100); |
| | 179 | for (local i = 1 ; i <= arr.length() ; ++i) |
| | 180 | arr[i] = i; |
| | 181 | |
| | 182 | fp.writeBytes(arr, 11, 10); |
| | 183 | fp.writeBytes(arr, 1, 10); |
| | 184 | fp.writeBytes(arr, 21, 10); |
| | 185 | fp.writeBytes(arr, 31); |
| | 186 | |
| | 187 | /* done with the file */ |
| | 188 | fp.closeFile(); |
| | 189 | |
| | 190 | /* -------------------------------------------------------------------- */ |
| | 191 | /* |
| | 192 | * done |
| | 193 | */ |
| | 194 | "Done!\n"; |
| | 195 | } |
| | 196 | |