text.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 1993,97 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. Begin
  32. Do_Write(t.Handle,Longint(t.Bufptr),t.BufPos);
  33. t.BufPos:=0;
  34. End;
  35. Procedure FileOpenFunc(var t:TextRec);
  36. var
  37. Flags : Longint;
  38. Begin
  39. Case t.mode Of
  40. fmInput : Flags:=$1000;
  41. fmOutput : Flags:=$1101;
  42. fmAppend : Flags:=$1011;
  43. else
  44. HandleError(102);
  45. End;
  46. Do_Open(t,PChar(@t.Name),Flags);
  47. t.CloseFunc:=@FileCloseFunc;
  48. t.FlushFunc:=nil;
  49. if t.Mode=fmInput then
  50. t.InOutFunc:=@FileReadFunc
  51. else
  52. begin
  53. t.InOutFunc:=@FileWriteFunc;
  54. { Only install flushing if its a NOT a file }
  55. if Do_Isdevice(t.Handle) then
  56. t.FlushFunc:=@FileWriteFunc;
  57. end;
  58. End;
  59. Procedure assign(var t:Text;const s:String);
  60. Begin
  61. FillChar(t,SizEof(TextRec),0);
  62. { only set things that are not zero }
  63. TextRec(t).Handle:=UnusedHandle;
  64. TextRec(t).mode:=fmClosed;
  65. TextRec(t).BufSize:=TextRecBufSize;
  66. TextRec(t).Bufptr:=@TextRec(t).Buffer;
  67. TextRec(t).OpenFunc:=@FileOpenFunc;
  68. Move(s[1],TextRec(t).Name,Length(s));
  69. End;
  70. Procedure assign(var t:Text;p:pchar);
  71. begin
  72. Assign(t,StrPas(p));
  73. end;
  74. Procedure assign(var t:Text;c:char);
  75. begin
  76. Assign(t,string(c));
  77. end;
  78. Procedure Close(var t : Text);[IOCheck];
  79. Begin
  80. if InOutRes<>0 then
  81. Exit;
  82. If (TextRec(t).mode<>fmClosed) Then
  83. Begin
  84. { Write pending buffer }
  85. If Textrec(t).Mode=fmoutput then
  86. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  87. TextRec(t).mode:=fmClosed;
  88. { Only close functions not connected to stdout.}
  89. If ((TextRec(t).Handle<>StdInputHandle) and
  90. (TextRec(t).Handle<>StdOutputHandle) and
  91. (TextRec(t).Handle<>StdErrorHandle)) Then
  92. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  93. { Reset buffer for safety }
  94. TextRec(t).BufPos:=0;
  95. TextRec(t).BufEnd:=0;
  96. End;
  97. End;
  98. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  99. Begin
  100. Case TextRec(t).mode Of {This gives the fastest code}
  101. fmInput,fmOutput,fmInOut : Close(t);
  102. fmClosed : ;
  103. else
  104. Begin
  105. InOutRes:=102;
  106. exit;
  107. End;
  108. End;
  109. TextRec(t).mode:=mode;
  110. TextRec(t).bufpos:=0;
  111. TextRec(t).bufend:=0;
  112. FileFunc(TextRec(t).OpenFunc)(TextRec(t))
  113. End;
  114. Procedure Rewrite(var t : Text);[IOCheck];
  115. Begin
  116. If InOutRes<>0 then
  117. exit;
  118. OpenText(t,fmOutput,1);
  119. End;
  120. Procedure Reset(var t : Text);[IOCheck];
  121. Begin
  122. If InOutRes<>0 then
  123. exit;
  124. OpenText(t,fmInput,0);
  125. End;
  126. Procedure Append(var t : Text);[IOCheck];
  127. Begin
  128. If InOutRes<>0 then
  129. exit;
  130. OpenText(t,fmAppend,1);
  131. End;
  132. Procedure Flush(var t : Text);[IOCheck];
  133. Begin
  134. If InOutRes<>0 then
  135. exit;
  136. If TextRec(t).mode<>fmOutput Then
  137. begin
  138. InOutres:=105;
  139. exit;
  140. end;
  141. { Not the flushfunc but the inoutfunc should be used, becuase that
  142. writes the data, flushfunc doesn't need to be assigned }
  143. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  144. End;
  145. Procedure Erase(var t:Text);[IOCheck];
  146. Begin
  147. If InOutRes <> 0 then
  148. exit;
  149. If TextRec(t).mode=fmClosed Then
  150. Do_Erase(PChar(@TextRec(t).Name));
  151. End;
  152. Procedure Rename(var t : text;p:pchar);[IOCheck];
  153. Begin
  154. If InOutRes <> 0 then
  155. exit;
  156. If TextRec(t).mode=fmClosed Then
  157. Begin
  158. Do_Rename(PChar(@TextRec(t).Name),p);
  159. Move(p^,TextRec(t).Name,StrLen(p)+1);
  160. End;
  161. End;
  162. Procedure Rename(var t : Text;const s : string);[IOCheck];
  163. var
  164. p : array[0..255] Of Char;
  165. Begin
  166. If InOutRes <> 0 then
  167. exit;
  168. Move(s[1],p,Length(s));
  169. p[Length(s)]:=#0;
  170. Rename(t,Pchar(@p));
  171. End;
  172. Procedure Rename(var t : Text;c : char);[IOCheck];
  173. var
  174. p : array[0..1] Of Char;
  175. Begin
  176. If InOutRes <> 0 then
  177. exit;
  178. p[0]:=c;
  179. p[1]:=#0;
  180. Rename(t,Pchar(@p));
  181. End;
  182. Function Eof(Var t: Text): Boolean;[IOCheck];
  183. Begin
  184. If (InOutRes<>0) then
  185. exit(true);
  186. if (TextRec(t).mode<>fmInput) Then
  187. begin
  188. InOutRes:=104;
  189. exit(true);
  190. end;
  191. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  192. begin
  193. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  194. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  195. exit(true);
  196. end;
  197. {$ifdef EOF_CTRLZ}
  198. Eof:=(TextRec(t).Bufptr^[TextRec(t).BufPos]=#26);
  199. {$else}
  200. Eof:=false;
  201. {$endif EOL_CTRLZ}
  202. end;
  203. Function Eof:Boolean;
  204. Begin
  205. Eof:=Eof(Input);
  206. End;
  207. Function SeekEof (Var t : Text) : Boolean;
  208. Begin
  209. If (InOutRes<>0) then
  210. exit(true);
  211. if (TextRec(t).mode<>fmInput) Then
  212. begin
  213. InOutRes:=104;
  214. exit(true);
  215. end;
  216. repeat
  217. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  218. begin
  219. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  220. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  221. exit(true);
  222. end;
  223. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  224. #26 : exit(true);
  225. #10,#13,
  226. #9,' ' : ;
  227. else
  228. exit(false);
  229. end;
  230. inc(TextRec(t).BufPos);
  231. until false;
  232. End;
  233. Function SeekEof : Boolean;
  234. Begin
  235. SeekEof:=SeekEof(Input);
  236. End;
  237. Function Eoln(var t:Text) : Boolean;
  238. Begin
  239. If (InOutRes<>0) then
  240. exit(true);
  241. if (TextRec(t).mode<>fmInput) Then
  242. begin
  243. InOutRes:=104;
  244. exit(true);
  245. end;
  246. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  247. begin
  248. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  249. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  250. exit(true);
  251. end;
  252. Eoln:=(TextRec(t).Bufptr^[TextRec(t).BufPos] in [#10,#13]);
  253. End;
  254. Function Eoln : Boolean;
  255. Begin
  256. Eoln:=Eoln(Input);
  257. End;
  258. Function SeekEoln (Var t : Text) : Boolean;
  259. Begin
  260. If (InOutRes<>0) then
  261. exit(true);
  262. if (TextRec(t).mode<>fmInput) Then
  263. begin
  264. InOutRes:=104;
  265. exit(true);
  266. end;
  267. repeat
  268. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  269. begin
  270. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  271. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  272. exit(true);
  273. end;
  274. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  275. #26,
  276. #10,#13 : exit(true);
  277. #9,' ' : ;
  278. else
  279. exit(false);
  280. end;
  281. inc(TextRec(t).BufPos);
  282. until false;
  283. End;
  284. Function SeekEoln : Boolean;
  285. Begin
  286. SeekEoln:=SeekEoln(Input);
  287. End;
  288. Procedure SetTextBuf(Var F : Text; Var Buf);[INTERNPROC: In_settextbuf_file_x];
  289. Procedure SetTextBuf(Var F : Text; Var Buf; Size : Word);
  290. Begin
  291. TextRec(f).BufPtr:=@Buf;
  292. TextRec(f).BufSize:=Size;
  293. TextRec(f).BufPos:=0;
  294. TextRec(f).BufEnd:=0;
  295. End;
  296. {*****************************************************************************
  297. Write(Ln)
  298. *****************************************************************************}
  299. Procedure WriteBuffer(var f:TextRec;var b;len:longint);
  300. var
  301. p : pchar;
  302. left,
  303. idx : longint;
  304. begin
  305. p:=pchar(@b);
  306. idx:=0;
  307. left:=f.BufSize-f.BufPos;
  308. while len>left do
  309. begin
  310. move(p[idx],f.Bufptr^[f.BufPos],left);
  311. dec(len,left);
  312. inc(idx,left);
  313. inc(f.BufPos,left);
  314. FileFunc(f.InOutFunc)(f);
  315. left:=f.BufSize-f.BufPos;
  316. end;
  317. move(p[idx],f.Bufptr^[f.BufPos],len);
  318. inc(f.BufPos,len);
  319. end;
  320. Procedure WriteBlanks(var f:TextRec;len:longint);
  321. var
  322. left : longint;
  323. begin
  324. left:=f.BufSize-f.BufPos;
  325. while len>left do
  326. begin
  327. FillChar(f.Bufptr^[f.BufPos],left,' ');
  328. dec(len,left);
  329. inc(f.BufPos,left);
  330. FileFunc(f.InOutFunc)(f);
  331. left:=f.BufSize-f.BufPos;
  332. end;
  333. FillChar(f.Bufptr^[f.BufPos],len,' ');
  334. inc(f.BufPos,len);
  335. end;
  336. Procedure Write_End(var f:TextRec);[Public,Alias:'FPC_WRITE_END'];
  337. begin
  338. if f.FlushFunc<>nil then
  339. FileFunc(f.FlushFunc)(f);
  340. end;
  341. Procedure Writeln_End(var f:TextRec);[Public,Alias:'FPC_WRITELN_END'];
  342. const
  343. {$IFDEF SHORT_LINEBREAK}
  344. eollen=1;
  345. eol : array[0..0] of char=(#10);
  346. {$ELSE SHORT_LINEBREAK}
  347. eollen=2;
  348. eol : array[0..1] of char=(#13,#10);
  349. {$ENDIF SHORT_LINEBREAK}
  350. begin
  351. If InOutRes <> 0 then exit;
  352. { Write EOL }
  353. WriteBuffer(f,eol,eollen);
  354. { Flush }
  355. if f.FlushFunc<>nil then
  356. FileFunc(f.FlushFunc)(f);
  357. end;
  358. Procedure Write_Str(Len : Longint;var f : TextRec;const s : String);[Public,Alias:'FPC_WRITE_TEXT_STRING'];
  359. Begin
  360. If (InOutRes<>0) then
  361. exit;
  362. if (f.mode<>fmOutput) Then
  363. begin
  364. InOutRes:=105;
  365. exit;
  366. end;
  367. If Len>Length(s) Then
  368. WriteBlanks(f,Len-Length(s));
  369. WriteBuffer(f,s[1],Length(s));
  370. End;
  371. Type
  372. array00 = array[0..0] Of Char;
  373. Procedure Write_Array(Len : Longint;var f : TextRec;const p : array00);[Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_ARRAY'];
  374. var
  375. ArrayLen : longint;
  376. Begin
  377. If (InOutRes<>0) then
  378. exit;
  379. if (f.mode<>fmOutput) Then
  380. begin
  381. InOutRes:=105;
  382. exit;
  383. end;
  384. ArrayLen:=StrLen(p);
  385. If Len>ArrayLen Then
  386. WriteBlanks(f,Len-ArrayLen);
  387. WriteBuffer(f,p,ArrayLen);
  388. End;
  389. Procedure Write_PChar(Len : Longint;var f : TextRec;p : PChar);[Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_POINTER'];
  390. var
  391. PCharLen : longint;
  392. Begin
  393. If (p=nil) or (InOutRes<>0) then
  394. exit;
  395. if (f.mode<>fmOutput) Then
  396. begin
  397. InOutRes:=105;
  398. exit;
  399. end;
  400. PCharLen:=StrLen(p);
  401. If Len>PCharLen Then
  402. WriteBlanks(f,Len-PCharLen);
  403. WriteBuffer(f,p^,PCharLen);
  404. End;
  405. Procedure Write_Text_AnsiString (Len : Longint; Var T : TextRec; S : Pointer);[Public,alias:'FPC_WRITE_TEXT_ANSISTRING'];
  406. {
  407. Writes a AnsiString to the Text file T
  408. }
  409. begin
  410. If S=Nil then
  411. exit;
  412. Write_pchar (Len,t,PChar(S));
  413. end;
  414. Procedure Write_LongInt(Len : Longint;var t : TextRec;l : Longint);[Public,Alias:'FPC_WRITE_TEXT_LONGINT'];
  415. var
  416. s : String;
  417. Begin
  418. If (InOutRes<>0) then
  419. exit;
  420. Str(l,s);
  421. Write_Str(Len,t,s);
  422. End;
  423. Procedure Write_Real(fixkomma,Len : Longint;var t : TextRec;r : real);[Public,Alias:'FPC_WRITE_TEXT_REAL'];
  424. var
  425. s : String;
  426. Begin
  427. If (InOutRes<>0) then
  428. exit;
  429. {$ifdef i386}
  430. Str_real(Len,fixkomma,r,rt_s64real,s);
  431. {$else}
  432. Str_real(Len,fixkomma,r,rt_s32real,s);
  433. {$endif}
  434. Write_Str(Len,t,s);
  435. End;
  436. Procedure Write_Cardinal(Len : Longint;var t : TextRec;l : cardinal);[Public,Alias:'FPC_WRITE_TEXT_CARDINAL'];
  437. var
  438. s : String;
  439. Begin
  440. If (InOutRes<>0) then
  441. exit;
  442. Str(L,s);
  443. Write_Str(Len,t,s);
  444. End;
  445. {$ifdef SUPPORT_SINGLE}
  446. Procedure Write_Single(fixkomma,Len : Longint;var t : TextRec;r : single);[Public,Alias:'FPC_WRITE_TEXT_SINGLE'];
  447. var
  448. s : String;
  449. Begin
  450. If (InOutRes<>0) then
  451. exit;
  452. Str_real(Len,fixkomma,r,rt_s32real,s);
  453. Write_Str(Len,t,s);
  454. End;
  455. {$endif SUPPORT_SINGLE}
  456. {$ifdef SUPPORT_EXTENDED}
  457. Procedure Write_Extended(fixkomma,Len : Longint;var t : TextRec;r : extended);[Public,Alias:'FPC_WRITE_TEXT_EXTENDED'];
  458. var
  459. s : String;
  460. Begin
  461. If (InOutRes<>0) then
  462. exit;
  463. Str_real(Len,fixkomma,r,rt_s80real,s);
  464. Write_Str(Len,t,s);
  465. End;
  466. {$endif SUPPORT_EXTENDED}
  467. {$ifdef SUPPORT_COMP}
  468. Procedure Write_Comp(fixkomma,Len : Longint;var t : TextRec;r : comp);[Public,Alias:'FPC_WRITE_TEXT_COMP'];
  469. var
  470. s : String;
  471. Begin
  472. If (InOutRes<>0) then
  473. exit;
  474. Str_real(Len,fixkomma,r,rt_s64bit,s);
  475. Write_Str(Len,t,s);
  476. End;
  477. {$endif SUPPORT_COMP}
  478. {$ifdef SUPPORT_FIXED}
  479. Procedure Write_Fixed(fixkomma,Len : Longint;var t : TextRec;r : fixed);[Public,Alias:'FPC_WRITE_TEXT_FIXED'];
  480. var
  481. s : String;
  482. Begin
  483. If (InOutRes<>0) then
  484. exit;
  485. Str_real(Len,fixkomma,r,rt_f32bit,s);
  486. Write_Str(Len,t,s);
  487. End;
  488. {$endif SUPPORT_FIXED}
  489. Procedure Write_Boolean(Len : Longint;var t : TextRec;b : Boolean);[Public,Alias:'FPC_WRITE_TEXT_BOOLEAN'];
  490. Begin
  491. If (InOutRes<>0) then
  492. exit;
  493. { Can't use array[boolean] because b can be >0 ! }
  494. if b then
  495. Write_Str(Len,t,'TRUE')
  496. else
  497. Write_Str(Len,t,'FALSE');
  498. End;
  499. Procedure Write_Char(Len : Longint;var t : TextRec;c : Char);[Public,Alias:'FPC_WRITE_TEXT_CHAR'];
  500. Begin
  501. If (InOutRes<>0) then
  502. exit;
  503. if (TextRec(t).mode<>fmOutput) Then
  504. begin
  505. InOutRes:=105;
  506. exit;
  507. end;
  508. If Len>1 Then
  509. WriteBlanks(t,Len-1);
  510. If t.BufPos+1>=t.BufSize Then
  511. FileFunc(t.InOutFunc)(t);
  512. t.Bufptr^[t.BufPos]:=c;
  513. Inc(t.BufPos);
  514. End;
  515. {*****************************************************************************
  516. Read(Ln)
  517. *****************************************************************************}
  518. Function NextChar(var f:TextRec;var s:string):Boolean;
  519. begin
  520. if f.BufPos<f.BufEnd then
  521. begin
  522. s:=s+f.BufPtr^[f.BufPos];
  523. Inc(f.BufPos);
  524. If f.BufPos>=f.BufEnd Then
  525. FileFunc(f.InOutFunc)(f);
  526. NextChar:=true;
  527. end
  528. else
  529. NextChar:=false;
  530. end;
  531. Function IgnoreSpaces(var f:TextRec):Boolean;
  532. {
  533. Removes all leading spaces,tab,eols from the input buffer, returns true if
  534. the buffer is empty
  535. }
  536. var
  537. s : string;
  538. begin
  539. s:='';
  540. IgnoreSpaces:=false;
  541. while f.Bufptr^[f.BufPos] in [#9,#10,#13,' '] do
  542. if not NextChar(f,s) then
  543. exit;
  544. IgnoreSpaces:=true;
  545. end;
  546. Function ReadSign(var f:TextRec;var s:string):Boolean;
  547. {
  548. Read + and - sign, return true if buffer is empty
  549. }
  550. begin
  551. ReadSign:=(not (f.Bufptr^[f.BufPos] in ['-','+'])) or NextChar(f,s);
  552. end;
  553. Function ReadBase(var f:TextRec;var s:string;var Base:longint):boolean;
  554. {
  555. Read the base $ For 16 and % For 2, if buffer is empty return true
  556. }
  557. begin
  558. case f.BufPtr^[f.BufPos] of
  559. '$' : Base:=16;
  560. '%' : Base:=2;
  561. else
  562. Base:=10;
  563. end;
  564. ReadBase:=(Base=10) or NextChar(f,s);
  565. end;
  566. Function ReadNumeric(var f:TextRec;var s:string;base:longint):Boolean;
  567. {
  568. Read numeric input, if buffer is empty then return True
  569. }
  570. var
  571. c : char;
  572. begin
  573. ReadNumeric:=false;
  574. c:=f.BufPtr^[f.BufPos];
  575. while ((base>=10) and (c in ['0'..'9'])) or
  576. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  577. ((base=2) and (c in ['0'..'1'])) do
  578. begin
  579. if not NextChar(f,s) then
  580. exit;
  581. c:=f.BufPtr^[f.BufPos];
  582. end;
  583. ReadNumeric:=true;
  584. end;
  585. Procedure Read_End(var f:TextRec);[Public,Alias:'FPC_READ_END'];
  586. begin
  587. if f.FlushFunc<>nil then
  588. FileFunc(f.FlushFunc)(f);
  589. end;
  590. Procedure ReadLn_End(var f : TextRec);[Public,Alias:'FPC_READLN_END'];
  591. Begin
  592. { Check error and if file is open and load buf if empty }
  593. If (InOutRes<>0) then
  594. exit;
  595. if (f.mode<>fmInput) Then
  596. begin
  597. InOutRes:=104;
  598. exit;
  599. end;
  600. repeat
  601. If f.BufPos>=f.BufEnd Then
  602. begin
  603. FileFunc(f.InOutFunc)(f);
  604. if f.BufPos>=f.BufEnd then
  605. break;
  606. end;
  607. inc(f.BufPos);
  608. if (f.BufPtr^[f.BufPos-1]=#10) then
  609. exit;
  610. until false;
  611. { Flush if set }
  612. if f.FlushFunc<>nil then
  613. FileFunc(f.FlushFunc)(f);
  614. End;
  615. Procedure Read_String(var f : TextRec;var s : String);[Public,Alias:'FPC_READ_TEXT_STRING'];
  616. var
  617. maxlen,
  618. sPos,len : Longint;
  619. p,startp,maxp : pchar;
  620. Begin
  621. { Delete the string }
  622. s:='';
  623. { Check error and if file is open }
  624. If (InOutRes<>0) then
  625. exit;
  626. if (f.mode<>fmInput) Then
  627. begin
  628. InOutRes:=104;
  629. exit;
  630. end;
  631. { Read maximal until Maxlen is reached }
  632. sPos:=0;
  633. MaxLen:=high(s);
  634. repeat
  635. If f.BufPos>=f.BufEnd Then
  636. begin
  637. FileFunc(f.InOutFunc)(f);
  638. If f.BufPos>=f.BufEnd Then
  639. break;
  640. end;
  641. p:[email protected]^[f.BufPos];
  642. if SPos+f.BufEnd-f.BufPos>MaxLen then
  643. maxp:[email protected]^[f.BufPos+MaxLen-SPos]
  644. else
  645. maxp:[email protected]^[f.BufEnd];
  646. startp:=p;
  647. { search linefeed }
  648. while (p<maxp) and (P^<>#10) do
  649. inc(p);
  650. { calculate read bytes }
  651. len:=p-startp;
  652. inc(f.BufPos,Len);
  653. Move(startp^,s[sPos+1],Len);
  654. inc(sPos,Len);
  655. { was it a LF? then leave }
  656. if p^=#10 then
  657. begin
  658. if (spos>0) and (s[spos]=#13) then
  659. dec(sPos);
  660. break;
  661. end;
  662. { Maxlen reached ? }
  663. if spos=MaxLen then
  664. break;
  665. until false;
  666. { Set final length }
  667. s[0]:=chr(sPos);
  668. End;
  669. Procedure Read_Char(var f : TextRec;var c : Char);[Public,Alias:'FPC_READ_TEXT_CHAR'];
  670. Begin
  671. c:=#0;
  672. { Check error and if file is open }
  673. If (InOutRes<>0) then
  674. exit;
  675. if (f.mode<>fmInput) Then
  676. begin
  677. InOutRes:=104;
  678. exit;
  679. end;
  680. { Read next char or EOF }
  681. If f.BufPos>=f.BufEnd Then
  682. begin
  683. FileFunc(f.InOutFunc)(f);
  684. If f.BufPos>=f.BufEnd Then
  685. begin
  686. c:=#26;
  687. exit;
  688. end;
  689. end;
  690. c:=f.Bufptr^[f.BufPos];
  691. inc(f.BufPos);
  692. end;
  693. Procedure Read_PChar(var f : TextRec;var s : PChar);[Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER'];
  694. var
  695. p,maxp,startp,sidx : PChar;
  696. len : longint;
  697. Begin
  698. { Delete the string }
  699. s^:=#0;
  700. { Check error and if file is open }
  701. If (InOutRes<>0) then
  702. exit;
  703. if (f.mode<>fmInput) Then
  704. begin
  705. InOutRes:=104;
  706. exit;
  707. end;
  708. { Read until #10 is found }
  709. sidx:=s;
  710. repeat
  711. If f.BufPos>=f.BufEnd Then
  712. begin
  713. FileFunc(f.InOutFunc)(f);
  714. If f.BufPos>=f.BufEnd Then
  715. break;
  716. end;
  717. p:[email protected]^[f.BufPos];
  718. maxp:[email protected]^[f.BufEnd];
  719. startp:=p;
  720. { search linefeed }
  721. while (p<maxp) and (P^<>#10) do
  722. inc(p);
  723. { calculate read bytes }
  724. len:=p-startp;
  725. inc(f.BufPos,Len);
  726. { update output string, take MaxLen into count }
  727. Move(startp^,sidx^,Len);
  728. inc(sidx,len);
  729. { was it a LF? then leave }
  730. if p^=#10 then
  731. begin
  732. If pchar(p-1)^=#13 Then
  733. dec(p);
  734. break;
  735. end;
  736. until false;
  737. sidx^:=#0;
  738. End;
  739. Procedure Read_Array(var f : TextRec;var s : array00);[Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY'];
  740. var
  741. p,maxp,startp,sidx : PChar;
  742. len : longint;
  743. Begin
  744. { Delete the string }
  745. s[0]:=#0;
  746. { Check error and if file is open }
  747. If (InOutRes<>0) then
  748. exit;
  749. if (f.mode<>fmInput) Then
  750. begin
  751. InOutRes:=104;
  752. exit;
  753. end;
  754. { Read until #10 is found }
  755. sidx:=pchar(@s);
  756. repeat
  757. If f.BufPos>=f.BufEnd Then
  758. begin
  759. FileFunc(f.InOutFunc)(f);
  760. If f.BufPos>=f.BufEnd Then
  761. break;
  762. end;
  763. p:[email protected]^[f.BufPos];
  764. maxp:[email protected]^[f.BufEnd];
  765. startp:=p;
  766. { search linefeed }
  767. while (p<maxp) and (P^<>#10) do
  768. inc(p);
  769. { calculate read bytes }
  770. len:=p-startp;
  771. inc(f.BufPos,Len);
  772. { update output string, take MaxLen into count }
  773. Move(startp^,sidx^,Len);
  774. inc(sidx,len);
  775. { was it a LF? then leave }
  776. if p^=#10 then
  777. begin
  778. If pchar(p-1)^=#13 Then
  779. dec(p);
  780. break;
  781. end;
  782. until false;
  783. sidx^:=#0;
  784. End;
  785. Procedure Read_AnsiString(var f : TextRec;var s : AnsiString);[Public,Alias:'FPC_READ_TEXT_ANSISTRING'];
  786. var
  787. p,maxp,startp,sidx : PChar;
  788. maxlen,spos,len : longint;
  789. Begin
  790. { Delete the string }
  791. Decr_ansi_ref (Pointer(S));
  792. { We assign room for 1024 characters totally at random.... }
  793. Pointer(s):=Pointer(NewAnsiString(1024));
  794. MaxLen:=1024;
  795. { Check error and if file is open }
  796. If (InOutRes<>0) then
  797. exit;
  798. if (f.mode<>fmInput) Then
  799. begin
  800. InOutRes:=104;
  801. exit;
  802. end;
  803. { Read until #10 is found }
  804. sidx:=pchar(@s);
  805. spos:=0;
  806. repeat
  807. If f.BufPos>=f.BufEnd Then
  808. begin
  809. FileFunc(f.InOutFunc)(f);
  810. If f.BufPos>=f.BufEnd Then
  811. break;
  812. end;
  813. p:[email protected]^[f.BufPos];
  814. if SPos+f.BufEnd-f.BufPos>MaxLen then
  815. maxp:[email protected]^[f.BufPos+MaxLen-SPos]
  816. else
  817. maxp:[email protected]^[f.BufEnd];
  818. startp:=p;
  819. { search linefeed }
  820. while (p<maxp) and (P^<>#10) do
  821. inc(p);
  822. { calculate read bytes }
  823. len:=p-startp;
  824. inc(f.BufPos,Len);
  825. Move(startp^,sidx^,Len);
  826. inc(sidx,len);
  827. inc(spos,len);
  828. { was it a LF? then leave }
  829. if p^=#10 then
  830. begin
  831. If pchar(sidx-1)^=#13 Then
  832. begin
  833. dec(sidx);
  834. dec(spos);
  835. end;
  836. break;
  837. end;
  838. { Maxlen reached ? }
  839. if spos=MaxLen then
  840. break;
  841. until false;
  842. sidx^:=#0;
  843. PAnsiRec(Pointer(S)-FirstOff)^.Len:=spos;
  844. End;
  845. Procedure Read_Longint(var f : TextRec;var l : Longint);[Public,Alias:'FPC_READ_TEXT_LONGINT'];
  846. var
  847. hs : String;
  848. code : Word;
  849. base : longint;
  850. Begin
  851. l:=0;
  852. { Leave if error or not open file, else check for empty buf }
  853. If (InOutRes<>0) then
  854. exit;
  855. if (f.mode<>fmInput) Then
  856. begin
  857. InOutRes:=104;
  858. exit;
  859. end;
  860. If f.BufPos>=f.BufEnd Then
  861. FileFunc(f.InOutFunc)(f);
  862. hs:='';
  863. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  864. ReadNumeric(f,hs,Base);
  865. Val(hs,l,code);
  866. If code<>0 Then
  867. HandleError(106);
  868. End;
  869. Procedure Read_Integer(var f : TextRec;var l : Integer);[Public,Alias:'FPC_READ_TEXT_INTEGER'];
  870. var
  871. ll : Longint;
  872. Begin
  873. l:=0;
  874. If InOutRes <> 0 then
  875. exit;
  876. Read_Longint(f,ll);
  877. If (ll<-32768) or (ll>32767) Then
  878. HandleError(106);
  879. l:=ll;
  880. End;
  881. Procedure Read_Word(var f : TextRec;var l : Word);[Public,Alias:'FPC_READ_TEXT_WORD'];
  882. var
  883. ll : Longint;
  884. Begin
  885. l:=0;
  886. If InOutRes <> 0 then
  887. exit;
  888. Read_Longint(f,ll);
  889. If (ll<0) or (ll>$ffff) Then
  890. HandleError(106);
  891. l:=ll;
  892. End;
  893. Procedure Read_Byte(var f : TextRec;var l : byte);[Public,Alias:'FPC_READ_TEXT_BYTE'];
  894. var
  895. ll : Longint;
  896. Begin
  897. l:=0;
  898. If InOutRes <> 0 then
  899. exit;
  900. Read_Longint(f,ll);
  901. If (ll<0) or (ll>255) Then
  902. HandleError(106);
  903. l:=ll;
  904. End;
  905. Procedure Read_Shortint(var f : TextRec;var l : shortint);[Public,Alias:'FPC_READ_TEXT_SHORTINT'];
  906. var
  907. ll : Longint;
  908. Begin
  909. l:=0;
  910. If InOutRes <> 0 then
  911. exit;
  912. Read_Longint(f,ll);
  913. If (ll<-128) or (ll>127) Then
  914. HandleError(106);
  915. l:=ll;
  916. End;
  917. Procedure Read_Cardinal(var f : TextRec;var l : cardinal);[Public,Alias:'FPC_READ_TEXT_CARDINAL'];
  918. var
  919. hs : String;
  920. code : Word;
  921. base : longint;
  922. Begin
  923. l:=0;
  924. { Leave if error or not open file, else check for empty buf }
  925. If (InOutRes<>0) then
  926. exit;
  927. if (f.mode<>fmInput) Then
  928. begin
  929. InOutRes:=104;
  930. exit;
  931. end;
  932. If f.BufPos>=f.BufEnd Then
  933. FileFunc(f.InOutFunc)(f);
  934. hs:='';
  935. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  936. ReadNumeric(f,hs,Base);
  937. val(hs,l,code);
  938. If code<>0 Then
  939. HandleError(106);
  940. End;
  941. function ReadRealStr(var f:TextRec):string;
  942. var
  943. hs : string;
  944. begin
  945. ReadRealStr:='';
  946. { Leave if error or not open file, else check for empty buf }
  947. If (InOutRes<>0) then
  948. exit;
  949. if (f.mode<>fmInput) Then
  950. begin
  951. InOutRes:=104;
  952. exit;
  953. end;
  954. If f.BufPos>=f.BufEnd Then
  955. FileFunc(f.InOutFunc)(f);
  956. hs:='';
  957. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  958. begin
  959. { First check for a . }
  960. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  961. begin
  962. hs:=hs+'.';
  963. Inc(f.BufPos);
  964. If f.BufPos>=f.BufEnd Then
  965. FileFunc(f.InOutFunc)(f);
  966. ReadNumeric(f,hs,10);
  967. end;
  968. { Also when a point is found check for a E }
  969. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  970. begin
  971. hs:=hs+'E';
  972. Inc(f.BufPos);
  973. If f.BufPos>=f.BufEnd Then
  974. FileFunc(f.InOutFunc)(f);
  975. if ReadSign(f,hs) then
  976. ReadNumeric(f,hs,10);
  977. end;
  978. end;
  979. ReadRealStr:=hs;
  980. end;
  981. Procedure Read_Real(var f : TextRec;var d : Real);[Public,Alias:'FPC_READ_TEXT_REAL'];
  982. var
  983. code : Word;
  984. Begin
  985. val(ReadRealStr(f),d,code);
  986. If code<>0 Then
  987. HandleError(106);
  988. End;
  989. {$ifdef SUPPORT_SINGLE}
  990. Procedure Read_Single(var f : TextRec;var d : single);[Public,Alias:'FPC_READ_TEXT_SINGLE'];
  991. var
  992. code : Word;
  993. Begin
  994. val(ReadRealStr(f),d,code);
  995. If code<>0 Then
  996. HandleError(106);
  997. End;
  998. {$endif SUPPORT_SINGLE}
  999. {$ifdef SUPPORT_EXTENDED}
  1000. Procedure Read_Extended(var f : TextRec;var d : extended);[Public,Alias:'FPC_READ_TEXT_EXTENDED'];
  1001. var
  1002. code : Word;
  1003. Begin
  1004. val(ReadRealStr(f),d,code);
  1005. If code<>0 Then
  1006. HandleError(106);
  1007. End;
  1008. {$endif SUPPORT_EXTENDED}
  1009. {$ifdef SUPPORT_COMP}
  1010. Procedure Read_Comp(var f : TextRec;var d : comp);[Public,Alias:'FPC_READ_TEXT_COMP'];
  1011. var
  1012. code : Word;
  1013. Begin
  1014. val(ReadRealStr(f),d,code);
  1015. If code<>0 Then
  1016. HandleError(106);
  1017. End;
  1018. {$endif SUPPORT_COMP}
  1019. {$ifdef SUPPORT_FIXED}
  1020. Procedure Read_Fixed(var f : TextRec;var d : fixed);[Public,Alias:'FPC_READ_TEXT_FIXED'];
  1021. var
  1022. code : Word;
  1023. Begin
  1024. val(ReadRealStr(f),d,code);
  1025. If code<>0 Then
  1026. HandleError(106);
  1027. End;
  1028. {$endif SUPPORT_FIXED}
  1029. {*****************************************************************************
  1030. Initializing
  1031. *****************************************************************************}
  1032. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1033. begin
  1034. Assign(f,'');
  1035. TextRec(f).Handle:=hdl;
  1036. TextRec(f).Mode:=mode;
  1037. TextRec(f).Closefunc:=@FileCloseFunc;
  1038. case mode of
  1039. fmInput : TextRec(f).InOutFunc:=@FileReadFunc;
  1040. fmOutput : begin
  1041. TextRec(f).InOutFunc:=@FileWriteFunc;
  1042. TextRec(f).FlushFunc:=@FileWriteFunc;
  1043. end;
  1044. else
  1045. HandleError(102);
  1046. end;
  1047. end;
  1048. {
  1049. $Log$
  1050. Revision 1.37 1998-12-15 22:43:06 peter
  1051. * removed temp symbols
  1052. Revision 1.36 1998/12/11 18:07:39 peter
  1053. * fixed read(char) with empty buffer
  1054. Revision 1.35 1998/11/27 14:50:58 peter
  1055. + open strings, $P switch support
  1056. Revision 1.34 1998/11/16 12:21:48 peter
  1057. * fixes for 0.99.8
  1058. Revision 1.33 1998/10/23 00:03:29 peter
  1059. * write(pchar) has check for nil
  1060. Revision 1.32 1998/10/20 14:37:45 peter
  1061. * fixed maxlen which was not correct after my read_string update
  1062. Revision 1.31 1998/10/10 15:28:48 peter
  1063. + read single,fixed
  1064. + val with code:longint
  1065. + val for fixed
  1066. Revision 1.30 1998/09/29 08:39:07 michael
  1067. + Ansistring write now gets pointer.
  1068. Revision 1.29 1998/09/28 14:27:08 michael
  1069. + AnsiStrings update
  1070. Revision 1.28 1998/09/24 23:32:24 peter
  1071. * fixed small bug with a #13#10 on a line
  1072. Revision 1.27 1998/09/18 12:23:22 peter
  1073. * fixed a bug introduced by my previous update
  1074. Revision 1.26 1998/09/17 16:34:18 peter
  1075. * new eof,eoln,seekeoln,seekeof
  1076. * speed upgrade for read_string
  1077. * inoutres 104/105 updates for read_* and write_*
  1078. Revision 1.25 1998/09/14 10:48:23 peter
  1079. * FPC_ names
  1080. * Heap manager is now system independent
  1081. Revision 1.24 1998/09/08 10:14:06 peter
  1082. + textrecbufsize
  1083. Revision 1.23 1998/08/26 15:33:28 peter
  1084. * reset bufpos,bufend in opentext like tp7
  1085. Revision 1.22 1998/08/26 11:23:25 pierre
  1086. * close did not reset the bufpos and bufend fields
  1087. led to problems when using the same file several times
  1088. Revision 1.21 1998/08/17 22:42:17 michael
  1089. + Flush on close only for output files cd ../inc
  1090. Revision 1.20 1998/08/11 00:05:28 peter
  1091. * $ifdef ver0_99_5 updates
  1092. Revision 1.19 1998/07/30 13:26:16 michael
  1093. + Added support for ErrorProc variable. All internal functions are required
  1094. to call HandleError instead of runerror from now on.
  1095. This is necessary for exception support.
  1096. Revision 1.18 1998/07/29 21:44:35 michael
  1097. + Implemented reading/writing of ansistrings
  1098. Revision 1.17 1998/07/19 19:55:33 michael
  1099. + fixed rename. Changed p to p^
  1100. Revision 1.16 1998/07/10 11:02:40 peter
  1101. * support_fixed, becuase fixed is not 100% yet for the m68k
  1102. Revision 1.15 1998/07/06 15:56:43 michael
  1103. Added length checking for string reading
  1104. Revision 1.14 1998/07/02 12:14:56 carl
  1105. + Each IOCheck routine now check InOutRes before, just like TP
  1106. Revision 1.13 1998/07/01 15:30:00 peter
  1107. * better readln/writeln
  1108. Revision 1.12 1998/07/01 14:48:10 carl
  1109. * bugfix of WRITE_TEXT_BOOLEAN , was not TP compatible
  1110. + added explicit typecast in OpenText
  1111. Revision 1.11 1998/06/25 09:44:22 daniel
  1112. + RTLLITE directive to compile minimal RTL.
  1113. Revision 1.10 1998/06/04 23:46:03 peter
  1114. * comp,extended are only i386 added support_comp,support_extended
  1115. Revision 1.9 1998/06/02 16:47:56 pierre
  1116. * bug for boolean values greater than one fixed
  1117. Revision 1.8 1998/05/31 14:14:54 peter
  1118. * removed warnings using comp()
  1119. Revision 1.7 1998/05/27 00:19:21 peter
  1120. * fixed crt input
  1121. Revision 1.6 1998/05/21 19:31:01 peter
  1122. * objects compiles for linux
  1123. + assign(pchar), assign(char), rename(pchar), rename(char)
  1124. * fixed read_text_as_array
  1125. + read_text_as_pchar which was not yet in the rtl
  1126. Revision 1.5 1998/05/12 10:42:45 peter
  1127. * moved getopts to inc/, all supported OS's need argc,argv exported
  1128. + strpas, strlen are now exported in the systemunit
  1129. * removed logs
  1130. * removed $ifdef ver_above
  1131. Revision 1.4 1998/04/07 22:40:46 florian
  1132. * final fix of comp writing
  1133. }