text.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. { $Id$
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1993,97 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {
  11. Possible Defines:
  12. EXTENDED_EOF Use extended EOF checking for textfile, necessary for
  13. Pipes and Sockets under Linux
  14. EOF_CTRLZ Is Ctrl-Z (#26) a EOF mark for textfiles
  15. SHORT_LINEBREAK Use short Linebreaks #10 instead of #10#13
  16. Both EXTENDED_EOF and SHORT_LINEBREAK are defined in the Linux system
  17. unit (syslinux.pp)
  18. }
  19. {****************************************************************************
  20. subroutines For TextFile handling
  21. ****************************************************************************}
  22. Procedure FileCloseFunc(Var t:TextRec);
  23. Begin
  24. Do_Close(t.Handle);
  25. t.Handle:=UnusedHandle;
  26. End;
  27. Procedure FileInOutFunc(var t:TextRec);
  28. Begin
  29. Case t.mode Of
  30. fmoutput : Do_Write(t.Handle,Longint(t.Bufptr),t.BufPos);
  31. fminput : t.BufEnd:=Do_Read(t.Handle,Longint(t.Bufptr),t.BufSize);
  32. else
  33. RunError(102);
  34. End;
  35. t.BufPos:=0;
  36. End;
  37. Procedure FileOpenFunc(var t:TextRec);
  38. var
  39. Flags : Longint;
  40. Begin
  41. t.InOutFunc:=@FileInOutFunc;
  42. t.FlushFunc:=@FileInOutFunc;
  43. t.CloseFunc:=@FileCloseFunc;
  44. Case t.mode Of
  45. fmInput : Flags:=$1000;
  46. fmOutput : Flags:=$1101;
  47. fmAppend : Flags:=$1011;
  48. End;
  49. Do_Open(t,PChar(@TextRec(t).Name),Flags);
  50. End;
  51. Procedure assign(var t:Text;const s:String);
  52. Begin
  53. FillChar(t,SizEof(TextRec),0);
  54. TextRec(t).Handle:=UnusedHandle;
  55. TextRec(t).mode:=fmClosed;
  56. TextRec(t).BufSize:=128;
  57. TextRec(t).Bufpos:=0;
  58. TextRec(T).Bufend:=0;
  59. TextRec(t).Bufptr:=@TextRec(t).Buffer;
  60. TextRec(t).OpenFunc:=@FileOpenFunc;
  61. Move(s[1],TextRec(t).Name,Length(s));
  62. End;
  63. Procedure assign(var t:Text;p:pchar);
  64. begin
  65. Assign(t,StrPas(p));
  66. end;
  67. Procedure assign(var t:Text;c:char);
  68. begin
  69. Assign(t,string(c));
  70. end;
  71. Procedure Close(var t : Text);[Public,Alias: 'CLOSE_TEXT',IOCheck];
  72. Begin
  73. If (TextRec(t).mode<>fmClosed) Then
  74. Begin
  75. FileFunc(TextRec(t).FlushFunc)(TextRec(t));
  76. TextRec(t).mode:=fmClosed;
  77. { Only close functions not connected to stdout.}
  78. If ((TextRec(t).Handle<>StdInputHandle) or
  79. (TextRec(t).Handle<>StdOutputHandle) or
  80. (TextRec(t).Handle<>StdErrorHandle)) Then
  81. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  82. End;
  83. End;
  84. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  85. Begin
  86. Case TextRec(t).mode Of {This gives the fastest code}
  87. fmInput,fmOutput,fmInOut : Close(t);
  88. fmClosed : ;
  89. else
  90. Begin
  91. InOutRes:=102;
  92. exit;
  93. End;
  94. End;
  95. TextRec(t).mode:=mode;
  96. { If TextRec(t).Name[0]<>#0 Then }
  97. FileFunc(TextRec(t).OpenFunc)(TextRec(t))
  98. { else
  99. Begin
  100. TextRec(t).Handle:=defHdl;
  101. TextRec(t).InOutFunc:=@FileInOutFunc;
  102. TextRec(t).FlushFunc:=@FileInOutFunc;
  103. TextRec(t).CloseFunc:=@FileCloseFunc;
  104. End; }
  105. End;
  106. Procedure Rewrite(var t : Text);[IOCheck];
  107. Begin
  108. OpenText(t,fmOutput,1);
  109. End;
  110. Procedure Reset(var t : Text);[IOCheck];
  111. Begin
  112. OpenText(t,fmInput,0);
  113. End;
  114. Procedure Append(var t : Text);[IOCheck];
  115. Begin
  116. OpenText(t,fmAppend,1);
  117. End;
  118. Procedure Flush(var t : Text);[IOCheck];
  119. Begin
  120. If TextRec(t).mode<>fmOutput Then
  121. exit;
  122. FileFunc(TextRec(t).FlushFunc)(TextRec(t));
  123. End;
  124. Procedure Erase(var t:Text);[IOCheck];
  125. Begin
  126. If TextRec(t).mode=fmClosed Then
  127. Do_Erase(PChar(@TextRec(t).Name));
  128. End;
  129. Procedure Rename(var t : text;p:pchar);[IOCheck];
  130. Begin
  131. If TextRec(t).mode=fmClosed Then
  132. Begin
  133. Do_Rename(PChar(@TextRec(t).Name),p);
  134. Move(p,TextRec(t).Name,StrLen(p)+1);
  135. End;
  136. End;
  137. Procedure Rename(var t : Text;const s : string);[IOCheck];
  138. var
  139. p : array[0..255] Of Char;
  140. Begin
  141. Move(s[1],p,Length(s));
  142. p[Length(s)]:=#0;
  143. Rename(t,Pchar(@p));
  144. End;
  145. Procedure Rename(var t : Text;c : char);[IOCheck];
  146. var
  147. p : array[0..1] Of Char;
  148. Begin
  149. p[0]:=c;
  150. p[1]:=#0;
  151. Rename(t,Pchar(@p));
  152. End;
  153. Function Eof(Var t: Text): Boolean;[IOCheck];
  154. Begin
  155. {$IFNDEF EXTENDED_EOF}
  156. {$IFDEF EOF_CTRLZ}
  157. Eof:=TextRec(t).Buffer[TextRec(t).BufPos]=#26;
  158. If Eof Then
  159. Exit;
  160. {$ENDIF EOL_CTRLZ}
  161. Eof:=(Do_FileSize(TextRec(t).Handle)<=Do_FilePos(TextRec(t).Handle));
  162. If Eof Then
  163. Eof:=TextRec(t).BufEnd <= TextRec(t).BufPos;
  164. {$ELSE EXTENDED_EOF}
  165. { The previous method will NOT work on stdin and pipes or sockets.
  166. So how to do it ?
  167. 1) Check if characters in buffer - Yes ? Eof=false;
  168. 2) Read buffer full. If 0 Chars Read : Eof !
  169. Michael.}
  170. If TextRec(T).mode=fmClosed Then { Sanity Check }
  171. Begin
  172. Eof:=True;
  173. Exit;
  174. End;
  175. If (TextRec(T).BufPos < TextRec(T).BufEnd) Then
  176. Begin
  177. Eof:=False;
  178. Exit
  179. End;
  180. TextRec(T).BufPos:=0;
  181. TextRec(T).BufEnd:=Do_Read(TextRec(T).Handle,Longint(TextRec(T).BufPtr),TextRec(T).BufSize);
  182. If TextRec(T).BufEnd<0 Then
  183. TextRec(T).BufEnd:=0;
  184. Eof:=(TextRec(T).BufEnd=0);
  185. {$ENDIF EXTENDED_EOF}
  186. End;
  187. Function Eof:Boolean;
  188. Begin
  189. Eof:=Eof(Input);
  190. End;
  191. Function SeekEof (Var F : Text) : Boolean;
  192. Var
  193. TR : ^TextRec;
  194. Temp : Longint;
  195. Begin
  196. TR:=@TextRec(f);
  197. If TR^.mode<>fmInput Then exit (true);
  198. SeekEof:=True;
  199. {No data in buffer ? Fill it }
  200. If TR^.BufPos>=TR^.BufEnd Then
  201. FileFunc(TR^.InOutFunc)(TR^);
  202. Temp:=TR^.BufPos;
  203. while (TR^.BufPos<TR^.BufEnd) Do
  204. Begin
  205. If (TR^.Bufptr^[Temp] In [#9,#10,#13,' ']) Then
  206. Inc(Temp)
  207. else
  208. Begin
  209. SeekEof:=False;
  210. TR^.BufPos:=Temp;
  211. exit;
  212. End;
  213. If Temp>=TR^.BufEnd Then
  214. Begin
  215. FileFunc(TR^.InOutFunc)(TR^);
  216. Temp:=TR^.BufPos+1;
  217. End;
  218. End;
  219. End;
  220. Function SeekEof : Boolean;
  221. Begin
  222. SeekEof:=SeekEof(Input);
  223. End;
  224. Function Eoln(var t:Text) : Boolean;
  225. Begin
  226. { maybe we need new data }
  227. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  228. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  229. Eoln:=Eof(t) or (TextRec(t).Bufptr^[TextRec(t).BufPos] In [#10,#13]);
  230. End;
  231. Function Eoln : Boolean;
  232. Begin
  233. Eoln:=Eoln(Input);
  234. End;
  235. Function SeekEoln (Var F : Text) : Boolean;
  236. Var
  237. TR : ^TextRec;
  238. Temp : Longint;
  239. Begin
  240. TR:=@TextRec(f);
  241. If TR^.mode<>fmInput Then
  242. exit (true);
  243. SeekEoln:=True;
  244. {No data in buffer ? Fill it }
  245. If TR^.BufPos>=TR^.BufEnd Then
  246. FileFunc(TR^.InOutFunc)(TR^);
  247. Temp:=TR^.BufPos;
  248. while (TR^.BufPos<TR^.BufEnd) Do
  249. Begin
  250. Case (TR^.Bufptr^[Temp]) Of
  251. #10 : Exit;
  252. #9,' ' : Inc(Temp)
  253. else
  254. Begin
  255. SeekEoln:=False;
  256. TR^.BufPos:=Temp;
  257. exit;
  258. End;
  259. End;
  260. If Temp>=TR^.BufEnd Then
  261. Begin
  262. FileFunc(TR^.InOutFunc)(TR^);
  263. Temp:=TR^.BufPos+1;
  264. End;
  265. End;
  266. End;
  267. Function SeekEoln : Boolean;
  268. Begin
  269. SeekEoln:=SeekEoln(Input);
  270. End;
  271. Procedure SetTextBuf(Var F : Text; Var Buf);[INTERNPROC: In_settextbuf_file_x];
  272. Procedure SetTextBuf(Var F : Text; Var Buf; Size : Word);
  273. Begin
  274. TextRec(f).BufPtr:=@Buf;
  275. TextRec(f).BufSize:=Size;
  276. TextRec(f).BufPos:=0;
  277. TextRec(f).BufEnd:=0;
  278. End;
  279. {*****************************************************************************
  280. Write(Ln)
  281. *****************************************************************************}
  282. Procedure w(Len : Longint;var f : TextRec;var s : String);[Public,Alias: 'WRITE_TEXT_STRING'];
  283. var
  284. hbytes,Pos,copybytes : Longint;
  285. hs : String;
  286. Begin
  287. If f.mode<>fmOutput Then
  288. exit;
  289. copybytes:=Length(s);
  290. If Len>copybytes Then
  291. Begin
  292. hs:=Space(Len-copybytes);
  293. w(0,f,hs);
  294. End;
  295. Pos:=1;
  296. hbytes:=f.BufSize-f.BufPos;
  297. { If no room in Buffer, do a flush. }
  298. If hbytes=0 Then
  299. FileFunc(f.FlushFunc)(f);
  300. while copybytes>hbytes Do
  301. Begin
  302. Move(s[Pos],f.Bufptr^[f.BufPos],hbytes);
  303. f.BufPos:=f.BufPos+hbytes;
  304. dec(copybytes,hbytes);
  305. Inc(Pos,hbytes);
  306. FileFunc(f.InOutFunc)(f);
  307. hbytes:=f.BufSize-f.BufPos;
  308. End;
  309. Move(s[Pos],f.Bufptr^[f.BufPos],copybytes);
  310. f.BufPos:=f.BufPos+copybytes;
  311. End;
  312. Procedure w(var t : TextRec);[Public,Alias: 'WRITELN_TEXT'];
  313. var
  314. hs : String;
  315. Begin
  316. {$IFDEF SHORT_LINEBREAK}
  317. hs:=#10;
  318. {$ELSE}
  319. hs:=#13#10;
  320. {$ENDIF}
  321. w(0,t,hs);
  322. End;
  323. Type
  324. array00 = array[0..0] Of Char;
  325. Procedure w(Len : Longint;var f : TextRec;const p : array00);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_ARRAY'];
  326. var
  327. hbytes,Pos,copybytes : Longint;
  328. hs : String;
  329. Begin
  330. If f.mode<>fmOutput Then
  331. exit;
  332. copybytes:=StrLen(p);
  333. If Len>copybytes Then
  334. Begin
  335. hs:=Space(Len-copybytes);
  336. w(0,f,hs);
  337. End;
  338. Pos:=0;
  339. hbytes:=f.BufSize-f.BufPos;
  340. { If no room in buffer , do a flush. }
  341. If hbytes=0 Then
  342. FileFunc(f.FlushFunc)(f);
  343. while copybytes>hbytes Do
  344. Begin
  345. Move(p[Pos],f.Bufptr^[f.BufPos],hbytes);
  346. f.BufPos:=f.BufPos+hbytes;
  347. dec(copybytes,hbytes);
  348. Inc(Pos,hbytes);
  349. FileFunc(f.InOutFunc)(f);
  350. hbytes:=f.BufSize-f.BufPos;
  351. End;
  352. Move(p[Pos],f.Bufptr^[f.BufPos],copybytes);
  353. f.BufPos:=f.BufPos+copybytes;
  354. End;
  355. Procedure wa(Len : Longint;var f : TextRec;p : PChar);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_POINTER'];
  356. Begin
  357. w(Len,f,p);
  358. End;
  359. Procedure w(Len : Longint;var t : TextRec;l : Longint);[Public,Alias: 'WRITE_TEXT_LONGINT'];
  360. var
  361. s : String;
  362. Begin
  363. Str(l,s);
  364. w(Len,t,s);
  365. End;
  366. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : real);[Public,Alias: 'WRITE_TEXT_REAL'];
  367. var
  368. s : String;
  369. Begin
  370. {$ifdef i386}
  371. Str_real(Len,fixkomma,r,rt_s64real,s);
  372. {$else}
  373. Str_real(Len,fixkomma,r,rt_s32real,s);
  374. {$endif}
  375. w(Len,t,s);
  376. End;
  377. Procedure w(Len : Longint;var t : TextRec;l : cardinal);[Public,Alias: 'WRITE_TEXT_CARDINAL'];
  378. var
  379. s : String;
  380. Begin
  381. Str(L,s);
  382. w(Len,t,s);
  383. End;
  384. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : single);[Public,Alias: 'WRITE_TEXT_SINGLE'];
  385. var
  386. s : String;
  387. Begin
  388. Str_real(Len,fixkomma,r,rt_s32real,s);
  389. w(Len,t,s);
  390. End;
  391. {$ifdef SUPPORT_EXTENDED}
  392. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : extended);[Public,Alias: 'WRITE_TEXT_EXTENDED'];
  393. var
  394. s : String;
  395. Begin
  396. Str_real(Len,fixkomma,r,rt_s80real,s);
  397. w(Len,t,s);
  398. End;
  399. {$endif SUPPORT_EXTENDED}
  400. {$ifdef SUPPORT_COMP}
  401. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : comp);[Public,Alias: 'WRITE_TEXT_COMP'];
  402. var
  403. s : String;
  404. Begin
  405. Str_real(Len,fixkomma,r,rt_s64bit,s);
  406. w(Len,t,s);
  407. End;
  408. {$endif SUPPORT_COMP}
  409. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : fixed);[Public,Alias: 'WRITE_TEXT_FIXED'];
  410. var
  411. s : String;
  412. Begin
  413. Str_real(Len,fixkomma,r,rt_f32bit,s);
  414. w(Len,t,s);
  415. End;
  416. { Is called wc to avoid recursive calling. }
  417. Procedure wc(Len : Longint;var t : TextRec;b : Boolean);[Public,Alias: 'WRITE_TEXT_BOOLEAN'];
  418. const
  419. BoolString:array[0..1] Of String[5]=('False','True');
  420. Begin
  421. if b then
  422. w(Len,t,String(BoolString[1]))
  423. else
  424. w(Len,t,String(BoolString[0]));
  425. End;
  426. Procedure wc(Len : Longint;var t : TextRec;c : Char);[Public,Alias: 'WRITE_TEXT_CHAR'];
  427. var
  428. hs : String;
  429. Begin
  430. If t.mode<>fmOutput Then
  431. exit;
  432. If Len>1 Then
  433. Begin
  434. hs:=Space(Len-1);
  435. w(0,t,hs);
  436. End;
  437. If t.BufPos+1>=t.BufSize Then
  438. FileFunc(t.FlushFunc)(t);
  439. t.Bufptr^[t.BufPos]:=c;
  440. Inc(t.BufPos);
  441. End;
  442. {*****************************************************************************
  443. Read(Ln)
  444. *****************************************************************************}
  445. Function OpenInput(var f:TextRec):boolean;
  446. begin
  447. If f.mode=fmInput Then
  448. begin
  449. { No characters in the buffer? Load them ! }
  450. If f.BufPos>=f.BufEnd Then
  451. FileFunc(f.InOutFunc)(f);
  452. OpenInput:=true;
  453. end
  454. else
  455. OpenInput:=false;
  456. end;
  457. Function NextChar(var f:TextRec;var s:string):Boolean;
  458. begin
  459. if f.BufPos<f.BufEnd then
  460. begin
  461. s:=s+f.BufPtr^[f.BufPos];
  462. Inc(f.BufPos);
  463. If f.BufPos>=f.BufEnd Then
  464. FileFunc(f.InOutFunc)(f);
  465. NextChar:=true;
  466. end
  467. else
  468. NextChar:=false;
  469. end;
  470. Function IgnoreSpaces(var f:TextRec):Boolean;
  471. {
  472. Removes all leading spaces,tab,eols from the input buffer, returns true if
  473. the buffer is empty
  474. }
  475. var
  476. s : string;
  477. begin
  478. s:='';
  479. IgnoreSpaces:=false;
  480. while f.Bufptr^[f.BufPos] in [#9,#10,#13,' '] do
  481. if not NextChar(f,s) then
  482. exit;
  483. IgnoreSpaces:=true;
  484. end;
  485. Function ReadSign(var f:TextRec;var s:string):Boolean;
  486. {
  487. Read + and - sign, return true if buffer is empty
  488. }
  489. begin
  490. ReadSign:=(not (f.Bufptr^[f.BufPos] in ['-','+'])) or NextChar(f,s);
  491. end;
  492. Function ReadBase(var f:TextRec;var s:string;var Base:longint):boolean;
  493. {
  494. Read the base $ For 16 and % For 2, if buffer is empty return true
  495. }
  496. begin
  497. case f.BufPtr^[f.BufPos] of
  498. '$' : Base:=16;
  499. '%' : Base:=2;
  500. else
  501. Base:=10;
  502. end;
  503. ReadBase:=(Base=10) or NextChar(f,s);
  504. end;
  505. Function ReadNumeric(var f:TextRec;var s:string;base:longint):Boolean;
  506. {
  507. Read numeric input, if buffer is empty then return True
  508. }
  509. var
  510. c : char;
  511. begin
  512. ReadNumeric:=false;
  513. c:=f.BufPtr^[f.BufPos];
  514. while ((base>=10) and (c in ['0'..'9'])) or
  515. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  516. ((base=2) and (c in ['0'..'1'])) do
  517. begin
  518. if not NextChar(f,s) then
  519. exit;
  520. c:=f.BufPtr^[f.BufPos];
  521. end;
  522. ReadNumeric:=true;
  523. end;
  524. Procedure r(var f : TextRec);[Public,Alias: 'READLN_TEXT'];
  525. Begin
  526. if not OpenInput(f) then
  527. exit;
  528. while (f.BufPos<f.BufEnd) do
  529. begin
  530. inc(f.BufPos);
  531. if (f.BufPtr^[f.BufPos-1]=#10) then
  532. exit;
  533. If f.BufPos>=f.BufEnd Then
  534. FileFunc(f.InOutFunc)(f);
  535. end;
  536. End;
  537. Procedure r(var f : TextRec;var s : String);[Public,Alias: 'READ_TEXT_STRING'];
  538. var
  539. Temp,sPos : Word;
  540. Begin
  541. { Delete the string }
  542. s:='';
  543. if not OpenInput(f) then
  544. exit;
  545. Temp:=f.BufPos;
  546. sPos:=1;
  547. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  548. Begin
  549. { search linefeed }
  550. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  551. Inc(Temp);
  552. { copy String. Take 255 char limit in account.}
  553. If sPos+Temp-f.BufPos<=255 Then
  554. Begin
  555. Move (f.Bufptr^[f.BufPos],s[sPos],Temp-f.BufPos);
  556. sPos:=sPos+Temp-f.BufPos;
  557. If s[sPos-1]=#13 Then
  558. dec(sPos);
  559. End
  560. else
  561. Begin
  562. If (sPos<=255) Then
  563. Move(f.Bufptr^[f.BufPos],s[sPos],256-sPos);
  564. sPos:=256
  565. End;
  566. { update f.BufPos }
  567. f.BufPos:=Temp;
  568. If Temp>=f.BufEnd Then
  569. Begin
  570. FileFunc(f.InOutFunc)(f);
  571. Temp:=f.BufPos;
  572. End
  573. End;
  574. s[0]:=chr(sPos-1);
  575. End;
  576. Procedure r(var f : TextRec;var c : Char);[Public,Alias: 'READ_TEXT_CHAR'];
  577. Begin
  578. c:=#0;
  579. if not OpenInput(f) then
  580. exit;
  581. If f.BufPos>=f.BufEnd Then
  582. c:=#26
  583. else
  584. c:=f.Bufptr^[f.BufPos];
  585. Inc(f.BufPos);
  586. End;
  587. Procedure r(var f : TextRec;var s : PChar);[Public,Alias:'READ_TEXT_PCHAR_AS_POINTER'];
  588. var
  589. p : PChar;
  590. Temp : byte;
  591. Begin
  592. { Delete the string }
  593. s^:=#0;
  594. p:=s;
  595. if not OpenInput(f) then
  596. exit;
  597. Temp:=f.BufPos;
  598. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  599. Begin
  600. { search linefeed }
  601. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  602. inc(Temp);
  603. { copy string. }
  604. Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
  605. Inc(Longint(p),Temp-f.BufPos);
  606. If pchar(p-1)^=#13 Then
  607. dec(p);
  608. { update f.BufPos }
  609. f.BufPos:=Temp;
  610. If Temp>=f.BufEnd Then
  611. Begin
  612. FileFunc(f.InOutFunc)(f);
  613. Temp:=f.BufPos;
  614. End
  615. End;
  616. p^:=#0;
  617. End;
  618. Procedure r(var f : TextRec;var s : array00);[Public,Alias:'READ_TEXT_PCHAR_AS_ARRAY'];
  619. var
  620. p : PChar;
  621. Temp : byte;
  622. Begin
  623. { Delete the string }
  624. s[0]:=#0;
  625. p:=pchar(@s);
  626. if not OpenInput(f) then
  627. exit;
  628. Temp:=f.BufPos;
  629. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  630. Begin
  631. { search linefeed }
  632. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  633. inc(Temp);
  634. { copy string. }
  635. Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
  636. Inc(Longint(p),Temp-f.BufPos);
  637. If pchar(p-1)^=#13 Then
  638. dec(p);
  639. { update f.BufPos }
  640. f.BufPos:=Temp;
  641. If Temp>=f.BufEnd Then
  642. Begin
  643. FileFunc(f.InOutFunc)(f);
  644. Temp:=f.BufPos;
  645. End
  646. End;
  647. p^:=#0;
  648. End;
  649. Procedure r(var f : TextRec;var l : Longint);[Public,Alias: 'READ_TEXT_LONGINT'];
  650. var
  651. hs : String;
  652. code : Word;
  653. base : longint;
  654. Begin
  655. l:=0;
  656. hs:='';
  657. if not OpenInput(f) then
  658. exit;
  659. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  660. ReadNumeric(f,hs,Base);
  661. Val(hs,l,code);
  662. If code<>0 Then
  663. RunError(106);
  664. End;
  665. Procedure r(var f : TextRec;var l : Integer);[Public,Alias: 'READ_TEXT_INTEGER'];
  666. var
  667. ll : Longint;
  668. Begin
  669. r(f,ll);
  670. l:=0;
  671. If (ll<-32768) or (ll>32767) Then
  672. RunError(106);
  673. l:=ll;
  674. End;
  675. Procedure r(var f : TextRec;var l : Word);[Public,Alias: 'READ_TEXT_WORD'];
  676. var
  677. ll : Longint;
  678. Begin
  679. r(f,ll);
  680. l:=0;
  681. If (ll<0) or (ll>$ffff) Then
  682. RunError(106);
  683. l:=ll;
  684. End;
  685. Procedure r(var f : TextRec;var l : byte);[Public,Alias: 'READ_TEXT_BYTE'];
  686. var
  687. ll : Longint;
  688. Begin
  689. r(f,ll);
  690. l:=0;
  691. If (ll<0) or (ll>255) Then
  692. RunError(106);
  693. l:=ll;
  694. End;
  695. Procedure r(var f : TextRec;var l : shortint);[Public,Alias: 'READ_TEXT_SHORTINT'];
  696. var
  697. ll : Longint;
  698. Begin
  699. r(f,ll);
  700. l:=0;
  701. If (ll<-128) or (ll>127) Then
  702. RunError(106);
  703. l:=ll;
  704. End;
  705. Procedure r(var f : TextRec;var l : cardinal);[Public,Alias: 'READ_TEXT_CARDINAL'];
  706. var
  707. hs : String;
  708. code : Word;
  709. base : longint;
  710. Begin
  711. l:=0;
  712. hs:='';
  713. if not OpenInput(f) then
  714. exit;
  715. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  716. ReadNumeric(f,hs,Base);
  717. val(hs,l,code);
  718. If code<>0 Then
  719. RunError(106);
  720. End;
  721. Procedure r(var f : TextRec;var d : Real);[Public,Alias: 'READ_TEXT_REAL'];
  722. var
  723. hs : String;
  724. code : Word;
  725. Begin
  726. d:=0.0;
  727. hs:='';
  728. if not OpenInput(f) then
  729. exit;
  730. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  731. begin
  732. { First check for a . }
  733. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  734. begin
  735. hs:=hs+'.';
  736. Inc(f.BufPos);
  737. If f.BufPos>=f.BufEnd Then
  738. FileFunc(f.InOutFunc)(f);
  739. ReadNumeric(f,hs,10);
  740. end;
  741. { Also when a point is found check for a E }
  742. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  743. begin
  744. hs:=hs+'E';
  745. Inc(f.BufPos);
  746. If f.BufPos>=f.BufEnd Then
  747. FileFunc(f.InOutFunc)(f);
  748. if ReadSign(f,hs) then
  749. ReadNumeric(f,hs,10);
  750. end;
  751. end;
  752. val(hs,d,code);
  753. If code<>0 Then
  754. RunError(106);
  755. End;
  756. {$ifdef SUPPORT_EXTENDED}
  757. Procedure r(var f : TextRec;var d : extended);[Public,Alias: 'READ_TEXT_EXTENDED'];
  758. var
  759. hs : String;
  760. code : Word;
  761. Begin
  762. d:=0.0;
  763. hs:='';
  764. if not OpenInput(f) then
  765. exit;
  766. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  767. begin
  768. { First check for a . }
  769. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  770. begin
  771. hs:=hs+'.';
  772. Inc(f.BufPos);
  773. If f.BufPos>=f.BufEnd Then
  774. FileFunc(f.InOutFunc)(f);
  775. ReadNumeric(f,hs,10);
  776. end;
  777. { Also when a point is found check for a E }
  778. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  779. begin
  780. hs:=hs+'E';
  781. Inc(f.BufPos);
  782. If f.BufPos>=f.BufEnd Then
  783. FileFunc(f.InOutFunc)(f);
  784. if ReadSign(f,hs) then
  785. ReadNumeric(f,hs,10);
  786. end;
  787. end;
  788. val(hs,d,code);
  789. If code<>0 Then
  790. RunError(106);
  791. End;
  792. {$endif SUPPORT_EXTENDED}
  793. {$ifdef SUPPORT_COMP}
  794. Procedure r(var f : TextRec;var d : comp);[Public,Alias: 'READ_TEXT_COMP'];
  795. var
  796. hs : String;
  797. code : Word;
  798. Begin
  799. d:=comp(0.0);
  800. hs:='';
  801. if not OpenInput(f) then
  802. exit;
  803. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  804. begin
  805. { First check for a . }
  806. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  807. begin
  808. hs:=hs+'.';
  809. Inc(f.BufPos);
  810. If f.BufPos>=f.BufEnd Then
  811. FileFunc(f.InOutFunc)(f);
  812. ReadNumeric(f,hs,10);
  813. end;
  814. { Also when a point is found check for a E }
  815. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  816. begin
  817. hs:=hs+'E';
  818. Inc(f.BufPos);
  819. If f.BufPos>=f.BufEnd Then
  820. FileFunc(f.InOutFunc)(f);
  821. if ReadSign(f,hs) then
  822. ReadNumeric(f,hs,10);
  823. end;
  824. end;
  825. val(hs,d,code);
  826. If code<>0 Then
  827. RunError(106);
  828. End;
  829. {$endif SUPPORT_COMP}
  830. {
  831. $Log$
  832. Revision 1.10 1998-06-04 23:46:03 peter
  833. * comp,extended are only i386 added support_comp,support_extended
  834. Revision 1.9 1998/06/02 16:47:56 pierre
  835. * bug for boolean values greater than one fixed
  836. Revision 1.8 1998/05/31 14:14:54 peter
  837. * removed warnings using comp()
  838. Revision 1.7 1998/05/27 00:19:21 peter
  839. * fixed crt input
  840. Revision 1.6 1998/05/21 19:31:01 peter
  841. * objects compiles for linux
  842. + assign(pchar), assign(char), rename(pchar), rename(char)
  843. * fixed read_text_as_array
  844. + read_text_as_pchar which was not yet in the rtl
  845. Revision 1.5 1998/05/12 10:42:45 peter
  846. * moved getopts to inc/, all supported OS's need argc,argv exported
  847. + strpas, strlen are now exported in the systemunit
  848. * removed logs
  849. * removed $ifdef ver_above
  850. Revision 1.4 1998/04/07 22:40:46 florian
  851. * final fix of comp writing
  852. }