text.inc 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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. { prevent unecessary system call }
  28. if t.BufPos=0 then
  29. exit;
  30. i:=Do_Write(t.Handle,t.Bufptr,t.BufPos);
  31. if i<>t.BufPos then
  32. InOutRes:=101;
  33. t.BufPos:=0;
  34. End;
  35. Procedure FileOpenFunc(var t:TextRec);
  36. var
  37. Flags : Longint;
  38. Begin
  39. Case t.mode Of
  40. fmInput : Flags:=$10000;
  41. fmOutput : Flags:=$11001;
  42. fmAppend : Flags:=$10101;
  43. else
  44. begin
  45. InOutRes:=102;
  46. exit;
  47. end;
  48. End;
  49. Do_Open(t,PChar(@t.Name),Flags);
  50. t.CloseFunc:=@FileCloseFunc;
  51. t.FlushFunc:=nil;
  52. if t.Mode=fmInput then
  53. t.InOutFunc:=@FileReadFunc
  54. else
  55. begin
  56. t.InOutFunc:=@FileWriteFunc;
  57. { Only install flushing if its a NOT a file, and only check if there
  58. was no error opening the file, becuase else we always get a bad
  59. file handle error 6 (PFV) }
  60. if (InOutRes=0) and
  61. Do_Isdevice(t.Handle) then
  62. t.FlushFunc:=@FileWriteFunc;
  63. end;
  64. End;
  65. Procedure Assign({$ifdef PARAOUTFILE}out{$else}var{$endif} t:Text;const s:String);
  66. Begin
  67. FillChar(t,SizeOf(TextRec),0);
  68. { only set things that are not zero }
  69. TextRec(t).Handle:=UnusedHandle;
  70. TextRec(t).mode:=fmClosed;
  71. TextRec(t).BufSize:=TextRecBufSize;
  72. TextRec(t).Bufptr:=@TextRec(t).Buffer;
  73. TextRec(t).OpenFunc:=@FileOpenFunc;
  74. Case DefaultTextLineBreakStyle Of
  75. tlbsLF: TextRec(t).LineEnd := #10;
  76. tlbsCRLF: TextRec(t).LineEnd := #13#10;
  77. tlbsCR: TextRec(t).LineEnd := #13;
  78. End;
  79. Move(s[1],TextRec(t).Name,Length(s));
  80. End;
  81. Procedure Assign({$ifdef PARAOUTFILE}out{$else}var{$endif} t:Text;p:pchar);
  82. begin
  83. Assign(t,StrPas(p));
  84. end;
  85. Procedure Assign({$ifdef PARAOUTFILE}out{$else}var{$endif} t:Text;c:char);
  86. begin
  87. Assign(t,string(c));
  88. end;
  89. Procedure Close(var t : Text);[IOCheck];
  90. Begin
  91. if InOutRes<>0 then
  92. Exit;
  93. case TextRec(t).mode of
  94. fmInput,fmOutput,fmAppend:
  95. Begin
  96. { Write pending buffer }
  97. If Textrec(t).Mode=fmoutput then
  98. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  99. { Only close functions not connected to stdout.}
  100. If ((TextRec(t).Handle<>StdInputHandle) and
  101. (TextRec(t).Handle<>StdOutputHandle) and
  102. (TextRec(t).Handle<>StdErrorHandle)) Then
  103. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  104. TextRec(t).mode := fmClosed;
  105. { Reset buffer for safety }
  106. TextRec(t).BufPos:=0;
  107. TextRec(t).BufEnd:=0;
  108. End
  109. else inOutRes := 103;
  110. End;
  111. End;
  112. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  113. Begin
  114. Case TextRec(t).mode Of {This gives the fastest code}
  115. fmInput,fmOutput,fmInOut : Close(t);
  116. fmClosed : ;
  117. else
  118. Begin
  119. InOutRes:=102;
  120. exit;
  121. End;
  122. End;
  123. TextRec(t).mode:=mode;
  124. TextRec(t).bufpos:=0;
  125. TextRec(t).bufend:=0;
  126. FileFunc(TextRec(t).OpenFunc)(TextRec(t));
  127. { reset the mode to closed when an error has occured }
  128. if InOutRes<>0 then
  129. TextRec(t).mode:=fmClosed;
  130. End;
  131. Procedure Rewrite(var t : Text);[IOCheck];
  132. Begin
  133. If InOutRes<>0 then
  134. exit;
  135. OpenText(t,fmOutput,1);
  136. End;
  137. Procedure Reset(var t : Text);[IOCheck];
  138. Begin
  139. If InOutRes<>0 then
  140. exit;
  141. OpenText(t,fmInput,0);
  142. End;
  143. Procedure Append(var t : Text);[IOCheck];
  144. Begin
  145. If InOutRes<>0 then
  146. exit;
  147. OpenText(t,fmAppend,1);
  148. End;
  149. Procedure Flush(var t : Text);[IOCheck];
  150. Begin
  151. If InOutRes<>0 then
  152. exit;
  153. if TextRec(t).mode<>fmOutput then
  154. begin
  155. if TextRec(t).mode=fmInput then
  156. InOutRes:=105
  157. else
  158. InOutRes:=103;
  159. exit;
  160. end;
  161. { Not the flushfunc but the inoutfunc should be used, becuase that
  162. writes the data, flushfunc doesn't need to be assigned }
  163. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  164. End;
  165. Procedure Erase(var t:Text);[IOCheck];
  166. Begin
  167. If InOutRes <> 0 then
  168. exit;
  169. If TextRec(t).mode=fmClosed Then
  170. Do_Erase(PChar(@TextRec(t).Name));
  171. End;
  172. Procedure Rename(var t : text;p:pchar);[IOCheck];
  173. Begin
  174. If InOutRes <> 0 then
  175. exit;
  176. If TextRec(t).mode=fmClosed Then
  177. Begin
  178. Do_Rename(PChar(@TextRec(t).Name),p);
  179. { check error code of do_rename }
  180. If InOutRes = 0 then
  181. Move(p^,TextRec(t).Name,StrLen(p)+1);
  182. End;
  183. End;
  184. Procedure Rename(var t : Text;const s : string);[IOCheck];
  185. var
  186. p : array[0..255] Of Char;
  187. Begin
  188. If InOutRes <> 0 then
  189. exit;
  190. Move(s[1],p,Length(s));
  191. p[Length(s)]:=#0;
  192. Rename(t,Pchar(@p));
  193. End;
  194. Procedure Rename(var t : Text;c : char);[IOCheck];
  195. var
  196. p : array[0..1] Of Char;
  197. Begin
  198. If InOutRes <> 0 then
  199. exit;
  200. p[0]:=c;
  201. p[1]:=#0;
  202. Rename(t,Pchar(@p));
  203. End;
  204. Function Eof(Var t: Text): Boolean;[IOCheck];
  205. Begin
  206. If (InOutRes<>0) then
  207. exit(true);
  208. if (TextRec(t).mode<>fmInput) Then
  209. begin
  210. if TextRec(t).mode=fmOutput then
  211. InOutRes:=104
  212. else
  213. InOutRes:=103;
  214. exit(true);
  215. end;
  216. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  217. begin
  218. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  219. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  220. exit(true);
  221. end;
  222. Eof:=CtrlZMarksEOF and (TextRec(t).Bufptr^[TextRec(t).BufPos]=#26);
  223. end;
  224. Function Eof:Boolean;
  225. Begin
  226. Eof:=Eof(Input);
  227. End;
  228. Function SeekEof (Var t : Text) : Boolean;
  229. var
  230. oldfilepos : Int64;
  231. oldbufpos, oldbufend : SizeInt;
  232. 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. #26 :
  276. if CtrlZMarksEOF then
  277. begin
  278. SeekEof := true;
  279. break;
  280. end;
  281. #10,#13,#9,' ' :
  282. ;
  283. else
  284. begin
  285. SeekEof := false;
  286. break;
  287. end;
  288. end;
  289. inc(TextRec(t).BufPos);
  290. until false;
  291. { restore file position if not working with a device }
  292. if not isdevice then
  293. { if we didn't modify the buffer, simply restore the BufPos and BufEnd }
  294. { (the latter becuase it's now probably set to zero because nothing was }
  295. { was read anymore) }
  296. if (reads = 0) then
  297. begin
  298. TextRec(t).BufPos:=oldbufpos;
  299. TextRec(t).BufEnd:=oldbufend;
  300. end
  301. { otherwise return to the old filepos and reset the buffer }
  302. else
  303. begin
  304. do_seek(TextRec(t).handle,oldfilepos);
  305. InOutRes:=0;
  306. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  307. TextRec(t).BufPos:=oldbufpos;
  308. end;
  309. End;
  310. Function SeekEof : Boolean;
  311. Begin
  312. SeekEof:=SeekEof(Input);
  313. End;
  314. Function Eoln(var t:Text) : Boolean;
  315. Begin
  316. If (InOutRes<>0) then
  317. exit(true);
  318. if (TextRec(t).mode<>fmInput) Then
  319. begin
  320. if TextRec(t).mode=fmOutPut then
  321. InOutRes:=104
  322. else
  323. InOutRes:=103;
  324. exit(true);
  325. end;
  326. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  327. begin
  328. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  329. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  330. exit(true);
  331. end;
  332. if CtrlZMarksEOF and (TextRec (T).BufPtr^[TextRec (T).BufPos] = #26) then
  333. exit (true);
  334. Eoln:=(TextRec(t).Bufptr^[TextRec(t).BufPos] in [#10,#13]);
  335. End;
  336. Function Eoln : Boolean;
  337. Begin
  338. Eoln:=Eoln(Input);
  339. End;
  340. Function SeekEoln (Var t : Text) : Boolean;
  341. Begin
  342. If (InOutRes<>0) then
  343. exit(true);
  344. if (TextRec(t).mode<>fmInput) Then
  345. begin
  346. if TextRec(t).mode=fmOutput then
  347. InOutRes:=104
  348. else
  349. InOutRes:=103;
  350. exit(true);
  351. end;
  352. repeat
  353. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  354. begin
  355. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  356. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  357. exit(true);
  358. end;
  359. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  360. #26: if CtrlZMarksEOF then
  361. exit (true);
  362. #10,#13 : exit(true);
  363. #9,' ' : ;
  364. else
  365. exit(false);
  366. end;
  367. inc(TextRec(t).BufPos);
  368. until false;
  369. End;
  370. Function SeekEoln : Boolean;
  371. Begin
  372. SeekEoln:=SeekEoln(Input);
  373. End;
  374. Procedure SetTextBuf(Var F : Text; Var Buf; Size : SizeInt);
  375. Begin
  376. TextRec(f).BufPtr:=@Buf;
  377. TextRec(f).BufSize:=Size;
  378. TextRec(f).BufPos:=0;
  379. TextRec(f).BufEnd:=0;
  380. End;
  381. Procedure SetTextLineEnding(Var f:Text; Ending:string);
  382. Begin
  383. TextRec(F).LineEnd:=Ending;
  384. End;
  385. Function fpc_get_input:PText;compilerproc;
  386. begin
  387. fpc_get_input:=@Input;
  388. end;
  389. Function fpc_get_output:PText;compilerproc;
  390. begin
  391. fpc_get_output:=@Output;
  392. end;
  393. {*****************************************************************************
  394. Write(Ln)
  395. *****************************************************************************}
  396. Procedure fpc_WriteBuffer(var f:Text;const b;len:SizeInt);[Public,Alias:'FPC_WRITEBUFFER'];
  397. var
  398. p : pchar;
  399. left,
  400. idx : SizeInt;
  401. begin
  402. p:=pchar(@b);
  403. idx:=0;
  404. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  405. while len>left do
  406. begin
  407. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],left);
  408. dec(len,left);
  409. inc(idx,left);
  410. inc(TextRec(f).BufPos,left);
  411. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  412. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  413. end;
  414. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],len);
  415. inc(TextRec(f).BufPos,len);
  416. end;
  417. Procedure fpc_WriteBlanks(var f:Text;len:longint);[Public,Alias:'FPC_WRITEBLANKS'];
  418. var
  419. left : longint;
  420. begin
  421. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  422. while len>left do
  423. begin
  424. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],left,' ');
  425. dec(len,left);
  426. inc(TextRec(f).BufPos,left);
  427. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  428. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  429. end;
  430. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],len,' ');
  431. inc(TextRec(f).BufPos,len);
  432. end;
  433. Procedure fpc_Write_End(var f:Text);[Public,Alias:'FPC_WRITE_END']; iocheck; compilerproc;
  434. begin
  435. if TextRec(f).FlushFunc<>nil then
  436. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  437. end;
  438. Procedure fpc_Writeln_End(var f:Text);[Public,Alias:'FPC_WRITELN_END']; iocheck; compilerproc;
  439. begin
  440. If InOutRes <> 0 then exit;
  441. case TextRec(f).mode of
  442. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  443. begin
  444. { Write EOL }
  445. fpc_WriteBuffer(f,TextRec(f).LineEnd[1],length(TextRec(f).LineEnd));
  446. { Flush }
  447. if TextRec(f).FlushFunc<>nil then
  448. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  449. end;
  450. fmInput: InOutRes:=105
  451. else InOutRes:=103;
  452. end;
  453. end;
  454. Procedure fpc_Write_Text_ShortStr(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR']; compilerproc;
  455. Begin
  456. If (InOutRes<>0) then
  457. exit;
  458. case TextRec(f).mode of
  459. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  460. begin
  461. If Len>Length(s) Then
  462. fpc_WriteBlanks(f,Len-Length(s));
  463. fpc_WriteBuffer(f,s[1],Length(s));
  464. end;
  465. fmInput: InOutRes:=105
  466. else InOutRes:=103;
  467. end;
  468. End;
  469. { provide local access to write_str }
  470. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  471. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; [Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_ARRAY']; compilerproc;
  472. var
  473. ArrayLen : longint;
  474. p : pchar;
  475. Begin
  476. If (InOutRes<>0) then
  477. exit;
  478. case TextRec(f).mode of
  479. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  480. begin
  481. p:=pchar(@s);
  482. if (zerobased) then
  483. begin
  484. { can't use StrLen, since that one could try to read past the end }
  485. { of the heap (JM) }
  486. ArrayLen:=IndexByte(p^,high(s)+1,0);
  487. { IndexByte returns -1 if not found (JM) }
  488. if ArrayLen = -1 then
  489. ArrayLen := high(s)+1;
  490. end
  491. else
  492. ArrayLen := high(s)+1;
  493. If Len>ArrayLen Then
  494. fpc_WriteBlanks(f,Len-ArrayLen);
  495. fpc_WriteBuffer(f,p^,ArrayLen);
  496. end;
  497. fmInput: InOutRes:=105
  498. else InOutRes:=103;
  499. end;
  500. End;
  501. Procedure fpc_Write_Text_PChar_As_Pointer(Len : Longint;var f : Text;p : PChar); iocheck; [Public,Alias:'FPC_WRITE_TEXT_PCHAR_AS_POINTER']; compilerproc;
  502. var
  503. PCharLen : longint;
  504. Begin
  505. If (p=nil) or (InOutRes<>0) then
  506. exit;
  507. case TextRec(f).mode of
  508. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  509. begin
  510. PCharLen:=StrLen(p);
  511. If Len>PCharLen Then
  512. fpc_WriteBlanks(f,Len-PCharLen);
  513. fpc_WriteBuffer(f,p^,PCharLen);
  514. end;
  515. fmInput: InOutRes:=105
  516. else InOutRes:=103;
  517. end;
  518. End;
  519. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; const S : AnsiString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; compilerproc;
  520. {
  521. Writes a AnsiString to the Text file T
  522. }
  523. var
  524. SLen : longint;
  525. begin
  526. If (InOutRes<>0) then
  527. exit;
  528. case TextRec(f).mode of
  529. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  530. begin
  531. SLen:=Length(s);
  532. If Len>SLen Then
  533. fpc_WriteBlanks(f,Len-SLen);
  534. if slen > 0 then
  535. fpc_WriteBuffer(f,PChar(S)^,SLen);
  536. end;
  537. fmInput: InOutRes:=105
  538. else InOutRes:=103;
  539. end;
  540. end;
  541. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; [Public,alias:'FPC_WRITE_TEXT_WIDESTR']; compilerproc;
  542. {
  543. Writes a WideString to the Text file T
  544. }
  545. var
  546. SLen : longint;
  547. a: ansistring;
  548. begin
  549. If (pointer(S)=nil) or (InOutRes<>0) then
  550. exit;
  551. case TextRec(f).mode of
  552. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  553. begin
  554. SLen:=Length(s);
  555. If Len>SLen Then
  556. fpc_WriteBlanks(f,Len-SLen);
  557. a:=s;
  558. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  559. fpc_WriteBuffer(f,pchar(a)^,length(a));
  560. end;
  561. fmInput: InOutRes:=105
  562. else InOutRes:=103;
  563. end;
  564. end;
  565. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SINT']; compilerproc;
  566. var
  567. s : String;
  568. Begin
  569. If (InOutRes<>0) then
  570. exit;
  571. Str(l,s);
  572. Write_Str(Len,t,s);
  573. End;
  574. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_UINT']; compilerproc;
  575. var
  576. s : String;
  577. Begin
  578. If (InOutRes<>0) then
  579. exit;
  580. Str(L,s);
  581. Write_Str(Len,t,s);
  582. End;
  583. {$ifndef CPU64}
  584. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; [public,alias:'FPC_WRITE_TEXT_QWORD']; compilerproc;
  585. var
  586. s : string;
  587. begin
  588. if (InOutRes<>0) then
  589. exit;
  590. str(q,s);
  591. write_str(len,t,s);
  592. end;
  593. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; [public,alias:'FPC_WRITE_TEXT_INT64']; compilerproc;
  594. var
  595. s : string;
  596. begin
  597. if (InOutRes<>0) then
  598. exit;
  599. str(i,s);
  600. write_str(len,t,s);
  601. end;
  602. {$endif CPU64}
  603. {$ifndef FPUNONE}
  604. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; [Public,Alias:'FPC_WRITE_TEXT_FLOAT']; compilerproc;
  605. var
  606. s : String;
  607. Begin
  608. If (InOutRes<>0) then
  609. exit;
  610. Str_real(Len,fixkomma,r,treal_type(rt),s);
  611. Write_Str(Len,t,s);
  612. End;
  613. {$endif}
  614. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; [Public,Alias:'FPC_WRITE_TEXT_ENUM']; compilerproc;
  615. type Ptypeinfo=^Ttypeinfo;
  616. Ttypeinfo=packed record
  617. kind:byte;
  618. name:shortstring;
  619. end;
  620. Penuminfo=^Tenuminfo;
  621. Tenuminfo={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  622. ordtype:byte;
  623. minvalue, maxvalue:longint;
  624. basetype:pointer;
  625. namelist:shortstring;
  626. end;
  627. Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  628. o:longint;
  629. s:Pstring;
  630. end;
  631. var
  632. p:Pstring;
  633. l,h,m,offset:cardinal;
  634. sorted_array:^Tsorted_array;
  635. s:string;
  636. begin
  637. if textrec(t).mode<>fmoutput then
  638. begin
  639. if textrec(t).mode=fminput then
  640. inoutres:=105
  641. else
  642. inoutres:=103;
  643. exit;
  644. end;
  645. if Pcardinal(ord2strindex)^=0 then
  646. begin
  647. {The compiler did generate a lookup table.}
  648. offset:=2+length(Ptypeinfo(typinfo)^.name);
  649. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  650. offset:=align(offset, sizeof(ptrint));
  651. {$endif}
  652. with Penuminfo(Pbyte(typinfo)+offset)^ do
  653. begin
  654. if (ordinal<minvalue) or (ordinal>maxvalue) then
  655. begin
  656. inoutres:=107; {Invalid ordinal value for this enum.}
  657. exit;
  658. end;
  659. dec(ordinal,minvalue);
  660. end;
  661. {Get the address of the string.}
  662. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  663. p:=Pshortstring((PPpointer(ord2strindex+align(sizeof(longint), sizeof(ptrint)))+ordinal)^);
  664. {$else}
  665. p:=Pshortstring((PPpointer(ord2strindex+sizeof(longint))+ordinal)^);
  666. {$endif}
  667. if p=nil then
  668. begin
  669. inoutres:=107; {Invalid ordinal value for this enum.}
  670. exit;
  671. end;
  672. s:=p^;
  673. end
  674. else
  675. begin
  676. {The compiler did generate a sorted array of (ordvalue,Pstring) tuples.}
  677. sorted_array:=pointer(Pcardinal(ord2strindex)+2);
  678. {Use a binary search to get the string.}
  679. l:=0;
  680. h:=(Pcardinal(ord2strindex)+1)^-1;
  681. repeat
  682. m:=(l+h) div 2;
  683. if ordinal>sorted_array[m].o then
  684. l:=m+1
  685. else if ordinal<sorted_array[m].o then
  686. h:=m-1
  687. else
  688. break;
  689. if l>h then
  690. begin
  691. inoutres:=107; {Invalid ordinal value for this enum.}
  692. exit;
  693. end;
  694. until false;
  695. s:=sorted_array[m].s^;
  696. end;
  697. fpc_writeBuffer(t,s[1],length(s));
  698. {Pad the string with spaces if necessary.}
  699. if len>length(s) then
  700. fpc_writeblanks(t,len-length(s));
  701. end;
  702. {$ifdef FPC_HAS_STR_CURRENCY}
  703. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; [Public,Alias:'FPC_WRITE_TEXT_CURRENCY']; compilerproc;
  704. var
  705. s : String;
  706. Begin
  707. If (InOutRes<>0) then
  708. exit;
  709. str(c:Len:fixkomma,s);
  710. Write_Str(Len,t,s);
  711. End;
  712. {$endif FPC_HAS_STR_CURRENCY}
  713. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; [Public,Alias:'FPC_WRITE_TEXT_BOOLEAN']; compilerproc;
  714. Begin
  715. If (InOutRes<>0) then
  716. exit;
  717. { Can't use array[boolean] because b can be >0 ! }
  718. if b then
  719. Write_Str(Len,t,'TRUE')
  720. else
  721. Write_Str(Len,t,'FALSE');
  722. End;
  723. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; [Public,Alias:'FPC_WRITE_TEXT_CHAR']; compilerproc;
  724. Begin
  725. If (InOutRes<>0) then
  726. exit;
  727. if (TextRec(t).mode<>fmOutput) Then
  728. begin
  729. if TextRec(t).mode=fmClosed then
  730. InOutRes:=103
  731. else
  732. InOutRes:=105;
  733. exit;
  734. end;
  735. If Len>1 Then
  736. fpc_WriteBlanks(t,Len-1);
  737. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  738. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  739. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  740. Inc(TextRec(t).BufPos);
  741. End;
  742. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; [Public,Alias:'FPC_WRITE_TEXT_WIDECHAR']; compilerproc;
  743. var
  744. a : ansistring;
  745. Begin
  746. If (InOutRes<>0) then
  747. exit;
  748. if (TextRec(t).mode<>fmOutput) Then
  749. begin
  750. if TextRec(t).mode=fmClosed then
  751. InOutRes:=103
  752. else
  753. InOutRes:=105;
  754. exit;
  755. end;
  756. If Len>1 Then
  757. fpc_WriteBlanks(t,Len-1);
  758. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  759. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  760. { a widechar can be translated into more than a single ansichar }
  761. a:=c;
  762. fpc_WriteBuffer(t,pchar(a)^,length(a));
  763. End;
  764. {*****************************************************************************
  765. Read(Ln)
  766. *****************************************************************************}
  767. Function NextChar(var f:Text;var s:string):Boolean;
  768. begin
  769. NextChar:=false;
  770. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  771. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  772. begin
  773. if length(s)<high(s) then
  774. begin
  775. inc(s[0]);
  776. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  777. end;
  778. Inc(TextRec(f).BufPos);
  779. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  780. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  781. NextChar:=true;
  782. end;
  783. end;
  784. Function IgnoreSpaces(var f:Text):Boolean;
  785. {
  786. Removes all leading spaces,tab,eols from the input buffer, returns true if
  787. the buffer is empty
  788. }
  789. var
  790. s : string;
  791. begin
  792. s:='';
  793. IgnoreSpaces:=false;
  794. { Return false when already at EOF }
  795. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  796. exit;
  797. (* Check performed separately to avoid accessing memory outside buffer *)
  798. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  799. exit;
  800. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  801. begin
  802. if not NextChar(f,s) then
  803. exit;
  804. { EOF? }
  805. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  806. break;
  807. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  808. break;
  809. end;
  810. IgnoreSpaces:=true;
  811. end;
  812. procedure ReadNumeric(var f:Text;var s:string);
  813. {
  814. Read numeric input, if buffer is empty then return True
  815. }
  816. begin
  817. repeat
  818. if not NextChar(f,s) then
  819. exit;
  820. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  821. end;
  822. function CheckRead(var f:Text):Boolean;
  823. begin
  824. CheckRead:=False;
  825. { Check error and if file is open and load buf if empty }
  826. If (InOutRes<>0) then
  827. exit;
  828. if (TextRec(f).mode<>fmInput) Then
  829. begin
  830. case TextRec(f).mode of
  831. fmOutPut,fmAppend:
  832. InOutRes:=104;
  833. else
  834. InOutRes:=103;
  835. end;
  836. exit;
  837. end;
  838. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  839. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  840. CheckRead:=True;
  841. end;
  842. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  843. begin
  844. if TextRec(f).FlushFunc<>nil then
  845. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  846. end;
  847. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  848. var prev: char;
  849. Begin
  850. If not CheckRead(f) then
  851. exit;
  852. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  853. { Flush if set }
  854. begin
  855. if (TextRec(f).FlushFunc<>nil) then
  856. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  857. exit;
  858. end;
  859. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  860. Exit;
  861. repeat
  862. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  863. inc(TextRec(f).BufPos);
  864. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  865. { #13#10 = Dos), so if we've got #10, we can safely exit }
  866. if prev = #10 then
  867. exit;
  868. {$ifdef MACOS}
  869. if prev = #13 then
  870. {StdInput on macos never have dos line ending, so this is safe.}
  871. if TextRec(f).Handle = StdInputHandle then
  872. exit;
  873. {$endif MACOS}
  874. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  875. begin
  876. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  877. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  878. { Flush if set }
  879. begin
  880. if (TextRec(f).FlushFunc<>nil) then
  881. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  882. exit;
  883. end;
  884. end;
  885. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  886. Exit;
  887. if (prev=#13) then
  888. { is there also a #10 after it? }
  889. begin
  890. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  891. { yes, skip that one as well }
  892. inc(TextRec(f).BufPos);
  893. exit;
  894. end;
  895. until false;
  896. End;
  897. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  898. var
  899. sPos,len : Longint;
  900. p,startp,maxp : pchar;
  901. end_of_string:boolean;
  902. Begin
  903. ReadPCharLen:=0;
  904. If not CheckRead(f) then
  905. exit;
  906. { Read maximal until Maxlen is reached }
  907. sPos:=0;
  908. end_of_string:=false;
  909. repeat
  910. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  911. begin
  912. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  913. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  914. break;
  915. end;
  916. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  917. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  918. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  919. else
  920. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  921. startp:=p;
  922. { find stop character }
  923. while p<maxp do
  924. begin
  925. { Optimization: Do a quick check for a control character first }
  926. if (p^<' ') then
  927. begin
  928. if (p^ in [#10,#13]) or
  929. (ctrlZmarkseof and (p^=#26)) then
  930. begin
  931. end_of_string:=true;
  932. break;
  933. end;
  934. end;
  935. inc(p);
  936. end;
  937. { calculate read bytes }
  938. len:=p-startp;
  939. inc(TextRec(f).BufPos,Len);
  940. Move(startp^,s[sPos],Len);
  941. inc(sPos,Len);
  942. until (spos=MaxLen) or end_of_string;
  943. ReadPCharLen:=spos;
  944. End;
  945. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; [Public,Alias:'FPC_READ_TEXT_SHORTSTR']; compilerproc;
  946. Begin
  947. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  948. End;
  949. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER']; compilerproc;
  950. Begin
  951. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  952. End;
  953. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_ARRAY']; compilerproc;
  954. var
  955. len: longint;
  956. Begin
  957. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  958. if zerobased and
  959. (len > high(s)) then
  960. len := high(s);
  961. if (len <= high(s)) then
  962. s[len] := #0;
  963. End;
  964. Procedure fpc_Read_Text_AnsiStr(var f : Text;out s : AnsiString); iocheck; [Public,Alias:'FPC_READ_TEXT_ANSISTR']; compilerproc;
  965. var
  966. slen,len : SizeInt;
  967. Begin
  968. slen:=0;
  969. Repeat
  970. // SetLength will reallocate the length.
  971. SetLength(S,slen+255);
  972. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  973. inc(slen,len);
  974. Until len<255;
  975. // Set actual length
  976. SetLength(S,Slen);
  977. End;
  978. procedure fpc_Read_Text_Char(var f : Text; out c: char); iocheck; [Public,Alias:'FPC_READ_TEXT_CHAR'];compilerproc;
  979. Begin
  980. c:=#0;
  981. If not CheckRead(f) then
  982. exit;
  983. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  984. begin
  985. c := #26;
  986. exit;
  987. end;
  988. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  989. inc(TextRec(f).BufPos);
  990. end;
  991. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; [Public,Alias:'FPC_READ_TEXT_SINT']; compilerproc;
  992. var
  993. hs : String;
  994. code : longint;
  995. Begin
  996. l:=0;
  997. If not CheckRead(f) then
  998. exit;
  999. hs:='';
  1000. if IgnoreSpaces(f) then
  1001. begin
  1002. { When spaces were found and we are now at EOF,
  1003. then we return 0 }
  1004. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1005. exit;
  1006. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1007. exit;
  1008. ReadNumeric(f,hs);
  1009. end;
  1010. if (hs = '') then
  1011. L := 0
  1012. else
  1013. begin
  1014. Val(hs,l,code);
  1015. if Code <> 0 then
  1016. InOutRes:=106;
  1017. end;
  1018. End;
  1019. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; [Public,Alias:'FPC_READ_TEXT_UINT']; compilerproc;
  1020. var
  1021. hs : String;
  1022. code : longint;
  1023. Begin
  1024. u:=0;
  1025. If not CheckRead(f) then
  1026. exit;
  1027. hs:='';
  1028. if IgnoreSpaces(f) then
  1029. begin
  1030. { When spaces were found and we are now at EOF,
  1031. then we return 0 }
  1032. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1033. exit;
  1034. ReadNumeric(f,hs);
  1035. end;
  1036. if (hs = '') then
  1037. u := 0
  1038. else
  1039. begin
  1040. val(hs,u,code);
  1041. If code<>0 Then
  1042. InOutRes:=106;
  1043. end;
  1044. End;
  1045. {$ifndef FPUNONE}
  1046. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; [Public,Alias:'FPC_READ_TEXT_FLOAT']; compilerproc;
  1047. var
  1048. hs : string;
  1049. code : Word;
  1050. begin
  1051. v:=0.0;
  1052. If not CheckRead(f) then
  1053. exit;
  1054. hs:='';
  1055. if IgnoreSpaces(f) then
  1056. begin
  1057. { When spaces were found and we are now at EOF,
  1058. then we return 0 }
  1059. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1060. exit;
  1061. ReadNumeric(f,hs);
  1062. end;
  1063. val(hs,v,code);
  1064. If code<>0 Then
  1065. InOutRes:=106;
  1066. end;
  1067. {$endif}
  1068. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck; [Public,Alias:'FPC_READ_TEXT_ENUM'];compilerproc;
  1069. var s:string;
  1070. code:valsint;
  1071. begin
  1072. if not checkread(t) then
  1073. exit;
  1074. s:='';
  1075. if ignorespaces(t) then
  1076. begin
  1077. { When spaces were found and we are now at EOF, then we return 0 }
  1078. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1079. exit;
  1080. ReadNumeric(t,s);
  1081. end;
  1082. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1083. if code<>0 then
  1084. InOutRes:=106;
  1085. end;
  1086. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; [Public,Alias:'FPC_READ_TEXT_CURRENCY']; compilerproc;
  1087. var
  1088. hs : string;
  1089. code : Word;
  1090. begin
  1091. {$ifdef FPUNONE}
  1092. v:=0;
  1093. {$else}
  1094. v:=0.0;
  1095. {$endif}
  1096. If not CheckRead(f) then
  1097. exit;
  1098. hs:='';
  1099. if IgnoreSpaces(f) then
  1100. begin
  1101. { When spaces were found and we are now at EOF,
  1102. then we return 0 }
  1103. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1104. exit;
  1105. ReadNumeric(f,hs);
  1106. end;
  1107. val(hs,v,code);
  1108. If code<>0 Then
  1109. InOutRes:=106;
  1110. end;
  1111. {$ifndef cpu64}
  1112. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; [public,alias:'FPC_READ_TEXT_QWORD']; compilerproc;
  1113. var
  1114. hs : String;
  1115. code : longint;
  1116. Begin
  1117. q:=0;
  1118. If not CheckRead(f) then
  1119. exit;
  1120. hs:='';
  1121. if IgnoreSpaces(f) then
  1122. begin
  1123. { When spaces were found and we are now at EOF,
  1124. then we return 0 }
  1125. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1126. exit;
  1127. ReadNumeric(f,hs);
  1128. end;
  1129. val(hs,q,code);
  1130. If code<>0 Then
  1131. InOutRes:=106;
  1132. End;
  1133. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; [public,alias:'FPC_READ_TEXT_INT64']; compilerproc;
  1134. var
  1135. hs : String;
  1136. code : Longint;
  1137. Begin
  1138. i:=0;
  1139. If not CheckRead(f) then
  1140. exit;
  1141. hs:='';
  1142. if IgnoreSpaces(f) then
  1143. begin
  1144. { When spaces were found and we are now at EOF,
  1145. then we return 0 }
  1146. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1147. exit;
  1148. ReadNumeric(f,hs);
  1149. end;
  1150. Val(hs,i,code);
  1151. If code<>0 Then
  1152. InOutRes:=106;
  1153. End;
  1154. {$endif CPU64}
  1155. {*****************************************************************************
  1156. WriteStr/ReadStr
  1157. *****************************************************************************}
  1158. const
  1159. StrPtrIndex = 1;
  1160. { leave space for 128 bit string pointers :) (used for writestr) }
  1161. ShortStrLenIndex = 17;
  1162. { how many bytes of the string have been processed already (used for readstr) }
  1163. BytesReadIndex = 17;
  1164. threadvar
  1165. ReadWriteStrText: textrec;
  1166. procedure WriteStrShort(var t: textrec);
  1167. var
  1168. str: pshortstring;
  1169. newbytes,
  1170. oldlen: longint;
  1171. begin
  1172. if (t.bufpos=0) then
  1173. exit;
  1174. str:=pshortstring(ppointer(@t.userdata[StrPtrIndex])^);
  1175. newbytes:=t.BufPos;
  1176. oldlen:=length(str^);
  1177. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1178. begin
  1179. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1180. {$ifdef writestr_iolencheck}
  1181. // GPC only gives an io error if {$no-truncate-strings} is active
  1182. // FPC does not have this setting (it never gives errors when a
  1183. // a string expression is truncated)
  1184. { "disk full" }
  1185. inoutres:=101;
  1186. {$endif}
  1187. end;
  1188. setlength(str^,length(str^)+newbytes);
  1189. move(t.bufptr^,str^[oldlen+1],newbytes);
  1190. t.bufpos:=0;
  1191. end;
  1192. procedure WriteStrAnsi(var t: textrec);
  1193. var
  1194. str: pansistring;
  1195. oldlen: longint;
  1196. begin
  1197. if (t.bufpos=0) then
  1198. exit;
  1199. str:=pansistring(ppointer(@t.userdata[StrPtrIndex])^);
  1200. oldlen:=length(str^);
  1201. setlength(str^,oldlen+t.bufpos);
  1202. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1203. t.bufpos:=0;
  1204. end;
  1205. procedure WriteStrWide(var t: textrec);
  1206. var
  1207. temp: ansistring;
  1208. str: pwidestring;
  1209. begin
  1210. if (t.bufpos=0) then
  1211. exit;
  1212. str:=pwidestring(ppointer(@t.userdata[StrPtrIndex])^);
  1213. setlength(temp,t.bufpos);
  1214. move(t.bufptr^,temp[1],t.bufpos);
  1215. str^:=str^+temp;
  1216. t.bufpos:=0;
  1217. end;
  1218. procedure SetupWriteStrCommon(out t: textrec);
  1219. begin
  1220. // initialise
  1221. Assign(text(t),'');
  1222. t.mode:=fmOutput;
  1223. t.OpenFunc:=nil;
  1224. t.CloseFunc:=nil;
  1225. end;
  1226. function fpc_SetupWriteStr_Shortstr(out s: shortstring): PText; compilerproc;
  1227. begin
  1228. setupwritestrcommon(ReadWriteStrText);
  1229. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1230. ReadWriteStrText.userdata[ShortStrLenIndex]:=high(s);
  1231. setlength(s,0);
  1232. ReadWriteStrText.InOutFunc:=@WriteStrShort;
  1233. ReadWriteStrText.FlushFunc:=@WriteStrShort;
  1234. result:=@ReadWriteStrText;
  1235. end;
  1236. function fpc_SetupWriteStr_Ansistr(out s: ansistring): PText; compilerproc;
  1237. begin
  1238. setupwritestrcommon(ReadWriteStrText);
  1239. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1240. // automatically done by out-semantics
  1241. // setlength(s,0);
  1242. ReadWriteStrText.InOutFunc:=@WriteStrAnsi;
  1243. ReadWriteStrText.FlushFunc:=@WriteStrAnsi;
  1244. result:=@ReadWriteStrText;
  1245. end;
  1246. function fpc_SetupWriteStr_Widestr(out s: widestring): PText; compilerproc;
  1247. begin
  1248. setupwritestrcommon(ReadWriteStrText);
  1249. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1250. // automatically done by out-semantics
  1251. // setlength(s,0);
  1252. ReadWriteStrText.InOutFunc:=@WriteStrWide;
  1253. ReadWriteStrText.FlushFunc:=@WriteStrWide;
  1254. result:=@ReadWriteStrText;
  1255. end;
  1256. procedure ReadAnsiStrFinal(var t: textrec);
  1257. begin
  1258. { finalise the temp ansistring }
  1259. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1260. end;
  1261. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1262. var
  1263. newbytes: sizeint;
  1264. begin
  1265. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1266. if (t.BufSize <= newbytes) then
  1267. newbytes := t.BufSize;
  1268. if (newbytes > 0) then
  1269. begin
  1270. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1271. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1272. end;
  1273. t.BufEnd:=newbytes;
  1274. t.BufPos:=0;
  1275. end;
  1276. procedure ReadStrAnsi(var t: textrec);
  1277. var
  1278. str: pansistring;
  1279. begin
  1280. str:=pansistring(@t.userdata[StrPtrIndex]);
  1281. ReadStrCommon(t,@str^[1],length(str^));
  1282. end;
  1283. procedure SetupReadStrCommon(out t: textrec);
  1284. begin
  1285. // initialise
  1286. Assign(text(t),'');
  1287. t.mode:=fmInput;
  1288. t.OpenFunc:=nil;
  1289. t.CloseFunc:=nil;
  1290. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1291. end;
  1292. function fpc_SetupReadStr_Ansistr(const s: ansistring): PText; [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1293. begin
  1294. setupreadstrcommon(ReadWriteStrText);
  1295. { we need a reference, because 's' may be a temporary expression }
  1296. PAnsiString(@ReadWriteStrText.userdata[StrPtrIndex])^:=s;
  1297. ReadWriteStrText.InOutFunc:=@ReadStrAnsi;
  1298. { this is called at the end, by fpc_read_end }
  1299. ReadWriteStrText.FlushFunc:=@ReadAnsiStrFinal;
  1300. result:=@ReadWriteStrText;
  1301. end;
  1302. function fpc_SetupReadStr_Ansistr_Intern(const s: ansistring): PText; [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1303. function fpc_SetupReadStr_Shortstr(const s: shortstring): PText; compilerproc;
  1304. begin
  1305. { the reason we convert the short string to ansistring, is because the semantics of
  1306. readstr are defined as:
  1307. *********************
  1308. Apart from the restrictions imposed by requirements given in this clause,
  1309. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  1310. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  1311. char-type (or a subrange of char-type), the integer-type (or a subrange of
  1312. integer-type), the real-type, a fixed-string-type, or a
  1313. variable-string-type, shall be equivalent to
  1314. begin
  1315. rewrite(f);
  1316. writeln(f, e);
  1317. reset(f);
  1318. read(f, v 1 ,...,v n )
  1319. end
  1320. *********************
  1321. This means that any side effects caused by the evaluation of v 1 .. v n
  1322. must not affect the value of e (= our argument s) -> we need a copy of it.
  1323. An ansistring is the easiest way to get a threadsafe copy, and allows us
  1324. to use the other ansistring readstr helpers too.
  1325. }
  1326. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1327. end;
  1328. function fpc_SetupReadStr_Widestr(const s: widestring): PText; compilerproc;
  1329. begin
  1330. { we use an ansistring to avoid code duplication, and let the }
  1331. { assignment convert the widestring to an equivalent ansistring }
  1332. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1333. end;
  1334. {*****************************************************************************
  1335. Initializing
  1336. *****************************************************************************}
  1337. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1338. begin
  1339. Assign(f,'');
  1340. TextRec(f).Handle:=hdl;
  1341. TextRec(f).Mode:=mode;
  1342. TextRec(f).Closefunc:=@FileCloseFunc;
  1343. case mode of
  1344. fmInput :
  1345. TextRec(f).InOutFunc:=@FileReadFunc;
  1346. fmOutput :
  1347. begin
  1348. TextRec(f).InOutFunc:=@FileWriteFunc;
  1349. if Do_Isdevice(hdl) then
  1350. TextRec(f).FlushFunc:=@FileWriteFunc;
  1351. end;
  1352. else
  1353. HandleError(102);
  1354. end;
  1355. end;