text.inc 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. {*****************************************************************************
  385. Write(Ln)
  386. *****************************************************************************}
  387. Procedure fpc_WriteBuffer(var f:Text;const b;len:longint);[Public,Alias:'FPC_WRITEBUFFER'];
  388. var
  389. p : pchar;
  390. left,
  391. idx : longint;
  392. begin
  393. p:=pchar(@b);
  394. idx:=0;
  395. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  396. while len>left do
  397. begin
  398. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],left);
  399. dec(len,left);
  400. inc(idx,left);
  401. inc(TextRec(f).BufPos,left);
  402. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  403. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  404. end;
  405. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],len);
  406. inc(TextRec(f).BufPos,len);
  407. end;
  408. Procedure fpc_WriteBlanks(var f:Text;len:longint);[Public,Alias:'FPC_WRITEBLANKS'];
  409. var
  410. left : longint;
  411. begin
  412. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  413. while len>left do
  414. begin
  415. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],left,' ');
  416. dec(len,left);
  417. inc(TextRec(f).BufPos,left);
  418. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  419. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  420. end;
  421. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],len,' ');
  422. inc(TextRec(f).BufPos,len);
  423. end;
  424. Procedure fpc_Write_End(var f:Text);[Public,Alias:'FPC_WRITE_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  425. begin
  426. if TextRec(f).FlushFunc<>nil then
  427. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  428. end;
  429. Procedure fpc_Writeln_End(var f:Text);[Public,Alias:'FPC_WRITELN_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  430. begin
  431. If InOutRes <> 0 then exit;
  432. case TextRec(f).mode of
  433. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  434. begin
  435. { Write EOL }
  436. fpc_WriteBuffer(f,TextRec(f).LineEnd[1],length(TextRec(f).LineEnd));
  437. { Flush }
  438. if TextRec(f).FlushFunc<>nil then
  439. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  440. end;
  441. fmInput: InOutRes:=105
  442. else InOutRes:=103;
  443. end;
  444. end;
  445. Procedure fpc_Write_Text_ShortStr(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  446. Begin
  447. If (InOutRes<>0) then
  448. exit;
  449. case TextRec(f).mode of
  450. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  451. begin
  452. If Len>Length(s) Then
  453. fpc_WriteBlanks(f,Len-Length(s));
  454. fpc_WriteBuffer(f,s[1],Length(s));
  455. end;
  456. fmInput: InOutRes:=105
  457. else InOutRes:=103;
  458. end;
  459. End;
  460. { provide local access to write_str }
  461. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  462. 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}
  463. var
  464. ArrayLen : longint;
  465. p : pchar;
  466. Begin
  467. If (InOutRes<>0) then
  468. exit;
  469. case TextRec(f).mode of
  470. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  471. begin
  472. p:=pchar(@s);
  473. { can't use StrLen, since that one could try to read past the end }
  474. { of the heap (JM) }
  475. ArrayLen:=IndexByte(p^,high(s)+1,0);
  476. { IndexByte returns -1 if not found (JM) }
  477. if ArrayLen = -1 then
  478. ArrayLen := high(s)+1;
  479. If Len>ArrayLen Then
  480. fpc_WriteBlanks(f,Len-ArrayLen);
  481. fpc_WriteBuffer(f,p^,ArrayLen);
  482. end;
  483. fmInput: InOutRes:=105
  484. else InOutRes:=103;
  485. end;
  486. End;
  487. 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}
  488. var
  489. PCharLen : longint;
  490. Begin
  491. If (p=nil) or (InOutRes<>0) then
  492. exit;
  493. case TextRec(f).mode of
  494. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  495. begin
  496. PCharLen:=StrLen(p);
  497. If Len>PCharLen Then
  498. fpc_WriteBlanks(f,Len-PCharLen);
  499. fpc_WriteBuffer(f,p^,PCharLen);
  500. end;
  501. fmInput: InOutRes:=105
  502. else InOutRes:=103;
  503. end;
  504. End;
  505. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; S : AnsiString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  506. {
  507. Writes a AnsiString to the Text file T
  508. }
  509. var
  510. SLen : longint;
  511. begin
  512. If (InOutRes<>0) then
  513. exit;
  514. case TextRec(f).mode of
  515. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  516. begin
  517. SLen:=Length(s);
  518. If Len>SLen Then
  519. fpc_WriteBlanks(f,Len-SLen);
  520. if slen > 0 then
  521. fpc_WriteBuffer(f,PChar(S)^,SLen);
  522. end;
  523. fmInput: InOutRes:=105
  524. else InOutRes:=103;
  525. end;
  526. end;
  527. {$ifdef HASWIDESTRING}
  528. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; S : WideString); iocheck; [Public,alias:'FPC_WRITE_TEXT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  529. {
  530. Writes a WideString to the Text file T
  531. }
  532. var
  533. SLen : longint;
  534. begin
  535. If (pointer(S)=nil) or (InOutRes<>0) then
  536. exit;
  537. case TextRec(f).mode of
  538. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  539. begin
  540. SLen:=Length(s);
  541. If Len>SLen Then
  542. fpc_WriteBlanks(f,Len-SLen);
  543. fpc_WriteBuffer(f,PChar(AnsiString(S))^,SLen);
  544. end;
  545. fmInput: InOutRes:=105
  546. else InOutRes:=103;
  547. end;
  548. end;
  549. {$endif HASWIDESTRING}
  550. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  551. var
  552. s : String;
  553. Begin
  554. If (InOutRes<>0) then
  555. exit;
  556. Str(l,s);
  557. Write_Str(Len,t,s);
  558. End;
  559. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_UINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  560. var
  561. s : String;
  562. Begin
  563. If (InOutRes<>0) then
  564. exit;
  565. Str(L,s);
  566. Write_Str(Len,t,s);
  567. End;
  568. {$ifndef CPU64}
  569. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; [public,alias:'FPC_WRITE_TEXT_QWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  570. var
  571. s : string;
  572. begin
  573. if (InOutRes<>0) then
  574. exit;
  575. str(q,s);
  576. write_str(len,t,s);
  577. end;
  578. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; [public,alias:'FPC_WRITE_TEXT_INT64']; {$ifdef hascompilerproc} compilerproc; {$endif}
  579. var
  580. s : string;
  581. begin
  582. if (InOutRes<>0) then
  583. exit;
  584. str(i,s);
  585. write_str(len,t,s);
  586. end;
  587. {$endif CPU64}
  588. 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}
  589. var
  590. s : String;
  591. Begin
  592. If (InOutRes<>0) then
  593. exit;
  594. Str_real(Len,fixkomma,r,treal_type(rt),s);
  595. Write_Str(Len,t,s);
  596. End;
  597. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; [Public,Alias:'FPC_WRITE_TEXT_BOOLEAN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  598. Begin
  599. If (InOutRes<>0) then
  600. exit;
  601. { Can't use array[boolean] because b can be >0 ! }
  602. if b then
  603. Write_Str(Len,t,'TRUE')
  604. else
  605. Write_Str(Len,t,'FALSE');
  606. End;
  607. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; [Public,Alias:'FPC_WRITE_TEXT_CHAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  608. Begin
  609. If (InOutRes<>0) then
  610. exit;
  611. if (TextRec(t).mode<>fmOutput) Then
  612. begin
  613. if TextRec(t).mode=fmClosed then
  614. InOutRes:=103
  615. else
  616. InOutRes:=105;
  617. exit;
  618. end;
  619. If Len>1 Then
  620. fpc_WriteBlanks(t,Len-1);
  621. If TextRec(t).BufPos+1>=TextRec(t).BufSize Then
  622. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  623. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  624. Inc(TextRec(t).BufPos);
  625. End;
  626. {$ifdef HASWIDECHAR}
  627. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; [Public,Alias:'FPC_WRITE_TEXT_WIDECHAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  628. var
  629. ch : char;
  630. Begin
  631. If (InOutRes<>0) then
  632. exit;
  633. if (TextRec(t).mode<>fmOutput) Then
  634. begin
  635. if TextRec(t).mode=fmClosed then
  636. InOutRes:=103
  637. else
  638. InOutRes:=105;
  639. exit;
  640. end;
  641. If Len>1 Then
  642. fpc_WriteBlanks(t,Len-1);
  643. If TextRec(t).BufPos+1>=TextRec(t).BufSize Then
  644. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  645. ch:=c;
  646. TextRec(t).Bufptr^[TextRec(t).BufPos]:=ch;
  647. Inc(TextRec(t).BufPos);
  648. End;
  649. {$endif HASWIDECHAR}
  650. {*****************************************************************************
  651. Read(Ln)
  652. *****************************************************************************}
  653. Function NextChar(var f:Text;var s:string):Boolean;
  654. begin
  655. if TextRec(f).BufPos<TextRec(f).BufEnd then
  656. begin
  657. if length(s)<high(s) then
  658. begin
  659. inc(s[0]);
  660. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  661. end;
  662. Inc(TextRec(f).BufPos);
  663. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  664. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  665. NextChar:=true;
  666. end
  667. else
  668. NextChar:=false;
  669. end;
  670. Function IgnoreSpaces(var f:Text):Boolean;
  671. {
  672. Removes all leading spaces,tab,eols from the input buffer, returns true if
  673. the buffer is empty
  674. }
  675. var
  676. s : string;
  677. begin
  678. s:='';
  679. IgnoreSpaces:=false;
  680. { Return false when already at EOF }
  681. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  682. exit;
  683. while (TextRec(f).Bufptr^[TextRec(f).BufPos] in [#9,#10,#13,' ']) do
  684. begin
  685. if not NextChar(f,s) then
  686. exit;
  687. { EOF? }
  688. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  689. break;
  690. end;
  691. IgnoreSpaces:=true;
  692. end;
  693. procedure ReadNumeric(var f:Text;var s:string);
  694. {
  695. Read numeric input, if buffer is empty then return True
  696. }
  697. begin
  698. repeat
  699. if not NextChar(f,s) then
  700. exit;
  701. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] in [#9,#10,#13,' ']);
  702. end;
  703. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  704. begin
  705. if TextRec(f).FlushFunc<>nil then
  706. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  707. end;
  708. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  709. var prev: char;
  710. Begin
  711. { Check error and if file is open and load buf if empty }
  712. If (InOutRes<>0) then
  713. exit;
  714. if (TextRec(f).mode<>fmInput) Then
  715. begin
  716. case TextRec(f).mode of
  717. fmOutPut,fmAppend:
  718. InOutRes:=104
  719. else
  720. InOutRes:=103;
  721. end;
  722. exit;
  723. end;
  724. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  725. begin
  726. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  727. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  728. { Flush if set }
  729. begin
  730. if (TextRec(f).FlushFunc<>nil) then
  731. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  732. exit;
  733. end;
  734. end;
  735. repeat
  736. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  737. inc(TextRec(f).BufPos);
  738. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  739. { #13#10 = Dos), so if we've got #10, we can safely exit }
  740. if prev = #10 then
  741. exit;
  742. {$ifdef MACOS}
  743. if prev = #13 then
  744. {StdInput on macos never have dos line ending, so this is safe.}
  745. if TextRec(f).Handle = StdInputHandle then
  746. exit;
  747. {$endif MACOS}
  748. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  749. begin
  750. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  751. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  752. { Flush if set }
  753. begin
  754. if (TextRec(f).FlushFunc<>nil) then
  755. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  756. exit;
  757. end;
  758. end;
  759. if (prev=#13) then
  760. { is there also a #10 after it? }
  761. begin
  762. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  763. { yes, skip that one as well }
  764. inc(TextRec(f).BufPos);
  765. exit;
  766. end;
  767. until false;
  768. End;
  769. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  770. var
  771. sPos,len : Longint;
  772. p,startp,maxp : pchar;
  773. Begin
  774. ReadPCharLen:=0;
  775. { Check error and if file is open }
  776. If (InOutRes<>0) then
  777. exit;
  778. if (TextRec(f).mode<>fmInput) Then
  779. begin
  780. case TextRec(f).mode of
  781. fmOutPut,fmAppend:
  782. InOutRes:=104
  783. else
  784. InOutRes:=103;
  785. end;
  786. exit;
  787. end;
  788. { Read maximal until Maxlen is reached }
  789. sPos:=0;
  790. repeat
  791. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  792. begin
  793. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  794. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  795. break;
  796. end;
  797. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  798. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  799. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  800. else
  801. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  802. startp:=p;
  803. { search linefeed }
  804. while (p<maxp) and not(P^ in [#10,#13]) do
  805. inc(p);
  806. { calculate read bytes }
  807. len:=p-startp;
  808. inc(TextRec(f).BufPos,Len);
  809. Move(startp^,s[sPos],Len);
  810. inc(sPos,Len);
  811. { was it a LF or CR? then leave }
  812. if (spos=MaxLen) or
  813. ((p<maxp) and (p^ in [#10,#13])) then
  814. break;
  815. until false;
  816. ReadPCharLen:=spos;
  817. End;
  818. Procedure fpc_Read_Text_ShortStr(var f : Text;var s : String); iocheck; [Public,Alias:'FPC_READ_TEXT_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  819. Begin
  820. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  821. End;
  822. 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}
  823. Begin
  824. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  825. End;
  826. 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}
  827. var
  828. len: longint;
  829. Begin
  830. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  831. if len <= high(s) then
  832. s[len] := #0;
  833. End;
  834. Procedure fpc_Read_Text_AnsiStr(var f : Text;var s : AnsiString); iocheck; [Public,Alias:'FPC_READ_TEXT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  835. var
  836. slen,len : longint;
  837. Begin
  838. slen:=0;
  839. Repeat
  840. // SetLength will reallocate the length.
  841. SetLength(S,slen+255);
  842. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  843. inc(slen,len);
  844. Until len<255;
  845. // Set actual length
  846. SetLength(S,Slen);
  847. End;
  848. {$ifdef hascompilerproc}
  849. procedure fpc_Read_Text_Char(var f : Text; var c: char); iocheck; [Public,Alias:'FPC_READ_TEXT_CHAR'];compilerproc;
  850. {$else hascompilerproc}
  851. Function fpc_Read_Text_Char(var f : Text):char;[Public,Alias:'FPC_READ_TEXT_CHAR'];
  852. {$endif hascompilerproc}
  853. Begin
  854. {$ifdef hascompilerproc}
  855. c:=#0;
  856. {$else hascompilerproc}
  857. fpc_Read_Text_Char:=#0;
  858. {$endif hascompilerproc}
  859. { Check error and if file is open }
  860. If (InOutRes<>0) then
  861. exit;
  862. if (TextRec(f).mode<>fmInput) Then
  863. begin
  864. case TextRec(f).mode of
  865. fmOutPut,fmAppend:
  866. InOutRes:=104
  867. else
  868. InOutRes:=103;
  869. end;
  870. exit;
  871. end;
  872. { Read next char or EOF }
  873. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  874. begin
  875. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  876. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  877. {$ifdef hascompilerproc}
  878. begin
  879. c := #26;
  880. exit;
  881. end;
  882. {$else hascompilerproc}
  883. exit(#26);
  884. {$endif hascompilerproc}
  885. end;
  886. {$ifdef hascompilerproc}
  887. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  888. {$else hascompilerproc}
  889. fpc_Read_Text_Char:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  890. {$endif hascompilerproc}
  891. inc(TextRec(f).BufPos);
  892. end;
  893. {$ifdef hascompilerproc}
  894. Procedure fpc_Read_Text_SInt(var f : Text; var l : ValSInt); iocheck; [Public,Alias:'FPC_READ_TEXT_SINT']; compilerproc;
  895. {$else hascompilerproc}
  896. Function fpc_Read_Text_SInt(var f : Text):ValSInt;[Public,Alias:'FPC_READ_TEXT_SINT'];
  897. {$endif hascompilerproc}
  898. var
  899. hs : String;
  900. code : longint;
  901. Begin
  902. {$ifdef hascompilerproc}
  903. l:=0;
  904. {$else hascompilerproc}
  905. fpc_Read_Text_SInt:=0;
  906. {$endif hascompilerproc}
  907. { Leave if error or not open file, else check for empty buf }
  908. If (InOutRes<>0) then
  909. exit;
  910. if (TextRec(f).mode<>fmInput) Then
  911. begin
  912. case TextRec(f).mode of
  913. fmOutPut,fmAppend:
  914. InOutRes:=104
  915. else
  916. InOutRes:=103;
  917. end;
  918. exit;
  919. end;
  920. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  921. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  922. hs:='';
  923. if IgnoreSpaces(f) then
  924. begin
  925. { When spaces were found and we are now at EOF,
  926. then we return 0 }
  927. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  928. exit;
  929. ReadNumeric(f,hs);
  930. end;
  931. {$ifdef hascompilerproc}
  932. Val(hs,l,code);
  933. {$else hascompilerproc}
  934. Val(hs,fpc_Read_Text_SInt,code);
  935. {$endif hascompilerproc}
  936. If code<>0 Then
  937. InOutRes:=106;
  938. End;
  939. {$ifdef hascompilerproc}
  940. Procedure fpc_Read_Text_UInt(var f : Text; var u : ValUInt); iocheck; [Public,Alias:'FPC_READ_TEXT_UINT']; compilerproc;
  941. {$else hascompilerproc}
  942. Function fpc_Read_Text_UInt(var f : Text):ValUInt;[Public,Alias:'FPC_READ_TEXT_UINT'];
  943. {$endif hascompilerproc}
  944. var
  945. hs : String;
  946. code : longint;
  947. Begin
  948. {$ifdef hascompilerproc}
  949. u:=0;
  950. {$else hascompilerproc}
  951. fpc_Read_Text_UInt:=0;
  952. {$endif hascompilerproc}
  953. { Leave if error or not open file, else check for empty buf }
  954. If (InOutRes<>0) then
  955. exit;
  956. if (TextRec(f).mode<>fmInput) Then
  957. begin
  958. case TextRec(f).mode of
  959. fmOutPut,fmAppend:
  960. InOutRes:=104
  961. else
  962. InOutRes:=103;
  963. end;
  964. exit;
  965. end;
  966. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  967. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  968. hs:='';
  969. if IgnoreSpaces(f) then
  970. begin
  971. { When spaces were found and we are now at EOF,
  972. then we return 0 }
  973. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  974. exit;
  975. ReadNumeric(f,hs);
  976. end;
  977. {$ifdef hascompilerproc}
  978. val(hs,u,code);
  979. {$else hascompilerproc}
  980. val(hs,fpc_Read_Text_UInt,code);
  981. {$endif hascompilerproc}
  982. If code<>0 Then
  983. InOutRes:=106;
  984. End;
  985. {$ifdef hascompilerproc}
  986. procedure fpc_Read_Text_Float(var f : Text; var v : ValReal); iocheck; [Public,Alias:'FPC_READ_TEXT_FLOAT']; compilerproc;
  987. {$else hascompilerproc}
  988. Function fpc_Read_Text_Float(var f : Text):ValReal;[Public,Alias:'FPC_READ_TEXT_FLOAT'];
  989. {$endif hascompilerproc}
  990. var
  991. hs : string;
  992. code : Word;
  993. begin
  994. {$ifdef hascompilerproc}
  995. v:=0.0;
  996. {$else hascompilerproc}
  997. fpc_Read_Text_Float:=0.0;
  998. {$endif hascompilerproc}
  999. { Leave if error or not open file, else check for empty buf }
  1000. If (InOutRes<>0) then
  1001. exit;
  1002. if (TextRec(f).mode<>fmInput) Then
  1003. begin
  1004. case TextRec(f).mode of
  1005. fmOutPut,fmAppend:
  1006. InOutRes:=104
  1007. else
  1008. InOutRes:=103;
  1009. end;
  1010. exit;
  1011. end;
  1012. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1013. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1014. hs:='';
  1015. if IgnoreSpaces(f) then
  1016. begin
  1017. { When spaces were found and we are now at EOF,
  1018. then we return 0 }
  1019. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1020. exit;
  1021. ReadNumeric(f,hs);
  1022. end;
  1023. {$ifdef hascompilerproc}
  1024. val(hs,v,code);
  1025. {$else hascompilerproc}
  1026. val(hs,fpc_Read_Text_Float,code);
  1027. {$endif hascompilerproc}
  1028. If code<>0 Then
  1029. InOutRes:=106;
  1030. end;
  1031. {$ifndef cpu64}
  1032. {$ifdef hascompilerproc}
  1033. procedure fpc_Read_Text_QWord(var f : text; var q : qword); iocheck; [public,alias:'FPC_READ_TEXT_QWORD']; compilerproc;
  1034. {$else hascompilerproc}
  1035. function fpc_Read_Text_QWord(var f : text) : qword;[public,alias:'FPC_READ_TEXT_QWORD'];
  1036. {$endif hascompilerproc}
  1037. var
  1038. hs : String;
  1039. code : longint;
  1040. Begin
  1041. {$ifdef hascompilerproc}
  1042. q:=0;
  1043. {$else hascompilerproc}
  1044. fpc_Read_Text_QWord:=0;
  1045. {$endif hascompilerproc}
  1046. { Leave if error or not open file, else check for empty buf }
  1047. If (InOutRes<>0) then
  1048. exit;
  1049. if (TextRec(f).mode<>fmInput) Then
  1050. begin
  1051. case TextRec(f).mode of
  1052. fmOutPut,fmAppend:
  1053. InOutRes:=104
  1054. else
  1055. InOutRes:=103;
  1056. end;
  1057. exit;
  1058. end;
  1059. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1060. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1061. hs:='';
  1062. if IgnoreSpaces(f) then
  1063. begin
  1064. { When spaces were found and we are now at EOF,
  1065. then we return 0 }
  1066. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1067. exit;
  1068. ReadNumeric(f,hs);
  1069. end;
  1070. {$ifdef hascompilerproc}
  1071. val(hs,q,code);
  1072. {$else hascompilerproc}
  1073. val(hs,fpc_Read_Text_QWord,code);
  1074. {$endif hascompilerproc}
  1075. If code<>0 Then
  1076. InOutRes:=106;
  1077. End;
  1078. {$ifdef hascompilerproc}
  1079. procedure fpc_Read_Text_Int64(var f : text; var i : int64); iocheck; [public,alias:'FPC_READ_TEXT_INT64']; compilerproc;
  1080. {$else hascompilerproc}
  1081. function fpc_Read_Text_Int64(var f : text) : int64;[public,alias:'FPC_READ_TEXT_INT64']; {$ifdef hascompilerproc} compilerproc; {$endif}
  1082. {$endif hascompilerproc}
  1083. var
  1084. hs : String;
  1085. code : Longint;
  1086. Begin
  1087. {$ifdef hascompilerproc}
  1088. i:=0;
  1089. {$else hascompilerproc}
  1090. fpc_Read_Text_Int64:=0;
  1091. {$endif hascompilerproc}
  1092. { Leave if error or not open file, else check for empty buf }
  1093. If (InOutRes<>0) then
  1094. exit;
  1095. if (TextRec(f).mode<>fmInput) Then
  1096. begin
  1097. case TextRec(f).mode of
  1098. fmOutPut,fmAppend:
  1099. InOutRes:=104
  1100. else
  1101. InOutRes:=103;
  1102. end;
  1103. exit;
  1104. end;
  1105. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1106. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1107. hs:='';
  1108. if IgnoreSpaces(f) then
  1109. begin
  1110. { When spaces were found and we are now at EOF,
  1111. then we return 0 }
  1112. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1113. exit;
  1114. ReadNumeric(f,hs);
  1115. end;
  1116. {$ifdef hascompilerproc}
  1117. Val(hs,i,code);
  1118. {$else hascompilerproc}
  1119. Val(hs,fpc_Read_Text_Int64,code);
  1120. {$endif hascompilerproc}
  1121. If code<>0 Then
  1122. InOutRes:=106;
  1123. End;
  1124. {$endif CPU64}
  1125. {*****************************************************************************
  1126. Initializing
  1127. *****************************************************************************}
  1128. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1129. begin
  1130. Assign(f,'');
  1131. TextRec(f).Handle:=hdl;
  1132. TextRec(f).Mode:=mode;
  1133. TextRec(f).Closefunc:=@FileCloseFunc;
  1134. case mode of
  1135. fmInput :
  1136. TextRec(f).InOutFunc:=@FileReadFunc;
  1137. fmOutput :
  1138. begin
  1139. TextRec(f).InOutFunc:=@FileWriteFunc;
  1140. TextRec(f).FlushFunc:=@FileWriteFunc;
  1141. end;
  1142. else
  1143. HandleError(102);
  1144. end;
  1145. end;
  1146. {
  1147. $Log$
  1148. Revision 1.26 2004-09-21 23:36:51 hajny
  1149. * SetTextLineEnding implemented, FileRec.Name position alignment for CPU64
  1150. Revision 1.25 2004/08/20 10:04:39 olle
  1151. * prefixed write[buffer|blanks] with fpc_ and made them externally visible
  1152. Revision 1.24 2004/06/21 18:48:48 olle
  1153. + handles mac line endings without blocking the console, on Mac OS only
  1154. Revision 1.23 2004/05/01 20:52:50 peter
  1155. * ValSInt fixed for 64 bit
  1156. Revision 1.22 2004/04/29 18:59:43 peter
  1157. * str() helpers now also use valint/valuint
  1158. * int64/qword helpers disabled for cpu64
  1159. Revision 1.21 2004/04/22 21:10:56 peter
  1160. * do_read/do_write addr argument changed to pointer
  1161. Revision 1.20 2002/11/29 16:26:52 peter
  1162. * fixed ignorespaces which was broken by the previous commit
  1163. when a line started with spaces
  1164. Revision 1.19 2002/11/29 15:50:27 peter
  1165. * fix for tw1896
  1166. Revision 1.18 2002/09/07 15:07:46 peter
  1167. * old logs removed and tabs fixed
  1168. Revision 1.17 2002/07/01 16:29:05 peter
  1169. * sLineBreak changed to normal constant like Kylix
  1170. }