text.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : extended);[Public,Alias: 'WRITE_TEXT_EXTENDED'];
  392. var
  393. s : String;
  394. Begin
  395. Str_real(Len,fixkomma,r,rt_s80real,s);
  396. w(Len,t,s);
  397. End;
  398. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : comp);[Public,Alias: 'WRITE_TEXT_COMP'];
  399. var
  400. s : String;
  401. Begin
  402. Str_real(Len,fixkomma,r,rt_s64bit,s);
  403. w(Len,t,s);
  404. End;
  405. Procedure w(fixkomma,Len : Longint;var t : TextRec;r : fixed);[Public,Alias: 'WRITE_TEXT_FIXED'];
  406. var
  407. s : String;
  408. Begin
  409. Str_real(Len,fixkomma,r,rt_f32bit,s);
  410. w(Len,t,s);
  411. End;
  412. { Is called wc to avoid recursive calling. }
  413. Procedure wc(Len : Longint;var t : TextRec;b : Boolean);[Public,Alias: 'WRITE_TEXT_BOOLEAN'];
  414. const
  415. BoolString:array[0..1] Of String[5]=('False','True');
  416. Begin
  417. w(Len,t,String(BoolString[byte(b)]));
  418. End;
  419. Procedure wc(Len : Longint;var t : TextRec;c : Char);[Public,Alias: 'WRITE_TEXT_CHAR'];
  420. var
  421. hs : String;
  422. Begin
  423. If t.mode<>fmOutput Then
  424. exit;
  425. If Len>1 Then
  426. Begin
  427. hs:=Space(Len-1);
  428. w(0,t,hs);
  429. End;
  430. If t.BufPos+1>=t.BufSize Then
  431. FileFunc(t.FlushFunc)(t);
  432. t.Bufptr^[t.BufPos]:=c;
  433. Inc(t.BufPos);
  434. End;
  435. {*****************************************************************************
  436. Read(Ln)
  437. *****************************************************************************}
  438. Function OpenInput(var f:TextRec):boolean;
  439. begin
  440. If f.mode=fmInput Then
  441. begin
  442. { No characters in the buffer? Load them ! }
  443. If f.BufPos>=f.BufEnd Then
  444. FileFunc(f.InOutFunc)(f);
  445. OpenInput:=true;
  446. end
  447. else
  448. OpenInput:=false;
  449. end;
  450. Function NextChar(var f:TextRec;var s:string):Boolean;
  451. begin
  452. if f.BufPos<f.BufEnd then
  453. begin
  454. s:=s+f.BufPtr^[f.BufPos];
  455. Inc(f.BufPos);
  456. If f.BufPos>=f.BufEnd Then
  457. FileFunc(f.InOutFunc)(f);
  458. NextChar:=true;
  459. end
  460. else
  461. NextChar:=false;
  462. end;
  463. Function IgnoreSpaces(var f:TextRec):Boolean;
  464. {
  465. Removes all leading spaces,tab,eols from the input buffer, returns true if
  466. the buffer is empty
  467. }
  468. var
  469. s : string;
  470. begin
  471. s:='';
  472. IgnoreSpaces:=false;
  473. while f.Bufptr^[f.BufPos] in [#9,#10,#13,' '] do
  474. if not NextChar(f,s) then
  475. exit;
  476. IgnoreSpaces:=true;
  477. end;
  478. Function ReadSign(var f:TextRec;var s:string):Boolean;
  479. {
  480. Read + and - sign, return true if buffer is empty
  481. }
  482. begin
  483. ReadSign:=(not (f.Bufptr^[f.BufPos] in ['-','+'])) or NextChar(f,s);
  484. end;
  485. Function ReadBase(var f:TextRec;var s:string;var Base:longint):boolean;
  486. {
  487. Read the base $ For 16 and % For 2, if buffer is empty return true
  488. }
  489. begin
  490. case f.BufPtr^[f.BufPos] of
  491. '$' : Base:=16;
  492. '%' : Base:=2;
  493. else
  494. Base:=10;
  495. end;
  496. ReadBase:=(Base=10) or NextChar(f,s);
  497. end;
  498. Function ReadNumeric(var f:TextRec;var s:string;base:longint):Boolean;
  499. {
  500. Read numeric input, if buffer is empty then return True
  501. }
  502. var
  503. c : char;
  504. begin
  505. ReadNumeric:=false;
  506. c:=f.BufPtr^[f.BufPos];
  507. while ((base>=10) and (c in ['0'..'9'])) or
  508. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  509. ((base=2) and (c in ['0'..'1'])) do
  510. begin
  511. if not NextChar(f,s) then
  512. exit;
  513. c:=f.BufPtr^[f.BufPos];
  514. end;
  515. ReadNumeric:=true;
  516. end;
  517. Procedure r(var f : TextRec);[Public,Alias: 'READLN_TEXT'];
  518. Begin
  519. if not OpenInput(f) then
  520. exit;
  521. while (f.BufPos<f.BufEnd) do
  522. begin
  523. inc(f.BufPos);
  524. if (f.BufPtr^[f.BufPos-1]=#10) then
  525. exit;
  526. If f.BufPos>=f.BufEnd Then
  527. FileFunc(f.InOutFunc)(f);
  528. end;
  529. End;
  530. Procedure r(var f : TextRec;var s : String);[Public,Alias: 'READ_TEXT_STRING'];
  531. var
  532. Temp,sPos : Word;
  533. Begin
  534. { Delete the string }
  535. s:='';
  536. if not OpenInput(f) then
  537. exit;
  538. Temp:=f.BufPos;
  539. sPos:=1;
  540. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  541. Begin
  542. { search linefeed }
  543. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  544. Inc(Temp);
  545. { copy String. Take 255 char limit in account.}
  546. If sPos+Temp-f.BufPos<=255 Then
  547. Begin
  548. Move (f.Bufptr^[f.BufPos],s[sPos],Temp-f.BufPos);
  549. sPos:=sPos+Temp-f.BufPos;
  550. If s[sPos-1]=#13 Then
  551. dec(sPos);
  552. End
  553. else
  554. Begin
  555. If (sPos<=255) Then
  556. Move(f.Bufptr^[f.BufPos],s[sPos],256-sPos);
  557. sPos:=256
  558. End;
  559. { update f.BufPos }
  560. f.BufPos:=Temp;
  561. If Temp>=f.BufEnd Then
  562. Begin
  563. FileFunc(f.InOutFunc)(f);
  564. Temp:=f.BufPos;
  565. End
  566. End;
  567. s[0]:=chr(sPos-1);
  568. End;
  569. Procedure r(var f : TextRec;var c : Char);[Public,Alias: 'READ_TEXT_CHAR'];
  570. Begin
  571. c:=#0;
  572. if not OpenInput(f) then
  573. exit;
  574. If f.BufPos>=f.BufEnd Then
  575. c:=#26
  576. else
  577. c:=f.Bufptr^[f.BufPos];
  578. Inc(f.BufPos);
  579. End;
  580. Procedure r(var f : TextRec;var s : PChar);[Public,Alias:'READ_TEXT_PCHAR_AS_POINTER'];
  581. var
  582. p : PChar;
  583. Temp : byte;
  584. Begin
  585. { Delete the string }
  586. s^:=#0;
  587. p:=s;
  588. if not OpenInput(f) then
  589. exit;
  590. Temp:=f.BufPos;
  591. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  592. Begin
  593. { search linefeed }
  594. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  595. inc(Temp);
  596. { copy string. }
  597. Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
  598. Inc(Longint(p),Temp-f.BufPos);
  599. If pchar(p-1)^=#13 Then
  600. dec(p);
  601. { update f.BufPos }
  602. f.BufPos:=Temp;
  603. If Temp>=f.BufEnd Then
  604. Begin
  605. FileFunc(f.InOutFunc)(f);
  606. Temp:=f.BufPos;
  607. End
  608. End;
  609. p^:=#0;
  610. End;
  611. Procedure r(var f : TextRec;var s : array00);[Public,Alias:'READ_TEXT_PCHAR_AS_ARRAY'];
  612. var
  613. p : PChar;
  614. Temp : byte;
  615. Begin
  616. { Delete the string }
  617. s[0]:=#0;
  618. p:=pchar(@s);
  619. if not OpenInput(f) then
  620. exit;
  621. Temp:=f.BufPos;
  622. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  623. Begin
  624. { search linefeed }
  625. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  626. inc(Temp);
  627. { copy string. }
  628. Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
  629. Inc(Longint(p),Temp-f.BufPos);
  630. If pchar(p-1)^=#13 Then
  631. dec(p);
  632. { update f.BufPos }
  633. f.BufPos:=Temp;
  634. If Temp>=f.BufEnd Then
  635. Begin
  636. FileFunc(f.InOutFunc)(f);
  637. Temp:=f.BufPos;
  638. End
  639. End;
  640. p^:=#0;
  641. End;
  642. Procedure r(var f : TextRec;var l : Longint);[Public,Alias: 'READ_TEXT_LONGINT'];
  643. var
  644. hs : String;
  645. code : Word;
  646. base : longint;
  647. Begin
  648. l:=0;
  649. hs:='';
  650. if not OpenInput(f) then
  651. exit;
  652. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  653. ReadNumeric(f,hs,Base);
  654. Val(hs,l,code);
  655. If code<>0 Then
  656. RunError(106);
  657. End;
  658. Procedure r(var f : TextRec;var l : Integer);[Public,Alias: 'READ_TEXT_INTEGER'];
  659. var
  660. ll : Longint;
  661. Begin
  662. r(f,ll);
  663. l:=0;
  664. If (ll<-32768) or (ll>32767) Then
  665. RunError(106);
  666. l:=ll;
  667. End;
  668. Procedure r(var f : TextRec;var l : Word);[Public,Alias: 'READ_TEXT_WORD'];
  669. var
  670. ll : Longint;
  671. Begin
  672. r(f,ll);
  673. l:=0;
  674. If (ll<0) or (ll>$ffff) Then
  675. RunError(106);
  676. l:=ll;
  677. End;
  678. Procedure r(var f : TextRec;var l : byte);[Public,Alias: 'READ_TEXT_BYTE'];
  679. var
  680. ll : Longint;
  681. Begin
  682. r(f,ll);
  683. l:=0;
  684. If (ll<0) or (ll>255) Then
  685. RunError(106);
  686. l:=ll;
  687. End;
  688. Procedure r(var f : TextRec;var l : shortint);[Public,Alias: 'READ_TEXT_SHORTINT'];
  689. var
  690. ll : Longint;
  691. Begin
  692. r(f,ll);
  693. l:=0;
  694. If (ll<-128) or (ll>127) Then
  695. RunError(106);
  696. l:=ll;
  697. End;
  698. Procedure r(var f : TextRec;var l : cardinal);[Public,Alias: 'READ_TEXT_CARDINAL'];
  699. var
  700. hs : String;
  701. code : Word;
  702. base : longint;
  703. Begin
  704. l:=0;
  705. hs:='';
  706. if not OpenInput(f) then
  707. exit;
  708. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  709. ReadNumeric(f,hs,Base);
  710. val(hs,l,code);
  711. If code<>0 Then
  712. RunError(106);
  713. End;
  714. Procedure r(var f : TextRec;var d : Real);[Public,Alias: 'READ_TEXT_REAL'];
  715. var
  716. hs : String;
  717. code : Word;
  718. Begin
  719. d:=0.0;
  720. hs:='';
  721. if not OpenInput(f) then
  722. exit;
  723. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  724. begin
  725. { First check for a . }
  726. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  727. begin
  728. hs:=hs+'.';
  729. Inc(f.BufPos);
  730. If f.BufPos>=f.BufEnd Then
  731. FileFunc(f.InOutFunc)(f);
  732. ReadNumeric(f,hs,10);
  733. end;
  734. { Also when a point is found check for a E }
  735. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  736. begin
  737. hs:=hs+'E';
  738. Inc(f.BufPos);
  739. If f.BufPos>=f.BufEnd Then
  740. FileFunc(f.InOutFunc)(f);
  741. if ReadSign(f,hs) then
  742. ReadNumeric(f,hs,10);
  743. end;
  744. end;
  745. val(hs,d,code);
  746. If code<>0 Then
  747. RunError(106);
  748. End;
  749. Procedure r(var f : TextRec;var d : extended);[Public,Alias: 'READ_TEXT_EXTENDED'];
  750. var
  751. hs : String;
  752. code : Word;
  753. Begin
  754. d:=0.0;
  755. hs:='';
  756. if not OpenInput(f) then
  757. exit;
  758. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  759. begin
  760. { First check for a . }
  761. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  762. begin
  763. hs:=hs+'.';
  764. Inc(f.BufPos);
  765. If f.BufPos>=f.BufEnd Then
  766. FileFunc(f.InOutFunc)(f);
  767. ReadNumeric(f,hs,10);
  768. end;
  769. { Also when a point is found check for a E }
  770. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  771. begin
  772. hs:=hs+'E';
  773. Inc(f.BufPos);
  774. If f.BufPos>=f.BufEnd Then
  775. FileFunc(f.InOutFunc)(f);
  776. if ReadSign(f,hs) then
  777. ReadNumeric(f,hs,10);
  778. end;
  779. end;
  780. val(hs,d,code);
  781. If code<>0 Then
  782. RunError(106);
  783. End;
  784. Procedure r(var f : TextRec;var d : comp);[Public,Alias: 'READ_TEXT_COMP'];
  785. var
  786. hs : String;
  787. code : Word;
  788. Begin
  789. d:=0.0;
  790. hs:='';
  791. if not OpenInput(f) then
  792. exit;
  793. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  794. begin
  795. { First check for a . }
  796. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  797. begin
  798. hs:=hs+'.';
  799. Inc(f.BufPos);
  800. If f.BufPos>=f.BufEnd Then
  801. FileFunc(f.InOutFunc)(f);
  802. ReadNumeric(f,hs,10);
  803. end;
  804. { Also when a point is found check for a E }
  805. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  806. begin
  807. hs:=hs+'E';
  808. Inc(f.BufPos);
  809. If f.BufPos>=f.BufEnd Then
  810. FileFunc(f.InOutFunc)(f);
  811. if ReadSign(f,hs) then
  812. ReadNumeric(f,hs,10);
  813. end;
  814. end;
  815. val(hs,d,code);
  816. If code<>0 Then
  817. RunError(106);
  818. End;
  819. {
  820. $Log$
  821. Revision 1.7 1998-05-27 00:19:21 peter
  822. * fixed crt input
  823. Revision 1.6 1998/05/21 19:31:01 peter
  824. * objects compiles for linux
  825. + assign(pchar), assign(char), rename(pchar), rename(char)
  826. * fixed read_text_as_array
  827. + read_text_as_pchar which was not yet in the rtl
  828. Revision 1.5 1998/05/12 10:42:45 peter
  829. * moved getopts to inc/, all supported OS's need argc,argv exported
  830. + strpas, strlen are now exported in the systemunit
  831. * removed logs
  832. * removed $ifdef ver_above
  833. Revision 1.4 1998/04/07 22:40:46 florian
  834. * final fix of comp writing
  835. }