text.inc 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. Possible Defines:
  13. EOF_CTRLZ Is Ctrl-Z (#26) a EOF mark for textfiles
  14. SHORT_LINEBREAK Use short Linebreaks #10 instead of #10#13
  15. SHORT_LINEBREAK is defined in the Linux system unit (syslinux.pp)
  16. }
  17. {****************************************************************************
  18. subroutines For TextFile handling
  19. ****************************************************************************}
  20. Procedure FileCloseFunc(Var t:TextRec);
  21. Begin
  22. Do_Close(t.Handle);
  23. t.Handle:=UnusedHandle;
  24. End;
  25. Procedure FileReadFunc(var t:TextRec);
  26. Begin
  27. t.BufEnd:=Do_Read(t.Handle,Longint(t.Bufptr),t.BufSize);
  28. t.BufPos:=0;
  29. End;
  30. Procedure FileWriteFunc(var t:TextRec);
  31. var
  32. i : longint;
  33. Begin
  34. i:=Do_Write(t.Handle,Longint(t.Bufptr),t.BufPos);
  35. if i<>t.BufPos then
  36. InOutRes:=101;
  37. t.BufPos:=0;
  38. End;
  39. Procedure FileOpenFunc(var t:TextRec);
  40. var
  41. Flags : Longint;
  42. Begin
  43. Case t.mode Of
  44. fmInput : Flags:=$10000;
  45. fmOutput : Flags:=$11001;
  46. fmAppend : Flags:=$10101;
  47. else
  48. begin
  49. InOutRes:=102;
  50. exit;
  51. end;
  52. End;
  53. Do_Open(t,PChar(@t.Name),Flags);
  54. t.CloseFunc:=@FileCloseFunc;
  55. t.FlushFunc:=nil;
  56. if t.Mode=fmInput then
  57. t.InOutFunc:=@FileReadFunc
  58. else
  59. begin
  60. t.InOutFunc:=@FileWriteFunc;
  61. { Only install flushing if its a NOT a file, and only check if there
  62. was no error opening the file, becuase else we always get a bad
  63. file handle error 6 (PFV) }
  64. if (InOutRes=0) and
  65. Do_Isdevice(t.Handle) then
  66. t.FlushFunc:=@FileWriteFunc;
  67. end;
  68. End;
  69. Procedure assign(var t:Text;const s:String);
  70. Begin
  71. FillChar(t,SizEof(TextRec),0);
  72. { only set things that are not zero }
  73. TextRec(t).Handle:=UnusedHandle;
  74. TextRec(t).mode:=fmClosed;
  75. TextRec(t).BufSize:=TextRecBufSize;
  76. TextRec(t).Bufptr:=@TextRec(t).Buffer;
  77. TextRec(t).OpenFunc:=@FileOpenFunc;
  78. Move(s[1],TextRec(t).Name,Length(s));
  79. End;
  80. Procedure assign(var t:Text;p:pchar);
  81. begin
  82. Assign(t,StrPas(p));
  83. end;
  84. Procedure assign(var t:Text;c:char);
  85. begin
  86. Assign(t,string(c));
  87. end;
  88. Procedure Close(var t : Text);[IOCheck];
  89. Begin
  90. if InOutRes<>0 then
  91. Exit;
  92. If (TextRec(t).mode<>fmClosed) Then
  93. Begin
  94. { Write pending buffer }
  95. If Textrec(t).Mode=fmoutput then
  96. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  97. TextRec(t).mode:=fmClosed;
  98. { Only close functions not connected to stdout.}
  99. If ((TextRec(t).Handle<>StdInputHandle) and
  100. (TextRec(t).Handle<>StdOutputHandle) and
  101. (TextRec(t).Handle<>StdErrorHandle)) Then
  102. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  103. { Reset buffer for safety }
  104. TextRec(t).BufPos:=0;
  105. TextRec(t).BufEnd:=0;
  106. End;
  107. End;
  108. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  109. Begin
  110. Case TextRec(t).mode Of {This gives the fastest code}
  111. fmInput,fmOutput,fmInOut : Close(t);
  112. fmClosed : ;
  113. else
  114. Begin
  115. InOutRes:=102;
  116. exit;
  117. End;
  118. End;
  119. TextRec(t).mode:=mode;
  120. TextRec(t).bufpos:=0;
  121. TextRec(t).bufend:=0;
  122. FileFunc(TextRec(t).OpenFunc)(TextRec(t));
  123. { reset the mode to closed when an error has occured }
  124. if InOutRes<>0 then
  125. TextRec(t).mode:=fmClosed;
  126. End;
  127. Procedure Rewrite(var t : Text);[IOCheck];
  128. Begin
  129. If InOutRes<>0 then
  130. exit;
  131. OpenText(t,fmOutput,1);
  132. End;
  133. Procedure Reset(var t : Text);[IOCheck];
  134. Begin
  135. If InOutRes<>0 then
  136. exit;
  137. OpenText(t,fmInput,0);
  138. End;
  139. Procedure Append(var t : Text);[IOCheck];
  140. Begin
  141. If InOutRes<>0 then
  142. exit;
  143. OpenText(t,fmAppend,1);
  144. End;
  145. Procedure Flush(var t : Text);[IOCheck];
  146. Begin
  147. If InOutRes<>0 then
  148. exit;
  149. if TextRec(t).mode<>fmOutput then
  150. begin
  151. if TextRec(t).mode=fmClosed then
  152. InOutRes:=103
  153. else
  154. InOutRes:=105;
  155. exit;
  156. end;
  157. { Not the flushfunc but the inoutfunc should be used, becuase that
  158. writes the data, flushfunc doesn't need to be assigned }
  159. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  160. End;
  161. Procedure Erase(var t:Text);[IOCheck];
  162. Begin
  163. If InOutRes <> 0 then
  164. exit;
  165. If TextRec(t).mode=fmClosed Then
  166. Do_Erase(PChar(@TextRec(t).Name));
  167. End;
  168. Procedure Rename(var t : text;p:pchar);[IOCheck];
  169. Begin
  170. If InOutRes <> 0 then
  171. exit;
  172. If TextRec(t).mode=fmClosed Then
  173. Begin
  174. Do_Rename(PChar(@TextRec(t).Name),p);
  175. Move(p^,TextRec(t).Name,StrLen(p)+1);
  176. End;
  177. End;
  178. Procedure Rename(var t : Text;const s : string);[IOCheck];
  179. var
  180. p : array[0..255] Of Char;
  181. Begin
  182. If InOutRes <> 0 then
  183. exit;
  184. Move(s[1],p,Length(s));
  185. p[Length(s)]:=#0;
  186. Rename(t,Pchar(@p));
  187. End;
  188. Procedure Rename(var t : Text;c : char);[IOCheck];
  189. var
  190. p : array[0..1] Of Char;
  191. Begin
  192. If InOutRes <> 0 then
  193. exit;
  194. p[0]:=c;
  195. p[1]:=#0;
  196. Rename(t,Pchar(@p));
  197. End;
  198. Function Eof(Var t: Text): Boolean;[IOCheck];
  199. Begin
  200. If (InOutRes<>0) then
  201. exit(true);
  202. if (TextRec(t).mode<>fmInput) Then
  203. begin
  204. if TextRec(t).mode=fmClosed then
  205. InOutRes:=103
  206. else
  207. InOutRes:=104;
  208. exit(true);
  209. end;
  210. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  211. begin
  212. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  213. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  214. exit(true);
  215. end;
  216. {$ifdef EOF_CTRLZ}
  217. Eof:=(TextRec(t).Bufptr^[TextRec(t).BufPos]=#26);
  218. {$else}
  219. Eof:=false;
  220. {$endif EOL_CTRLZ}
  221. end;
  222. Function Eof:Boolean;
  223. Begin
  224. Eof:=Eof(Input);
  225. End;
  226. Function SeekEof (Var t : Text) : Boolean;
  227. Begin
  228. If (InOutRes<>0) then
  229. exit(true);
  230. if (TextRec(t).mode<>fmInput) Then
  231. begin
  232. if TextRec(t).mode=fmClosed then
  233. InOutRes:=103
  234. else
  235. InOutRes:=104;
  236. exit(true);
  237. end;
  238. repeat
  239. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  240. begin
  241. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  242. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  243. exit(true);
  244. end;
  245. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  246. #26 : exit(true);
  247. #10,#13,
  248. #9,' ' : ;
  249. else
  250. exit(false);
  251. end;
  252. inc(TextRec(t).BufPos);
  253. until false;
  254. End;
  255. Function SeekEof : Boolean;
  256. Begin
  257. SeekEof:=SeekEof(Input);
  258. End;
  259. Function Eoln(var t:Text) : Boolean;
  260. Begin
  261. If (InOutRes<>0) then
  262. exit(true);
  263. if (TextRec(t).mode<>fmInput) Then
  264. begin
  265. if TextRec(t).mode=fmClosed then
  266. InOutRes:=103
  267. else
  268. InOutRes:=104;
  269. exit(true);
  270. end;
  271. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  272. begin
  273. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  274. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  275. exit(true);
  276. end;
  277. Eoln:=(TextRec(t).Bufptr^[TextRec(t).BufPos] in [#10,#13]);
  278. End;
  279. Function Eoln : Boolean;
  280. Begin
  281. Eoln:=Eoln(Input);
  282. End;
  283. Function SeekEoln (Var t : Text) : Boolean;
  284. Begin
  285. If (InOutRes<>0) then
  286. exit(true);
  287. if (TextRec(t).mode<>fmInput) Then
  288. begin
  289. if TextRec(t).mode=fmClosed then
  290. InOutRes:=103
  291. else
  292. InOutRes:=104;
  293. exit(true);
  294. end;
  295. repeat
  296. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  297. begin
  298. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  299. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  300. exit(true);
  301. end;
  302. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  303. #26,
  304. #10,#13 : exit(true);
  305. #9,' ' : ;
  306. else
  307. exit(false);
  308. end;
  309. inc(TextRec(t).BufPos);
  310. until false;
  311. End;
  312. Function SeekEoln : Boolean;
  313. Begin
  314. SeekEoln:=SeekEoln(Input);
  315. End;
  316. Procedure SetTextBuf(Var F : Text; Var Buf);[INTERNPROC: In_settextbuf_file_x];
  317. Procedure SetTextBuf(Var F : Text; Var Buf; Size : Longint);
  318. Begin
  319. TextRec(f).BufPtr:=@Buf;
  320. TextRec(f).BufSize:=Size;
  321. TextRec(f).BufPos:=0;
  322. TextRec(f).BufEnd:=0;
  323. End;
  324. {*****************************************************************************
  325. Write(Ln)
  326. *****************************************************************************}
  327. Procedure WriteBuffer(var f:TextRec;var b;len:longint);
  328. var
  329. p : pchar;
  330. left,
  331. idx : longint;
  332. begin
  333. p:=pchar(@b);
  334. idx:=0;
  335. left:=f.BufSize-f.BufPos;
  336. while len>left do
  337. begin
  338. move(p[idx],f.Bufptr^[f.BufPos],left);
  339. dec(len,left);
  340. inc(idx,left);
  341. inc(f.BufPos,left);
  342. FileFunc(f.InOutFunc)(f);
  343. left:=f.BufSize-f.BufPos;
  344. end;
  345. move(p[idx],f.Bufptr^[f.BufPos],len);
  346. inc(f.BufPos,len);
  347. end;
  348. Procedure WriteBlanks(var f:TextRec;len:longint);
  349. var
  350. left : longint;
  351. begin
  352. left:=f.BufSize-f.BufPos;
  353. while len>left do
  354. begin
  355. FillChar(f.Bufptr^[f.BufPos],left,' ');
  356. dec(len,left);
  357. inc(f.BufPos,left);
  358. FileFunc(f.InOutFunc)(f);
  359. left:=f.BufSize-f.BufPos;
  360. end;
  361. FillChar(f.Bufptr^[f.BufPos],len,' ');
  362. inc(f.BufPos,len);
  363. end;
  364. Procedure Write_End(var f:TextRec);[Public,Alias:'FPC_WRITE_END'];
  365. begin
  366. if f.FlushFunc<>nil then
  367. FileFunc(f.FlushFunc)(f);
  368. end;
  369. Procedure Writeln_End(var f:TextRec);[Public,Alias:'FPC_WRITELN_END'];
  370. const
  371. {$IFDEF SHORT_LINEBREAK}
  372. eollen=1;
  373. eol : array[0..0] of char=(#10);
  374. {$ELSE SHORT_LINEBREAK}
  375. eollen=2;
  376. eol : array[0..1] of char=(#13,#10);
  377. {$ENDIF SHORT_LINEBREAK}
  378. begin
  379. If InOutRes <> 0 then exit;
  380. { Write EOL }
  381. WriteBuffer(f,eol,eollen);
  382. { Flush }
  383. if f.FlushFunc<>nil then
  384. FileFunc(f.FlushFunc)(f);
  385. end;
  386. Procedure Write_Str(Len : Longint;var f : TextRec;const s : String);[Public,Alias:'FPC_WRITE_TEXT_SHORTSTR'];
  387. Begin
  388. If (InOutRes<>0) then
  389. exit;
  390. if (f.mode<>fmOutput) Then
  391. begin
  392. if TextRec(f).mode=fmClosed then
  393. InOutRes:=103
  394. else
  395. InOutRes:=105;
  396. exit;
  397. end;
  398. If Len>Length(s) Then
  399. WriteBlanks(f,Len-Length(s));
  400. WriteBuffer(f,s[1],Length(s));
  401. End;
  402. Procedure Write_Array(Len : Longint;var f : TextRec;const s : array of char);[Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_ARRAY'];
  403. var
  404. ArrayLen : longint;
  405. p : pchar;
  406. Begin
  407. If (InOutRes<>0) then
  408. exit;
  409. if (f.mode<>fmOutput) Then
  410. begin
  411. if TextRec(f).mode=fmClosed then
  412. InOutRes:=103
  413. else
  414. InOutRes:=105;
  415. exit;
  416. end;
  417. p:=pchar(@s);
  418. ArrayLen:=StrLen(p);
  419. if ArrayLen>sizeof(s) then
  420. ArrayLen:=sizeof(s);
  421. If Len>ArrayLen Then
  422. WriteBlanks(f,Len-ArrayLen);
  423. WriteBuffer(f,p^,ArrayLen);
  424. End;
  425. Procedure Write_PChar(Len : Longint;var f : TextRec;p : PChar);[Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_POINTER'];
  426. var
  427. PCharLen : longint;
  428. Begin
  429. If (p=nil) or (InOutRes<>0) then
  430. exit;
  431. if (f.mode<>fmOutput) Then
  432. begin
  433. if TextRec(f).mode=fmClosed then
  434. InOutRes:=103
  435. else
  436. InOutRes:=105;
  437. exit;
  438. end;
  439. PCharLen:=StrLen(p);
  440. If Len>PCharLen Then
  441. WriteBlanks(f,Len-PCharLen);
  442. WriteBuffer(f,p^,PCharLen);
  443. End;
  444. Procedure Write_Text_AnsiString (Len : Longint; Var T : TextRec; S : Pointer);[Public,alias:'FPC_WRITE_TEXT_ANSISTR'];
  445. {
  446. Writes a AnsiString to the Text file T
  447. }
  448. begin
  449. If S=Nil then
  450. exit;
  451. Write_pchar (Len,t,PChar(S));
  452. end;
  453. Procedure Write_SInt(Len : Longint;var t : TextRec;l : ValSInt);[Public,Alias:'FPC_WRITE_TEXT_SINT'];
  454. var
  455. s : String;
  456. Begin
  457. If (InOutRes<>0) then
  458. exit;
  459. Str(l,s);
  460. Write_Str(Len,t,s);
  461. End;
  462. Procedure Write_UInt(Len : Longint;var t : TextRec;l : ValUInt);[Public,Alias:'FPC_WRITE_TEXT_UINT'];
  463. var
  464. s : String;
  465. Begin
  466. If (InOutRes<>0) then
  467. exit;
  468. Str(L,s);
  469. Write_Str(Len,t,s);
  470. End;
  471. {$ifdef INT64}
  472. procedure write_qword(len : longint;var t : textrec;q : qword);[public,alias:'FPC_WRITE_TEXT_QWORD'];
  473. var
  474. s : string;
  475. begin
  476. if (InOutRes<>0) then
  477. exit;
  478. qword_str(q,s);
  479. write_str(len,t,s);
  480. end;
  481. procedure write_int64(len : longint;var t : textrec;i : int64);[public,alias:'FPC_WRITE_TEXT_INT64'];
  482. var
  483. s : string;
  484. begin
  485. if (InOutRes<>0) then
  486. exit;
  487. int64_str(i,s);
  488. write_str(len,t,s);
  489. end;
  490. {$endif INT64}
  491. Procedure Write_Float(rt,fixkomma,Len : Longint;var t : TextRec;r : ValReal);[Public,Alias:'FPC_WRITE_TEXT_FLOAT'];
  492. var
  493. s : String;
  494. Begin
  495. If (InOutRes<>0) then
  496. exit;
  497. Str_real(Len,fixkomma,r,treal_type(rt),s);
  498. Write_Str(Len,t,s);
  499. End;
  500. Procedure Write_Boolean(Len : Longint;var t : TextRec;b : Boolean);[Public,Alias:'FPC_WRITE_TEXT_BOOLEAN'];
  501. Begin
  502. If (InOutRes<>0) then
  503. exit;
  504. { Can't use array[boolean] because b can be >0 ! }
  505. if b then
  506. Write_Str(Len,t,'TRUE')
  507. else
  508. Write_Str(Len,t,'FALSE');
  509. End;
  510. Procedure Write_Char(Len : Longint;var t : TextRec;c : Char);[Public,Alias:'FPC_WRITE_TEXT_CHAR'];
  511. Begin
  512. If (InOutRes<>0) then
  513. exit;
  514. if (TextRec(t).mode<>fmOutput) Then
  515. begin
  516. if TextRec(t).mode=fmClosed then
  517. InOutRes:=103
  518. else
  519. InOutRes:=105;
  520. exit;
  521. end;
  522. If Len>1 Then
  523. WriteBlanks(t,Len-1);
  524. If t.BufPos+1>=t.BufSize Then
  525. FileFunc(t.InOutFunc)(t);
  526. t.Bufptr^[t.BufPos]:=c;
  527. Inc(t.BufPos);
  528. End;
  529. {*****************************************************************************
  530. Read(Ln)
  531. *****************************************************************************}
  532. Function NextChar(var f:TextRec;var s:string):Boolean;
  533. begin
  534. if f.BufPos<f.BufEnd then
  535. begin
  536. if length(s)<high(s) then
  537. begin
  538. inc(s[0]);
  539. s[length(s)]:=f.BufPtr^[f.BufPos];
  540. end;
  541. Inc(f.BufPos);
  542. If f.BufPos>=f.BufEnd Then
  543. FileFunc(f.InOutFunc)(f);
  544. NextChar:=true;
  545. end
  546. else
  547. NextChar:=false;
  548. end;
  549. Function IgnoreSpaces(var f:TextRec):Boolean;
  550. {
  551. Removes all leading spaces,tab,eols from the input buffer, returns true if
  552. the buffer is empty
  553. }
  554. var
  555. s : string;
  556. begin
  557. s:='';
  558. IgnoreSpaces:=false;
  559. while f.Bufptr^[f.BufPos] in [#9,#10,#13,' '] do
  560. if not NextChar(f,s) then
  561. exit;
  562. IgnoreSpaces:=true;
  563. end;
  564. Function ReadSign(var f:TextRec;var s:string):Boolean;
  565. {
  566. Read + and - sign, return true if buffer is empty
  567. }
  568. begin
  569. ReadSign:=(not (f.Bufptr^[f.BufPos] in ['-','+'])) or NextChar(f,s);
  570. end;
  571. Function ReadBase(var f:TextRec;var s:string;var Base:longint):boolean;
  572. {
  573. Read the base $ For 16 and % For 2, if buffer is empty return true
  574. }
  575. begin
  576. case f.BufPtr^[f.BufPos] of
  577. '$' : Base:=16;
  578. '%' : Base:=2;
  579. else
  580. Base:=10;
  581. end;
  582. ReadBase:=(Base=10) or NextChar(f,s);
  583. end;
  584. Function ReadNumeric(var f:TextRec;var s:string;base:longint):Boolean;
  585. {
  586. Read numeric input, if buffer is empty then return True
  587. }
  588. var
  589. c : char;
  590. begin
  591. ReadNumeric:=false;
  592. c:=f.BufPtr^[f.BufPos];
  593. while ((base>=10) and (c in ['0'..'9'])) or
  594. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  595. ((base=2) and (c in ['0'..'1'])) do
  596. begin
  597. if not NextChar(f,s) then
  598. exit;
  599. c:=f.BufPtr^[f.BufPos];
  600. end;
  601. ReadNumeric:=true;
  602. end;
  603. Procedure Read_End(var f:TextRec);[Public,Alias:'FPC_READ_END'];
  604. begin
  605. if f.FlushFunc<>nil then
  606. FileFunc(f.FlushFunc)(f);
  607. end;
  608. Procedure ReadLn_End(var f : TextRec);[Public,Alias:'FPC_READLN_END'];
  609. var prev: char;
  610. Begin
  611. { Check error and if file is open and load buf if empty }
  612. If (InOutRes<>0) then
  613. exit;
  614. if (f.mode<>fmInput) Then
  615. begin
  616. if TextRec(f).mode=fmClosed then
  617. InOutRes:=103
  618. else
  619. InOutRes:=104;
  620. exit;
  621. end;
  622. if f.BufPos>=f.BufEnd Then
  623. begin
  624. FileFunc(f.InOutFunc)(f);
  625. if (f.BufPos>=f.BufEnd) then
  626. { Flush if set }
  627. begin
  628. if (f.FlushFunc<>nil) then
  629. FileFunc(f.FlushFunc)(f);
  630. exit;
  631. end;
  632. end;
  633. repeat
  634. prev := f.BufPtr^[f.BufPos];
  635. inc(f.BufPos);
  636. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  637. { #13#10 = Dos), so if we've got #10, we can safely exit }
  638. if prev = #10 then
  639. exit;
  640. if f.BufPos>=f.BufEnd Then
  641. begin
  642. FileFunc(f.InOutFunc)(f);
  643. if (f.BufPos>=f.BufEnd) then
  644. { Flush if set }
  645. begin
  646. if (f.FlushFunc<>nil) then
  647. FileFunc(f.FlushFunc)(f);
  648. exit;
  649. end;
  650. end;
  651. if (prev=#13) then
  652. { is there also a #10 after it? }
  653. begin
  654. if (f.BufPtr^[f.BufPos]=#10) then
  655. { yes, skip that one as well }
  656. inc(f.BufPos);
  657. exit;
  658. end;
  659. until false;
  660. End;
  661. Function ReadPCharLen(var f:TextRec;s:pchar;maxlen:longint):longint;
  662. var
  663. sPos,len : Longint;
  664. p,startp,maxp : pchar;
  665. Begin
  666. ReadPCharLen:=0;
  667. { Check error and if file is open }
  668. If (InOutRes<>0) then
  669. exit;
  670. if (f.mode<>fmInput) Then
  671. begin
  672. if TextRec(f).mode=fmClosed then
  673. InOutRes:=103
  674. else
  675. InOutRes:=104;
  676. exit;
  677. end;
  678. { Read maximal until Maxlen is reached }
  679. sPos:=0;
  680. repeat
  681. If f.BufPos>=f.BufEnd Then
  682. begin
  683. FileFunc(f.InOutFunc)(f);
  684. If f.BufPos>=f.BufEnd Then
  685. break;
  686. end;
  687. p:[email protected]^[f.BufPos];
  688. if SPos+f.BufEnd-f.BufPos>MaxLen then
  689. maxp:[email protected]^[f.BufPos+MaxLen-SPos]
  690. else
  691. maxp:[email protected]^[f.BufEnd];
  692. startp:=p;
  693. { search linefeed }
  694. while (p<maxp) and not(P^ in [#10,#13]) do
  695. inc(p);
  696. { calculate read bytes }
  697. len:=p-startp;
  698. inc(f.BufPos,Len);
  699. Move(startp^,s[sPos],Len);
  700. inc(sPos,Len);
  701. { was it a LF or CR? then leave }
  702. if (spos=MaxLen) or
  703. ((p<maxp) and (p^ in [#10,#13])) then
  704. break;
  705. until false;
  706. ReadPCharLen:=spos;
  707. End;
  708. Procedure Read_String(var f : TextRec;var s : String);[Public,Alias:'FPC_READ_TEXT_SHORTSTR'];
  709. Begin
  710. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  711. End;
  712. Procedure Read_PChar(var f : TextRec;var s : PChar);[Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER'];
  713. Begin
  714. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  715. End;
  716. Procedure Read_Array(var f : TextRec;var s : array of char);[Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY'];
  717. Begin
  718. pchar(pchar(@s)+ReadPCharLen(f,pchar(@s),high(s)))^:=#0;
  719. End;
  720. Procedure Read_AnsiString(var f : TextRec;var s : AnsiString);[Public,Alias:'FPC_READ_TEXT_ANSISTR'];
  721. var
  722. slen,len : longint;
  723. Begin
  724. slen:=0;
  725. Repeat
  726. // SetLength will reallocate the length.
  727. SetLength(S,slen+255);
  728. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  729. inc(slen,len);
  730. Until len<255;
  731. // Set actual length
  732. SetLength(S,Slen);
  733. End;
  734. Function Read_Char(var f : TextRec):char;[Public,Alias:'FPC_READ_TEXT_CHAR'];
  735. Begin
  736. Read_Char:=#0;
  737. { Check error and if file is open }
  738. If (InOutRes<>0) then
  739. exit;
  740. if (f.mode<>fmInput) Then
  741. begin
  742. if TextRec(f).mode=fmClosed then
  743. InOutRes:=103
  744. else
  745. InOutRes:=104;
  746. exit;
  747. end;
  748. { Read next char or EOF }
  749. If f.BufPos>=f.BufEnd Then
  750. begin
  751. FileFunc(f.InOutFunc)(f);
  752. If f.BufPos>=f.BufEnd Then
  753. exit(#26);
  754. end;
  755. Read_Char:=f.Bufptr^[f.BufPos];
  756. inc(f.BufPos);
  757. end;
  758. Function Read_SInt(var f : TextRec):ValSInt;[Public,Alias:'FPC_READ_TEXT_SINT'];
  759. var
  760. hs : String;
  761. code : Longint;
  762. base : longint;
  763. Begin
  764. Read_SInt:=0;
  765. { Leave if error or not open file, else check for empty buf }
  766. If (InOutRes<>0) then
  767. exit;
  768. if (f.mode<>fmInput) Then
  769. begin
  770. if TextRec(f).mode=fmClosed then
  771. InOutRes:=103
  772. else
  773. InOutRes:=104;
  774. exit;
  775. end;
  776. If f.BufPos>=f.BufEnd Then
  777. FileFunc(f.InOutFunc)(f);
  778. hs:='';
  779. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  780. ReadNumeric(f,hs,Base);
  781. Val(hs,Read_SInt,code);
  782. If code<>0 Then
  783. InOutRes:=106;
  784. End;
  785. Function Read_UInt(var f : TextRec):ValUInt;[Public,Alias:'FPC_READ_TEXT_UINT'];
  786. var
  787. hs : String;
  788. code : longint;
  789. base : longint;
  790. Begin
  791. Read_UInt:=0;
  792. { Leave if error or not open file, else check for empty buf }
  793. If (InOutRes<>0) then
  794. exit;
  795. if (f.mode<>fmInput) Then
  796. begin
  797. if TextRec(f).mode=fmClosed then
  798. InOutRes:=103
  799. else
  800. InOutRes:=104;
  801. exit;
  802. end;
  803. If f.BufPos>=f.BufEnd Then
  804. FileFunc(f.InOutFunc)(f);
  805. hs:='';
  806. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  807. ReadNumeric(f,hs,Base);
  808. val(hs,Read_UInt,code);
  809. If code<>0 Then
  810. InOutRes:=106;
  811. End;
  812. Function Read_Float(var f : TextRec):ValReal;[Public,Alias:'FPC_READ_TEXT_FLOAT'];
  813. var
  814. hs : string;
  815. code : Word;
  816. begin
  817. Read_Float:=0.0;
  818. { Leave if error or not open file, else check for empty buf }
  819. If (InOutRes<>0) then
  820. exit;
  821. if (f.mode<>fmInput) Then
  822. begin
  823. if TextRec(f).mode=fmClosed then
  824. InOutRes:=103
  825. else
  826. InOutRes:=104;
  827. exit;
  828. end;
  829. If f.BufPos>=f.BufEnd Then
  830. FileFunc(f.InOutFunc)(f);
  831. hs:='';
  832. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  833. begin
  834. { First check for a . }
  835. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  836. begin
  837. hs:=hs+'.';
  838. Inc(f.BufPos);
  839. If f.BufPos>=f.BufEnd Then
  840. FileFunc(f.InOutFunc)(f);
  841. ReadNumeric(f,hs,10);
  842. end;
  843. { Also when a point is found check for a E }
  844. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  845. begin
  846. hs:=hs+'E';
  847. Inc(f.BufPos);
  848. If f.BufPos>=f.BufEnd Then
  849. FileFunc(f.InOutFunc)(f);
  850. if ReadSign(f,hs) then
  851. ReadNumeric(f,hs,10);
  852. end;
  853. end;
  854. val(hs,Read_Float,code);
  855. If code<>0 Then
  856. InOutRes:=106;
  857. end;
  858. {$ifdef INT64}
  859. function Read_QWord(var f : textrec) : qword;[public,alias:'FPC_READ_TEXT_QWORD'];
  860. var
  861. hs : String;
  862. code : longint;
  863. base : longint;
  864. Begin
  865. Read_QWord:=0;
  866. { Leave if error or not open file, else check for empty buf }
  867. If (InOutRes<>0) then
  868. exit;
  869. if (f.mode<>fmInput) Then
  870. begin
  871. if TextRec(f).mode=fmClosed then
  872. InOutRes:=103
  873. else
  874. InOutRes:=104;
  875. exit;
  876. end;
  877. If f.BufPos>=f.BufEnd Then
  878. FileFunc(f.InOutFunc)(f);
  879. hs:='';
  880. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  881. ReadNumeric(f,hs,Base);
  882. val(hs,Read_QWord,code);
  883. If code<>0 Then
  884. InOutRes:=106;
  885. End;
  886. function Read_Int64(var f : textrec) : int64;[public,alias:'FPC_READ_TEXT_INT64'];
  887. var
  888. hs : String;
  889. code : Longint;
  890. base : longint;
  891. Begin
  892. Read_Int64:=0;
  893. { Leave if error or not open file, else check for empty buf }
  894. If (InOutRes<>0) then
  895. exit;
  896. if (f.mode<>fmInput) Then
  897. begin
  898. if TextRec(f).mode=fmClosed then
  899. InOutRes:=103
  900. else
  901. InOutRes:=104;
  902. exit;
  903. end;
  904. If f.BufPos>=f.BufEnd Then
  905. FileFunc(f.InOutFunc)(f);
  906. hs:='';
  907. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  908. ReadNumeric(f,hs,Base);
  909. Val(hs,Read_Int64,code);
  910. If code<>0 Then
  911. InOutRes:=106;
  912. End;
  913. {$endif INT64}
  914. {*****************************************************************************
  915. Initializing
  916. *****************************************************************************}
  917. procedure OpenStdIO(var f:text;mode,hdl:longint);
  918. begin
  919. Assign(f,'');
  920. TextRec(f).Handle:=hdl;
  921. TextRec(f).Mode:=mode;
  922. TextRec(f).Closefunc:=@FileCloseFunc;
  923. case mode of
  924. fmInput :
  925. TextRec(f).InOutFunc:=@FileReadFunc;
  926. fmOutput :
  927. begin
  928. TextRec(f).InOutFunc:=@FileWriteFunc;
  929. TextRec(f).FlushFunc:=@FileWriteFunc;
  930. end;
  931. else
  932. HandleError(102);
  933. end;
  934. end;
  935. {
  936. $Log$
  937. Revision 1.69 2000-02-09 16:59:31 peter
  938. * truncated log
  939. Revision 1.68 2000/01/31 12:11:53 jonas
  940. * committed the rest of my fix :)
  941. Revision 1.67 2000/01/31 10:15:43 pierre
  942. * Jonas' fix for bug811
  943. Revision 1.66 2000/01/23 12:22:37 florian
  944. * reading of 64 bit type implemented
  945. Revision 1.65 2000/01/20 20:19:37 florian
  946. * writing of int64/qword fixed
  947. Revision 1.64 2000/01/08 17:08:36 jonas
  948. + Mac linebreak (#13) support for readln
  949. Revision 1.63 2000/01/07 16:41:36 daniel
  950. * copyright 2000
  951. Revision 1.62 2000/01/07 16:32:25 daniel
  952. * copyright 2000 added
  953. Revision 1.61 1999/12/02 17:40:06 peter
  954. * read_int64 dummy added
  955. Revision 1.60 1999/11/06 14:35:39 peter
  956. * truncated log
  957. Revision 1.59 1999/10/26 12:25:19 peter
  958. * inoutres 103 for closed files, just like delphi
  959. Revision 1.58 1999/10/04 20:42:45 peter
  960. * read ansistring speedup (no length(s) calls anymore)
  961. Revision 1.57 1999/09/10 17:14:43 peter
  962. * remove CR when reading one char less then size
  963. Revision 1.56 1999/09/10 15:40:33 peter
  964. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  965. Revision 1.55 1999/09/08 16:12:24 peter
  966. * fixed inoutres for diskfull
  967. Revision 1.54 1999/09/07 07:44:58 peter
  968. * fixed array of char writing which didn't write the last char
  969. Revision 1.53 1999/08/19 11:16:14 peter
  970. * settextbuf size is now longint
  971. Revision 1.52 1999/08/03 21:58:45 peter
  972. * small speed improvements
  973. Revision 1.51 1999/07/26 09:43:24 florian
  974. + write helper routine for in64 implemented
  975. }