text.inc 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; S : WideString); iocheck; [Public,alias:'FPC_WRITE_TEXT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  530. {
  531. Writes a WideString to the Text file T
  532. }
  533. var
  534. SLen : longint;
  535. begin
  536. If (pointer(S)=nil) or (InOutRes<>0) then
  537. exit;
  538. case TextRec(f).mode of
  539. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  540. begin
  541. SLen:=Length(s);
  542. If Len>SLen Then
  543. fpc_WriteBlanks(f,Len-SLen);
  544. fpc_WriteBuffer(f,PChar(AnsiString(S))^,SLen);
  545. end;
  546. fmInput: InOutRes:=105
  547. else InOutRes:=103;
  548. end;
  549. end;
  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. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; [Public,Alias:'FPC_WRITE_TEXT_WIDECHAR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  627. var
  628. ch : char;
  629. Begin
  630. If (InOutRes<>0) then
  631. exit;
  632. if (TextRec(t).mode<>fmOutput) Then
  633. begin
  634. if TextRec(t).mode=fmClosed then
  635. InOutRes:=103
  636. else
  637. InOutRes:=105;
  638. exit;
  639. end;
  640. If Len>1 Then
  641. fpc_WriteBlanks(t,Len-1);
  642. If TextRec(t).BufPos+1>=TextRec(t).BufSize Then
  643. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  644. ch:=c;
  645. TextRec(t).Bufptr^[TextRec(t).BufPos]:=ch;
  646. Inc(TextRec(t).BufPos);
  647. End;
  648. {*****************************************************************************
  649. Read(Ln)
  650. *****************************************************************************}
  651. Function NextChar(var f:Text;var s:string):Boolean;
  652. begin
  653. NextChar:=false;
  654. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  655. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) 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. end;
  668. Function IgnoreSpaces(var f:Text):Boolean;
  669. {
  670. Removes all leading spaces,tab,eols from the input buffer, returns true if
  671. the buffer is empty
  672. }
  673. var
  674. s : string;
  675. begin
  676. s:='';
  677. IgnoreSpaces:=false;
  678. { Return false when already at EOF }
  679. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  680. exit;
  681. (* Check performed separately to avoid accessing memory outside buffer *)
  682. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  683. exit;
  684. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  685. begin
  686. if not NextChar(f,s) then
  687. exit;
  688. { EOF? }
  689. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  690. break;
  691. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  692. break;
  693. end;
  694. IgnoreSpaces:=true;
  695. end;
  696. procedure ReadNumeric(var f:Text;var s:string);
  697. {
  698. Read numeric input, if buffer is empty then return True
  699. }
  700. begin
  701. repeat
  702. if not NextChar(f,s) then
  703. exit;
  704. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  705. end;
  706. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  707. begin
  708. if TextRec(f).FlushFunc<>nil then
  709. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  710. end;
  711. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; {$ifdef hascompilerproc} compilerproc; {$endif}
  712. var prev: char;
  713. Begin
  714. { Check error and if file is open and load buf if empty }
  715. If (InOutRes<>0) then
  716. exit;
  717. if (TextRec(f).mode<>fmInput) Then
  718. begin
  719. case TextRec(f).mode of
  720. fmOutPut,fmAppend:
  721. InOutRes:=104
  722. else
  723. InOutRes:=103;
  724. end;
  725. exit;
  726. end;
  727. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  728. begin
  729. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  730. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  731. { Flush if set }
  732. begin
  733. if (TextRec(f).FlushFunc<>nil) then
  734. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  735. exit;
  736. end;
  737. end;
  738. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  739. Exit;
  740. repeat
  741. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  742. inc(TextRec(f).BufPos);
  743. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  744. { #13#10 = Dos), so if we've got #10, we can safely exit }
  745. if prev = #10 then
  746. exit;
  747. {$ifdef MACOS}
  748. if prev = #13 then
  749. {StdInput on macos never have dos line ending, so this is safe.}
  750. if TextRec(f).Handle = StdInputHandle then
  751. exit;
  752. {$endif MACOS}
  753. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  754. begin
  755. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  756. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  757. { Flush if set }
  758. begin
  759. if (TextRec(f).FlushFunc<>nil) then
  760. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  761. exit;
  762. end;
  763. end;
  764. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  765. Exit;
  766. if (prev=#13) then
  767. { is there also a #10 after it? }
  768. begin
  769. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  770. { yes, skip that one as well }
  771. inc(TextRec(f).BufPos);
  772. exit;
  773. end;
  774. until false;
  775. End;
  776. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  777. var
  778. sPos,len : Longint;
  779. p,startp,maxp : pchar;
  780. Begin
  781. ReadPCharLen:=0;
  782. { Check error and if file is open }
  783. If (InOutRes<>0) then
  784. exit;
  785. if (TextRec(f).mode<>fmInput) Then
  786. begin
  787. case TextRec(f).mode of
  788. fmOutPut,fmAppend:
  789. InOutRes:=104
  790. else
  791. InOutRes:=103;
  792. end;
  793. exit;
  794. end;
  795. { Read maximal until Maxlen is reached }
  796. sPos:=0;
  797. repeat
  798. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  799. begin
  800. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  801. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  802. break;
  803. end;
  804. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  805. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  806. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  807. else
  808. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  809. startp:=p;
  810. { search linefeed }
  811. while (p<maxp) and not(P^ in [#10,#13]) do
  812. inc(p);
  813. { calculate read bytes }
  814. len:=p-startp;
  815. inc(TextRec(f).BufPos,Len);
  816. Move(startp^,s[sPos],Len);
  817. inc(sPos,Len);
  818. { was it a LF or CR? then leave }
  819. if (spos=MaxLen) or
  820. ((p<maxp) and (p^ in [#10,#13])) then
  821. break;
  822. until false;
  823. ReadPCharLen:=spos;
  824. End;
  825. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; [Public,Alias:'FPC_READ_TEXT_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  826. Begin
  827. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  828. End;
  829. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text;out s : PChar); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER']; {$ifdef hascompilerproc} compilerproc; {$endif}
  830. Begin
  831. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  832. End;
  833. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  834. var
  835. len: longint;
  836. Begin
  837. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  838. if len <= high(s) then
  839. s[len] := #0;
  840. End;
  841. Procedure fpc_Read_Text_AnsiStr(var f : Text;out s : AnsiString); iocheck; [Public,Alias:'FPC_READ_TEXT_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  842. var
  843. slen,len : longint;
  844. Begin
  845. slen:=0;
  846. Repeat
  847. // SetLength will reallocate the length.
  848. SetLength(S,slen+255);
  849. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  850. inc(slen,len);
  851. Until len<255;
  852. // Set actual length
  853. SetLength(S,Slen);
  854. End;
  855. {$ifdef hascompilerproc}
  856. procedure fpc_Read_Text_Char(var f : Text; out c: char); iocheck; [Public,Alias:'FPC_READ_TEXT_CHAR'];compilerproc;
  857. {$else hascompilerproc}
  858. Function fpc_Read_Text_Char(var f : Text):char;[Public,Alias:'FPC_READ_TEXT_CHAR'];
  859. {$endif hascompilerproc}
  860. Begin
  861. {$ifdef hascompilerproc}
  862. c:=#0;
  863. {$else hascompilerproc}
  864. fpc_Read_Text_Char:=#0;
  865. {$endif hascompilerproc}
  866. { Check error and if file is open }
  867. If (InOutRes<>0) then
  868. exit;
  869. if (TextRec(f).mode<>fmInput) Then
  870. begin
  871. case TextRec(f).mode of
  872. fmOutPut,fmAppend:
  873. InOutRes:=104
  874. else
  875. InOutRes:=103;
  876. end;
  877. exit;
  878. end;
  879. { Read next char or EOF }
  880. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  881. begin
  882. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  883. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  884. {$ifdef hascompilerproc}
  885. begin
  886. c := #26;
  887. exit;
  888. end;
  889. {$else hascompilerproc}
  890. exit(#26);
  891. {$endif hascompilerproc}
  892. end;
  893. {$ifdef hascompilerproc}
  894. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  895. {$else hascompilerproc}
  896. fpc_Read_Text_Char:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  897. {$endif hascompilerproc}
  898. inc(TextRec(f).BufPos);
  899. end;
  900. {$ifdef hascompilerproc}
  901. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; [Public,Alias:'FPC_READ_TEXT_SINT']; compilerproc;
  902. {$else hascompilerproc}
  903. Function fpc_Read_Text_SInt(var f : Text):ValSInt;[Public,Alias:'FPC_READ_TEXT_SINT'];
  904. {$endif hascompilerproc}
  905. var
  906. hs : String;
  907. code : longint;
  908. Begin
  909. {$ifdef hascompilerproc}
  910. l:=0;
  911. {$else hascompilerproc}
  912. fpc_Read_Text_SInt:=0;
  913. {$endif hascompilerproc}
  914. { Leave if error or not open file, else check for empty buf }
  915. If (InOutRes<>0) then
  916. exit;
  917. if (TextRec(f).mode<>fmInput) Then
  918. begin
  919. case TextRec(f).mode of
  920. fmOutPut,fmAppend:
  921. InOutRes:=104
  922. else
  923. InOutRes:=103;
  924. end;
  925. exit;
  926. end;
  927. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  928. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  929. hs:='';
  930. if IgnoreSpaces(f) then
  931. begin
  932. { When spaces were found and we are now at EOF,
  933. then we return 0 }
  934. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  935. exit;
  936. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  937. exit;
  938. ReadNumeric(f,hs);
  939. end;
  940. {$ifdef hascompilerproc}
  941. if (hs = '') then
  942. L := 0
  943. else
  944. begin
  945. Val(hs,l,code);
  946. if Code <> 0 then
  947. InOutRes:=106;
  948. end;
  949. {$else hascompilerproc}
  950. if (hs = '') then
  951. fpc_Read_Text_SInt := 0
  952. else
  953. begin
  954. Val(hs,fpc_Read_Text_SInt,code);
  955. if Code <> 0 then
  956. InOutRes:=106;
  957. end;
  958. {$endif hascompilerproc}
  959. End;
  960. {$ifdef hascompilerproc}
  961. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; [Public,Alias:'FPC_READ_TEXT_UINT']; compilerproc;
  962. {$else hascompilerproc}
  963. Function fpc_Read_Text_UInt(var f : Text):ValUInt;[Public,Alias:'FPC_READ_TEXT_UINT'];
  964. {$endif hascompilerproc}
  965. var
  966. hs : String;
  967. code : longint;
  968. Begin
  969. {$ifdef hascompilerproc}
  970. u:=0;
  971. {$else hascompilerproc}
  972. fpc_Read_Text_UInt:=0;
  973. {$endif hascompilerproc}
  974. { Leave if error or not open file, else check for empty buf }
  975. If (InOutRes<>0) then
  976. exit;
  977. if (TextRec(f).mode<>fmInput) Then
  978. begin
  979. case TextRec(f).mode of
  980. fmOutPut,fmAppend:
  981. InOutRes:=104
  982. else
  983. InOutRes:=103;
  984. end;
  985. exit;
  986. end;
  987. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  988. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  989. hs:='';
  990. if IgnoreSpaces(f) then
  991. begin
  992. { When spaces were found and we are now at EOF,
  993. then we return 0 }
  994. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  995. exit;
  996. ReadNumeric(f,hs);
  997. end;
  998. {$ifdef hascompilerproc}
  999. val(hs,u,code);
  1000. {$else hascompilerproc}
  1001. val(hs,fpc_Read_Text_UInt,code);
  1002. {$endif hascompilerproc}
  1003. If code<>0 Then
  1004. InOutRes:=106;
  1005. End;
  1006. {$ifdef hascompilerproc}
  1007. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; [Public,Alias:'FPC_READ_TEXT_FLOAT']; compilerproc;
  1008. {$else hascompilerproc}
  1009. Function fpc_Read_Text_Float(var f : Text):ValReal;[Public,Alias:'FPC_READ_TEXT_FLOAT'];
  1010. {$endif hascompilerproc}
  1011. var
  1012. hs : string;
  1013. code : Word;
  1014. begin
  1015. {$ifdef hascompilerproc}
  1016. v:=0.0;
  1017. {$else hascompilerproc}
  1018. fpc_Read_Text_Float:=0.0;
  1019. {$endif hascompilerproc}
  1020. { Leave if error or not open file, else check for empty buf }
  1021. If (InOutRes<>0) then
  1022. exit;
  1023. if (TextRec(f).mode<>fmInput) Then
  1024. begin
  1025. case TextRec(f).mode of
  1026. fmOutPut,fmAppend:
  1027. InOutRes:=104
  1028. else
  1029. InOutRes:=103;
  1030. end;
  1031. exit;
  1032. end;
  1033. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1034. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1035. hs:='';
  1036. if IgnoreSpaces(f) then
  1037. begin
  1038. { When spaces were found and we are now at EOF,
  1039. then we return 0 }
  1040. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1041. exit;
  1042. ReadNumeric(f,hs);
  1043. end;
  1044. {$ifdef hascompilerproc}
  1045. val(hs,v,code);
  1046. {$else hascompilerproc}
  1047. val(hs,fpc_Read_Text_Float,code);
  1048. {$endif hascompilerproc}
  1049. If code<>0 Then
  1050. InOutRes:=106;
  1051. end;
  1052. {$ifndef cpu64}
  1053. {$ifdef hascompilerproc}
  1054. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; [public,alias:'FPC_READ_TEXT_QWORD']; compilerproc;
  1055. {$else hascompilerproc}
  1056. function fpc_Read_Text_QWord(var f : text) : qword;[public,alias:'FPC_READ_TEXT_QWORD'];
  1057. {$endif hascompilerproc}
  1058. var
  1059. hs : String;
  1060. code : longint;
  1061. Begin
  1062. {$ifdef hascompilerproc}
  1063. q:=0;
  1064. {$else hascompilerproc}
  1065. fpc_Read_Text_QWord:=0;
  1066. {$endif hascompilerproc}
  1067. { Leave if error or not open file, else check for empty buf }
  1068. If (InOutRes<>0) then
  1069. exit;
  1070. if (TextRec(f).mode<>fmInput) Then
  1071. begin
  1072. case TextRec(f).mode of
  1073. fmOutPut,fmAppend:
  1074. InOutRes:=104
  1075. else
  1076. InOutRes:=103;
  1077. end;
  1078. exit;
  1079. end;
  1080. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1081. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1082. hs:='';
  1083. if IgnoreSpaces(f) then
  1084. begin
  1085. { When spaces were found and we are now at EOF,
  1086. then we return 0 }
  1087. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1088. exit;
  1089. ReadNumeric(f,hs);
  1090. end;
  1091. {$ifdef hascompilerproc}
  1092. val(hs,q,code);
  1093. {$else hascompilerproc}
  1094. val(hs,fpc_Read_Text_QWord,code);
  1095. {$endif hascompilerproc}
  1096. If code<>0 Then
  1097. InOutRes:=106;
  1098. End;
  1099. {$ifdef hascompilerproc}
  1100. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; [public,alias:'FPC_READ_TEXT_INT64']; compilerproc;
  1101. {$else hascompilerproc}
  1102. function fpc_Read_Text_Int64(var f : text) : int64;[public,alias:'FPC_READ_TEXT_INT64']; {$ifdef hascompilerproc} compilerproc; {$endif}
  1103. {$endif hascompilerproc}
  1104. var
  1105. hs : String;
  1106. code : Longint;
  1107. Begin
  1108. {$ifdef hascompilerproc}
  1109. i:=0;
  1110. {$else hascompilerproc}
  1111. fpc_Read_Text_Int64:=0;
  1112. {$endif hascompilerproc}
  1113. { Leave if error or not open file, else check for empty buf }
  1114. If (InOutRes<>0) then
  1115. exit;
  1116. if (TextRec(f).mode<>fmInput) Then
  1117. begin
  1118. case TextRec(f).mode of
  1119. fmOutPut,fmAppend:
  1120. InOutRes:=104
  1121. else
  1122. InOutRes:=103;
  1123. end;
  1124. exit;
  1125. end;
  1126. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1127. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1128. hs:='';
  1129. if IgnoreSpaces(f) then
  1130. begin
  1131. { When spaces were found and we are now at EOF,
  1132. then we return 0 }
  1133. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1134. exit;
  1135. ReadNumeric(f,hs);
  1136. end;
  1137. {$ifdef hascompilerproc}
  1138. Val(hs,i,code);
  1139. {$else hascompilerproc}
  1140. Val(hs,fpc_Read_Text_Int64,code);
  1141. {$endif hascompilerproc}
  1142. If code<>0 Then
  1143. InOutRes:=106;
  1144. End;
  1145. {$endif CPU64}
  1146. {*****************************************************************************
  1147. Initializing
  1148. *****************************************************************************}
  1149. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1150. begin
  1151. Assign(f,'');
  1152. TextRec(f).Handle:=hdl;
  1153. TextRec(f).Mode:=mode;
  1154. TextRec(f).Closefunc:=@FileCloseFunc;
  1155. case mode of
  1156. fmInput :
  1157. TextRec(f).InOutFunc:=@FileReadFunc;
  1158. fmOutput :
  1159. begin
  1160. TextRec(f).InOutFunc:=@FileWriteFunc;
  1161. TextRec(f).FlushFunc:=@FileWriteFunc;
  1162. end;
  1163. else
  1164. HandleError(102);
  1165. end;
  1166. end;