text.inc 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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, because 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(out 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(out t:Text;p:pchar);
  82. begin
  83. Assign(t,StrPas(p));
  84. end;
  85. Procedure Assign(out 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, because 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 because 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);
  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);
  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); 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); 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. Procedure fpc_Write_Text_ShortStr_Iso(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR_ISO']; compilerproc;
  470. Begin
  471. If (InOutRes<>0) then
  472. exit;
  473. case TextRec(f).mode of
  474. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  475. begin
  476. { default value? }
  477. If Len=-1 then
  478. Len:=length(s);
  479. If Len>Length(s) Then
  480. begin
  481. fpc_WriteBlanks(f,Len-Length(s));
  482. fpc_WriteBuffer(f,s[1],Length(s));
  483. end
  484. else
  485. fpc_WriteBuffer(f,s[1],Len);
  486. end;
  487. fmInput: InOutRes:=105
  488. else InOutRes:=103;
  489. end;
  490. End;
  491. { provide local access to write_str }
  492. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  493. { provide local access to write_str_iso }
  494. procedure Write_Str_Iso(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR_ISO'];
  495. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  496. var
  497. ArrayLen : longint;
  498. p : pchar;
  499. Begin
  500. If (InOutRes<>0) then
  501. exit;
  502. case TextRec(f).mode of
  503. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  504. begin
  505. p:=pchar(@s);
  506. if zerobased then
  507. begin
  508. { can't use StrLen, since that one could try to read past the end }
  509. { of the heap (JM) }
  510. ArrayLen:=IndexByte(p^,high(s)+1,0);
  511. { IndexByte returns -1 if not found (JM) }
  512. if ArrayLen = -1 then
  513. ArrayLen := high(s)+1;
  514. end
  515. else
  516. ArrayLen := high(s)+1;
  517. If Len>ArrayLen Then
  518. fpc_WriteBlanks(f,Len-ArrayLen);
  519. fpc_WriteBuffer(f,p^,ArrayLen);
  520. end;
  521. fmInput: InOutRes:=105
  522. else InOutRes:=103;
  523. end;
  524. End;
  525. Procedure fpc_Write_Text_Pchar_as_Array_Iso(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  526. var
  527. ArrayLen : longint;
  528. p : pchar;
  529. Begin
  530. If (InOutRes<>0) then
  531. exit;
  532. case TextRec(f).mode of
  533. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  534. begin
  535. p:=pchar(@s);
  536. if zerobased then
  537. begin
  538. { can't use StrLen, since that one could try to read past the end }
  539. { of the heap (JM) }
  540. ArrayLen:=IndexByte(p^,high(s)+1,0);
  541. { IndexByte returns -1 if not found (JM) }
  542. if ArrayLen = -1 then
  543. ArrayLen := high(s)+1;
  544. end
  545. else
  546. ArrayLen := high(s)+1;
  547. { default value? }
  548. If Len=-1 then
  549. Len:=ArrayLen;
  550. If Len>ArrayLen Then
  551. begin
  552. fpc_WriteBlanks(f,Len-ArrayLen);
  553. fpc_WriteBuffer(f,p^,ArrayLen);
  554. end
  555. else
  556. fpc_WriteBuffer(f,p^,Len);
  557. end;
  558. fmInput: InOutRes:=105
  559. else InOutRes:=103;
  560. end;
  561. End;
  562. Procedure fpc_Write_Text_PChar_As_Pointer(Len : Longint;var f : Text;p : PChar); iocheck; compilerproc;
  563. var
  564. PCharLen : longint;
  565. Begin
  566. If (p=nil) or (InOutRes<>0) then
  567. exit;
  568. case TextRec(f).mode of
  569. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  570. begin
  571. PCharLen:=StrLen(p);
  572. If Len>PCharLen Then
  573. fpc_WriteBlanks(f,Len-PCharLen);
  574. fpc_WriteBuffer(f,p^,PCharLen);
  575. end;
  576. fmInput: InOutRes:=105
  577. else InOutRes:=103;
  578. end;
  579. End;
  580. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; const S : AnsiString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; compilerproc;
  581. {
  582. Writes a AnsiString to the Text file T
  583. }
  584. var
  585. SLen : longint;
  586. begin
  587. If (InOutRes<>0) then
  588. exit;
  589. case TextRec(f).mode of
  590. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  591. begin
  592. SLen:=Length(s);
  593. If Len>SLen Then
  594. fpc_WriteBlanks(f,Len-SLen);
  595. if slen > 0 then
  596. fpc_WriteBuffer(f,PChar(S)^,SLen);
  597. end;
  598. fmInput: InOutRes:=105
  599. else InOutRes:=103;
  600. end;
  601. end;
  602. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  603. Procedure fpc_Write_Text_UnicodeStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  604. {
  605. Writes a UnicodeString to the Text file T
  606. }
  607. var
  608. SLen : longint;
  609. a: ansistring;
  610. begin
  611. If (pointer(S)=nil) or (InOutRes<>0) then
  612. exit;
  613. case TextRec(f).mode of
  614. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  615. begin
  616. SLen:=Length(s);
  617. If Len>SLen Then
  618. fpc_WriteBlanks(f,Len-SLen);
  619. a:=s;
  620. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  621. fpc_WriteBuffer(f,pchar(a)^,length(a));
  622. end;
  623. fmInput: InOutRes:=105
  624. else InOutRes:=103;
  625. end;
  626. end;
  627. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  628. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  629. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  630. {
  631. Writes a WideString to the Text file T
  632. }
  633. var
  634. SLen : longint;
  635. a: ansistring;
  636. begin
  637. If (pointer(S)=nil) or (InOutRes<>0) then
  638. exit;
  639. case TextRec(f).mode of
  640. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  641. begin
  642. SLen:=Length(s);
  643. If Len>SLen Then
  644. fpc_WriteBlanks(f,Len-SLen);
  645. a:=s;
  646. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  647. fpc_WriteBuffer(f,pchar(a)^,length(a));
  648. end;
  649. fmInput: InOutRes:=105
  650. else InOutRes:=103;
  651. end;
  652. end;
  653. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  654. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  655. var
  656. s : String;
  657. Begin
  658. If (InOutRes<>0) then
  659. exit;
  660. Str(l,s);
  661. Write_Str(Len,t,s);
  662. End;
  663. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  664. var
  665. s : String;
  666. Begin
  667. If (InOutRes<>0) then
  668. exit;
  669. Str(L,s);
  670. Write_Str(Len,t,s);
  671. End;
  672. Procedure fpc_Write_Text_SInt_Iso(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  673. var
  674. s : String;
  675. Begin
  676. If (InOutRes<>0) then
  677. exit;
  678. Str(l,s);
  679. { default value? }
  680. if len=-1 then
  681. len:=11
  682. else if len<length(s) then
  683. len:=length(s);
  684. Write_Str_Iso(Len,t,s);
  685. End;
  686. Procedure fpc_Write_Text_UInt_Iso(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  687. var
  688. s : String;
  689. Begin
  690. If (InOutRes<>0) then
  691. exit;
  692. Str(L,s);
  693. { default value? }
  694. if len=-1 then
  695. len:=11
  696. else if len<length(s) then
  697. len:=length(s);
  698. Write_Str_Iso(Len,t,s);
  699. End;
  700. {$ifndef CPU64}
  701. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; compilerproc;
  702. var
  703. s : string;
  704. begin
  705. if (InOutRes<>0) then
  706. exit;
  707. str(q,s);
  708. write_str(len,t,s);
  709. end;
  710. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; compilerproc;
  711. var
  712. s : string;
  713. begin
  714. if (InOutRes<>0) then
  715. exit;
  716. str(i,s);
  717. write_str(len,t,s);
  718. end;
  719. procedure fpc_write_text_qword_iso(len : longint;var t : text;q : qword); iocheck; compilerproc;
  720. var
  721. s : string;
  722. begin
  723. if (InOutRes<>0) then
  724. exit;
  725. str(q,s);
  726. { default value? }
  727. if len=-1 then
  728. len:=20
  729. else if len<length(s) then
  730. len:=length(s);
  731. write_str_iso(len,t,s);
  732. end;
  733. procedure fpc_write_text_int64_iso(len : longint;var t : text;i : int64); iocheck; compilerproc;
  734. var
  735. s : string;
  736. begin
  737. if (InOutRes<>0) then
  738. exit;
  739. str(i,s);
  740. { default value? }
  741. if len=-1 then
  742. len:=20
  743. else if len<length(s) then
  744. len:=length(s);
  745. write_str_iso(len,t,s);
  746. end;
  747. {$endif CPU64}
  748. {$ifndef FPUNONE}
  749. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  750. var
  751. s : String;
  752. Begin
  753. If (InOutRes<>0) then
  754. exit;
  755. Str_real(Len,fixkomma,r,treal_type(rt),s);
  756. Write_Str(Len,t,s);
  757. End;
  758. Procedure fpc_Write_Text_Float_iso(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  759. var
  760. s : String;
  761. Begin
  762. If (InOutRes<>0) then
  763. exit;
  764. Str_real_iso(Len,fixkomma,r,treal_type(rt),s);
  765. Write_Str(Len,t,s);
  766. End;
  767. {$endif}
  768. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; compilerproc;
  769. type Ptypeinfo=^Ttypeinfo;
  770. Ttypeinfo=packed record
  771. kind:byte;
  772. name:shortstring;
  773. end;
  774. Penuminfo=^Tenuminfo;
  775. Tenuminfo={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  776. ordtype:byte;
  777. minvalue, maxvalue:longint;
  778. basetype:pointer;
  779. namelist:shortstring;
  780. end;
  781. Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  782. o:longint;
  783. s:Pstring;
  784. end;
  785. var
  786. p:Pstring;
  787. l,h,m,offset:cardinal;
  788. sorted_array:^Tsorted_array;
  789. s:string;
  790. begin
  791. if textrec(t).mode<>fmoutput then
  792. begin
  793. if textrec(t).mode=fminput then
  794. inoutres:=105
  795. else
  796. inoutres:=103;
  797. exit;
  798. end;
  799. if Pcardinal(ord2strindex)^=0 then
  800. begin
  801. {The compiler did generate a lookup table.}
  802. offset:=2+length(Ptypeinfo(typinfo)^.name);
  803. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  804. offset:=align(offset, sizeof(ptrint));
  805. {$endif}
  806. with Penuminfo(Pbyte(typinfo)+offset)^ do
  807. begin
  808. if (ordinal<minvalue) or (ordinal>maxvalue) then
  809. begin
  810. inoutres:=107; {Invalid ordinal value for this enum.}
  811. exit;
  812. end;
  813. dec(ordinal,minvalue);
  814. end;
  815. {Get the address of the string.}
  816. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  817. p:=Pshortstring((PPpointer(ord2strindex+align(sizeof(longint), sizeof(ptrint)))+ordinal)^);
  818. {$else}
  819. p:=Pshortstring((PPpointer(ord2strindex+sizeof(longint))+ordinal)^);
  820. {$endif}
  821. if p=nil then
  822. begin
  823. inoutres:=107; {Invalid ordinal value for this enum.}
  824. exit;
  825. end;
  826. s:=p^;
  827. end
  828. else
  829. begin
  830. {The compiler did generate a sorted array of (ordvalue,Pstring) tuples.}
  831. sorted_array:=pointer(Pcardinal(ord2strindex)+2);
  832. {Use a binary search to get the string.}
  833. l:=0;
  834. h:=(Pcardinal(ord2strindex)+1)^-1;
  835. repeat
  836. m:=(l+h) div 2;
  837. if ordinal>sorted_array[m].o then
  838. l:=m+1
  839. else if ordinal<sorted_array[m].o then
  840. h:=m-1
  841. else
  842. break;
  843. if l>h then
  844. begin
  845. inoutres:=107; {Invalid ordinal value for this enum.}
  846. exit;
  847. end;
  848. until false;
  849. s:=sorted_array[m].s^;
  850. end;
  851. fpc_writeBuffer(t,s[1],length(s));
  852. {Pad the string with spaces if necessary.}
  853. if len>length(s) then
  854. fpc_writeblanks(t,len-length(s));
  855. end;
  856. {$ifdef FPC_HAS_STR_CURRENCY}
  857. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; compilerproc;
  858. var
  859. s : String;
  860. Begin
  861. If (InOutRes<>0) then
  862. exit;
  863. str(c:Len:fixkomma,s);
  864. Write_Str(Len,t,s);
  865. End;
  866. {$endif FPC_HAS_STR_CURRENCY}
  867. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  868. Begin
  869. If (InOutRes<>0) then
  870. exit;
  871. { Can't use array[boolean] because b can be >0 ! }
  872. if b then
  873. Write_Str(Len,t,'TRUE')
  874. else
  875. Write_Str(Len,t,'FALSE');
  876. End;
  877. Procedure fpc_Write_Text_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  878. Begin
  879. If (InOutRes<>0) then
  880. exit;
  881. { Can't use array[boolean] because b can be >0 ! }
  882. { default value? }
  883. If Len=-1 then
  884. Len:=5;
  885. if b then
  886. Write_Str_Iso(Len,t,'true')
  887. else
  888. Write_Str_Iso(Len,t,'false');
  889. End;
  890. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  891. Begin
  892. If (InOutRes<>0) then
  893. exit;
  894. if (TextRec(t).mode<>fmOutput) Then
  895. begin
  896. if TextRec(t).mode=fmClosed then
  897. InOutRes:=103
  898. else
  899. InOutRes:=105;
  900. exit;
  901. end;
  902. If Len>1 Then
  903. fpc_WriteBlanks(t,Len-1);
  904. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  905. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  906. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  907. Inc(TextRec(t).BufPos);
  908. End;
  909. Procedure fpc_Write_Text_Char_Iso(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  910. Begin
  911. If (InOutRes<>0) then
  912. exit;
  913. if (TextRec(t).mode<>fmOutput) Then
  914. begin
  915. if TextRec(t).mode=fmClosed then
  916. InOutRes:=103
  917. else
  918. InOutRes:=105;
  919. exit;
  920. end;
  921. { default value? }
  922. If Len=-1 then
  923. Len:=1;
  924. If Len>1 Then
  925. fpc_WriteBlanks(t,Len-1)
  926. else If Len<1 Then
  927. exit;
  928. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  929. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  930. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  931. Inc(TextRec(t).BufPos);
  932. End;
  933. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  934. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  935. var
  936. a : ansistring;
  937. Begin
  938. If (InOutRes<>0) then
  939. exit;
  940. if (TextRec(t).mode<>fmOutput) Then
  941. begin
  942. if TextRec(t).mode=fmClosed then
  943. InOutRes:=103
  944. else
  945. InOutRes:=105;
  946. exit;
  947. end;
  948. If Len>1 Then
  949. fpc_WriteBlanks(t,Len-1);
  950. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  951. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  952. { a widechar can be translated into more than a single ansichar }
  953. a:=c;
  954. fpc_WriteBuffer(t,pchar(a)^,length(a));
  955. End;
  956. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  957. {*****************************************************************************
  958. Read(Ln)
  959. *****************************************************************************}
  960. Function NextChar(var f:Text;var s:string):Boolean;
  961. begin
  962. NextChar:=false;
  963. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  964. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  965. begin
  966. if length(s)<high(s) then
  967. begin
  968. inc(s[0]);
  969. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  970. end;
  971. Inc(TextRec(f).BufPos);
  972. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  973. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  974. NextChar:=true;
  975. end;
  976. end;
  977. Function IgnoreSpaces(var f:Text):Boolean;
  978. {
  979. Removes all leading spaces,tab,eols from the input buffer, returns true if
  980. the buffer is empty
  981. }
  982. var
  983. s : string;
  984. begin
  985. s:='';
  986. IgnoreSpaces:=false;
  987. { Return false when already at EOF }
  988. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  989. exit;
  990. (* Check performed separately to avoid accessing memory outside buffer *)
  991. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  992. exit;
  993. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  994. begin
  995. if not NextChar(f,s) then
  996. exit;
  997. { EOF? }
  998. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  999. break;
  1000. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1001. break;
  1002. end;
  1003. IgnoreSpaces:=true;
  1004. end;
  1005. procedure ReadNumeric(var f:Text;var s:string);
  1006. {
  1007. Read numeric input, if buffer is empty then return True
  1008. }
  1009. begin
  1010. repeat
  1011. if not NextChar(f,s) then
  1012. exit;
  1013. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  1014. end;
  1015. function CheckRead(var f:Text):Boolean;
  1016. begin
  1017. CheckRead:=False;
  1018. { Check error and if file is open and load buf if empty }
  1019. If (InOutRes<>0) then
  1020. exit;
  1021. if (TextRec(f).mode<>fmInput) Then
  1022. begin
  1023. case TextRec(f).mode of
  1024. fmOutPut,fmAppend:
  1025. InOutRes:=104;
  1026. else
  1027. InOutRes:=103;
  1028. end;
  1029. exit;
  1030. end;
  1031. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1032. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1033. CheckRead:=True;
  1034. end;
  1035. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  1036. begin
  1037. if TextRec(f).FlushFunc<>nil then
  1038. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1039. end;
  1040. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  1041. var prev: char;
  1042. Begin
  1043. If not CheckRead(f) then
  1044. exit;
  1045. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1046. { Flush if set }
  1047. begin
  1048. if (TextRec(f).FlushFunc<>nil) then
  1049. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1050. exit;
  1051. end;
  1052. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1053. Exit;
  1054. repeat
  1055. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1056. inc(TextRec(f).BufPos);
  1057. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1058. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1059. if prev = #10 then
  1060. exit;
  1061. {$ifdef MACOS}
  1062. if prev = #13 then
  1063. {StdInput on macos never have dos line ending, so this is safe.}
  1064. if TextRec(f).Handle = StdInputHandle then
  1065. exit;
  1066. {$endif MACOS}
  1067. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1068. begin
  1069. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1070. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1071. { Flush if set }
  1072. begin
  1073. if (TextRec(f).FlushFunc<>nil) then
  1074. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1075. exit;
  1076. end;
  1077. end;
  1078. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1079. Exit;
  1080. if (prev=#13) then
  1081. { is there also a #10 after it? }
  1082. begin
  1083. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1084. { yes, skip that one as well }
  1085. inc(TextRec(f).BufPos);
  1086. exit;
  1087. end;
  1088. until false;
  1089. End;
  1090. Procedure fpc_ReadLn_End_Iso(var f : Text);[Public,Alias:'FPC_READLN_END_ISO']; iocheck; compilerproc;
  1091. var prev: char;
  1092. Begin
  1093. If not CheckRead(f) then
  1094. exit;
  1095. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1096. { Flush if set }
  1097. begin
  1098. if (TextRec(f).FlushFunc<>nil) then
  1099. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1100. exit;
  1101. end;
  1102. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1103. begin
  1104. inc(TextRec(f).BufPos);
  1105. Exit;
  1106. end;
  1107. repeat
  1108. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1109. inc(TextRec(f).BufPos);
  1110. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1111. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1112. if prev = #10 then
  1113. exit;
  1114. {$ifdef MACOS}
  1115. if prev = #13 then
  1116. {StdInput on macos never have dos line ending, so this is safe.}
  1117. if TextRec(f).Handle = StdInputHandle then
  1118. exit;
  1119. {$endif MACOS}
  1120. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1121. begin
  1122. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1123. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1124. { Flush if set }
  1125. begin
  1126. if (TextRec(f).FlushFunc<>nil) then
  1127. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1128. exit;
  1129. end;
  1130. end;
  1131. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1132. begin
  1133. inc(TextRec(f).BufPos);
  1134. Exit;
  1135. end;
  1136. if (prev=#13) then
  1137. { is there also a #10 after it? }
  1138. begin
  1139. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1140. { yes, skip that one as well }
  1141. inc(TextRec(f).BufPos);
  1142. exit;
  1143. end;
  1144. until false;
  1145. End;
  1146. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  1147. var
  1148. sPos,len : Longint;
  1149. p,startp,maxp : pchar;
  1150. end_of_string:boolean;
  1151. Begin
  1152. ReadPCharLen:=0;
  1153. If not CheckRead(f) then
  1154. exit;
  1155. { Read maximal until Maxlen is reached }
  1156. sPos:=0;
  1157. end_of_string:=false;
  1158. repeat
  1159. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1160. begin
  1161. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1162. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1163. break;
  1164. end;
  1165. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1166. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  1167. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  1168. else
  1169. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1170. startp:=p;
  1171. { find stop character }
  1172. while p<maxp do
  1173. begin
  1174. { Optimization: Do a quick check for a control character first }
  1175. if (p^<' ') then
  1176. begin
  1177. if (p^ in [#10,#13]) or
  1178. (ctrlZmarkseof and (p^=#26)) then
  1179. begin
  1180. end_of_string:=true;
  1181. break;
  1182. end;
  1183. end;
  1184. inc(p);
  1185. end;
  1186. { calculate read bytes }
  1187. len:=p-startp;
  1188. inc(TextRec(f).BufPos,Len);
  1189. Move(startp^,s[sPos],Len);
  1190. inc(sPos,Len);
  1191. until (spos=MaxLen) or end_of_string;
  1192. ReadPCharLen:=spos;
  1193. End;
  1194. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  1195. Begin
  1196. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  1197. End;
  1198. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  1199. Begin
  1200. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  1201. End;
  1202. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  1203. var
  1204. len: longint;
  1205. Begin
  1206. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  1207. if zerobased and
  1208. (len > high(s)) then
  1209. len := high(s);
  1210. if (len <= high(s)) then
  1211. s[len] := #0;
  1212. End;
  1213. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1214. Procedure fpc_Read_Text_AnsiStr(var f : Text;out s : AnsiString); iocheck; compilerproc;
  1215. var
  1216. slen,len : SizeInt;
  1217. Begin
  1218. slen:=0;
  1219. Repeat
  1220. // SetLength will reallocate the length.
  1221. SetLength(S,slen+255);
  1222. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  1223. inc(slen,len);
  1224. Until len<255;
  1225. // Set actual length
  1226. SetLength(S,Slen);
  1227. End;
  1228. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1229. procedure fpc_Read_Text_Char(var f : Text; out c: char); iocheck;compilerproc;
  1230. Begin
  1231. c:=#0;
  1232. If not CheckRead(f) then
  1233. exit;
  1234. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1235. begin
  1236. c := #26;
  1237. exit;
  1238. end;
  1239. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1240. inc(TextRec(f).BufPos);
  1241. end;
  1242. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: char); iocheck;compilerproc;
  1243. Begin
  1244. c:=' ';
  1245. If not CheckRead(f) then
  1246. exit;
  1247. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1248. begin
  1249. c:=' ';
  1250. exit;
  1251. end;
  1252. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1253. inc(TextRec(f).BufPos);
  1254. if c=#13 then
  1255. begin
  1256. c:=' ';
  1257. If not CheckRead(f) or
  1258. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1259. exit;
  1260. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1261. inc(TextRec(f).BufPos);
  1262. { ignore #26 following a new line }
  1263. If not CheckRead(f) or
  1264. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1265. exit;
  1266. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1267. inc(TextRec(f).BufPos);
  1268. end
  1269. else if c=#10 then
  1270. begin
  1271. c:=' ';
  1272. { ignore #26 following a new line }
  1273. If not CheckRead(f) or
  1274. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1275. exit;
  1276. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1277. inc(TextRec(f).BufPos);
  1278. end
  1279. else if c=#26 then
  1280. c:=' ';
  1281. end;
  1282. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1283. var
  1284. hs : String;
  1285. code : longint;
  1286. Begin
  1287. l:=0;
  1288. If not CheckRead(f) then
  1289. exit;
  1290. hs:='';
  1291. if IgnoreSpaces(f) then
  1292. begin
  1293. { When spaces were found and we are now at EOF,
  1294. then we return 0 }
  1295. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1296. exit;
  1297. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1298. exit;
  1299. ReadNumeric(f,hs);
  1300. end;
  1301. if (hs = '') then
  1302. L := 0
  1303. else
  1304. begin
  1305. Val(hs,l,code);
  1306. if Code <> 0 then
  1307. InOutRes:=106;
  1308. end;
  1309. End;
  1310. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1311. var
  1312. hs : String;
  1313. code : longint;
  1314. Begin
  1315. u:=0;
  1316. If not CheckRead(f) then
  1317. exit;
  1318. hs:='';
  1319. if IgnoreSpaces(f) then
  1320. begin
  1321. { When spaces were found and we are now at EOF,
  1322. then we return 0 }
  1323. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1324. exit;
  1325. ReadNumeric(f,hs);
  1326. end;
  1327. if (hs = '') then
  1328. u := 0
  1329. else
  1330. begin
  1331. val(hs,u,code);
  1332. If code<>0 Then
  1333. InOutRes:=106;
  1334. end;
  1335. End;
  1336. {$ifndef FPUNONE}
  1337. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1338. var
  1339. hs : string;
  1340. code : Word;
  1341. begin
  1342. v:=0.0;
  1343. If not CheckRead(f) then
  1344. exit;
  1345. hs:='';
  1346. if IgnoreSpaces(f) then
  1347. begin
  1348. { When spaces were found and we are now at EOF,
  1349. then we return 0 }
  1350. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1351. exit;
  1352. ReadNumeric(f,hs);
  1353. end;
  1354. val(hs,v,code);
  1355. If code<>0 Then
  1356. InOutRes:=106;
  1357. end;
  1358. {$endif}
  1359. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1360. var s:string;
  1361. code:valsint;
  1362. begin
  1363. if not checkread(t) then
  1364. exit;
  1365. s:='';
  1366. if ignorespaces(t) then
  1367. begin
  1368. { When spaces were found and we are now at EOF, then we return 0 }
  1369. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1370. exit;
  1371. ReadNumeric(t,s);
  1372. end;
  1373. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1374. if code<>0 then
  1375. InOutRes:=106;
  1376. end;
  1377. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1378. var
  1379. hs : string;
  1380. code : Word;
  1381. begin
  1382. {$ifdef FPUNONE}
  1383. v:=0;
  1384. {$else}
  1385. v:=0.0;
  1386. {$endif}
  1387. If not CheckRead(f) then
  1388. exit;
  1389. hs:='';
  1390. if IgnoreSpaces(f) then
  1391. begin
  1392. { When spaces were found and we are now at EOF,
  1393. then we return 0 }
  1394. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1395. exit;
  1396. ReadNumeric(f,hs);
  1397. end;
  1398. val(hs,v,code);
  1399. If code<>0 Then
  1400. InOutRes:=106;
  1401. end;
  1402. {$ifndef cpu64}
  1403. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1404. var
  1405. hs : String;
  1406. code : longint;
  1407. Begin
  1408. q:=0;
  1409. If not CheckRead(f) then
  1410. exit;
  1411. hs:='';
  1412. if IgnoreSpaces(f) then
  1413. begin
  1414. { When spaces were found and we are now at EOF,
  1415. then we return 0 }
  1416. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1417. exit;
  1418. ReadNumeric(f,hs);
  1419. end;
  1420. val(hs,q,code);
  1421. If code<>0 Then
  1422. InOutRes:=106;
  1423. End;
  1424. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1425. var
  1426. hs : String;
  1427. code : Longint;
  1428. Begin
  1429. i:=0;
  1430. If not CheckRead(f) then
  1431. exit;
  1432. hs:='';
  1433. if IgnoreSpaces(f) then
  1434. begin
  1435. { When spaces were found and we are now at EOF,
  1436. then we return 0 }
  1437. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1438. exit;
  1439. ReadNumeric(f,hs);
  1440. end;
  1441. Val(hs,i,code);
  1442. If code<>0 Then
  1443. InOutRes:=106;
  1444. End;
  1445. {$endif CPU64}
  1446. {*****************************************************************************
  1447. WriteStr/ReadStr
  1448. *****************************************************************************}
  1449. const
  1450. StrPtrIndex = 1;
  1451. { leave space for 128 bit string pointers :) (used for writestr) }
  1452. ShortStrLenIndex = 17;
  1453. { how many bytes of the string have been processed already (used for readstr) }
  1454. BytesReadIndex = 17;
  1455. threadvar
  1456. ReadWriteStrText: textrec;
  1457. procedure WriteStrShort(var t: textrec);
  1458. var
  1459. str: pshortstring;
  1460. newbytes,
  1461. oldlen: longint;
  1462. begin
  1463. if (t.bufpos=0) then
  1464. exit;
  1465. str:=pshortstring(ppointer(@t.userdata[StrPtrIndex])^);
  1466. newbytes:=t.BufPos;
  1467. oldlen:=length(str^);
  1468. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1469. begin
  1470. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1471. {$ifdef writestr_iolencheck}
  1472. // GPC only gives an io error if {$no-truncate-strings} is active
  1473. // FPC does not have this setting (it never gives errors when a
  1474. // a string expression is truncated)
  1475. { "disk full" }
  1476. inoutres:=101;
  1477. {$endif}
  1478. end;
  1479. setlength(str^,length(str^)+newbytes);
  1480. move(t.bufptr^,str^[oldlen+1],newbytes);
  1481. t.bufpos:=0;
  1482. end;
  1483. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1484. procedure WriteStrAnsi(var t: textrec);
  1485. var
  1486. str: pansistring;
  1487. oldlen: longint;
  1488. begin
  1489. if (t.bufpos=0) then
  1490. exit;
  1491. str:=pansistring(ppointer(@t.userdata[StrPtrIndex])^);
  1492. oldlen:=length(str^);
  1493. setlength(str^,oldlen+t.bufpos);
  1494. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1495. t.bufpos:=0;
  1496. end;
  1497. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1498. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1499. procedure WriteStrWide(var t: textrec);
  1500. var
  1501. temp: ansistring;
  1502. str: pwidestring;
  1503. begin
  1504. if (t.bufpos=0) then
  1505. exit;
  1506. str:=pwidestring(ppointer(@t.userdata[StrPtrIndex])^);
  1507. setlength(temp,t.bufpos);
  1508. move(t.bufptr^,temp[1],t.bufpos);
  1509. str^:=str^+temp;
  1510. t.bufpos:=0;
  1511. end;
  1512. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1513. procedure SetupWriteStrCommon(out t: textrec);
  1514. begin
  1515. // initialise
  1516. Assign(text(t),'');
  1517. t.mode:=fmOutput;
  1518. t.OpenFunc:=nil;
  1519. t.CloseFunc:=nil;
  1520. end;
  1521. function fpc_SetupWriteStr_Shortstr(out s: shortstring): PText; compilerproc;
  1522. begin
  1523. setupwritestrcommon(ReadWriteStrText);
  1524. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1525. ReadWriteStrText.userdata[ShortStrLenIndex]:=high(s);
  1526. setlength(s,0);
  1527. ReadWriteStrText.InOutFunc:=@WriteStrShort;
  1528. ReadWriteStrText.FlushFunc:=@WriteStrShort;
  1529. result:=@ReadWriteStrText;
  1530. end;
  1531. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1532. function fpc_SetupWriteStr_Ansistr(out s: ansistring): PText; compilerproc;
  1533. begin
  1534. setupwritestrcommon(ReadWriteStrText);
  1535. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1536. // automatically done by out-semantics
  1537. // setlength(s,0);
  1538. ReadWriteStrText.InOutFunc:=@WriteStrAnsi;
  1539. ReadWriteStrText.FlushFunc:=@WriteStrAnsi;
  1540. result:=@ReadWriteStrText;
  1541. end;
  1542. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1543. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1544. function fpc_SetupWriteStr_Widestr(out s: widestring): PText; compilerproc;
  1545. begin
  1546. setupwritestrcommon(ReadWriteStrText);
  1547. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1548. // automatically done by out-semantics
  1549. // setlength(s,0);
  1550. ReadWriteStrText.InOutFunc:=@WriteStrWide;
  1551. ReadWriteStrText.FlushFunc:=@WriteStrWide;
  1552. result:=@ReadWriteStrText;
  1553. end;
  1554. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1555. procedure ReadAnsiStrFinal(var t: textrec);
  1556. begin
  1557. { finalise the temp ansistring }
  1558. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1559. end;
  1560. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1561. var
  1562. newbytes: sizeint;
  1563. begin
  1564. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1565. if (t.BufSize <= newbytes) then
  1566. newbytes := t.BufSize;
  1567. if (newbytes > 0) then
  1568. begin
  1569. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1570. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1571. end;
  1572. t.BufEnd:=newbytes;
  1573. t.BufPos:=0;
  1574. end;
  1575. procedure ReadStrAnsi(var t: textrec);
  1576. var
  1577. str: pansistring;
  1578. begin
  1579. str:=pansistring(@t.userdata[StrPtrIndex]);
  1580. ReadStrCommon(t,@str^[1],length(str^));
  1581. end;
  1582. procedure SetupReadStrCommon(out t: textrec);
  1583. begin
  1584. // initialise
  1585. Assign(text(t),'');
  1586. t.mode:=fmInput;
  1587. t.OpenFunc:=nil;
  1588. t.CloseFunc:=nil;
  1589. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1590. end;
  1591. function fpc_SetupReadStr_Ansistr(const s: ansistring): PText; [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1592. begin
  1593. setupreadstrcommon(ReadWriteStrText);
  1594. { we need a reference, because 's' may be a temporary expression }
  1595. PAnsiString(@ReadWriteStrText.userdata[StrPtrIndex])^:=s;
  1596. ReadWriteStrText.InOutFunc:=@ReadStrAnsi;
  1597. { this is called at the end, by fpc_read_end }
  1598. ReadWriteStrText.FlushFunc:=@ReadAnsiStrFinal;
  1599. result:=@ReadWriteStrText;
  1600. end;
  1601. function fpc_SetupReadStr_Ansistr_Intern(const s: ansistring): PText; [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1602. function fpc_SetupReadStr_Shortstr(const s: shortstring): PText; compilerproc;
  1603. begin
  1604. { the reason we convert the short string to ansistring, is because the semantics of
  1605. readstr are defined as:
  1606. *********************
  1607. Apart from the restrictions imposed by requirements given in this clause,
  1608. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  1609. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  1610. char-type (or a subrange of char-type), the integer-type (or a subrange of
  1611. integer-type), the real-type, a fixed-string-type, or a
  1612. variable-string-type, shall be equivalent to
  1613. begin
  1614. rewrite(f);
  1615. writeln(f, e);
  1616. reset(f);
  1617. read(f, v 1 ,...,v n )
  1618. end
  1619. *********************
  1620. This means that any side effects caused by the evaluation of v 1 .. v n
  1621. must not affect the value of e (= our argument s) -> we need a copy of it.
  1622. An ansistring is the easiest way to get a threadsafe copy, and allows us
  1623. to use the other ansistring readstr helpers too.
  1624. }
  1625. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1626. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1627. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  1628. runerror(217);
  1629. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1630. end;
  1631. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1632. function fpc_SetupReadStr_Widestr(const s: widestring): PText; compilerproc;
  1633. begin
  1634. { we use an ansistring to avoid code duplication, and let the }
  1635. { assignment convert the widestring to an equivalent ansistring }
  1636. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1637. end;
  1638. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1639. {*****************************************************************************
  1640. Initializing
  1641. *****************************************************************************}
  1642. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1643. begin
  1644. Assign(f,'');
  1645. TextRec(f).Handle:=hdl;
  1646. TextRec(f).Mode:=mode;
  1647. TextRec(f).Closefunc:=@FileCloseFunc;
  1648. case mode of
  1649. fmInput :
  1650. TextRec(f).InOutFunc:=@FileReadFunc;
  1651. fmOutput :
  1652. begin
  1653. TextRec(f).InOutFunc:=@FileWriteFunc;
  1654. if Do_Isdevice(hdl) then
  1655. TextRec(f).FlushFunc:=@FileWriteFunc;
  1656. end;
  1657. else
  1658. HandleError(102);
  1659. end;
  1660. end;