Streams64.Mod 36 KB

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