Streams64.Mod 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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: LONGWORD; propagate: BOOLEAN; VAR res: WORD );
  21. (** Any stream input procedure or method. *)
  22. Receiver* = PROCEDURE {DELEGATE} ( VAR buf: ARRAY OF CHAR; ofs, size, min: LONGWORD; VAR len: LONGWORD; VAR res: WORD );
  23. Connection* = OBJECT
  24. PROCEDURE Send*( CONST data: ARRAY OF CHAR; ofs, len: LONGWORD; propagate: BOOLEAN; VAR res: WORD );
  25. END Send;
  26. PROCEDURE Receive*( VAR data: ARRAY OF CHAR; ofs, size, min: LONGWORD; VAR len: LONGWORD; VAR res: WORD );
  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: LONGWORD;
  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: LONGWORD );
  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: LONGWORD );
  85. VAR n: LONGWORD;
  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: LONGWORD;
  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: LONGWORD;
  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: LONGWORD );
  440. BEGIN
  441. InitWriter( Send, size )
  442. END InitStringWriter;
  443. PROCEDURE Send( CONST buf: ARRAY OF CHAR; ofs, len: LONGWORD; propagate: BOOLEAN; VAR res: WORD );
  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. IF pos > LEN( buf ) THEN pos := LEN( buf ) END;
  455. tail := LONGWORD( pos ); sent := 0; res := Ok;
  456. END SetPos;
  457. PROCEDURE Update*;
  458. (* nothing to do *)
  459. END Update;
  460. (** Return the contents of the string writer (0X-terminated). *)
  461. PROCEDURE Get*( VAR s: ARRAY OF CHAR );
  462. VAR i, m: LONGWORD;
  463. BEGIN
  464. m := LEN( s ) - 1; i := 0;
  465. WHILE (i # tail) & (i < m) DO s[i] := buf[i]; INC( i ) END;
  466. s[i] := 0X; tail := 0; res := Ok
  467. END Get;
  468. (** Return the contents of the string writer (not 0X-terminated). The len parameters returns the string length. *)
  469. PROCEDURE GetRaw*( VAR s: ARRAY OF CHAR; VAR len: LONGWORD );
  470. VAR i, m: LONGWORD;
  471. BEGIN
  472. m := LEN( s ); i := 0;
  473. WHILE (i # tail) & (i < m) DO s[i] := buf[i]; INC( i ) END;
  474. len := i; tail := 0; res := Ok
  475. END GetRaw;
  476. END StringWriter;
  477. TYPE
  478. (** A reader buffers input received from a Receiver. Must not be shared between processes. *)
  479. Reader* = OBJECT
  480. VAR
  481. head, tail: LONGWORD;
  482. buf: POINTER TO ARRAY OF CHAR;
  483. res*: LONGINT; (** result of last input operation. *)
  484. receive: Receiver;
  485. received*: TSize; (** count of received bytes *)
  486. (* buf[buf.head..buf.tail-1] contains data to read. *)
  487. PROCEDURE & InitReader*( receive: Receiver; size: LONGWORD );
  488. BEGIN
  489. ASSERT ( receive # NIL );
  490. IF (buf = NIL) OR (LEN(buf) # size) THEN
  491. NEW( buf, size );
  492. END;
  493. SELF.receive := receive; Reset
  494. END InitReader;
  495. (** reset the reader by dropping the bytes in the buffer, resetting the result code and setting received to 0.
  496. This is used by seekable extensions of the reader *)
  497. PROCEDURE Reset*;
  498. BEGIN
  499. head := 0; tail := 0; res := Ok; received := 0
  500. END Reset;
  501. PROCEDURE CanSetPos*( ): BOOLEAN;
  502. BEGIN
  503. RETURN FALSE
  504. END CanSetPos;
  505. PROCEDURE SetPos*( pos: TSize );
  506. BEGIN
  507. HALT( 1234 )
  508. END SetPos;
  509. (** Return bytes currently available in input buffer. *)
  510. PROCEDURE Available*( ): LONGWORD;
  511. VAR n: LONGWORD;
  512. BEGIN
  513. IF (res = Ok) THEN
  514. IF (head = tail) THEN head := 0; receive( buf^, 0, LEN( buf ), 0, tail, res ); INC( received, tail );
  515. ELSIF (tail # LEN( buf )) THEN
  516. receive( buf^, tail, LEN( buf ) - tail, 0, n, res ); (* poll *)
  517. INC( tail, n ); INC( received, n )
  518. END;
  519. IF res = EOF THEN res := Ok END (* ignore EOF here *)
  520. END;
  521. RETURN tail - head
  522. END Available;
  523. (** Current read position. *)
  524. PROCEDURE Pos*( ): TSize;
  525. BEGIN
  526. RETURN received - (tail - head)
  527. END Pos;
  528. (** -- Read raw binary data -- *)
  529. (** Read one byte. x=0X if no success (e.g. file ended) *)
  530. PROCEDURE Char*( VAR x: CHAR );
  531. BEGIN
  532. IF (head = tail) & (res = Ok) THEN head := 0; receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail ) END;
  533. IF res = Ok THEN x := buf[head]; INC( head ) ELSE x := 0X END
  534. END Char;
  535. (** Like Read, but return result. Return 0X if no success (e.g. file ended) *)
  536. PROCEDURE Get*( ): CHAR;
  537. BEGIN
  538. IF (head = tail) & (res = Ok) THEN head := 0; receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail ) END;
  539. IF res = Ok THEN INC( head ); RETURN buf[head - 1] ELSE RETURN 0X END
  540. END Get;
  541. (** Like Get, but leave the byte in the input buffer. *)
  542. PROCEDURE Peek*( ): CHAR;
  543. BEGIN
  544. IF (head = tail) & (res = Ok) THEN
  545. head := 0; receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail );
  546. IF res = EOF THEN (* ignore EOF here *)
  547. res := Ok; tail := 0; RETURN 0X (* Peek returns 0X at eof *)
  548. END
  549. END;
  550. IF res = Ok THEN RETURN buf[head] ELSE RETURN 0X END
  551. END Peek;
  552. (** Read size bytes into x, starting at ofs. The len parameter returns the number of bytes that were actually read. *)
  553. PROCEDURE Bytes*( VAR x: ARRAY OF CHAR; ofs, size: LONGWORD; VAR len: LONGWORD );
  554. VAR n: LONGWORD;
  555. BEGIN
  556. ASSERT ( size >= 0 );
  557. len := 0;
  558. LOOP
  559. n := tail - head; (* bytes available *)
  560. IF n = 0 THEN (* no data available *)
  561. head := 0;
  562. IF res = Ok THEN (* fill buffer *)
  563. receive( buf^, 0, LEN( buf ), 1, tail, res ); INC( received, tail )
  564. END;
  565. IF res # Ok THEN (* should not be reading from erroneous rider *)
  566. WHILE size # 0 DO x[ofs] := 0X; INC( ofs ); DEC( size ) END; (* clear rest of buffer *)
  567. IF (res = EOF) & (len # 0) THEN res := Ok END; (* ignore EOF if some data being returned *)
  568. EXIT
  569. END;
  570. n := tail
  571. END;
  572. IF n > size THEN n := size END;
  573. ASSERT ( ofs + n <= LEN( x ) ); (* index check *)
  574. SYSTEM.MOVE( ADDRESSOF( buf[head] ), ADDRESSOF( x[ofs] ), n ); INC( head, n ); INC( len, n );
  575. IF size = n THEN EXIT END; (* done *)
  576. INC( ofs, n ); DEC( size, n )
  577. END
  578. END Bytes;
  579. (** Skip n bytes on the reader. *)
  580. PROCEDURE SkipBytes*( n: TSize );
  581. VAR ch: CHAR;
  582. BEGIN
  583. WHILE n > 0 DO ch := Get(); DEC( n ) END
  584. END SkipBytes;
  585. (** Read a SHORTINT. *)
  586. PROCEDURE RawSInt*( VAR x: SHORTINT );
  587. BEGIN
  588. x := SYSTEM.VAL( SHORTINT, Get() )
  589. END RawSInt;
  590. (** Read an INTEGER. *)
  591. PROCEDURE RawInt*( VAR x: INTEGER );
  592. VAR x0, x1: CHAR;
  593. BEGIN
  594. x0 := Get(); x1 := Get(); (* defined order *)
  595. x := ORD( x1 ) * 100H + ORD( x0 )
  596. END RawInt;
  597. (** Read a LONGINT. *)
  598. PROCEDURE RawLInt*( VAR x: LONGINT );
  599. VAR ignore: LONGWORD;
  600. BEGIN
  601. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4, ignore )
  602. END RawLInt;
  603. (** Read a HUGEINT. *)
  604. PROCEDURE RawHInt*( VAR x: HUGEINT );
  605. VAR ignore: LONGWORD;
  606. BEGIN
  607. Bytes( SYSTEM.VAL( Bytes8, x ), 0, 8, ignore )
  608. END RawHInt;
  609. (** Read a 64 bit value in network byte order (most significant byte first) *)
  610. PROCEDURE Net64*( ): HUGEINT;
  611. BEGIN
  612. RETURN Net32() * 100000000H + Net32()
  613. END Net64;
  614. (** Read a 32 bit value in network byte order (most significant byte first) *)
  615. PROCEDURE Net32*( ): LONGINT;
  616. BEGIN
  617. RETURN LONG( ORD( Get() ) ) * 1000000H + LONG( ORD( Get() ) ) * 10000H + LONG( ORD( Get() ) ) * 100H + LONG( ORD( Get() ) )
  618. END Net32;
  619. (** Read an unsigned 16bit value in network byte order (most significant byte first) *)
  620. PROCEDURE Net16*( ): LONGINT;
  621. BEGIN
  622. RETURN LONG( ORD( Get() ) ) * 100H + LONG( ORD( Get() ) )
  623. END Net16;
  624. (** Read an unsigned byte *)
  625. PROCEDURE Net8*( ): LONGINT;
  626. BEGIN
  627. RETURN LONG( ORD( Get() ) )
  628. END Net8;
  629. (** Read a SET. *)
  630. PROCEDURE RawSet*( VAR x: SET );
  631. VAR ignore: LONGWORD;
  632. BEGIN
  633. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4, ignore )
  634. END RawSet;
  635. (** Read a BOOLEAN. *)
  636. PROCEDURE RawBool*( VAR x: BOOLEAN );
  637. BEGIN
  638. x := (Get() # 0X)
  639. END RawBool;
  640. (** Read a REAL. *)
  641. PROCEDURE RawReal*( VAR x: REAL );
  642. VAR ignore: LONGWORD;
  643. BEGIN
  644. Bytes( SYSTEM.VAL( Bytes4, x ), 0, 4, ignore )
  645. END RawReal;
  646. (** Read a LONGREAL. *)
  647. PROCEDURE RawLReal*( VAR x: LONGREAL );
  648. VAR ignore: LONGWORD;
  649. BEGIN
  650. Bytes( SYSTEM.VAL( Bytes8, x ), 0, 8, ignore )
  651. END RawLReal;
  652. (** 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. *)
  653. PROCEDURE RawString*( VAR x: ARRAY OF CHAR );
  654. VAR i, m: LONGWORD; ch: CHAR;
  655. BEGIN
  656. i := 0; m := LEN( x ) - 1;
  657. LOOP
  658. ch := Get(); (* also returns 0X on error *)
  659. IF ch = 0X THEN EXIT END;
  660. IF i < m THEN x[i] := ch; INC( i ) END
  661. END;
  662. x[i] := 0X
  663. END RawString;
  664. (** Read a number in a compressed format. *)
  665. PROCEDURE RawNum*( VAR x: LONGINT );
  666. VAR ch: CHAR; n, y: LONGINT;
  667. BEGIN
  668. n := 0; y := 0; ch := Get();
  669. WHILE ch >= 80X DO INC( y, LSH( LONGINT( ORD( ch ) ) - 128, n ) ); INC( n, 7 ); ch := Get() END;
  670. x := ASH( LSH( LONGINT( ORD( ch ) ), 25 ), n - 25 ) + y
  671. END RawNum;
  672. (** -- Read formatted data (uses Peek for one character lookahead) -- *)
  673. (** Read an integer value in decimal or hexadecimal. If hex = TRUE, recognize the "H" postfix for hexadecimal numbers. *)
  674. PROCEDURE Int*( VAR x: LONGINT; hex: BOOLEAN );
  675. VAR vd, vh, sgn, d: LONGINT; ch: CHAR; ok: BOOLEAN;
  676. BEGIN
  677. vd := 0; vh := 0; sgn := 1; ok := FALSE;
  678. IF Peek() = "-" THEN sgn := -1; ch := Get() END;
  679. LOOP
  680. ch := Peek();
  681. IF (ch >= "0") & (ch <= "9") THEN d := ORD( ch ) - ORD( "0" )
  682. ELSIF hex & (CAP( ch ) >= "A") & (CAP( ch ) <= "F") THEN d := ORD( CAP( ch ) ) - ORD( "A" ) + 10
  683. ELSE EXIT
  684. END;
  685. vd := 10 * vd + d; vh := 16 * vh + d; (* ignore overflow *)
  686. ch := Get(); ok := TRUE
  687. END;
  688. IF hex & (CAP( ch ) = "H") THEN (* optional "H" present *)
  689. vd := vh; (* use the hex value *)
  690. ch := Get()
  691. END;
  692. x := sgn * vd;
  693. IF (res = 0) & ~ok THEN res := FormatError END
  694. END Int;
  695. (** Read a floating-point number. EBNF: Real = Digit {Digit} '.' Digit {Digit} ['e'|'E' ['+'|'-'] Digit {Digit}]. *)
  696. PROCEDURE Real* (VAR real: LONGREAL);
  697. VAR e: INTEGER; y, g: LONGREAL; neg, negE: BOOLEAN; ch: CHAR;
  698. BEGIN
  699. ch := Get();
  700. WHILE (ch = "0") DO ch := Get() END;
  701. IF ch = "-" THEN neg := TRUE; ch := Get(); ELSE neg := FALSE END;
  702. WHILE (ch = " ") OR (ch = "0") DO ch := Get(); END;
  703. y := 0;
  704. WHILE ("0" <= ch) & (ch <= "9") DO
  705. y := y * 10 + (ORD(ch) - ORD("0"));
  706. ch := Get();
  707. END;
  708. IF ch = "." THEN
  709. ch := Get();
  710. g := 1;
  711. WHILE ("0" <= ch) & (ch <= "9") DO
  712. g := g / 10; y := y + g * (ORD(ch) - ORD("0"));
  713. ch := Get();
  714. END;
  715. END;
  716. IF (ch = "d") OR (ch = "D") OR (ch = "e") OR (ch = "E") THEN
  717. ch := Get(); e := 0;
  718. IF ch = "-" THEN negE := TRUE; ch := Get()
  719. ELSIF ch = "+" THEN negE := FALSE; ch := Get()
  720. ELSE negE := FALSE
  721. END;
  722. WHILE (ch = "0") DO ch := Get() END;
  723. WHILE ("0" <= ch) & (ch <= "9") DO
  724. e := e * 10 + (ORD(ch) - ORD("0"));
  725. ch := Get();
  726. END;
  727. IF negE THEN y := y / Ten(e)
  728. ELSE y := y * Ten(e)
  729. END;
  730. END;
  731. IF neg THEN y := -y END;
  732. real := y
  733. END Real;
  734. (** Return TRUE iff at the end of a line (or file). *)
  735. PROCEDURE EOLN*( ): BOOLEAN;
  736. VAR ch: CHAR;
  737. BEGIN
  738. ch := Peek(); RETURN (ch = CR) OR (ch = LF) OR (res # Ok)
  739. END EOLN;
  740. (** Read all characters until the end of the line (inclusive). If the input string is larger than x, read the full string and assign
  741. the truncated 0X-terminated value to x. *)
  742. PROCEDURE Ln*( VAR x: ARRAY OF CHAR );
  743. VAR i, m: LONGWORD; ch: CHAR;
  744. BEGIN
  745. i := 0; m := LEN( x ) - 1;
  746. LOOP
  747. ch := Peek();
  748. IF (ch = CR) OR (ch = LF) OR (res # Ok) THEN EXIT END;
  749. IF i < m THEN x[i] := ch; INC( i ) END;
  750. ch := Get()
  751. END;
  752. x[i] := 0X;
  753. IF ch = CR THEN ch := Get() END;
  754. IF Peek() = LF THEN ch := Get() END
  755. END Ln;
  756. (** Read all characters until the end of the line (inclusive) or an <EOT> character.
  757. If the input string is larger than x, read the full string and assign the truncated 0X-terminated
  758. value to x. *)
  759. PROCEDURE LnEOT*( VAR x: ARRAY OF CHAR );
  760. VAR i, m: LONGWORD; ch: CHAR;
  761. BEGIN
  762. i := 0; m := LEN( x ) - 1;
  763. LOOP
  764. ch := Peek();
  765. IF (ch = CR) OR (ch = LF) OR (ch = EOT) OR (res # Ok) THEN EXIT END;
  766. IF i < m THEN x[i] := ch; INC( i ) END;
  767. ch := Get()
  768. END;
  769. x[i] := 0X;
  770. IF ch = CR THEN ch := Get() END;
  771. IF Peek() = LF THEN ch := Get() END;
  772. IF ch = EOT THEN ch := Get() END
  773. END LnEOT;
  774. (** Skip over all characters until the end of the line (inclusive). *)
  775. PROCEDURE SkipLn*;
  776. VAR ch: CHAR;
  777. BEGIN
  778. LOOP
  779. ch := Peek();
  780. IF (ch = CR) OR (ch = LF) OR (res # Ok) THEN EXIT END;
  781. ch := Get()
  782. END;
  783. IF ch = CR THEN ch := Get() END;
  784. IF Peek() = LF THEN ch := Get() END
  785. END SkipLn;
  786. (** Skip over space and TAB characters. *)
  787. PROCEDURE SkipSpaces*;
  788. VAR ch: CHAR;
  789. BEGIN
  790. LOOP
  791. ch := Peek();
  792. IF (ch # TAB) & (ch # SP) THEN EXIT END;
  793. ch := Get()
  794. END
  795. END SkipSpaces;
  796. (** Skip over space, TAB and EOLN characters. *)
  797. PROCEDURE SkipWhitespace*;
  798. VAR ch: CHAR;
  799. BEGIN
  800. LOOP
  801. ch := Peek();
  802. IF (ch # SP) & (ch # CR) & (ch # LF) & (ch # TAB) THEN EXIT END;
  803. ch := Get()
  804. END
  805. END SkipWhitespace;
  806. (** Read a token, consisting of any string of characters terminated by space, TAB or EOLN. *)
  807. PROCEDURE Token*( VAR token: ARRAY OF CHAR );
  808. VAR j, max: LONGWORD; ch: CHAR;
  809. BEGIN
  810. j := 0; max := LEN( token ) - 1;
  811. LOOP
  812. ch := Peek();
  813. IF (ch = SP) OR (ch = CR) OR (ch = LF) OR (ch = TAB) OR (res # Ok) THEN EXIT END;
  814. IF j < max THEN token[j] := ch; INC( j ) END;
  815. ch := Get()
  816. END;
  817. token[j] := 0X
  818. END Token;
  819. (** Read an optionally "" or '' enquoted string. Will not read past the end of a line. *)
  820. PROCEDURE String*( VAR string: ARRAY OF CHAR );
  821. VAR c, delimiter: CHAR; i, len: LONGWORD;
  822. BEGIN
  823. c := Peek();
  824. IF (c # "'") & (c # '"') THEN Token( string )
  825. ELSE
  826. delimiter := Get(); c := Peek(); i := 0; len := LEN( string ) - 1;
  827. WHILE (i < len) & (c # delimiter) & (c # CR) & (c # LF) & (res = Ok) DO string[i] := Get(); INC( i ); c := Peek() END;
  828. IF (c = delimiter) THEN c := Get() END;
  829. string[i] := 0X
  830. END
  831. END String;
  832. (** First skip whitespace, then read string *)
  833. PROCEDURE GetString*(VAR string : ARRAY OF CHAR): BOOLEAN;
  834. VAR c: CHAR;
  835. BEGIN
  836. SkipWhitespace;
  837. c := Peek();
  838. String(string);
  839. RETURN (string[0] # 0X) OR (c = "'") OR (c = '"');
  840. END GetString;
  841. (** First skip whitespace, then read integer *)
  842. PROCEDURE GetInteger*(VAR integer : LONGINT; isHexadecimal : BOOLEAN): BOOLEAN;
  843. BEGIN
  844. SkipWhitespace;
  845. Int(integer, isHexadecimal);
  846. RETURN res = Ok;
  847. END GetInteger;
  848. (** First skip whitespace, then read a real *)
  849. PROCEDURE GetReal*(VAR real: LONGREAL): BOOLEAN;
  850. BEGIN
  851. SkipWhitespace;
  852. Real(real);
  853. RETURN res = Ok
  854. END GetReal;
  855. (** First skip whitespace, then read 1 byte character *)
  856. PROCEDURE GetChar*(VAR ch : CHAR): BOOLEAN;
  857. BEGIN
  858. SkipWhitespace;
  859. Char(ch);
  860. RETURN ch # 0X;
  861. END GetChar;
  862. END Reader;
  863. TYPE
  864. (** A special reader that buffers input set by SetString or SetRawString. *)
  865. StringReader* = OBJECT (Reader)
  866. PROCEDURE & InitStringReader*( size: LONGWORD );
  867. BEGIN
  868. InitReader( Receive, size )
  869. END InitStringReader;
  870. PROCEDURE CanSetPos*( ): BOOLEAN;
  871. BEGIN
  872. RETURN TRUE
  873. END CanSetPos;
  874. (** Set the reader position *)
  875. PROCEDURE SetPos*( pos: TSize );
  876. BEGIN
  877. IF pos > LEN( buf ) THEN pos := LEN( buf ) END;
  878. head := LONGWORD( pos ); tail := LEN( buf ); received := LEN( buf ); res := Ok;
  879. END SetPos;
  880. PROCEDURE Receive( VAR buf: ARRAY OF CHAR; ofs, size, min: LONGWORD; VAR len: LONGWORD; VAR res: WORD );
  881. BEGIN
  882. IF min = 0 THEN res := Ok ELSE res := EOF END;
  883. len := 0;
  884. END Receive;
  885. (** Set the contents of the string buffer. The s parameter is a 0X-terminated string. *)
  886. PROCEDURE Set*(CONST s: ARRAY OF CHAR );
  887. VAR len: LONGWORD;
  888. BEGIN
  889. len := 0;
  890. WHILE s[len] # 0X DO INC( len ) END;
  891. IF len > LEN( buf ) THEN len := LEN( buf ) END;
  892. head := 0; tail := len; received := len; res := Ok;
  893. IF len > 0 THEN
  894. SYSTEM.MOVE( ADDRESSOF( s[0] ), ADDRESSOF( buf[0] ), len )
  895. END;
  896. END Set;
  897. (** Set the contents of the string buffer. The len parameter specifies the size of the buffer s. *)
  898. PROCEDURE SetRaw*(CONST s: ARRAY OF CHAR; ofs, len: LONGWORD );
  899. BEGIN
  900. IF len > LEN( buf ) THEN len := LEN( buf ) END;
  901. head := 0; tail := len; received := len; res := Ok;
  902. ASSERT ( (len >= 0) & (ofs + len <= LEN( s )) ); (* index check *)
  903. IF len > 0 THEN
  904. SYSTEM.MOVE( ADDRESSOF( s[ofs] ), ADDRESSOF( buf[0] ), len )
  905. END;
  906. END SetRaw;
  907. END StringReader;
  908. Bytes2 = ARRAY 2 OF CHAR;
  909. Bytes4 = ARRAY 4 OF CHAR;
  910. Bytes8 = ARRAY 8 OF CHAR;
  911. VAR
  912. months: ARRAY 12 * 4 + 1 OF CHAR;
  913. (** Open a writer to the specified stream sender. Update must be called after writing to ensure the buffer is written to the stream. *)
  914. PROCEDURE OpenWriter*( VAR b: Writer; send: Sender );
  915. BEGIN
  916. NEW( b, send, DefaultWriterSize )
  917. END OpenWriter;
  918. (** Open a reader from the specified stream receiver. *)
  919. PROCEDURE OpenReader*( VAR b: Reader; receive: Receiver );
  920. BEGIN
  921. NEW( b, receive, DefaultReaderSize )
  922. END OpenReader;
  923. (** Copy the contents of a reader to a writer *)
  924. PROCEDURE Copy* (r: Reader; w: Writer);
  925. VAR char: CHAR;
  926. BEGIN
  927. WHILE r.res = Ok DO
  928. r.Char (char);
  929. IF r.res = Ok THEN w.Char (char) END
  930. END;
  931. END Copy;
  932. (** from module Reals.Mod *)
  933. (*** the following procedures stem from Reals.Mod and are needed for Writer.Float and Writer.FloatFix *)
  934. (** Returns the NaN code (0 <= h < 1048576, MIN(LONGINT) <= l <= MAX(LONGINT)) or (-1,-1) if not NaN/Infinite. *)
  935. PROCEDURE NaNCodeL( x: LONGREAL; VAR h, l: LONGINT );
  936. BEGIN
  937. SYSTEM.GET( ADDRESSOF( x ) + H, h ); SYSTEM.GET( ADDRESSOF( x ) + L, l );
  938. IF ASH( h, -20 ) MOD 2048 = 2047 THEN (* Infinite or NaN *)
  939. h := h MOD 100000H (* lowest 20 bits *)
  940. ELSE h := -1; l := -1
  941. END
  942. END NaNCodeL;
  943. (** Returns the shifted binary exponent (0 <= e < 2048). *)
  944. PROCEDURE ExpoL( x: LONGREAL ): LONGINT;
  945. VAR i: LONGINT;
  946. BEGIN
  947. SYSTEM.GET( ADDRESSOF( x ) + H, i ); RETURN ASH( i, -20 ) MOD 2048
  948. END ExpoL;
  949. (** Convert hexadecimal to LONGREAL. h and l are the high and low parts.*)
  950. PROCEDURE RealL( h, l: LONGINT ): LONGREAL;
  951. VAR x: LONGREAL;
  952. BEGIN
  953. SYSTEM.PUT( ADDRESSOF( x ) + H, h ); SYSTEM.PUT( ADDRESSOF( x ) + L, l ); RETURN x
  954. END RealL;
  955. (** Returns 10^e (e <= 308, 308 < e delivers IEEE-code +INF). *)
  956. PROCEDURE Ten( e: LONGINT ): LONGREAL; (* naiive version *)
  957. VAR r: LONGREAL;
  958. BEGIN
  959. IF e < -307 THEN RETURN 0
  960. ELSIF 308 < e THEN RETURN RealL( 2146435072, 0 )
  961. END;
  962. r := 1;
  963. WHILE (e > 0) DO r := r * 10; DEC( e ); END;
  964. WHILE (e < 0) DO r := r / 10; INC( e ); END;
  965. RETURN r;
  966. END Ten;
  967. PROCEDURE InitHL;
  968. VAR i: ADDRESS; dmy: INTEGER; littleEndian: BOOLEAN;
  969. BEGIN
  970. dmy := 1; i := ADDRESSOF( dmy );
  971. SYSTEM.GET( i, littleEndian ); (* indirection via i avoids warning on SUN cc -O *)
  972. IF littleEndian THEN H := 4; L := 0 ELSE H := 0; L := 4 END
  973. END InitHL;
  974. BEGIN
  975. months := " Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"; InitHL;
  976. END Streams64.
  977. (**
  978. Notes:
  979. o Any single buffer instance must not be accessed by more than one process concurrently.
  980. o The interface is blocking (synchronous). If an output buffer is full, it is written with a synchronous write, which returns
  981. only when all the data has been written. If an input buffer is empty, it is read with a synchronous read, which only returns
  982. once some data has been read. The only exception is the Available() procedure, which "peeks" at the input stream
  983. and returns 0 if no data is currently available.
  984. o All procedures set res to the error code reported by the lower-level I/O operation (non-zero indicates error).
  985. E.g. closing an underlying TCP connection will result in the Read* procedures returning a non-zero error code.
  986. o res is sticky. Once it becomes non-zero, it remains non-zero.
  987. 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.
  988. o All output written to an erroneous buffer is ignored.
  989. o The value returned when reading from an erroneous buffer is undefined, except for the Read procedure, which returns 0X.
  990. 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.
  991. o Raw format is little-endian 2's complement integers, IEEE reals and 0X-terminated strings.
  992. o Syntax for ReadInt with hex = FALSE: num = ["-"] digit {digit}. digit = "0".."9".
  993. o Syntax for ReadInt with hex = TRUE: ["-"] hexdigit {hexdigit} ["H"|"h"]. hexdigit = digit | "A".."F" | "a".."f".
  994. o ReadInt with hex = TRUE allows "A".."F" as digits, and looks for a "H" character after the number.
  995. If present, the number is interpreted as hexadecimal. If hexadecimal digits are present, but no "H" flag,
  996. the resulting decimal value is undefined.
  997. o ReadInt ignores overflow.
  998. 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,
  999. or an error occurs.
  1000. o A Receiver receives up to size bytes from input into buf at ofs and returns the number of bytes read in len.
  1001. It returns res non-zero on error. It waits until at least min bytes (possibly zero) are available, or an error occurs.
  1002. o EOLN and ReadLn recognize the following end-of-line characters: CR, LF and CR/LF.
  1003. o To read an unstructured file token-by-token: WHILE (r.res = 0) DO SkipWhitespace; ReadToken END
  1004. o To read a line structured file token-by-token: WHILE r.res = 0 DO SkipSpaces; WHILE ~EOLN DO ReadToken; SkipSpaces END END
  1005. o A string writer is not flushed when it becomes full, but res is set to a non-zero value.
  1006. o Update has no effect on a string writer.
  1007. o GetString can be called on a string writer to return the buffer contents and reset it to empty.
  1008. o GetString always appends a 0X character to the buffer, but returns the true length (excluding the added 0X) in the len parameter,
  1009. so it can also be used for binary data that includes 0X characters.
  1010. o Receive procedure should set res to EOF when attempting to read past the end of file.
  1011. *)
  1012. (*
  1013. to do:
  1014. o stream byte count
  1015. o read formatted data
  1016. o reads for all formatted writes
  1017. o write reals
  1018. o low-level version that can be used in kernel (below KernelLog)
  1019. *)