text.inc 33 KB

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