text.inc 21 KB

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