text.inc 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. int_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. int_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) and
  626. { Flush if set }
  627. (f.FlushFunc<>nil) then
  628. FileFunc(f.FlushFunc)(f);
  629. exit;
  630. end;
  631. repeat
  632. prev := f.BufPtr^[f.BufPos];
  633. inc(f.BufPos);
  634. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  635. { #13#10 = Dos), so if we've got #10, we can safely exit }
  636. if prev = #10 then
  637. exit;
  638. If f.BufPos>=f.BufEnd Then
  639. begin
  640. FileFunc(f.InOutFunc)(f);
  641. if (f.BufPos>=f.BufEnd) and
  642. { Flush if set }
  643. (f.FlushFunc<>nil) then
  644. FileFunc(f.FlushFunc)(f);
  645. exit;
  646. end;
  647. if (prev=#13) then
  648. { is there also a #10 after it? }
  649. begin
  650. if (f.BufPtr^[f.BufPos]=#10) then
  651. { yes, skip that one as well }
  652. inc(f.BufPos);
  653. exit;
  654. end;
  655. until false;
  656. End;
  657. Function ReadPCharLen(var f:TextRec;s:pchar;maxlen:longint):longint;
  658. var
  659. sPos,len : Longint;
  660. p,startp,maxp : pchar;
  661. Begin
  662. ReadPCharLen:=0;
  663. { Check error and if file is open }
  664. If (InOutRes<>0) then
  665. exit;
  666. if (f.mode<>fmInput) Then
  667. begin
  668. if TextRec(f).mode=fmClosed then
  669. InOutRes:=103
  670. else
  671. InOutRes:=104;
  672. exit;
  673. end;
  674. { Read maximal until Maxlen is reached }
  675. sPos:=0;
  676. repeat
  677. If f.BufPos>=f.BufEnd Then
  678. begin
  679. FileFunc(f.InOutFunc)(f);
  680. If f.BufPos>=f.BufEnd Then
  681. break;
  682. end;
  683. p:[email protected]^[f.BufPos];
  684. if SPos+f.BufEnd-f.BufPos>MaxLen then
  685. maxp:[email protected]^[f.BufPos+MaxLen-SPos]
  686. else
  687. maxp:[email protected]^[f.BufEnd];
  688. startp:=p;
  689. { search linefeed }
  690. while (p<maxp) and not(P^ in [#10,#13]) do
  691. inc(p);
  692. { calculate read bytes }
  693. len:=p-startp;
  694. inc(f.BufPos,Len);
  695. Move(startp^,s[sPos],Len);
  696. inc(sPos,Len);
  697. { was it a LF or CR? then leave }
  698. if (spos=MaxLen) or
  699. ((p<maxp) and (p^ in [#10,#13])) then
  700. break;
  701. until false;
  702. ReadPCharLen:=spos;
  703. End;
  704. Procedure Read_String(var f : TextRec;var s : String);[Public,Alias:'FPC_READ_TEXT_SHORTSTR'];
  705. Begin
  706. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  707. End;
  708. Procedure Read_PChar(var f : TextRec;var s : PChar);[Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER'];
  709. Begin
  710. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  711. End;
  712. Procedure Read_Array(var f : TextRec;var s : array of char);[Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY'];
  713. Begin
  714. pchar(pchar(@s)+ReadPCharLen(f,pchar(@s),high(s)))^:=#0;
  715. End;
  716. Procedure Read_AnsiString(var f : TextRec;var s : AnsiString);[Public,Alias:'FPC_READ_TEXT_ANSISTR'];
  717. var
  718. slen,len : longint;
  719. Begin
  720. slen:=0;
  721. Repeat
  722. // SetLength will reallocate the length.
  723. SetLength(S,slen+255);
  724. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  725. inc(slen,len);
  726. Until len<255;
  727. // Set actual length
  728. SetLength(S,Slen);
  729. End;
  730. Function Read_Char(var f : TextRec):char;[Public,Alias:'FPC_READ_TEXT_CHAR'];
  731. Begin
  732. Read_Char:=#0;
  733. { Check error and if file is open }
  734. If (InOutRes<>0) then
  735. exit;
  736. if (f.mode<>fmInput) Then
  737. begin
  738. if TextRec(f).mode=fmClosed then
  739. InOutRes:=103
  740. else
  741. InOutRes:=104;
  742. exit;
  743. end;
  744. { Read next char or EOF }
  745. If f.BufPos>=f.BufEnd Then
  746. begin
  747. FileFunc(f.InOutFunc)(f);
  748. If f.BufPos>=f.BufEnd Then
  749. exit(#26);
  750. end;
  751. Read_Char:=f.Bufptr^[f.BufPos];
  752. inc(f.BufPos);
  753. end;
  754. Function Read_SInt(var f : TextRec):ValSInt;[Public,Alias:'FPC_READ_TEXT_SINT'];
  755. var
  756. hs : String;
  757. code : Longint;
  758. base : longint;
  759. Begin
  760. Read_SInt:=0;
  761. { Leave if error or not open file, else check for empty buf }
  762. If (InOutRes<>0) then
  763. exit;
  764. if (f.mode<>fmInput) Then
  765. begin
  766. if TextRec(f).mode=fmClosed then
  767. InOutRes:=103
  768. else
  769. InOutRes:=104;
  770. exit;
  771. end;
  772. If f.BufPos>=f.BufEnd Then
  773. FileFunc(f.InOutFunc)(f);
  774. hs:='';
  775. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  776. ReadNumeric(f,hs,Base);
  777. Val(hs,Read_SInt,code);
  778. If code<>0 Then
  779. InOutRes:=106;
  780. End;
  781. Function Read_UInt(var f : TextRec):ValUInt;[Public,Alias:'FPC_READ_TEXT_UINT'];
  782. var
  783. hs : String;
  784. code : longint;
  785. base : longint;
  786. Begin
  787. Read_UInt:=0;
  788. { Leave if error or not open file, else check for empty buf }
  789. If (InOutRes<>0) then
  790. exit;
  791. if (f.mode<>fmInput) Then
  792. begin
  793. if TextRec(f).mode=fmClosed then
  794. InOutRes:=103
  795. else
  796. InOutRes:=104;
  797. exit;
  798. end;
  799. If f.BufPos>=f.BufEnd Then
  800. FileFunc(f.InOutFunc)(f);
  801. hs:='';
  802. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  803. ReadNumeric(f,hs,Base);
  804. val(hs,Read_UInt,code);
  805. If code<>0 Then
  806. InOutRes:=106;
  807. End;
  808. Function Read_Float(var f : TextRec):ValReal;[Public,Alias:'FPC_READ_TEXT_FLOAT'];
  809. var
  810. hs : string;
  811. code : Word;
  812. begin
  813. Read_Float:=0.0;
  814. { Leave if error or not open file, else check for empty buf }
  815. If (InOutRes<>0) then
  816. exit;
  817. if (f.mode<>fmInput) Then
  818. begin
  819. if TextRec(f).mode=fmClosed then
  820. InOutRes:=103
  821. else
  822. InOutRes:=104;
  823. exit;
  824. end;
  825. If f.BufPos>=f.BufEnd Then
  826. FileFunc(f.InOutFunc)(f);
  827. hs:='';
  828. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  829. begin
  830. { First check for a . }
  831. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  832. begin
  833. hs:=hs+'.';
  834. Inc(f.BufPos);
  835. If f.BufPos>=f.BufEnd Then
  836. FileFunc(f.InOutFunc)(f);
  837. ReadNumeric(f,hs,10);
  838. end;
  839. { Also when a point is found check for a E }
  840. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  841. begin
  842. hs:=hs+'E';
  843. Inc(f.BufPos);
  844. If f.BufPos>=f.BufEnd Then
  845. FileFunc(f.InOutFunc)(f);
  846. if ReadSign(f,hs) then
  847. ReadNumeric(f,hs,10);
  848. end;
  849. end;
  850. val(hs,Read_Float,code);
  851. If code<>0 Then
  852. InOutRes:=106;
  853. end;
  854. {$ifdef INT64}
  855. procedure read_qword(len : longint;var t : textrec;q : qword);[public,alias:'FPC_READ_TEXT_QWORD'];
  856. begin
  857. { !!!!!!!!!!!!! }
  858. end;
  859. procedure read_int64(len : longint;var t : textrec;q : int64);[public,alias:'FPC_READ_TEXT_INT64'];
  860. begin
  861. { !!!!!!!!!!!!! }
  862. end;
  863. {$endif INT64}
  864. {*****************************************************************************
  865. Initializing
  866. *****************************************************************************}
  867. procedure OpenStdIO(var f:text;mode,hdl:longint);
  868. begin
  869. Assign(f,'');
  870. TextRec(f).Handle:=hdl;
  871. TextRec(f).Mode:=mode;
  872. TextRec(f).Closefunc:=@FileCloseFunc;
  873. case mode of
  874. fmInput :
  875. TextRec(f).InOutFunc:=@FileReadFunc;
  876. fmOutput :
  877. begin
  878. TextRec(f).InOutFunc:=@FileWriteFunc;
  879. TextRec(f).FlushFunc:=@FileWriteFunc;
  880. end;
  881. else
  882. HandleError(102);
  883. end;
  884. end;
  885. {
  886. $Log$
  887. Revision 1.64 2000-01-08 17:08:36 jonas
  888. + Mac linebreak (#13) support for readln
  889. Revision 1.63 2000/01/07 16:41:36 daniel
  890. * copyright 2000
  891. Revision 1.62 2000/01/07 16:32:25 daniel
  892. * copyright 2000 added
  893. Revision 1.61 1999/12/02 17:40:06 peter
  894. * read_int64 dummy added
  895. Revision 1.60 1999/11/06 14:35:39 peter
  896. * truncated log
  897. Revision 1.59 1999/10/26 12:25:19 peter
  898. * inoutres 103 for closed files, just like delphi
  899. Revision 1.58 1999/10/04 20:42:45 peter
  900. * read ansistring speedup (no length(s) calls anymore)
  901. Revision 1.57 1999/09/10 17:14:43 peter
  902. * remove CR when reading one char less then size
  903. Revision 1.56 1999/09/10 15:40:33 peter
  904. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  905. Revision 1.55 1999/09/08 16:12:24 peter
  906. * fixed inoutres for diskfull
  907. Revision 1.54 1999/09/07 07:44:58 peter
  908. * fixed array of char writing which didn't write the last char
  909. Revision 1.53 1999/08/19 11:16:14 peter
  910. * settextbuf size is now longint
  911. Revision 1.52 1999/08/03 21:58:45 peter
  912. * small speed improvements
  913. Revision 1.51 1999/07/26 09:43:24 florian
  914. + write helper routine for in64 implemented
  915. Revision 1.50 1999/07/08 15:18:14 michael
  916. * Now ansistring of arbitrary length can be read
  917. Revision 1.49 1999/07/05 20:04:29 peter
  918. * removed temp defines
  919. Revision 1.48 1999/07/01 15:39:52 florian
  920. + qword/int64 type released
  921. Revision 1.47 1999/06/30 22:17:24 florian
  922. + fpuint64 to system unit interface added: if it is true, the rtl
  923. uses the fpu to do int64 operations, if possible
  924. Revision 1.46 1999/05/06 09:05:16 peter
  925. * generic write_float str_float
  926. Revision 1.45 1999/04/26 18:27:26 peter
  927. * fixed write array
  928. * read array with maxlen
  929. Revision 1.44 1999/04/08 15:57:57 peter
  930. + subrange checking for readln()
  931. Revision 1.43 1999/04/07 22:05:18 peter
  932. * fixed bug with readln where it sometime didn't read until eol
  933. Revision 1.42 1999/03/16 17:49:39 jonas
  934. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  935. * in text.inc: changed RTE 106 when read integer values are out of bounds to RTE 201
  936. * in systemh.inc: disabled "support_fixed" for the i386 because it gave internal errors,
  937. Revision 1.41 1999/03/02 18:23:37 peter
  938. * changed so handlerror() -> inoutres:= to have $I- support
  939. Revision 1.40 1999/03/01 15:41:04 peter
  940. * use external names
  941. * removed all direct assembler modes
  942. }