Streams.Mod 32 KB

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