Streams.Mod 36 KB

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