Streams.Mod 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  2. MODULE Streams; (** AUTHOR "pjm/be"; PURPOSE "I/O buffering and formatted writing and reading"; *)
  3. IMPORT SYSTEM, RC := RealConversions;
  4. CONST
  5. Ok* = 0; (** zero result code means no error occurred *)
  6. EOF* = 4201; (** error returned when Receive reads past end of file or stream *)
  7. EOT* = 1AX; (** EOT character *)
  8. StringFull = 4202;
  9. FormatError* = 4203; (** error returned when ReadInt fails *)
  10. DefaultWriterSize* = 4096;
  11. DefaultReaderSize* = 4096;
  12. Invalid* = -1; (** invalid stream position *)
  13. CONST
  14. CR = 0DX; LF = 0AX; TAB = 9X; SP = 20X;
  15. TYPE
  16. BufferOffset* = LONGINT; (* offset in the stream biffer *)
  17. TYPE
  18. Position* = LONGINT; (* position in the stream *)
  19. StreamSize* = LONGINT; (* size of the stream *)
  20. (** Any stream output procedure or method. *)
  21. Sender* = PROCEDURE {DELEGATE} ( CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: WORD );
  22. (** Any stream input procedure or method. *)
  23. Receiver* = PROCEDURE {DELEGATE} ( VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len: LONGINT; VAR res: WORD );
  24. Connection* = OBJECT
  25. PROCEDURE Send*( CONST data: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: WORD );
  26. END Send;
  27. PROCEDURE Receive*( VAR data: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len: LONGINT; VAR res: WORD );
  28. END Receive;
  29. PROCEDURE Close*;
  30. END Close;
  31. END Connection;
  32. TYPE
  33. (** A writer buffers output before it is sent to a Sender. Must not be shared between processes. *)
  34. Writer* = OBJECT
  35. VAR
  36. tail: LONGINT;
  37. buf: POINTER TO ARRAY OF CHAR;
  38. res*: WORD; (** result of last output operation. *)
  39. send: Sender;
  40. sent*: LONGINT; (** count of sent bytes *)
  41. (* buf[0..tail-1] contains data to write. *)
  42. PROCEDURE & InitWriter*( send: Sender; size: LONGINT );
  43. BEGIN
  44. ASSERT ( send # NIL );
  45. IF (buf = NIL) OR (LEN(buf) # size) THEN
  46. NEW( buf, size );
  47. END;
  48. SELF.send := send; Reset
  49. END InitWriter;
  50. PROCEDURE Reset*;
  51. BEGIN
  52. tail := 0; res := Ok; sent := 0
  53. END Reset;
  54. PROCEDURE CanSetPos*( ): BOOLEAN;
  55. BEGIN
  56. RETURN FALSE
  57. END CanSetPos;
  58. PROCEDURE SetPos*( pos: Position );
  59. BEGIN
  60. HALT( 1234 )
  61. END SetPos;
  62. PROCEDURE Update*;
  63. BEGIN
  64. IF (res = Ok) THEN
  65. send( buf^, 0, tail, TRUE , res );
  66. IF res = Ok THEN INC( sent, tail ); tail := 0 END
  67. END
  68. END Update;
  69. (** Current write position. *)
  70. PROCEDURE Pos*( ): Position;
  71. BEGIN
  72. RETURN sent + tail
  73. END Pos;
  74. (** -- Write raw binary data -- *)
  75. (** Write one byte. *)
  76. PROCEDURE Char*( x: CHAR );
  77. BEGIN
  78. IF (tail = LEN( buf )) & (res = Ok) THEN
  79. send( buf^, 0, tail, FALSE , res );
  80. IF res = Ok THEN INC( sent, tail ); tail := 0 END
  81. END;
  82. IF res = Ok THEN buf[tail] := x; INC( tail ) END
  83. END Char;
  84. (** Write len bytes from x, starting at ofs. *)
  85. PROCEDURE Bytes*(CONST x: ARRAY OF CHAR; ofs, len: LONGINT );
  86. VAR n: LONGINT;
  87. BEGIN
  88. ASSERT ( len >= 0 );
  89. LOOP
  90. n := LEN( buf ) - tail; (* space available *)
  91. IF n = 0 THEN
  92. IF res = Ok THEN (* send current buffer *)
  93. send( buf^, 0, tail, FALSE , res );
  94. IF res = Ok THEN INC( sent, tail ); tail := 0 ELSE EXIT END
  95. ELSE
  96. EXIT (* should not be writing on an erroneous rider *)
  97. END;
  98. n := LEN( buf )
  99. END;
  100. IF n > len THEN n := len END;
  101. ASSERT ( tail + n <= LEN( buf ) ); (* index check *)
  102. SYSTEM.MOVE( ADDRESSOF( x[ofs] ), ADDRESSOF( buf[tail] ), n ); INC( tail, n );
  103. IF len = n THEN EXIT END; (* done *)
  104. INC( ofs, n ); DEC( len, n )
  105. END
  106. END Bytes;
  107. (** Write a SHORTINT. *)
  108. PROCEDURE RawSInt*( x: SHORTINT );
  109. BEGIN
  110. Char( SYSTEM.VAL( CHAR, x ) )
  111. END RawSInt;
  112. (** Write an INTEGER. *)
  113. PROCEDURE RawInt*( x: INTEGER );
  114. BEGIN
  115. Bytes( SYSTEM.VAL( Bytes2, x ), 0, 2 )
  116. END RawInt;
  117. (** Write a LONGINT. *)
  118. PROCEDURE RawLInt*( x: LONGINT );
  119. BEGIN
  120. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4 )
  121. END RawLInt;
  122. (** Write a HUGEINT. *)
  123. PROCEDURE RawHInt*( x: HUGEINT );
  124. BEGIN
  125. Bytes( SYSTEM.VAL( Bytes8, x ), 0, 8 )
  126. END RawHInt;
  127. (** Write a 64 bit value in network byte order (most significant byte first) *)
  128. PROCEDURE Net64*( x: HUGEINT );
  129. BEGIN
  130. Net32(LONGINT( x DIV 100000000H MOD 100000000H ));
  131. Net32(LONGINT( x MOD 100000000H ));
  132. END Net64;
  133. (** Write a 32 bit value in network byte order (most significant byte first) *)
  134. PROCEDURE Net32*( x: LONGINT );
  135. BEGIN
  136. Char( CHR( x DIV 1000000H MOD 100H ) ); Char( CHR( x DIV 10000H MOD 100H ) ); Char( CHR( x DIV 100H MOD 100H ) );
  137. Char( CHR( x MOD 100H ) )
  138. END Net32;
  139. (** Write a 16 bit value in network byte order (most significant byte first) *)
  140. PROCEDURE Net16*( x: LONGINT );
  141. BEGIN
  142. Char( CHR( x DIV 100H MOD 100H ) ); Char( CHR( x MOD 100H ) )
  143. END Net16;
  144. (** write unsigned byte *)
  145. PROCEDURE Net8*( x: LONGINT );
  146. BEGIN
  147. Char( CHR( x MOD 100H ) )
  148. END Net8;
  149. (** Write a SET. *)
  150. PROCEDURE RawSet*( x: SET );
  151. BEGIN
  152. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4 )
  153. END RawSet;
  154. (** Write a BOOLEAN. *)
  155. PROCEDURE RawBool*( x: BOOLEAN );
  156. BEGIN
  157. IF x THEN Char( 1X ) ELSE Char( 0X ) END
  158. END RawBool;
  159. (** Write a REAL. *)
  160. PROCEDURE RawReal*( x: REAL );
  161. BEGIN
  162. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4 )
  163. END RawReal;
  164. (** Write a LONGREAL. *)
  165. PROCEDURE RawLReal*( x: LONGREAL );
  166. BEGIN
  167. Bytes( SYSTEM.VAL( Bytes8, x ), 0, 8 )
  168. END RawLReal;
  169. (** Write a 0X-terminated string, including the 0X terminator. *)
  170. PROCEDURE RawString*(CONST x: ARRAY OF CHAR );
  171. VAR i: LONGINT;
  172. BEGIN
  173. i := 0;
  174. WHILE x[i] # 0X DO Char( x[i] ); INC( i ) END;
  175. Char( 0X )
  176. END RawString;
  177. (** Write a number in a compressed format. *)
  178. PROCEDURE RawNum*( x: HUGEINT );
  179. BEGIN
  180. WHILE (x < -64) OR (x > 63) DO Char( CHR( x MOD 128 + 128 ) ); x := x DIV 128 END;
  181. Char( CHR( x MOD 128 ) )
  182. END RawNum;
  183. (** -- Write formatted data -- *)
  184. (** Write an ASCII end-of-line (CR/LF). *)
  185. PROCEDURE Ln*;
  186. BEGIN
  187. Char( CR ); Char( LF )
  188. END Ln;
  189. (** Write a 0X-terminated string, excluding the 0X terminator. *)
  190. PROCEDURE String*(CONST x: ARRAY OF CHAR );
  191. VAR i: LONGINT;
  192. BEGIN
  193. i := 0;
  194. WHILE (i<LEN(x)) & (x[i] # 0X) DO Char( x[i] ); INC( i ) END
  195. END String;
  196. (** Write an integer in decimal right-justified in a field of at least w characters. *)
  197. PROCEDURE Int*( x: HUGEINT; w: SIZE );
  198. VAR i: SIZE; x0: HUGEINT;
  199. a: ARRAY 21 OF CHAR;
  200. BEGIN
  201. IF x < 0 THEN
  202. IF x = MIN( HUGEINT ) THEN
  203. DEC( w, 20 );
  204. WHILE w > 0 DO Char( " " ); DEC( w ) END;
  205. String( "-9223372036854775808" ); RETURN
  206. ELSE DEC( w ); x0 := -x
  207. END
  208. ELSE x0 := x
  209. END;
  210. i := 0;
  211. REPEAT a[i] := CHR( x0 MOD 10 + 30H ); x0 := x0 DIV 10; INC( i ) UNTIL x0 = 0;
  212. WHILE w > i DO Char( " " ); DEC( w ) END;
  213. IF x < 0 THEN Char( "-" ) END;
  214. REPEAT DEC( i ); Char( a[i] ) UNTIL i = 0
  215. END Int;
  216. (** Write a SET in Oberon notation. *)
  217. (* PROCEDURE Set*( s: SET ); (* from P. Saladin *)
  218. VAR i, last: LONGINT; dots: BOOLEAN;
  219. BEGIN
  220. Char( "{" ); last := MIN( LONGINT ); dots := FALSE;
  221. FOR i := MIN( SET ) TO MAX( SET ) DO
  222. IF i IN s THEN
  223. IF last = (i - 1) THEN
  224. IF dots THEN String( ".." ); dots := FALSE END;
  225. IF (i = MAX( SET )) OR ~((i + 1) IN s) THEN Int( i, 1 ) END
  226. ELSE
  227. IF last >= MIN( SET ) THEN String( ", " ) END;
  228. Int( i, 1 ); dots := TRUE
  229. END;
  230. last := i
  231. END
  232. END;
  233. Char( "}" )
  234. END Set; *)
  235. PROCEDURE Set*( s: SET ); (* from P. Saladin *)
  236. VAR i, last: LONGINT; dots: BOOLEAN;
  237. BEGIN
  238. Char( "{" ); last := MAX( LONGINT ); dots := FALSE;
  239. FOR i := MAX( SET ) TO 0 BY -1 DO
  240. IF i IN s THEN
  241. IF last = (i + 1) THEN
  242. IF dots THEN String( ".." ); dots := FALSE END;
  243. IF (i = 0) OR ~((i - 1) IN s) THEN Int( i, 1 ) END
  244. ELSE
  245. IF last <= MAX( SET ) THEN String( ", " ) END;
  246. Int( i, 1 ); dots := TRUE
  247. END;
  248. last := i
  249. END
  250. END;
  251. Char( "}" )
  252. END Set;
  253. (**
  254. Write an integer in hexadecimal right-justified in a field of at least ABS(w) characters.
  255. If w < 0 THEN w least significant hex digits of x are written (potentially including leading zeros)
  256. *)
  257. PROCEDURE Hex*(x: HUGEINT; w: SIZE);
  258. VAR filler: CHAR; i,maxw: SIZE; a: ARRAY 20 OF CHAR; y: HUGEINT;
  259. BEGIN
  260. IF w < 0 THEN filler := '0'; w := -w; maxw := w ELSE filler := ' '; maxw := 16 END;
  261. i := 0;
  262. REPEAT
  263. y := x MOD 10H;
  264. IF y < 10 THEN a[i] := CHR(y+ORD('0')) ELSE a[i] := CHR(y-10+ORD('A')) END;
  265. x := x DIV 10H;
  266. INC(i);
  267. UNTIL (x=0) OR (i=maxw);
  268. WHILE w > i DO Char(filler); DEC( w ) END;
  269. REPEAT DEC( i ); Char( a[i] ) UNTIL i = 0
  270. END Hex;
  271. (** Write "x" as a hexadecimal address. Do not use Hex because of arithmetic shift of the sign !*)
  272. PROCEDURE Address* (x: ADDRESS);
  273. BEGIN
  274. Hex(x,-2*SIZEOF(ADDRESS));
  275. END Address;
  276. (** Write "x" as a size. *)
  277. PROCEDURE Size* (x: SIZE);
  278. BEGIN
  279. Int(x, 0);
  280. END Size;
  281. PROCEDURE Pair( ch: CHAR; x: LONGINT );
  282. BEGIN
  283. IF ch # 0X THEN Char( ch ) END;
  284. Char( CHR( ORD( "0" ) + x DIV 10 MOD 10 ) ); Char( CHR( ORD( "0" ) + x MOD 10 ) )
  285. END Pair;
  286. (** Write the date and time in ISO format (yyyy-mm-dd hh:mm:ss). The t and d parameters are in Oberon time and date format.
  287. If all parameters are within range, the output string is exactly 19 characters wide. The t or d parameter can be -1, in which
  288. case the time or date respectively are left out. *)
  289. PROCEDURE Date*( t, d: LONGINT );
  290. VAR ch: CHAR;
  291. BEGIN
  292. IF d # -1 THEN
  293. Int( 1900 + d DIV 512, 4 ); (* year *)
  294. Pair( "-", d DIV 32 MOD 16 ); (* month *)
  295. Pair( "-", d MOD 32 ); (* day *)
  296. ch := " " (* space between date and time *)
  297. ELSE
  298. ch := 0X (* no space before time *)
  299. END;
  300. IF t # -1 THEN
  301. Pair( ch, t DIV 4096 MOD 32 ); (* hour *)
  302. Pair( ":", t DIV 64 MOD 64 ); (* min *)
  303. Pair( ":", t MOD 64 ) (* sec *)
  304. END
  305. END Date;
  306. (** Write the date and time in RFC 822/1123 format without the optional day of the week (dd mmm yyyy hh:mm:ss SZZZZ) .
  307. The t and d parameters are in Oberon time and date format. The tz parameter specifies the time zone offset in minutes
  308. (from -720 to 720 in steps of 30). If all parameters are within range, the output string is exactly 26 characters wide.
  309. The t, d or tz parameter can be -1, in which case the time, date or timezone respectively are left out. *)
  310. PROCEDURE Date822*( t, d, tz: LONGINT );
  311. VAR i, m: LONGINT; ch: CHAR;
  312. BEGIN
  313. IF d # -1 THEN
  314. Int( d MOD 32, 2 ); (* day *)
  315. m := (d DIV 32 MOD 16 - 1) * 4; (* month *)
  316. FOR i := m TO m + 3 DO Char( months[i] ) END;
  317. Int( 1900 + d DIV 512, 5 ); (* year *)
  318. ch := " " (* space *)
  319. ELSE
  320. ch := 0X (* no space *)
  321. END;
  322. IF t # -1 THEN
  323. Pair( ch, t DIV 4096 MOD 32 ); (* hour *)
  324. Pair( ":", t DIV 64 MOD 64 ); (* min *)
  325. Pair( ":", t MOD 64 ); (* sec *)
  326. ch := " " (* space *)
  327. ELSE
  328. (* leave ch as before *)
  329. END;
  330. IF tz # -1 THEN
  331. IF ch # 0X THEN Char( ch ) END;
  332. IF tz >= 0 THEN Pair( "+", tz DIV 60 ) ELSE Pair( "-", (-tz) DIV 60 ) END;
  333. Pair( 0X, ABS( tz ) MOD 60 )
  334. END
  335. END Date822;
  336. (** Write LONGREAL x using n character positions. *)
  337. PROCEDURE Float*( x: LONGREAL; n: WORD );
  338. VAR
  339. buf: ARRAY 32 OF CHAR;
  340. BEGIN
  341. RC.RealToString( x, n, buf );
  342. String( buf )
  343. END Float;
  344. (** Write LONGREAL x in a fixed point notation. n is the overall minimal length for the output field, f the number of fraction digits following the decimal point, D the fixed exponent (printed only when D # 0). *)
  345. PROCEDURE FloatFix*( x: LONGREAL; n, f, D: WORD );
  346. VAR
  347. buf: ARRAY 64 OF CHAR;
  348. BEGIN
  349. RC.RealToStringFix( x, n, f, D, buf );
  350. String( buf )
  351. END FloatFix;
  352. END Writer;
  353. (** A special writer that buffers output to be fetched by GetString or GetRawString. *)
  354. StringWriter* = OBJECT (Writer)
  355. PROCEDURE & InitStringWriter*( size: LONGINT );
  356. BEGIN
  357. InitWriter( Send, size )
  358. END InitStringWriter;
  359. PROCEDURE Send( CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: WORD );
  360. BEGIN
  361. res := StringFull
  362. END Send;
  363. PROCEDURE CanSetPos*( ): BOOLEAN;
  364. BEGIN
  365. RETURN TRUE;
  366. END CanSetPos;
  367. (* Set the position for the writer *)
  368. PROCEDURE SetPos*( pos: Position );
  369. BEGIN
  370. IF pos > LEN( buf ) THEN pos := LEN( buf ) END;
  371. tail := pos; sent := 0; res := Ok;
  372. END SetPos;
  373. PROCEDURE Update*;
  374. (* nothing to do *)
  375. END Update;
  376. (** Return the contents of the string writer (0X-terminated). *)
  377. PROCEDURE Get*( VAR s: ARRAY OF CHAR );
  378. VAR i, m: LONGINT;
  379. BEGIN
  380. m := LEN( s ) - 1; i := 0;
  381. WHILE (i # tail) & (i < m) DO s[i] := buf[i]; INC( i ) END;
  382. s[i] := 0X; tail := 0; res := Ok
  383. END Get;
  384. (** Return the contents of the string writer (not 0X-terminated). The len parameters returns the string length. *)
  385. PROCEDURE GetRaw*( VAR s: ARRAY OF CHAR; VAR len: LONGINT );
  386. VAR i, m: LONGINT;
  387. BEGIN
  388. m := LEN( s ); i := 0;
  389. WHILE (i # tail) & (i < m) DO s[i] := buf[i]; INC( i ) END;
  390. len := i; tail := 0; res := Ok
  391. END GetRaw;
  392. END StringWriter;
  393. TYPE
  394. (** A reader buffers input received from a Receiver. Must not be shared between processes. *)
  395. Reader* = OBJECT
  396. VAR
  397. head, tail: LONGINT;
  398. buf: POINTER TO ARRAY OF CHAR;
  399. res*: WORD; (** result of last input operation. *)
  400. receive: Receiver;
  401. received*: LONGINT; (** count of received bytes *)
  402. (* buf[buf.head..buf.tail-1] contains data to read. *)
  403. PROCEDURE & InitReader*( receive: Receiver; size: LONGINT );
  404. BEGIN
  405. ASSERT ( receive # NIL );
  406. IF (buf = NIL) OR (LEN(buf) # size) THEN
  407. NEW( buf, size );
  408. END;
  409. SELF.receive := receive; Reset
  410. END InitReader;
  411. (** reset the reader by dropping the bytes in the buffer, resetting the result code and setting received to 0.
  412. This is used by seekable extensions of the reader *)
  413. PROCEDURE Reset*;
  414. BEGIN
  415. head := 0; tail := 0; res := Ok; received := 0
  416. END Reset;
  417. PROCEDURE CanSetPos*( ): BOOLEAN;
  418. BEGIN
  419. RETURN FALSE
  420. END CanSetPos;
  421. PROCEDURE SetPos*( pos: Position );
  422. BEGIN
  423. HALT( 1234 )
  424. END SetPos;
  425. (** Return bytes currently available in input buffer. *)
  426. PROCEDURE Available*( ): LONGINT;
  427. VAR n: LONGINT;
  428. BEGIN
  429. IF (res = Ok) THEN
  430. IF (head = tail) THEN head := 0; receive( buf^, 0, LEN( buf ), 0, tail, res ); INC( received, tail );
  431. ELSIF (tail # LEN( buf )) THEN
  432. receive( buf^, tail, LEN( buf ) - tail, 0, n, res ); (* poll *)
  433. INC( tail, n ); INC( received, n )
  434. END;
  435. IF res = EOF THEN res := Ok END (* ignore EOF here *)
  436. END;
  437. RETURN tail - head
  438. END Available;
  439. (** Current read position. *)
  440. PROCEDURE Pos*( ): Position;
  441. BEGIN
  442. RETURN received - (tail - head)
  443. END Pos;
  444. (** -- Read raw binary data -- *)
  445. (** Read one byte. x=0X if no success (e.g. file ended) *)
  446. PROCEDURE Char*( VAR x: CHAR );
  447. BEGIN
  448. IF (head = tail) & (res = Ok) THEN head := 0; receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail ) END;
  449. IF res = Ok THEN x := buf[head]; INC( head ) ELSE x := 0X END
  450. END Char;
  451. (** Like Read, but return result. Return 0X if no success (e.g. file ended) *)
  452. PROCEDURE Get*( ): CHAR;
  453. BEGIN
  454. IF (head = tail) & (res = Ok) THEN head := 0; receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail ) END;
  455. IF res = Ok THEN INC( head ); RETURN buf[head - 1] ELSE RETURN 0X END
  456. END Get;
  457. (** Like Get, but leave the byte in the input buffer. *)
  458. PROCEDURE Peek*( ): CHAR;
  459. BEGIN
  460. IF (head = tail) & (res = Ok) THEN
  461. head := 0; receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail );
  462. IF res = EOF THEN (* ignore EOF here *)
  463. res := Ok; tail := 0; RETURN 0X (* Peek returns 0X at eof *)
  464. END
  465. END;
  466. IF res = Ok THEN RETURN buf[head] ELSE RETURN 0X END
  467. END Peek;
  468. (** Read size bytes into x, starting at ofs. The len parameter returns the number of bytes that were actually read. *)
  469. PROCEDURE Bytes*( VAR x: ARRAY OF CHAR; ofs, size: LONGINT; VAR len: LONGINT );
  470. VAR n: LONGINT;
  471. BEGIN
  472. ASSERT ( size >= 0 );
  473. len := 0;
  474. LOOP
  475. n := tail - head; (* bytes available *)
  476. IF n = 0 THEN (* no data available *)
  477. head := 0;
  478. IF res = Ok THEN (* fill buffer *)
  479. receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail )
  480. END;
  481. IF res # Ok THEN (* should not be reading from erroneous rider *)
  482. WHILE size # 0 DO x[ofs] := 0X; INC( ofs ); DEC( size ) END; (* clear rest of buffer *)
  483. IF (res = EOF) & (len # 0) THEN res := Ok END; (* ignore EOF if some data being returned *)
  484. EXIT
  485. END;
  486. n := tail
  487. END;
  488. IF n > size THEN n := size END;
  489. ASSERT ( ofs + n <= LEN( x ) ); (* index check *)
  490. SYSTEM.MOVE( ADDRESSOF( buf[head] ), ADDRESSOF( x[ofs] ), n ); INC( head, n ); INC( len, n );
  491. IF size = n THEN EXIT END; (* done *)
  492. INC( ofs, n ); DEC( size, n )
  493. END
  494. END Bytes;
  495. (** Skip n bytes on the reader. *)
  496. PROCEDURE SkipBytes*( n: LONGINT );
  497. VAR ch: CHAR;
  498. BEGIN
  499. WHILE n > 0 DO ch := Get(); DEC( n ) END
  500. END SkipBytes;
  501. (** Read a SHORTINT. *)
  502. PROCEDURE RawSInt*( VAR x: SHORTINT );
  503. BEGIN
  504. x := SYSTEM.VAL( SHORTINT, Get() )
  505. END RawSInt;
  506. (** Read an INTEGER. *)
  507. PROCEDURE RawInt*( VAR x: INTEGER );
  508. VAR x0, x1: CHAR;
  509. BEGIN
  510. x0 := Get(); x1 := Get(); (* defined order *)
  511. x := ORD( x1 ) * 100H + ORD( x0 )
  512. END RawInt;
  513. (** Read a LONGINT. *)
  514. PROCEDURE RawLInt*( VAR x: LONGINT );
  515. VAR ignore: LONGINT;
  516. BEGIN
  517. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4, ignore )
  518. END RawLInt;
  519. (** Read a HUGEINT. *)
  520. PROCEDURE RawHInt*( VAR x: HUGEINT );
  521. VAR ignore: LONGINT;
  522. BEGIN
  523. Bytes( SYSTEM.VAL( Bytes8, x ), 0, 8, ignore )
  524. END RawHInt;
  525. (** Read a 64 bit value in network byte order (most significant byte first) *)
  526. PROCEDURE Net64*( ): HUGEINT;
  527. BEGIN
  528. RETURN Net32() * 100000000H + Net32()
  529. END Net64;
  530. (** Read a 32 bit value in network byte order (most significant byte first) *)
  531. PROCEDURE Net32*( ): LONGINT;
  532. BEGIN
  533. RETURN LONG( ORD( Get() ) ) * 1000000H + LONG( ORD( Get() ) ) * 10000H + LONG( ORD( Get() ) ) * 100H + LONG( ORD( Get() ) )
  534. END Net32;
  535. (** Read an unsigned 16bit value in network byte order (most significant byte first) *)
  536. PROCEDURE Net16*( ): LONGINT;
  537. BEGIN
  538. RETURN LONG( ORD( Get() ) ) * 100H + LONG( ORD( Get() ) )
  539. END Net16;
  540. (** Read an unsigned byte *)
  541. PROCEDURE Net8*( ): LONGINT;
  542. BEGIN
  543. RETURN LONG( ORD( Get() ) )
  544. END Net8;
  545. (** Read a SET. *)
  546. PROCEDURE RawSet*( VAR x: SET );
  547. VAR ignore: LONGINT;
  548. BEGIN
  549. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4, ignore )
  550. END RawSet;
  551. (** Read a BOOLEAN. *)
  552. PROCEDURE RawBool*( VAR x: BOOLEAN );
  553. BEGIN
  554. x := (Get() # 0X)
  555. END RawBool;
  556. (** Read a REAL. *)
  557. PROCEDURE RawReal*( VAR x: REAL );
  558. VAR ignore: LONGINT;
  559. BEGIN
  560. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4, ignore )
  561. END RawReal;
  562. (** Read a LONGREAL. *)
  563. PROCEDURE RawLReal*( VAR x: LONGREAL );
  564. VAR ignore: LONGINT;
  565. BEGIN
  566. Bytes( SYSTEM.VAL( Bytes8, x ), 0, 8, ignore )
  567. END RawLReal;
  568. (** Read a 0X-terminated string. If the input string is larger than x, read the full string and assign the truncated 0X-terminated value to x. *)
  569. PROCEDURE RawString*( VAR x: ARRAY OF CHAR );
  570. VAR i, m: LONGINT; ch: CHAR;
  571. BEGIN
  572. i := 0; m := LEN( x ) - 1;
  573. LOOP
  574. ch := Get(); (* also returns 0X on error *)
  575. IF ch = 0X THEN EXIT END;
  576. IF i < m THEN x[i] := ch; INC( i ) END
  577. END;
  578. x[i] := 0X
  579. END RawString;
  580. (** Read a number in a compressed format. *)
  581. PROCEDURE RawNum*( VAR x: LONGINT );
  582. VAR ch: CHAR; n, y: LONGINT;
  583. BEGIN
  584. n := 0; y := 0; ch := Get();
  585. WHILE ch >= 80X DO INC( y, LSH( LONGINT( ORD( ch ) ) - 128, n ) ); INC( n, 7 ); ch := Get() END;
  586. x := ASH( LSH( LONGINT( ORD( ch ) ), 25 ), n - 25 ) + y
  587. END RawNum;
  588. (** Read a huge number in a compressed format. *)
  589. PROCEDURE RawHNum*( VAR x: HUGEINT );
  590. VAR ch: CHAR; n, y: HUGEINT;
  591. BEGIN
  592. n := 0; y := 0; ch := Get();
  593. WHILE ch >= 80X DO INC( y, LSH( HUGEINT( ORD( ch ) ) - 128, n ) ); INC( n, 7 ); ch := Get() END;
  594. x := ASH( LSH( HUGEINT( ORD( ch ) ), 57 ), n - 57 ) + y
  595. END RawHNum;
  596. (** Read a size in a compressed format. *)
  597. PROCEDURE RawSize*( VAR x: SIZE );
  598. VAR ch: CHAR; n, y: SIZE;
  599. BEGIN
  600. n := 0; y := 0; ch := Get();
  601. WHILE ch >= 80X DO INC( y, LSH( SIZE( ORD( ch ) ) - 128, n ) ); INC( n, 7 ); ch := Get() END;
  602. x := ASH( LSH( SIZE( ORD( ch ) ), SIZE OF SIZE * 8 - 7 ), n - SIZE OF SIZE * 8 - 7 ) + y
  603. END RawSize;
  604. (** -- Read formatted data (uses Peek for one character lookahead) -- *)
  605. (** Read an integer value in decimal or hexadecimal. If hex = TRUE, recognize the "H" postfix for hexadecimal numbers. *)
  606. PROCEDURE Int*( VAR x: LONGINT; hex: BOOLEAN );
  607. VAR vd, vh: LONGINT; sgn, d: WORD; ch: CHAR; ok: BOOLEAN;
  608. BEGIN
  609. vd := 0; vh := 0; sgn := 1; ok := FALSE;
  610. IF Peek() = "-" THEN sgn := -1; ch := Get() END;
  611. LOOP
  612. ch := Peek();
  613. IF (ch >= "0") & (ch <= "9") THEN d := ORD( ch ) - ORD( "0" )
  614. ELSIF hex & (CAP( ch ) >= "A") & (CAP( ch ) <= "F") THEN d := ORD( CAP( ch ) ) - ORD( "A" ) + 10
  615. ELSE EXIT
  616. END;
  617. vd := 10 * vd + d; vh := 16 * vh + d; (* ignore overflow *)
  618. ch := Get(); ok := TRUE
  619. END;
  620. IF hex & (CAP( ch ) = "H") THEN (* optional "H" present *)
  621. vd := vh; (* use the hex value *)
  622. ch := Get()
  623. END;
  624. x := sgn * vd;
  625. IF (res = 0) & ~ok THEN res := FormatError END
  626. END Int;
  627. (** Read a huge integer value in decimal or hexadecimal. If hex = TRUE, recognize the "H" postfix for hexadecimal numbers. *)
  628. PROCEDURE HInt*( VAR x: HUGEINT; hex: BOOLEAN );
  629. VAR vd, vh: HUGEINT; sgn, d: WORD; ch: CHAR; ok: BOOLEAN;
  630. BEGIN
  631. vd := 0; vh := 0; sgn := 1; ok := FALSE;
  632. IF Peek() = "-" THEN sgn := -1; ch := Get() END;
  633. LOOP
  634. ch := Peek();
  635. IF (ch >= "0") & (ch <= "9") THEN d := ORD( ch ) - ORD( "0" )
  636. ELSIF hex & (CAP( ch ) >= "A") & (CAP( ch ) <= "F") THEN d := ORD( CAP( ch ) ) - ORD( "A" ) + 10
  637. ELSE EXIT
  638. END;
  639. vd := 10 * vd + d; vh := 16 * vh + d; (* ignore overflow *)
  640. ch := Get(); ok := TRUE
  641. END;
  642. IF hex & (CAP( ch ) = "H") THEN (* optional "H" present *)
  643. vd := vh; (* use the hex value *)
  644. ch := Get()
  645. END;
  646. x := sgn * vd;
  647. IF (res = 0) & ~ok THEN res := FormatError END
  648. END HInt;
  649. (** Read a size value in decimal or hexadecimal. If hex = TRUE, recognize the "H" postfix for hexadecimal numbers. *)
  650. PROCEDURE Size*( VAR x: SIZE; hex: BOOLEAN );
  651. VAR vd, vh: SIZE; d: WORD; ch: CHAR; ok: BOOLEAN;
  652. BEGIN
  653. vd := 0; vh := 0; ok := FALSE;
  654. LOOP
  655. ch := Peek();
  656. IF (ch >= "0") & (ch <= "9") THEN d := ORD( ch ) - ORD( "0" )
  657. ELSIF hex & (CAP( ch ) >= "A") & (CAP( ch ) <= "F") THEN d := ORD( CAP( ch ) ) - ORD( "A" ) + 10
  658. ELSE EXIT
  659. END;
  660. vd := 10 * vd + d; vh := 16 * vh + d; (* ignore overflow *)
  661. ch := Get(); ok := TRUE
  662. END;
  663. IF hex & (CAP( ch ) = "H") THEN (* optional "H" present *)
  664. vd := vh; (* use the hex value *)
  665. ch := Get()
  666. END;
  667. x := vd;
  668. IF (res = 0) & ~ok THEN res := FormatError END
  669. END Size;
  670. (** Return TRUE iff at the end of a line (or file). *)
  671. PROCEDURE EOLN*( ): BOOLEAN;
  672. VAR ch: CHAR;
  673. BEGIN
  674. ch := Peek(); RETURN (ch = CR) OR (ch = LF) OR (res # Ok)
  675. END EOLN;
  676. (** Read all characters until the end of the line (inclusive). If the input string is larger than x, read the full string and assign
  677. the truncated 0X-terminated value to x. *)
  678. PROCEDURE Ln*( VAR x: ARRAY OF CHAR );
  679. VAR i, m: LONGINT; ch: CHAR;
  680. BEGIN
  681. i := 0; m := LEN( x ) - 1;
  682. LOOP
  683. ch := Peek();
  684. IF (ch = CR) OR (ch = LF) OR (res # Ok) THEN EXIT END;
  685. IF i < m THEN x[i] := ch; INC( i ) END;
  686. ch := Get()
  687. END;
  688. x[i] := 0X;
  689. IF ch = CR THEN ch := Get() END;
  690. IF Peek() = LF THEN ch := Get() END
  691. END Ln;
  692. (** Read all characters until the end of the line (inclusive) or an <EOT> character.
  693. If the input string is larger than x, read the full string and assign the truncated 0X-terminated
  694. value to x. *)
  695. PROCEDURE LnEOT*( VAR x: ARRAY OF CHAR );
  696. VAR i, m: LONGINT; ch: CHAR;
  697. BEGIN
  698. i := 0; m := LEN( x ) - 1;
  699. LOOP
  700. ch := Peek();
  701. IF (ch = CR) OR (ch = LF) OR (ch = EOT) OR (res # Ok) THEN EXIT END;
  702. IF i < m THEN x[i] := ch; INC( i ) END;
  703. ch := Get()
  704. END;
  705. x[i] := 0X;
  706. IF ch = CR THEN ch := Get() END;
  707. IF Peek() = LF THEN ch := Get() END;
  708. IF ch = EOT THEN ch := Get() END
  709. END LnEOT;
  710. (** Skip over all characters until the end of the line (inclusive). *)
  711. PROCEDURE SkipLn*;
  712. VAR ch: CHAR;
  713. BEGIN
  714. LOOP
  715. ch := Peek();
  716. IF (ch = CR) OR (ch = LF) OR (res # Ok) THEN EXIT END;
  717. ch := Get()
  718. END;
  719. IF ch = CR THEN ch := Get() END;
  720. IF Peek() = LF THEN ch := Get() END
  721. END SkipLn;
  722. (** Skip over space and TAB characters. *)
  723. PROCEDURE SkipSpaces*;
  724. VAR ch: CHAR;
  725. BEGIN
  726. LOOP
  727. ch := Peek();
  728. IF (ch # TAB) & (ch # SP) THEN EXIT END;
  729. ch := Get()
  730. END
  731. END SkipSpaces;
  732. (** Skip over space, TAB and EOLN characters. *)
  733. PROCEDURE SkipWhitespace*;
  734. VAR ch: CHAR;
  735. BEGIN
  736. LOOP
  737. ch := Peek();
  738. IF (ch # SP) & (ch # CR) & (ch # LF) & (ch # TAB) THEN EXIT END;
  739. ch := Get()
  740. END
  741. END SkipWhitespace;
  742. (** Read a token, consisting of any string of characters terminated by space, TAB or EOLN. *)
  743. PROCEDURE Token*( VAR token: ARRAY OF CHAR );
  744. VAR j, max: LONGINT; ch: CHAR;
  745. BEGIN
  746. j := 0; max := LEN( token ) - 1;
  747. LOOP
  748. ch := Peek();
  749. IF (ch = SP) OR (ch = CR) OR (ch = LF) OR (ch = TAB) OR (res # Ok) THEN EXIT END;
  750. IF j < max THEN token[j] := ch; INC( j ) END;
  751. ch := Get()
  752. END;
  753. token[j] := 0X
  754. END Token;
  755. (** Read an optionally "" or '' enquoted string. Will not read past the end of a line. *)
  756. PROCEDURE String*( VAR string: ARRAY OF CHAR );
  757. VAR c, delimiter: CHAR; i, len: LONGINT;
  758. BEGIN
  759. c := Peek();
  760. IF (c # "'") & (c # '"') THEN Token( string )
  761. ELSE
  762. delimiter := Get(); c := Peek(); i := 0; len := LEN( string ) - 1;
  763. WHILE (i < len) & (c # delimiter) & (c # CR) & (c # LF) & (res = Ok) DO string[i] := Get(); INC( i ); c := Peek() END;
  764. IF (c = delimiter) THEN c := Get() END;
  765. string[i] := 0X
  766. END
  767. END String;
  768. (** First skip whitespace, then read string *)
  769. PROCEDURE GetString*(VAR string : ARRAY OF CHAR): BOOLEAN;
  770. VAR c: CHAR;
  771. BEGIN
  772. SkipWhitespace;
  773. c := Peek();
  774. String(string);
  775. RETURN (string[0] # 0X) OR (c = "'") OR (c = '"');
  776. END GetString;
  777. (** First skip whitespace, then read integer *)
  778. PROCEDURE GetInteger*(VAR integer : LONGINT; isHexadecimal : BOOLEAN): BOOLEAN;
  779. BEGIN
  780. SkipWhitespace;
  781. Int(integer, isHexadecimal);
  782. RETURN res = Ok;
  783. END GetInteger;
  784. (** First skip whitespace, then read size *)
  785. PROCEDURE GetSize*(VAR size : SIZE; isHexadecimal : BOOLEAN): BOOLEAN;
  786. BEGIN
  787. SkipWhitespace;
  788. Size(size, isHexadecimal);
  789. RETURN res = Ok;
  790. END GetSize;
  791. (** First skip whitespace, then read a real *)
  792. PROCEDURE GetReal*(VAR real: LONGREAL): BOOLEAN;
  793. BEGIN
  794. SkipWhitespace;
  795. real := RC.ScanReal(Get);
  796. RETURN res = Ok
  797. END GetReal;
  798. (** First skip whitespace, then read 1 byte character *)
  799. PROCEDURE GetChar*(VAR ch : CHAR): BOOLEAN;
  800. BEGIN
  801. SkipWhitespace;
  802. Char(ch);
  803. RETURN ch # 0X;
  804. END GetChar;
  805. END Reader;
  806. TYPE
  807. (** A special reader that buffers input set by SetString or SetRawString. *)
  808. StringReader* = OBJECT (Reader)
  809. PROCEDURE & InitStringReader*( size: LONGINT );
  810. BEGIN
  811. InitReader( Receive, size )
  812. END InitStringReader;
  813. PROCEDURE CanSetPos*( ): BOOLEAN;
  814. BEGIN
  815. RETURN TRUE
  816. END CanSetPos;
  817. (** Set the reader position *)
  818. PROCEDURE SetPos*( pos: Position );
  819. BEGIN
  820. IF pos > LEN( buf ) THEN pos := LEN( buf ) END;
  821. head := pos; tail := LEN( buf ); received := LEN( buf ); res := Ok;
  822. END SetPos;
  823. PROCEDURE Receive( VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len: LONGINT; VAR res: WORD );
  824. BEGIN
  825. IF min = 0 THEN res := Ok ELSE res := EOF END;
  826. len := 0;
  827. END Receive;
  828. (** Set the contents of the string buffer. The s parameter is a 0X-terminated string. *)
  829. PROCEDURE Set*(CONST s: ARRAY OF CHAR );
  830. VAR len: LONGINT;
  831. BEGIN
  832. len := 0;
  833. WHILE s[len] # 0X DO INC( len ) END;
  834. IF len > LEN( buf ) THEN len := LEN( buf ) END;
  835. head := 0; tail := len; received := len; res := Ok;
  836. IF len > 0 THEN
  837. SYSTEM.MOVE( ADDRESSOF( s[0] ), ADDRESSOF( buf[0] ), len )
  838. END;
  839. END Set;
  840. (** Set the contents of the string buffer. The len parameter specifies the size of the buffer s. *)
  841. PROCEDURE SetRaw*(CONST s: ARRAY OF CHAR; ofs, len: LONGINT );
  842. BEGIN
  843. IF len > LEN( buf ) THEN len := LEN( buf ) END;
  844. head := 0; tail := len; received := len; res := Ok;
  845. ASSERT ( (len >= 0) & (ofs + len <= LEN( s )) ); (* index check *)
  846. IF len > 0 THEN
  847. SYSTEM.MOVE( ADDRESSOF( s[ofs] ), ADDRESSOF( buf[0] ), len )
  848. END;
  849. END SetRaw;
  850. END StringReader;
  851. Bytes2 = ARRAY 2 OF CHAR;
  852. Bytes4 = ARRAY 4 OF CHAR;
  853. Bytes8 = ARRAY 8 OF CHAR;
  854. VAR
  855. months: ARRAY 12 * 4 + 1 OF CHAR;
  856. (** Open a writer to the specified stream sender. Update must be called after writing to ensure the buffer is written to the stream. *)
  857. PROCEDURE OpenWriter*( VAR b: Writer; send: Sender );
  858. BEGIN
  859. NEW( b, send, DefaultWriterSize )
  860. END OpenWriter;
  861. (** Open a reader from the specified stream receiver. *)
  862. PROCEDURE OpenReader*( VAR b: Reader; receive: Receiver );
  863. BEGIN
  864. NEW( b, receive, DefaultReaderSize )
  865. END OpenReader;
  866. (** Copy the contents of a reader to a writer *)
  867. PROCEDURE Copy* (r: Reader; w: Writer);
  868. VAR char: CHAR;
  869. BEGIN
  870. WHILE r.res = Ok DO
  871. r.Char (char);
  872. IF r.res = Ok THEN w.Char (char) END
  873. END;
  874. END Copy;
  875. BEGIN
  876. months := " Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec";
  877. END Streams.
  878. (**
  879. Notes:
  880. o Any single buffer instance must not be accessed by more than one process concurrently.
  881. o The interface is blocking (synchronous). If an output buffer is full, it is written with a synchronous write, which returns
  882. only when all the data has been written. If an input buffer is empty, it is read with a synchronous read, which only returns
  883. once some data has been read. The only exception is the Available() procedure, which "peeks" at the input stream
  884. and returns 0 if no data is currently available.
  885. o All procedures set res to the error code reported by the lower-level I/O operation (non-zero indicates error).
  886. E.g. closing an underlying TCP connection will result in the Read* procedures returning a non-zero error code.
  887. o res is sticky. Once it becomes non-zero, it remains non-zero.
  888. o The only way to detect end of file is to attempt to read past the end of file, which returns a non-zero error code.
  889. o All output written to an erroneous buffer is ignored.
  890. o The value returned when reading from an erroneous buffer is undefined, except for the Read procedure, which returns 0X.
  891. o ReadBytes sets the len parameter to the number of bytes that were actually read, e.g. if size = 10, and only 8 bytes are read, len is 8.
  892. o Raw format is little-endian 2's complement integers, IEEE reals and 0X-terminated strings.
  893. o Syntax for ReadInt with hex = FALSE: num = ["-"] digit {digit}. digit = "0".."9".
  894. o Syntax for ReadInt with hex = TRUE: ["-"] hexdigit {hexdigit} ["H"|"h"]. hexdigit = digit | "A".."F" | "a".."f".
  895. o ReadInt with hex = TRUE allows "A".."F" as digits, and looks for a "H" character after the number.
  896. If present, the number is interpreted as hexadecimal. If hexadecimal digits are present, but no "H" flag,
  897. the resulting decimal value is undefined.
  898. o ReadInt ignores overflow.
  899. o A Sender sends len bytes from buf at ofs to output and returns res non-zero on error. It waits until all the data is written,
  900. or an error occurs.
  901. o A Receiver receives up to size bytes from input into buf at ofs and returns the number of bytes read in len.
  902. It returns res non-zero on error. It waits until at least min bytes (possibly zero) are available, or an error occurs.
  903. o EOLN and ReadLn recognize the following end-of-line characters: CR, LF and CR/LF.
  904. o To read an unstructured file token-by-token: WHILE (r.res = 0) DO SkipWhitespace; ReadToken END
  905. o To read a line structured file token-by-token: WHILE r.res = 0 DO SkipSpaces; WHILE ~EOLN DO ReadToken; SkipSpaces END END
  906. o A string writer is not flushed when it becomes full, but res is set to a non-zero value.
  907. o Update has no effect on a string writer.
  908. o GetString can be called on a string writer to return the buffer contents and reset it to empty.
  909. o GetString always appends a 0X character to the buffer, but returns the true length (excluding the added 0X) in the len parameter,
  910. so it can also be used for binary data that includes 0X characters.
  911. o Receive procedure should set res to EOF when attempting to read past the end of file.
  912. *)
  913. (*
  914. to do:
  915. o stream byte count
  916. o read formatted data
  917. o reads for all formatted writes
  918. o write reals
  919. o low-level version that can be used in kernel (below KernelLog)
  920. *)