text.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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. }
  15. {****************************************************************************
  16. subroutines For TextFile handling
  17. ****************************************************************************}
  18. Procedure FileCloseFunc(Var t:TextRec);
  19. Begin
  20. Do_Close(t.Handle);
  21. t.Handle:=UnusedHandle;
  22. End;
  23. Procedure FileReadFunc(var t:TextRec);
  24. Begin
  25. t.BufEnd:=Do_Read(t.Handle,t.Bufptr,t.BufSize);
  26. t.BufPos:=0;
  27. End;
  28. Procedure FileWriteFunc(var t:TextRec);
  29. var
  30. i : longint;
  31. Begin
  32. i:=Do_Write(t.Handle,t.Bufptr,t.BufPos);
  33. if i<>t.BufPos then
  34. InOutRes:=101;
  35. t.BufPos:=0;
  36. End;
  37. Procedure FileOpenFunc(var t:TextRec);
  38. var
  39. Flags : Longint;
  40. Begin
  41. Case t.mode Of
  42. fmInput : Flags:=$10000;
  43. fmOutput : Flags:=$11001;
  44. fmAppend : Flags:=$10101;
  45. else
  46. begin
  47. InOutRes:=102;
  48. exit;
  49. end;
  50. End;
  51. Do_Open(t,PChar(@t.Name),Flags);
  52. t.CloseFunc:=@FileCloseFunc;
  53. t.FlushFunc:=nil;
  54. if t.Mode=fmInput then
  55. t.InOutFunc:=@FileReadFunc
  56. else
  57. begin
  58. t.InOutFunc:=@FileWriteFunc;
  59. { Only install flushing if its a NOT a file, and only check if there
  60. was no error opening the file, becuase else we always get a bad
  61. file handle error 6 (PFV) }
  62. if (InOutRes=0) and
  63. Do_Isdevice(t.Handle) then
  64. t.FlushFunc:=@FileWriteFunc;
  65. end;
  66. End;
  67. Procedure assign(var t:Text;const s:String);
  68. Begin
  69. FillChar(t,SizeOf(TextRec),0);
  70. { only set things that are not zero }
  71. TextRec(t).Handle:=UnusedHandle;
  72. TextRec(t).mode:=fmClosed;
  73. TextRec(t).BufSize:=TextRecBufSize;
  74. TextRec(t).Bufptr:=@TextRec(t).Buffer;
  75. TextRec(t).OpenFunc:=@FileOpenFunc;
  76. TextRec(t).LineEnd:=LineEnding;
  77. Move(s[1],TextRec(t).Name,Length(s));
  78. End;
  79. Procedure assign(var t:Text;p:pchar);
  80. begin
  81. Assign(t,StrPas(p));
  82. end;
  83. Procedure assign(var t:Text;c:char);
  84. begin
  85. Assign(t,string(c));
  86. end;
  87. Procedure Close(var t : Text);[IOCheck];
  88. Begin
  89. if InOutRes<>0 then
  90. Exit;
  91. case TextRec(t).mode of
  92. fmInput,fmOutPut,fmAppend:
  93. Begin
  94. { Write pending buffer }
  95. If Textrec(t).Mode=fmoutput then
  96. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  97. { Only close functions not connected to stdout.}
  98. If ((TextRec(t).Handle<>StdInputHandle) and
  99. (TextRec(t).Handle<>StdOutputHandle) and
  100. (TextRec(t).Handle<>StdErrorHandle)) Then
  101. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  102. TextRec(t).mode := fmClosed;
  103. { Reset buffer for safety }
  104. TextRec(t).BufPos:=0;
  105. TextRec(t).BufEnd:=0;
  106. End
  107. else inOutRes := 103;
  108. End;
  109. End;
  110. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  111. Begin
  112. Case TextRec(t).mode Of {This gives the fastest code}
  113. fmInput,fmOutput,fmInOut : Close(t);
  114. fmClosed : ;
  115. else
  116. Begin
  117. InOutRes:=102;
  118. exit;
  119. End;
  120. End;
  121. TextRec(t).mode:=mode;
  122. TextRec(t).bufpos:=0;
  123. TextRec(t).bufend:=0;
  124. FileFunc(TextRec(t).OpenFunc)(TextRec(t));
  125. { reset the mode to closed when an error has occured }
  126. if InOutRes<>0 then
  127. TextRec(t).mode:=fmClosed;
  128. End;
  129. Procedure Rewrite(var t : Text);[IOCheck];
  130. Begin
  131. If InOutRes<>0 then
  132. exit;
  133. OpenText(t,fmOutput,1);
  134. End;
  135. Procedure Reset(var t : Text);[IOCheck];
  136. Begin
  137. If InOutRes<>0 then
  138. exit;
  139. OpenText(t,fmInput,0);
  140. End;
  141. Procedure Append(var t : Text);[IOCheck];
  142. Begin
  143. If InOutRes<>0 then
  144. exit;
  145. OpenText(t,fmAppend,1);
  146. End;
  147. Procedure Flush(var t : Text);[IOCheck];
  148. Begin
  149. If InOutRes<>0 then
  150. exit;
  151. if TextRec(t).mode<>fmOutput then
  152. begin
  153. if TextRec(t).mode=fmInput then
  154. InOutRes:=105
  155. else
  156. InOutRes:=103;
  157. exit;
  158. end;
  159. { Not the flushfunc but the inoutfunc should be used, becuase that
  160. writes the data, flushfunc doesn't need to be assigned }
  161. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  162. End;
  163. Procedure Erase(var t:Text);[IOCheck];
  164. Begin
  165. If InOutRes <> 0 then
  166. exit;
  167. If TextRec(t).mode=fmClosed Then
  168. Do_Erase(PChar(@TextRec(t).Name));
  169. End;
  170. Procedure Rename(var t : text;p:pchar);[IOCheck];
  171. Begin
  172. If InOutRes <> 0 then
  173. exit;
  174. If TextRec(t).mode=fmClosed Then
  175. Begin
  176. Do_Rename(PChar(@TextRec(t).Name),p);
  177. { check error code of do_rename }
  178. If InOutRes = 0 then
  179. Move(p^,TextRec(t).Name,StrLen(p)+1);
  180. End;
  181. End;
  182. Procedure Rename(var t : Text;const s : string);[IOCheck];
  183. var
  184. p : array[0..255] Of Char;
  185. Begin
  186. If InOutRes <> 0 then
  187. exit;
  188. Move(s[1],p,Length(s));
  189. p[Length(s)]:=#0;
  190. Rename(t,Pchar(@p));
  191. End;
  192. Procedure Rename(var t : Text;c : char);[IOCheck];
  193. var
  194. p : array[0..1] Of Char;
  195. Begin
  196. If InOutRes <> 0 then
  197. exit;
  198. p[0]:=c;
  199. p[1]:=#0;
  200. Rename(t,Pchar(@p));
  201. End;
  202. Function Eof(Var t: Text): Boolean;[IOCheck];
  203. Begin
  204. If (InOutRes<>0) then
  205. exit(true);
  206. if (TextRec(t).mode<>fmInput) Then
  207. begin
  208. if TextRec(t).mode=fmOutput then
  209. InOutRes:=104
  210. else
  211. InOutRes:=103;
  212. exit(true);
  213. end;
  214. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  215. begin
  216. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  217. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  218. exit(true);
  219. end;
  220. {$ifdef EOF_CTRLZ}
  221. Eof:=(TextRec(t).Bufptr^[TextRec(t).BufPos]=#26);
  222. {$else}
  223. Eof:=false;
  224. {$endif EOL_CTRLZ}
  225. end;
  226. Function Eof:Boolean;
  227. Begin
  228. Eof:=Eof(Input);
  229. End;
  230. Function SeekEof (Var t : Text) : Boolean;
  231. var
  232. oldfilepos, oldbufpos, oldbufend, reads: longint;
  233. isdevice: boolean;
  234. Begin
  235. If (InOutRes<>0) then
  236. exit(true);
  237. if (TextRec(t).mode<>fmInput) Then
  238. begin
  239. if TextRec(t).mode=fmOutPut then
  240. InOutRes:=104
  241. else
  242. InOutRes:=103;
  243. exit(true);
  244. end;
  245. { try to save the current position in the file, seekeof() should not move }
  246. { the current file position (JM) }
  247. oldbufpos := TextRec(t).BufPos;
  248. oldbufend := TextRec(t).BufEnd;
  249. reads := 0;
  250. oldfilepos := -1;
  251. isdevice := Do_IsDevice(TextRec(t).handle);
  252. repeat
  253. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  254. begin
  255. { signal that the we will have to do a seek }
  256. inc(reads);
  257. if not isdevice and
  258. (reads = 1) then
  259. begin
  260. oldfilepos := Do_FilePos(TextRec(t).handle) - TextRec(t).BufEnd;
  261. InOutRes:=0;
  262. end;
  263. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  264. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  265. begin
  266. { if we only did a read in which we didn't read anything, the }
  267. { old buffer is still valid and we can simply restore the }
  268. { pointers (JM) }
  269. dec(reads);
  270. SeekEof := true;
  271. break;
  272. end;
  273. end;
  274. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  275. {$ifdef EOF_CTRLZ}
  276. #26 :
  277. begin
  278. SeekEof := true;
  279. break;
  280. end;
  281. {$endif EOF_CTRLZ}
  282. #10,#13,
  283. #9,' ' : ;
  284. else
  285. begin
  286. SeekEof := false;
  287. break;
  288. end;
  289. end;
  290. inc(TextRec(t).BufPos);
  291. until false;
  292. { restore file position if not working with a device }
  293. if not isdevice then
  294. { if we didn't modify the buffer, simply restore the BufPos and BufEnd }
  295. { (the latter becuase it's now probably set to zero because nothing was }
  296. { was read anymore) }
  297. if (reads = 0) then
  298. begin
  299. TextRec(t).BufPos:=oldbufpos;
  300. TextRec(t).BufEnd:=oldbufend;
  301. end
  302. { otherwise return to the old filepos and reset the buffer }
  303. else
  304. begin
  305. do_seek(TextRec(t).handle,oldfilepos);
  306. InOutRes:=0;
  307. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  308. TextRec(t).BufPos:=oldbufpos;
  309. end;
  310. End;
  311. Function SeekEof : Boolean;
  312. Begin
  313. SeekEof:=SeekEof(Input);
  314. End;
  315. Function Eoln(var t:Text) : Boolean;
  316. Begin
  317. If (InOutRes<>0) then
  318. exit(true);
  319. if (TextRec(t).mode<>fmInput) Then
  320. begin
  321. if TextRec(t).mode=fmOutPut then
  322. InOutRes:=104
  323. else
  324. InOutRes:=103;
  325. exit(true);
  326. end;
  327. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  328. begin
  329. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  330. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  331. exit(true);
  332. end;
  333. Eoln:=(TextRec(t).Bufptr^[TextRec(t).BufPos] in [#10,#13]);
  334. End;
  335. Function Eoln : Boolean;
  336. Begin
  337. Eoln:=Eoln(Input);
  338. End;
  339. Function SeekEoln (Var t : Text) : Boolean;
  340. Begin
  341. If (InOutRes<>0) then
  342. exit(true);
  343. if (TextRec(t).mode<>fmInput) Then
  344. begin
  345. if TextRec(t).mode=fmOutput then
  346. InOutRes:=104
  347. else
  348. InOutRes:=103;
  349. exit(true);
  350. end;
  351. repeat
  352. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  353. begin
  354. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  355. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  356. exit(true);
  357. end;
  358. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  359. #26,
  360. #10,#13 : exit(true);
  361. #9,' ' : ;
  362. else
  363. exit(false);
  364. end;
  365. inc(TextRec(t).BufPos);
  366. until false;
  367. End;
  368. Function SeekEoln : Boolean;
  369. Begin
  370. SeekEoln:=SeekEoln(Input);
  371. End;
  372. Procedure SetTextBuf(Var F : Text; Var Buf);[INTERNPROC: In_settextbuf_file_x];
  373. Procedure SetTextBuf(Var F : Text; Var Buf; Size : Longint);
  374. Begin
  375. TextRec(f).BufPtr:=@Buf;
  376. TextRec(f).BufSize:=Size;
  377. TextRec(f).BufPos:=0;
  378. TextRec(f).BufEnd:=0;
  379. End;
  380. Procedure SetTextLineEnding(Var f:Text; Ending:string);
  381. Begin
  382. TextRec(F).LineEnd:=Ending;
  383. End;
  384. Function fpc_get_input:PText;{$ifdef hascompilerproc}compilerproc;{$endif}
  385. begin
  386. fpc_get_input:=@Input;
  387. end;
  388. Function fpc_get_output:PText;{$ifdef hascompilerproc}compilerproc;{$endif}
  389. begin
  390. fpc_get_output:=@Output;
  391. end;
  392. {*****************************************************************************
  393. Write(Ln)
  394. *****************************************************************************}
  395. Procedure fpc_WriteBuffer(var f:Text;const b;len:longint);[Public,Alias:'FPC_WRITEBUFFER'];
  396. var
  397. p : pchar;
  398. left,
  399. idx : longint;
  400. begin
  401. p:=pchar(@b);
  402. idx:=0;
  403. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  404. while len>left do
  405. begin
  406. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],left);
  407. dec(len,left);
  408. inc(idx,left);
  409. inc(TextRec(f).BufPos,left);
  410. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  411. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  412. end;
  413. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],len);
  414. inc(TextRec(f).BufPos,len);
  415. end;
  416. Procedure fpc_WriteBlanks(var f:Text;len:longint);[Public,Alias:'FPC_WRITEBLANKS'];
  417. var
  418. left : longint;
  419. begin
  420. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  421. while len>left do
  422. begin
  423. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],left,' ');
  424. dec(len,left);
  425. inc(TextRec(f).BufPos,left);
  426. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  427. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  428. end;
  429. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],len,' ');
  430. inc(TextRec(f).BufPos,len);
  431. end;
  432. Procedure fpc_Write_End(var f:Text);[Public,Alias:'FPC_WRITE_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  433. begin
  434. if TextRec(f).FlushFunc<>nil then
  435. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  436. end;
  437. Procedure fpc_Writeln_End(var f:Text);[Public,Alias:'FPC_WRITELN_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  438. begin
  439. If InOutRes <> 0 then exit;
  440. case TextRec(f).mode of
  441. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  442. begin
  443. { Write EOL }
  444. fpc_WriteBuffer(f,TextRec(f).LineEnd[1],length(TextRec(f).LineEnd));
  445. { Flush }
  446. if TextRec(f).FlushFunc<>nil then
  447. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  448. end;
  449. fmInput: InOutRes:=105
  450. else InOutRes:=103;
  451. end;
  452. end;
  453. Procedure fpc_Write_Text_ShortStr(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  454. Begin
  455. If (InOutRes<>0) then
  456. exit;
  457. case TextRec(f).mode of
  458. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  459. begin
  460. If Len>Length(s) Then
  461. fpc_WriteBlanks(f,Len-Length(s));
  462. fpc_WriteBuffer(f,s[1],Length(s));
  463. end;
  464. fmInput: InOutRes:=105
  465. else InOutRes:=103;
  466. end;
  467. End;
  468. { provide local access to write_str }
  469. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  470. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of char); iocheck; [Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_ARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  471. var
  472. ArrayLen : longint;
  473. p : pchar;
  474. Begin
  475. If (InOutRes<>0) then
  476. exit;
  477. case TextRec(f).mode of
  478. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  479. begin
  480. p:=pchar(@s);
  481. { can't use StrLen, since that one could try to read past the end }
  482. { of the heap (JM) }
  483. ArrayLen:=IndexByte(p^,high(s)+1,0);
  484. { IndexByte returns -1 if not found (JM) }
  485. if ArrayLen = -1 then
  486. ArrayLen := high(s)+1;
  487. If Len>ArrayLen Then
  488. fpc_WriteBlanks(f,Len-ArrayLen);
  489. fpc_WriteBuffer(f,p^,ArrayLen);
  490. end;
  491. fmInput: InOutRes:=105
  492. else InOutRes:=103;
  493. end;
  494. End;
  495. Procedure fpc_Write_Text_PChar_As_Pointer(Len : Longint;var f : Text;p : PChar); iocheck; [Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_POINTER']; {$ifdef hascompilerproc} compilerproc; {$endif}
  496. var
  497. PCharLen : longint;
  498. Begin
  499. If (p=nil) or (InOutRes<>0) then
  500. exit;
  501. case TextRec(f).mode of
  502. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  503. begin
  504. PCharLen:=StrLen(p);
  505. If Len>PCharLen Then
  506. fpc_WriteBlanks(f,Len-PCharLen);
  507. fpc_WriteBuffer(f,p^,PCharLen);
  508. end;
  509. fmInput: InOutRes:=105
  510. else InOutRes:=103;
  511. end;
  512. End;
  513. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; S : AnsiString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  514. {
  515. Writes a AnsiString to the Text file T
  516. }
  517. var
  518. SLen : longint;
  519. begin
  520. If (InOutRes<>0) then
  521. exit;
  522. case TextRec(f).mode of
  523. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  524. begin
  525. SLen:=Length(s);
  526. If Len>SLen Then
  527. fpc_WriteBlanks(f,Len-SLen);
  528. if slen > 0 then
  529. fpc_WriteBuffer(f,PChar(S)^,SLen);
  530. end;
  531. fmInput: InOutRes:=105
  532. else InOutRes:=103;
  533. end;
  534. end;
  535. {$ifdef HASWIDESTRING}
  536. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; S : WideString); iocheck; [Public,alias:'FPC_WRITE_TEXT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  537. {
  538. Writes a WideString to the Text file T
  539. }
  540. var
  541. SLen : longint;
  542. begin
  543. If (pointer(S)=nil) or (InOutRes<>0) then
  544. exit;
  545. case TextRec(f).mode of
  546. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  547. begin
  548. SLen:=Length(s);
  549. If Len>SLen Then
  550. fpc_WriteBlanks(f,Len-SLen);
  551. fpc_WriteBuffer(f,PChar(AnsiString(S))^,SLen);
  552. end;
  553. fmInput: InOutRes:=105
  554. else InOutRes:=103;
  555. end;
  556. end;
  557. {$endif HASWIDESTRING}
  558. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  559. var
  560. s : String;
  561. Begin
  562. If (InOutRes<>0) then
  563. exit;
  564. Str(l,s);
  565. Write_Str(Len,t,s);
  566. End;
  567. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_UINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  568. var
  569. s : String;
  570. Begin
  571. If (InOutRes<>0) then
  572. exit;
  573. Str(L,s);
  574. Write_Str(Len,t,s);
  575. End;
  576. {$ifndef CPU64}
  577. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; [public,alias:'FPC_WRITE_TEXT_QWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  578. var
  579. s : string;
  580. begin
  581. if (InOutRes<>0) then
  582. exit;
  583. str(q,s);
  584. write_str(len,t,s);
  585. end;
  586. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; [public,alias:'FPC_WRITE_TEXT_INT64']; {$ifdef hascompilerproc} compilerproc; {$endif}
  587. var
  588. s : string;
  589. begin
  590. if (InOutRes<>0) then
  591. exit;
  592. str(i,s);
  593. write_str(len,t,s);
  594. end;
  595. {$endif CPU64}
  596. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; [Public,Alias:'FPC_WRITE_TEXT_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  597. var
  598. s : String;
  599. Begin
  600. If (InOutRes<>0) then
  601. exit;
  602. Str_real(Len,fixkomma,r,treal_type(rt),s);
  603. Write_Str(Len,t,s);
  604. End;
  605. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; [Public,Alias:'FPC_WRITE_TEXT_BOOLEAN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  606. Begin
  607. If (InOutRes<>0) then
  608. exit;
  609. { Can't use array[boolean] because b can be >0 ! }
  610. if b then
  611. Write_Str(Len,t,'TRUE')
  612. else
  613. Write_Str(Len,t,'FALSE');
  614. End;
  615. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; [Public,Alias:'FPC_WRITE_TEXT_CHAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  616. Begin
  617. If (InOutRes<>0) then
  618. exit;
  619. if (TextRec(t).mode<>fmOutput) Then
  620. begin
  621. if TextRec(t).mode=fmClosed then
  622. InOutRes:=103
  623. else
  624. InOutRes:=105;
  625. exit;
  626. end;
  627. If Len>1 Then
  628. fpc_WriteBlanks(t,Len-1);
  629. If TextRec(t).BufPos+1>=TextRec(t).BufSize Then
  630. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  631. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  632. Inc(TextRec(t).BufPos);
  633. End;
  634. {$ifdef HASWIDECHAR}
  635. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; [Public,Alias:'FPC_WRITE_TEXT_WIDECHAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  636. var
  637. ch : char;
  638. Begin
  639. If (InOutRes<>0) then
  640. exit;
  641. if (TextRec(t).mode<>fmOutput) Then
  642. begin
  643. if TextRec(t).mode=fmClosed then
  644. InOutRes:=103
  645. else
  646. InOutRes:=105;
  647. exit;
  648. end;
  649. If Len>1 Then
  650. fpc_WriteBlanks(t,Len-1);
  651. If TextRec(t).BufPos+1>=TextRec(t).BufSize Then
  652. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  653. ch:=c;
  654. TextRec(t).Bufptr^[TextRec(t).BufPos]:=ch;
  655. Inc(TextRec(t).BufPos);
  656. End;
  657. {$endif HASWIDECHAR}
  658. {*****************************************************************************
  659. Read(Ln)
  660. *****************************************************************************}
  661. Function NextChar(var f:Text;var s:string):Boolean;
  662. begin
  663. if TextRec(f).BufPos<TextRec(f).BufEnd then
  664. begin
  665. if length(s)<high(s) then
  666. begin
  667. inc(s[0]);
  668. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  669. end;
  670. Inc(TextRec(f).BufPos);
  671. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  672. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  673. NextChar:=true;
  674. end
  675. else
  676. NextChar:=false;
  677. end;
  678. Function IgnoreSpaces(var f:Text):Boolean;
  679. {
  680. Removes all leading spaces,tab,eols from the input buffer, returns true if
  681. the buffer is empty
  682. }
  683. var
  684. s : string;
  685. begin
  686. s:='';
  687. IgnoreSpaces:=false;
  688. { Return false when already at EOF }
  689. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  690. exit;
  691. while (TextRec(f).Bufptr^[TextRec(f).BufPos] in [#9,#10,#13,' ']) do
  692. begin
  693. if not NextChar(f,s) then
  694. exit;
  695. { EOF? }
  696. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  697. break;
  698. end;
  699. IgnoreSpaces:=true;
  700. end;
  701. procedure ReadNumeric(var f:Text;var s:string);
  702. {
  703. Read numeric input, if buffer is empty then return True
  704. }
  705. begin
  706. repeat
  707. if not NextChar(f,s) then
  708. exit;
  709. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] in [#9,#10,#13,' ']);
  710. end;
  711. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  712. begin
  713. if TextRec(f).FlushFunc<>nil then
  714. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  715. end;
  716. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  717. var prev: char;
  718. Begin
  719. { Check error and if file is open and load buf if empty }
  720. If (InOutRes<>0) then
  721. exit;
  722. if (TextRec(f).mode<>fmInput) Then
  723. begin
  724. case TextRec(f).mode of
  725. fmOutPut,fmAppend:
  726. InOutRes:=104
  727. else
  728. InOutRes:=103;
  729. end;
  730. exit;
  731. end;
  732. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  733. begin
  734. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  735. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  736. { Flush if set }
  737. begin
  738. if (TextRec(f).FlushFunc<>nil) then
  739. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  740. exit;
  741. end;
  742. end;
  743. repeat
  744. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  745. inc(TextRec(f).BufPos);
  746. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  747. { #13#10 = Dos), so if we've got #10, we can safely exit }
  748. if prev = #10 then
  749. exit;
  750. {$ifdef MACOS}
  751. if prev = #13 then
  752. {StdInput on macos never have dos line ending, so this is safe.}
  753. if TextRec(f).Handle = StdInputHandle then
  754. exit;
  755. {$endif MACOS}
  756. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  757. begin
  758. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  759. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  760. { Flush if set }
  761. begin
  762. if (TextRec(f).FlushFunc<>nil) then
  763. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  764. exit;
  765. end;
  766. end;
  767. if (prev=#13) then
  768. { is there also a #10 after it? }
  769. begin
  770. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  771. { yes, skip that one as well }
  772. inc(TextRec(f).BufPos);
  773. exit;
  774. end;
  775. until false;
  776. End;
  777. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  778. var
  779. sPos,len : Longint;
  780. p,startp,maxp : pchar;
  781. Begin
  782. ReadPCharLen:=0;
  783. { Check error and if file is open }
  784. If (InOutRes<>0) then
  785. exit;
  786. if (TextRec(f).mode<>fmInput) Then
  787. begin
  788. case TextRec(f).mode of
  789. fmOutPut,fmAppend:
  790. InOutRes:=104
  791. else
  792. InOutRes:=103;
  793. end;
  794. exit;
  795. end;
  796. { Read maximal until Maxlen is reached }
  797. sPos:=0;
  798. repeat
  799. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  800. begin
  801. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  802. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  803. break;
  804. end;
  805. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  806. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  807. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  808. else
  809. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  810. startp:=p;
  811. { search linefeed }
  812. while (p<maxp) and not(P^ in [#10,#13]) do
  813. inc(p);
  814. { calculate read bytes }
  815. len:=p-startp;
  816. inc(TextRec(f).BufPos,Len);
  817. Move(startp^,s[sPos],Len);
  818. inc(sPos,Len);
  819. { was it a LF or CR? then leave }
  820. if (spos=MaxLen) or
  821. ((p<maxp) and (p^ in [#10,#13])) then
  822. break;
  823. until false;
  824. ReadPCharLen:=spos;
  825. End;
  826. Procedure fpc_Read_Text_ShortStr(var f : Text;var s : String); iocheck; [Public,Alias:'FPC_READ_TEXT_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  827. Begin
  828. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  829. End;
  830. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text;var s : PChar); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER']; {$ifdef hascompilerproc} compilerproc; {$endif}
  831. Begin
  832. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  833. End;
  834. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;var s : array of char); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  835. var
  836. len: longint;
  837. Begin
  838. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  839. if len <= high(s) then
  840. s[len] := #0;
  841. End;
  842. Procedure fpc_Read_Text_AnsiStr(var f : Text;var s : AnsiString); iocheck; [Public,Alias:'FPC_READ_TEXT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  843. var
  844. slen,len : longint;
  845. Begin
  846. slen:=0;
  847. Repeat
  848. // SetLength will reallocate the length.
  849. SetLength(S,slen+255);
  850. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  851. inc(slen,len);
  852. Until len<255;
  853. // Set actual length
  854. SetLength(S,Slen);
  855. End;
  856. {$ifdef hascompilerproc}
  857. procedure fpc_Read_Text_Char(var f : Text; var c: char); iocheck; [Public,Alias:'FPC_READ_TEXT_CHAR'];compilerproc;
  858. {$else hascompilerproc}
  859. Function fpc_Read_Text_Char(var f : Text):char;[Public,Alias:'FPC_READ_TEXT_CHAR'];
  860. {$endif hascompilerproc}
  861. Begin
  862. {$ifdef hascompilerproc}
  863. c:=#0;
  864. {$else hascompilerproc}
  865. fpc_Read_Text_Char:=#0;
  866. {$endif hascompilerproc}
  867. { Check error and if file is open }
  868. If (InOutRes<>0) then
  869. exit;
  870. if (TextRec(f).mode<>fmInput) Then
  871. begin
  872. case TextRec(f).mode of
  873. fmOutPut,fmAppend:
  874. InOutRes:=104
  875. else
  876. InOutRes:=103;
  877. end;
  878. exit;
  879. end;
  880. { Read next char or EOF }
  881. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  882. begin
  883. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  884. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  885. {$ifdef hascompilerproc}
  886. begin
  887. c := #26;
  888. exit;
  889. end;
  890. {$else hascompilerproc}
  891. exit(#26);
  892. {$endif hascompilerproc}
  893. end;
  894. {$ifdef hascompilerproc}
  895. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  896. {$else hascompilerproc}
  897. fpc_Read_Text_Char:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  898. {$endif hascompilerproc}
  899. inc(TextRec(f).BufPos);
  900. end;
  901. {$ifdef hascompilerproc}
  902. Procedure fpc_Read_Text_SInt(var f : Text; var l : ValSInt); iocheck; [Public,Alias:'FPC_READ_TEXT_SINT']; compilerproc;
  903. {$else hascompilerproc}
  904. Function fpc_Read_Text_SInt(var f : Text):ValSInt;[Public,Alias:'FPC_READ_TEXT_SINT'];
  905. {$endif hascompilerproc}
  906. var
  907. hs : String;
  908. code : longint;
  909. Begin
  910. {$ifdef hascompilerproc}
  911. l:=0;
  912. {$else hascompilerproc}
  913. fpc_Read_Text_SInt:=0;
  914. {$endif hascompilerproc}
  915. { Leave if error or not open file, else check for empty buf }
  916. If (InOutRes<>0) then
  917. exit;
  918. if (TextRec(f).mode<>fmInput) Then
  919. begin
  920. case TextRec(f).mode of
  921. fmOutPut,fmAppend:
  922. InOutRes:=104
  923. else
  924. InOutRes:=103;
  925. end;
  926. exit;
  927. end;
  928. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  929. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  930. hs:='';
  931. if IgnoreSpaces(f) then
  932. begin
  933. { When spaces were found and we are now at EOF,
  934. then we return 0 }
  935. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  936. exit;
  937. ReadNumeric(f,hs);
  938. end;
  939. {$ifdef hascompilerproc}
  940. Val(hs,l,code);
  941. {$else hascompilerproc}
  942. Val(hs,fpc_Read_Text_SInt,code);
  943. {$endif hascompilerproc}
  944. If code<>0 Then
  945. InOutRes:=106;
  946. End;
  947. {$ifdef hascompilerproc}
  948. Procedure fpc_Read_Text_UInt(var f : Text; var u : ValUInt); iocheck; [Public,Alias:'FPC_READ_TEXT_UINT']; compilerproc;
  949. {$else hascompilerproc}
  950. Function fpc_Read_Text_UInt(var f : Text):ValUInt;[Public,Alias:'FPC_READ_TEXT_UINT'];
  951. {$endif hascompilerproc}
  952. var
  953. hs : String;
  954. code : longint;
  955. Begin
  956. {$ifdef hascompilerproc}
  957. u:=0;
  958. {$else hascompilerproc}
  959. fpc_Read_Text_UInt:=0;
  960. {$endif hascompilerproc}
  961. { Leave if error or not open file, else check for empty buf }
  962. If (InOutRes<>0) then
  963. exit;
  964. if (TextRec(f).mode<>fmInput) Then
  965. begin
  966. case TextRec(f).mode of
  967. fmOutPut,fmAppend:
  968. InOutRes:=104
  969. else
  970. InOutRes:=103;
  971. end;
  972. exit;
  973. end;
  974. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  975. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  976. hs:='';
  977. if IgnoreSpaces(f) then
  978. begin
  979. { When spaces were found and we are now at EOF,
  980. then we return 0 }
  981. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  982. exit;
  983. ReadNumeric(f,hs);
  984. end;
  985. {$ifdef hascompilerproc}
  986. val(hs,u,code);
  987. {$else hascompilerproc}
  988. val(hs,fpc_Read_Text_UInt,code);
  989. {$endif hascompilerproc}
  990. If code<>0 Then
  991. InOutRes:=106;
  992. End;
  993. {$ifdef hascompilerproc}
  994. procedure fpc_Read_Text_Float(var f : Text; var v : ValReal); iocheck; [Public,Alias:'FPC_READ_TEXT_FLOAT']; compilerproc;
  995. {$else hascompilerproc}
  996. Function fpc_Read_Text_Float(var f : Text):ValReal;[Public,Alias:'FPC_READ_TEXT_FLOAT'];
  997. {$endif hascompilerproc}
  998. var
  999. hs : string;
  1000. code : Word;
  1001. begin
  1002. {$ifdef hascompilerproc}
  1003. v:=0.0;
  1004. {$else hascompilerproc}
  1005. fpc_Read_Text_Float:=0.0;
  1006. {$endif hascompilerproc}
  1007. { Leave if error or not open file, else check for empty buf }
  1008. If (InOutRes<>0) then
  1009. exit;
  1010. if (TextRec(f).mode<>fmInput) Then
  1011. begin
  1012. case TextRec(f).mode of
  1013. fmOutPut,fmAppend:
  1014. InOutRes:=104
  1015. else
  1016. InOutRes:=103;
  1017. end;
  1018. exit;
  1019. end;
  1020. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1021. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1022. hs:='';
  1023. if IgnoreSpaces(f) then
  1024. begin
  1025. { When spaces were found and we are now at EOF,
  1026. then we return 0 }
  1027. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1028. exit;
  1029. ReadNumeric(f,hs);
  1030. end;
  1031. {$ifdef hascompilerproc}
  1032. val(hs,v,code);
  1033. {$else hascompilerproc}
  1034. val(hs,fpc_Read_Text_Float,code);
  1035. {$endif hascompilerproc}
  1036. If code<>0 Then
  1037. InOutRes:=106;
  1038. end;
  1039. {$ifndef cpu64}
  1040. {$ifdef hascompilerproc}
  1041. procedure fpc_Read_Text_QWord(var f : text; var q : qword); iocheck; [public,alias:'FPC_READ_TEXT_QWORD']; compilerproc;
  1042. {$else hascompilerproc}
  1043. function fpc_Read_Text_QWord(var f : text) : qword;[public,alias:'FPC_READ_TEXT_QWORD'];
  1044. {$endif hascompilerproc}
  1045. var
  1046. hs : String;
  1047. code : longint;
  1048. Begin
  1049. {$ifdef hascompilerproc}
  1050. q:=0;
  1051. {$else hascompilerproc}
  1052. fpc_Read_Text_QWord:=0;
  1053. {$endif hascompilerproc}
  1054. { Leave if error or not open file, else check for empty buf }
  1055. If (InOutRes<>0) then
  1056. exit;
  1057. if (TextRec(f).mode<>fmInput) Then
  1058. begin
  1059. case TextRec(f).mode of
  1060. fmOutPut,fmAppend:
  1061. InOutRes:=104
  1062. else
  1063. InOutRes:=103;
  1064. end;
  1065. exit;
  1066. end;
  1067. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1068. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1069. hs:='';
  1070. if IgnoreSpaces(f) then
  1071. begin
  1072. { When spaces were found and we are now at EOF,
  1073. then we return 0 }
  1074. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1075. exit;
  1076. ReadNumeric(f,hs);
  1077. end;
  1078. {$ifdef hascompilerproc}
  1079. val(hs,q,code);
  1080. {$else hascompilerproc}
  1081. val(hs,fpc_Read_Text_QWord,code);
  1082. {$endif hascompilerproc}
  1083. If code<>0 Then
  1084. InOutRes:=106;
  1085. End;
  1086. {$ifdef hascompilerproc}
  1087. procedure fpc_Read_Text_Int64(var f : text; var i : int64); iocheck; [public,alias:'FPC_READ_TEXT_INT64']; compilerproc;
  1088. {$else hascompilerproc}
  1089. function fpc_Read_Text_Int64(var f : text) : int64;[public,alias:'FPC_READ_TEXT_INT64']; {$ifdef hascompilerproc} compilerproc; {$endif}
  1090. {$endif hascompilerproc}
  1091. var
  1092. hs : String;
  1093. code : Longint;
  1094. Begin
  1095. {$ifdef hascompilerproc}
  1096. i:=0;
  1097. {$else hascompilerproc}
  1098. fpc_Read_Text_Int64:=0;
  1099. {$endif hascompilerproc}
  1100. { Leave if error or not open file, else check for empty buf }
  1101. If (InOutRes<>0) then
  1102. exit;
  1103. if (TextRec(f).mode<>fmInput) Then
  1104. begin
  1105. case TextRec(f).mode of
  1106. fmOutPut,fmAppend:
  1107. InOutRes:=104
  1108. else
  1109. InOutRes:=103;
  1110. end;
  1111. exit;
  1112. end;
  1113. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1114. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1115. hs:='';
  1116. if IgnoreSpaces(f) then
  1117. begin
  1118. { When spaces were found and we are now at EOF,
  1119. then we return 0 }
  1120. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1121. exit;
  1122. ReadNumeric(f,hs);
  1123. end;
  1124. {$ifdef hascompilerproc}
  1125. Val(hs,i,code);
  1126. {$else hascompilerproc}
  1127. Val(hs,fpc_Read_Text_Int64,code);
  1128. {$endif hascompilerproc}
  1129. If code<>0 Then
  1130. InOutRes:=106;
  1131. End;
  1132. {$endif CPU64}
  1133. {*****************************************************************************
  1134. Initializing
  1135. *****************************************************************************}
  1136. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1137. begin
  1138. Assign(f,'');
  1139. TextRec(f).Handle:=hdl;
  1140. TextRec(f).Mode:=mode;
  1141. TextRec(f).Closefunc:=@FileCloseFunc;
  1142. case mode of
  1143. fmInput :
  1144. TextRec(f).InOutFunc:=@FileReadFunc;
  1145. fmOutput :
  1146. begin
  1147. TextRec(f).InOutFunc:=@FileWriteFunc;
  1148. TextRec(f).FlushFunc:=@FileWriteFunc;
  1149. end;
  1150. else
  1151. HandleError(102);
  1152. end;
  1153. end;
  1154. {
  1155. $Log$
  1156. Revision 1.27 2004-11-09 23:10:22 peter
  1157. * use helper call to retrieve address of input/output to reduce
  1158. code that is generated in the main program for loading the
  1159. threadvar
  1160. Revision 1.26 2004/09/21 23:36:51 hajny
  1161. * SetTextLineEnding implemented, FileRec.Name position alignment for CPU64
  1162. Revision 1.25 2004/08/20 10:04:39 olle
  1163. * prefixed write[buffer|blanks] with fpc_ and made them externally visible
  1164. Revision 1.24 2004/06/21 18:48:48 olle
  1165. + handles mac line endings without blocking the console, on Mac OS only
  1166. Revision 1.23 2004/05/01 20:52:50 peter
  1167. * ValSInt fixed for 64 bit
  1168. Revision 1.22 2004/04/29 18:59:43 peter
  1169. * str() helpers now also use valint/valuint
  1170. * int64/qword helpers disabled for cpu64
  1171. Revision 1.21 2004/04/22 21:10:56 peter
  1172. * do_read/do_write addr argument changed to pointer
  1173. Revision 1.20 2002/11/29 16:26:52 peter
  1174. * fixed ignorespaces which was broken by the previous commit
  1175. when a line started with spaces
  1176. Revision 1.19 2002/11/29 15:50:27 peter
  1177. * fix for tw1896
  1178. Revision 1.18 2002/09/07 15:07:46 peter
  1179. * old logs removed and tabs fixed
  1180. Revision 1.17 2002/07/01 16:29:05 peter
  1181. * sLineBreak changed to normal constant like Kylix
  1182. }