text.inc 33 KB

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