Streams64.Mod 36 KB

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