text.inc 33 KB

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