Streams.Mod 35 KB

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