text.inc 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  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. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  100. { Only close functions not connected to stdout.}
  101. If ((TextRec(t).Handle<>StdInputHandle) and
  102. (TextRec(t).Handle<>StdOutputHandle) and
  103. (TextRec(t).Handle<>StdErrorHandle)) Then
  104. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  105. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  106. TextRec(t).mode := fmClosed;
  107. { Reset buffer for safety }
  108. TextRec(t).BufPos:=0;
  109. TextRec(t).BufEnd:=0;
  110. End
  111. else inOutRes := 103;
  112. End;
  113. End;
  114. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  115. Begin
  116. Case TextRec(t).mode Of {This gives the fastest code}
  117. fmInput,fmOutput,fmInOut : Close(t);
  118. fmClosed : ;
  119. else
  120. Begin
  121. InOutRes:=102;
  122. exit;
  123. End;
  124. End;
  125. TextRec(t).mode:=mode;
  126. TextRec(t).bufpos:=0;
  127. TextRec(t).bufend:=0;
  128. {$ifdef FPC_HAS_CPSTRING}
  129. { if no codepage is yet assigned then assign default ansi codepage }
  130. if TextRec(t).CodePage=CP_ACP then
  131. TextRec(t).CodePage:=DefaultSystemCodePage;
  132. {$endif}
  133. FileFunc(TextRec(t).OpenFunc)(TextRec(t));
  134. { reset the mode to closed when an error has occured }
  135. if InOutRes<>0 then
  136. TextRec(t).mode:=fmClosed;
  137. End;
  138. Procedure Rewrite(var t : Text);[IOCheck];
  139. Begin
  140. If InOutRes<>0 then
  141. exit;
  142. OpenText(t,fmOutput,1);
  143. End;
  144. Procedure Reset(var t : Text);[IOCheck];
  145. Begin
  146. If InOutRes<>0 then
  147. exit;
  148. OpenText(t,fmInput,0);
  149. End;
  150. Procedure Append(var t : Text);[IOCheck];
  151. Begin
  152. If InOutRes<>0 then
  153. exit;
  154. OpenText(t,fmAppend,1);
  155. End;
  156. Procedure Flush(var t : Text);[IOCheck];
  157. Begin
  158. If InOutRes<>0 then
  159. exit;
  160. if TextRec(t).mode<>fmOutput then
  161. begin
  162. if TextRec(t).mode=fmInput then
  163. InOutRes:=105
  164. else
  165. InOutRes:=103;
  166. exit;
  167. end;
  168. { Not the flushfunc but the inoutfunc should be used, because that
  169. writes the data, flushfunc doesn't need to be assigned }
  170. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  171. End;
  172. Procedure Erase(var t:Text);[IOCheck];
  173. Begin
  174. If InOutRes <> 0 then
  175. exit;
  176. If TextRec(t).mode=fmClosed Then
  177. Do_Erase(PChar(@TextRec(t).Name));
  178. End;
  179. Procedure Rename(var t : text;p:pchar);[IOCheck];
  180. Begin
  181. If InOutRes <> 0 then
  182. exit;
  183. If TextRec(t).mode=fmClosed Then
  184. Begin
  185. Do_Rename(PChar(@TextRec(t).Name),p);
  186. { check error code of do_rename }
  187. If InOutRes = 0 then
  188. Move(p^,TextRec(t).Name,StrLen(p)+1);
  189. End;
  190. End;
  191. Procedure Rename(var t : Text;const s : string);[IOCheck];
  192. var
  193. p : array[0..255] Of Char;
  194. Begin
  195. If InOutRes <> 0 then
  196. exit;
  197. Move(s[1],p,Length(s));
  198. p[Length(s)]:=#0;
  199. Rename(t,Pchar(@p));
  200. End;
  201. Procedure Rename(var t : Text;c : char);[IOCheck];
  202. var
  203. p : array[0..1] Of Char;
  204. Begin
  205. If InOutRes <> 0 then
  206. exit;
  207. p[0]:=c;
  208. p[1]:=#0;
  209. Rename(t,Pchar(@p));
  210. End;
  211. Function Eof(Var t: Text): Boolean;[IOCheck];
  212. Begin
  213. If (InOutRes<>0) then
  214. exit(true);
  215. if (TextRec(t).mode<>fmInput) Then
  216. begin
  217. if TextRec(t).mode=fmOutput then
  218. InOutRes:=104
  219. else
  220. InOutRes:=103;
  221. exit(true);
  222. end;
  223. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  224. begin
  225. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  226. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  227. exit(true);
  228. end;
  229. Eof:=CtrlZMarksEOF and (TextRec(t).Bufptr^[TextRec(t).BufPos]=#26);
  230. end;
  231. Function Eof:Boolean;
  232. Begin
  233. Eof:=Eof(Input);
  234. End;
  235. Function SeekEof (Var t : Text) : Boolean;
  236. var
  237. oldfilepos : Int64;
  238. oldbufpos, oldbufend : SizeInt;
  239. reads: longint;
  240. isdevice: boolean;
  241. Begin
  242. If (InOutRes<>0) then
  243. exit(true);
  244. if (TextRec(t).mode<>fmInput) Then
  245. begin
  246. if TextRec(t).mode=fmOutPut then
  247. InOutRes:=104
  248. else
  249. InOutRes:=103;
  250. exit(true);
  251. end;
  252. { try to save the current position in the file, seekeof() should not move }
  253. { the current file position (JM) }
  254. oldbufpos := TextRec(t).BufPos;
  255. oldbufend := TextRec(t).BufEnd;
  256. reads := 0;
  257. oldfilepos := -1;
  258. isdevice := Do_IsDevice(TextRec(t).handle);
  259. repeat
  260. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  261. begin
  262. { signal that the we will have to do a seek }
  263. inc(reads);
  264. if not isdevice and
  265. (reads = 1) then
  266. begin
  267. oldfilepos := Do_FilePos(TextRec(t).handle) - TextRec(t).BufEnd;
  268. InOutRes:=0;
  269. end;
  270. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  271. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  272. begin
  273. { if we only did a read in which we didn't read anything, the }
  274. { old buffer is still valid and we can simply restore the }
  275. { pointers (JM) }
  276. dec(reads);
  277. SeekEof := true;
  278. break;
  279. end;
  280. end;
  281. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  282. #26 :
  283. if CtrlZMarksEOF then
  284. begin
  285. SeekEof := true;
  286. break;
  287. end;
  288. #10,#13,#9,' ' :
  289. ;
  290. else
  291. begin
  292. SeekEof := false;
  293. break;
  294. end;
  295. end;
  296. inc(TextRec(t).BufPos);
  297. until false;
  298. { restore file position if not working with a device }
  299. if not isdevice then
  300. { if we didn't modify the buffer, simply restore the BufPos and BufEnd }
  301. { (the latter because it's now probably set to zero because nothing was }
  302. { was read anymore) }
  303. if (reads = 0) then
  304. begin
  305. TextRec(t).BufPos:=oldbufpos;
  306. TextRec(t).BufEnd:=oldbufend;
  307. end
  308. { otherwise return to the old filepos and reset the buffer }
  309. else
  310. begin
  311. do_seek(TextRec(t).handle,oldfilepos);
  312. InOutRes:=0;
  313. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  314. TextRec(t).BufPos:=oldbufpos;
  315. end;
  316. End;
  317. Function SeekEof : Boolean;
  318. Begin
  319. SeekEof:=SeekEof(Input);
  320. End;
  321. Function Eoln(var t:Text) : Boolean;
  322. Begin
  323. If (InOutRes<>0) then
  324. exit(true);
  325. if (TextRec(t).mode<>fmInput) Then
  326. begin
  327. if TextRec(t).mode=fmOutPut then
  328. InOutRes:=104
  329. else
  330. InOutRes:=103;
  331. exit(true);
  332. end;
  333. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  334. begin
  335. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  336. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  337. exit(true);
  338. end;
  339. if CtrlZMarksEOF and (TextRec (T).BufPtr^[TextRec (T).BufPos] = #26) then
  340. exit (true);
  341. Eoln:=(TextRec(t).Bufptr^[TextRec(t).BufPos] in [#10,#13]);
  342. End;
  343. Function Eoln : Boolean;
  344. Begin
  345. Eoln:=Eoln(Input);
  346. End;
  347. Function SeekEoln (Var t : Text) : Boolean;
  348. Begin
  349. If (InOutRes<>0) then
  350. exit(true);
  351. if (TextRec(t).mode<>fmInput) Then
  352. begin
  353. if TextRec(t).mode=fmOutput then
  354. InOutRes:=104
  355. else
  356. InOutRes:=103;
  357. exit(true);
  358. end;
  359. repeat
  360. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  361. begin
  362. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  363. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  364. exit(true);
  365. end;
  366. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  367. #26: if CtrlZMarksEOF then
  368. exit (true);
  369. #10,#13 : exit(true);
  370. #9,' ' : ;
  371. else
  372. exit(false);
  373. end;
  374. inc(TextRec(t).BufPos);
  375. until false;
  376. End;
  377. Function SeekEoln : Boolean;
  378. Begin
  379. SeekEoln:=SeekEoln(Input);
  380. End;
  381. Procedure SetTextBuf(Var F : Text; Var Buf; Size : SizeInt);
  382. Begin
  383. TextRec(f).BufPtr:=@Buf;
  384. TextRec(f).BufSize:=Size;
  385. TextRec(f).BufPos:=0;
  386. TextRec(f).BufEnd:=0;
  387. End;
  388. Procedure SetTextLineEnding(Var f:Text; Ending:string);
  389. Begin
  390. TextRec(F).LineEnd:=Ending;
  391. End;
  392. function GetTextCodePage(var T: Text): TSystemCodePage;
  393. begin
  394. {$ifdef FPC_HAS_CPSTRING}
  395. GetTextCodePage:=TextRec(T).CodePage;
  396. {$else}
  397. GetTextCodePage:=0;
  398. {$endif}
  399. end;
  400. procedure SetTextCodePage(var T: Text; CodePage: TSystemCodePage);
  401. begin
  402. {$ifdef FPC_HAS_CPSTRING}
  403. TextRec(T).CodePage:=CodePage;
  404. {$endif}
  405. end;
  406. Function fpc_get_input:PText;compilerproc;
  407. begin
  408. fpc_get_input:=@Input;
  409. end;
  410. Function fpc_get_output:PText;compilerproc;
  411. begin
  412. fpc_get_output:=@Output;
  413. end;
  414. {*****************************************************************************
  415. Write(Ln)
  416. *****************************************************************************}
  417. Procedure fpc_WriteBuffer(var f:Text;const b;len:SizeInt);
  418. var
  419. p : pchar;
  420. left,
  421. idx : SizeInt;
  422. begin
  423. p:=pchar(@b);
  424. idx:=0;
  425. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  426. while len>left do
  427. begin
  428. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],left);
  429. dec(len,left);
  430. inc(idx,left);
  431. inc(TextRec(f).BufPos,left);
  432. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  433. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  434. end;
  435. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],len);
  436. inc(TextRec(f).BufPos,len);
  437. end;
  438. Procedure fpc_WriteBlanks(var f:Text;len:longint);
  439. var
  440. left : longint;
  441. begin
  442. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  443. while len>left do
  444. begin
  445. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],left,' ');
  446. dec(len,left);
  447. inc(TextRec(f).BufPos,left);
  448. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  449. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  450. end;
  451. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],len,' ');
  452. inc(TextRec(f).BufPos,len);
  453. end;
  454. Procedure fpc_Write_End(var f:Text); iocheck; compilerproc;
  455. begin
  456. if TextRec(f).FlushFunc<>nil then
  457. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  458. end;
  459. Procedure fpc_Writeln_End(var f:Text); iocheck; compilerproc;
  460. begin
  461. If InOutRes <> 0 then exit;
  462. case TextRec(f).mode of
  463. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  464. begin
  465. { Write EOL }
  466. fpc_WriteBuffer(f,TextRec(f).LineEnd[1],length(TextRec(f).LineEnd));
  467. { Flush }
  468. if TextRec(f).FlushFunc<>nil then
  469. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  470. end;
  471. fmInput: InOutRes:=105
  472. else InOutRes:=103;
  473. end;
  474. end;
  475. Procedure fpc_Write_Text_ShortStr(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR']; compilerproc;
  476. Begin
  477. If (InOutRes<>0) then
  478. exit;
  479. case TextRec(f).mode of
  480. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  481. begin
  482. If Len>Length(s) Then
  483. fpc_WriteBlanks(f,Len-Length(s));
  484. fpc_WriteBuffer(f,s[1],Length(s));
  485. end;
  486. fmInput: InOutRes:=105
  487. else InOutRes:=103;
  488. end;
  489. End;
  490. Procedure fpc_Write_Text_ShortStr_Iso(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR_ISO']; compilerproc;
  491. Begin
  492. If (InOutRes<>0) then
  493. exit;
  494. case TextRec(f).mode of
  495. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  496. begin
  497. { default value? }
  498. If Len=-1 then
  499. Len:=length(s);
  500. If Len>Length(s) Then
  501. begin
  502. fpc_WriteBlanks(f,Len-Length(s));
  503. fpc_WriteBuffer(f,s[1],Length(s));
  504. end
  505. else
  506. fpc_WriteBuffer(f,s[1],Len);
  507. end;
  508. fmInput: InOutRes:=105
  509. else InOutRes:=103;
  510. end;
  511. End;
  512. { provide local access to write_str }
  513. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  514. { provide local access to write_str_iso }
  515. procedure Write_Str_Iso(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR_ISO'];
  516. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  517. var
  518. ArrayLen : longint;
  519. p : pchar;
  520. Begin
  521. If (InOutRes<>0) then
  522. exit;
  523. case TextRec(f).mode of
  524. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  525. begin
  526. p:=pchar(@s);
  527. if zerobased then
  528. begin
  529. { can't use StrLen, since that one could try to read past the end }
  530. { of the heap (JM) }
  531. ArrayLen:=IndexByte(p^,high(s)+1,0);
  532. { IndexByte returns -1 if not found (JM) }
  533. if ArrayLen = -1 then
  534. ArrayLen := high(s)+1;
  535. end
  536. else
  537. ArrayLen := high(s)+1;
  538. If Len>ArrayLen Then
  539. fpc_WriteBlanks(f,Len-ArrayLen);
  540. fpc_WriteBuffer(f,p^,ArrayLen);
  541. end;
  542. fmInput: InOutRes:=105
  543. else InOutRes:=103;
  544. end;
  545. End;
  546. Procedure fpc_Write_Text_Pchar_as_Array_Iso(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  547. var
  548. ArrayLen : longint;
  549. p : pchar;
  550. Begin
  551. If (InOutRes<>0) then
  552. exit;
  553. case TextRec(f).mode of
  554. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  555. begin
  556. p:=pchar(@s);
  557. if zerobased then
  558. begin
  559. { can't use StrLen, since that one could try to read past the end }
  560. { of the heap (JM) }
  561. ArrayLen:=IndexByte(p^,high(s)+1,0);
  562. { IndexByte returns -1 if not found (JM) }
  563. if ArrayLen = -1 then
  564. ArrayLen := high(s)+1;
  565. end
  566. else
  567. ArrayLen := high(s)+1;
  568. { default value? }
  569. If Len=-1 then
  570. Len:=ArrayLen;
  571. If Len>ArrayLen Then
  572. begin
  573. fpc_WriteBlanks(f,Len-ArrayLen);
  574. fpc_WriteBuffer(f,p^,ArrayLen);
  575. end
  576. else
  577. fpc_WriteBuffer(f,p^,Len);
  578. end;
  579. fmInput: InOutRes:=105
  580. else InOutRes:=103;
  581. end;
  582. End;
  583. Procedure fpc_Write_Text_PChar_As_Pointer(Len : Longint;var f : Text;p : PChar); iocheck; compilerproc;
  584. var
  585. PCharLen : longint;
  586. Begin
  587. If (p=nil) or (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. PCharLen:=StrLen(p);
  593. If Len>PCharLen Then
  594. fpc_WriteBlanks(f,Len-PCharLen);
  595. fpc_WriteBuffer(f,p^,PCharLen);
  596. end;
  597. fmInput: InOutRes:=105
  598. else InOutRes:=103;
  599. end;
  600. End;
  601. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; const S : RawByteString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; compilerproc;
  602. {
  603. Writes a AnsiString to the Text file T
  604. }
  605. var
  606. SLen: longint;
  607. a: RawByteString;
  608. begin
  609. If (InOutRes<>0) then
  610. exit;
  611. case TextRec(f).mode of
  612. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  613. begin
  614. SLen:=Length(s);
  615. If Len>SLen Then
  616. fpc_WriteBlanks(f,Len-SLen);
  617. if SLen > 0 then
  618. begin
  619. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  620. if TextRec(f).CodePage<>StringCodePage(S) then
  621. begin
  622. a:=fpc_AnsiStr_To_AnsiStr(S,TextRec(f).CodePage);
  623. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  624. end
  625. else
  626. {$endif}
  627. fpc_WriteBuffer(f,PAnsiChar(s)^,SLen);
  628. end;
  629. end;
  630. fmInput: InOutRes:=105
  631. else InOutRes:=103;
  632. end;
  633. end;
  634. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  635. Procedure fpc_Write_Text_UnicodeStr (Len : Longint; Var f : Text; const S : UnicodeString); iocheck; compilerproc;
  636. {
  637. Writes a UnicodeString to the Text file T
  638. }
  639. var
  640. SLen: longint;
  641. a: RawByteString;
  642. begin
  643. If (pointer(S)=nil) or (InOutRes<>0) then
  644. exit;
  645. case TextRec(f).mode of
  646. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  647. begin
  648. SLen:=Length(s);
  649. If Len>SLen Then
  650. fpc_WriteBlanks(f,Len-SLen);
  651. {$ifdef FPC_HAS_CPSTRING}
  652. WideStringManager.Unicode2AnsiMoveProc(PUnicodeChar(S),a,TextRec(f).CodePage,SLen);
  653. {$else}
  654. a:=s;
  655. {$endif FPC_HAS_CPSTRING}
  656. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  657. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  658. end;
  659. fmInput: InOutRes:=105
  660. else InOutRes:=103;
  661. end;
  662. end;
  663. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  664. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  665. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  666. {
  667. Writes a WideString to the Text file T
  668. }
  669. var
  670. SLen: longint;
  671. a: RawByteString;
  672. begin
  673. If (pointer(S)=nil) or (InOutRes<>0) then
  674. exit;
  675. case TextRec(f).mode of
  676. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  677. begin
  678. SLen:=Length(s);
  679. If Len>SLen Then
  680. fpc_WriteBlanks(f,Len-SLen);
  681. {$ifdef FPC_HAS_CPSTRING}
  682. widestringmanager.Wide2AnsiMoveProc(PWideChar(s), a, TextRec(f).CodePage, SLen);
  683. {$else}
  684. a:=s;
  685. {$endif}
  686. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  687. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  688. end;
  689. fmInput: InOutRes:=105
  690. else InOutRes:=103;
  691. end;
  692. end;
  693. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  694. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  695. var
  696. s : String;
  697. Begin
  698. If (InOutRes<>0) then
  699. exit;
  700. Str(l,s);
  701. Write_Str(Len,t,s);
  702. End;
  703. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  704. var
  705. s : String;
  706. Begin
  707. If (InOutRes<>0) then
  708. exit;
  709. Str(L,s);
  710. Write_Str(Len,t,s);
  711. End;
  712. Procedure fpc_Write_Text_SInt_Iso(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  713. var
  714. s : String;
  715. Begin
  716. If (InOutRes<>0) then
  717. exit;
  718. Str(l,s);
  719. { default value? }
  720. if len=-1 then
  721. len:=11
  722. else if len<length(s) then
  723. len:=length(s);
  724. Write_Str_Iso(Len,t,s);
  725. End;
  726. Procedure fpc_Write_Text_UInt_Iso(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  727. var
  728. s : String;
  729. Begin
  730. If (InOutRes<>0) then
  731. exit;
  732. Str(L,s);
  733. { default value? }
  734. if len=-1 then
  735. len:=11
  736. else if len<length(s) then
  737. len:=length(s);
  738. Write_Str_Iso(Len,t,s);
  739. End;
  740. {$ifndef CPU64}
  741. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; compilerproc;
  742. var
  743. s : string;
  744. begin
  745. if (InOutRes<>0) then
  746. exit;
  747. str(q,s);
  748. write_str(len,t,s);
  749. end;
  750. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; compilerproc;
  751. var
  752. s : string;
  753. begin
  754. if (InOutRes<>0) then
  755. exit;
  756. str(i,s);
  757. write_str(len,t,s);
  758. end;
  759. procedure fpc_write_text_qword_iso(len : longint;var t : text;q : qword); iocheck; compilerproc;
  760. var
  761. s : string;
  762. begin
  763. if (InOutRes<>0) then
  764. exit;
  765. str(q,s);
  766. { default value? }
  767. if len=-1 then
  768. len:=20
  769. else if len<length(s) then
  770. len:=length(s);
  771. write_str_iso(len,t,s);
  772. end;
  773. procedure fpc_write_text_int64_iso(len : longint;var t : text;i : int64); iocheck; compilerproc;
  774. var
  775. s : string;
  776. begin
  777. if (InOutRes<>0) then
  778. exit;
  779. str(i,s);
  780. { default value? }
  781. if len=-1 then
  782. len:=20
  783. else if len<length(s) then
  784. len:=length(s);
  785. write_str_iso(len,t,s);
  786. end;
  787. {$endif CPU64}
  788. {$if defined(CPU16) or defined(CPU8)}
  789. procedure fpc_write_text_longword(len : longint;var t : text;q : longword); iocheck; compilerproc;
  790. var
  791. s : string;
  792. begin
  793. if (InOutRes<>0) then
  794. exit;
  795. str(q,s);
  796. write_str(len,t,s);
  797. end;
  798. procedure fpc_write_text_longint(len : longint;var t : text;i : longint); iocheck; compilerproc;
  799. var
  800. s : string;
  801. begin
  802. if (InOutRes<>0) then
  803. exit;
  804. str(i,s);
  805. write_str(len,t,s);
  806. end;
  807. procedure fpc_write_text_longword_iso(len : longint;var t : text;q : longword); iocheck; compilerproc;
  808. var
  809. s : string;
  810. begin
  811. if (InOutRes<>0) then
  812. exit;
  813. str(q,s);
  814. { default value? }
  815. if len=-1 then
  816. len:=11
  817. else if len<length(s) then
  818. len:=length(s);
  819. write_str_iso(len,t,s);
  820. end;
  821. procedure fpc_write_text_longint_iso(len : longint;var t : text;i : longint); iocheck; compilerproc;
  822. var
  823. s : string;
  824. begin
  825. if (InOutRes<>0) then
  826. exit;
  827. str(i,s);
  828. { default value? }
  829. if len=-1 then
  830. len:=11
  831. else if len<length(s) then
  832. len:=length(s);
  833. write_str_iso(len,t,s);
  834. end;
  835. {$endif CPU16 or CPU8}
  836. {$ifndef FPUNONE}
  837. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  838. var
  839. s : String;
  840. Begin
  841. If (InOutRes<>0) then
  842. exit;
  843. Str_real(Len,fixkomma,r,treal_type(rt),s);
  844. Write_Str(Len,t,s);
  845. End;
  846. Procedure fpc_Write_Text_Float_iso(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  847. var
  848. s : String;
  849. Begin
  850. If (InOutRes<>0) then
  851. exit;
  852. Str_real_iso(Len,fixkomma,r,treal_type(rt),s);
  853. Write_Str(Len,t,s);
  854. End;
  855. {$endif}
  856. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; compilerproc;
  857. var
  858. s:string;
  859. begin
  860. if textrec(t).mode<>fmoutput then
  861. begin
  862. if textrec(t).mode=fminput then
  863. inoutres:=105
  864. else
  865. inoutres:=103;
  866. exit;
  867. end;
  868. inoutres := fpc_shortstr_enum_intern(ordinal, len, typinfo, ord2strindex, s);
  869. if (inoutres <> 0) then
  870. exit;
  871. fpc_writeBuffer(t,s[1],length(s));
  872. end;
  873. {$ifdef FPC_HAS_STR_CURRENCY}
  874. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; compilerproc;
  875. var
  876. s : String;
  877. Begin
  878. If (InOutRes<>0) then
  879. exit;
  880. str(c:Len:fixkomma,s);
  881. Write_Str(Len,t,s);
  882. End;
  883. {$endif FPC_HAS_STR_CURRENCY}
  884. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  885. Begin
  886. If (InOutRes<>0) then
  887. exit;
  888. { Can't use array[boolean] because b can be >0 ! }
  889. if b then
  890. Write_Str(Len,t,'TRUE')
  891. else
  892. Write_Str(Len,t,'FALSE');
  893. End;
  894. Procedure fpc_Write_Text_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  895. Begin
  896. If (InOutRes<>0) then
  897. exit;
  898. { Can't use array[boolean] because b can be >0 ! }
  899. { default value? }
  900. If Len=-1 then
  901. Len:=5;
  902. if b then
  903. Write_Str_Iso(Len,t,'true')
  904. else
  905. Write_Str_Iso(Len,t,'false');
  906. End;
  907. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  908. Begin
  909. If (InOutRes<>0) then
  910. exit;
  911. if (TextRec(t).mode<>fmOutput) Then
  912. begin
  913. if TextRec(t).mode=fmClosed then
  914. InOutRes:=103
  915. else
  916. InOutRes:=105;
  917. exit;
  918. end;
  919. If Len>1 Then
  920. fpc_WriteBlanks(t,Len-1);
  921. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  922. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  923. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  924. Inc(TextRec(t).BufPos);
  925. End;
  926. Procedure fpc_Write_Text_Char_Iso(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  927. Begin
  928. If (InOutRes<>0) then
  929. exit;
  930. if (TextRec(t).mode<>fmOutput) Then
  931. begin
  932. if TextRec(t).mode=fmClosed then
  933. InOutRes:=103
  934. else
  935. InOutRes:=105;
  936. exit;
  937. end;
  938. { default value? }
  939. If Len=-1 then
  940. Len:=1;
  941. If Len>1 Then
  942. fpc_WriteBlanks(t,Len-1)
  943. else If Len<1 Then
  944. exit;
  945. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  946. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  947. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  948. Inc(TextRec(t).BufPos);
  949. End;
  950. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  951. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  952. var
  953. a: RawByteString;
  954. Begin
  955. If (InOutRes<>0) then
  956. exit;
  957. if (TextRec(t).mode<>fmOutput) Then
  958. begin
  959. if TextRec(t).mode=fmClosed then
  960. InOutRes:=103
  961. else
  962. InOutRes:=105;
  963. exit;
  964. end;
  965. If Len>1 Then
  966. fpc_WriteBlanks(t,Len-1);
  967. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  968. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  969. { a widechar can be translated into more than a single ansichar }
  970. {$ifdef FPC_HAS_CPSTRING}
  971. widestringmanager.Wide2AnsiMoveProc(@c,a,TextRec(t).CodePage,1);
  972. {$else}
  973. a:=c;
  974. {$endif}
  975. fpc_WriteBuffer(t,PAnsiChar(a)^,Length(a));
  976. End;
  977. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  978. {*****************************************************************************
  979. Read(Ln)
  980. *****************************************************************************}
  981. Function NextChar(var f:Text;var s:string):Boolean;
  982. begin
  983. NextChar:=false;
  984. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  985. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  986. begin
  987. if length(s)<high(s) then
  988. begin
  989. inc(s[0]);
  990. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  991. end;
  992. Inc(TextRec(f).BufPos);
  993. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  994. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  995. NextChar:=true;
  996. end;
  997. end;
  998. Function IgnoreSpaces(var f:Text):Boolean;
  999. {
  1000. Removes all leading spaces,tab,eols from the input buffer, returns true if
  1001. the buffer is empty
  1002. }
  1003. var
  1004. s : string;
  1005. begin
  1006. s:='';
  1007. IgnoreSpaces:=false;
  1008. { Return false when already at EOF }
  1009. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1010. exit;
  1011. (* Check performed separately to avoid accessing memory outside buffer *)
  1012. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1013. exit;
  1014. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  1015. begin
  1016. if not NextChar(f,s) then
  1017. exit;
  1018. { EOF? }
  1019. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1020. break;
  1021. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1022. break;
  1023. end;
  1024. IgnoreSpaces:=true;
  1025. end;
  1026. procedure ReadNumeric(var f:Text;var s:string);
  1027. {
  1028. Read numeric input, if buffer is empty then return True
  1029. }
  1030. begin
  1031. repeat
  1032. if not NextChar(f,s) then
  1033. exit;
  1034. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  1035. end;
  1036. function CheckRead(var f:Text):Boolean;
  1037. begin
  1038. CheckRead:=False;
  1039. { Check error and if file is open and load buf if empty }
  1040. If (InOutRes<>0) then
  1041. exit;
  1042. if (TextRec(f).mode<>fmInput) Then
  1043. begin
  1044. case TextRec(f).mode of
  1045. fmOutPut,fmAppend:
  1046. InOutRes:=104;
  1047. else
  1048. InOutRes:=103;
  1049. end;
  1050. exit;
  1051. end;
  1052. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1053. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1054. CheckRead:=True;
  1055. end;
  1056. procedure ReadInteger(var f:Text;var s:string);
  1057. {
  1058. Ignore leading blanks (incl. EOF) and return the first characters matching
  1059. an integer in the format recognized by the Val procedure:
  1060. [+-]?[0-9]+
  1061. or [+-]?(0x|0X|x|X)[0-9A-Za-z]+
  1062. or [+-]?&[0-7]+
  1063. or [+-]?%[0-1]+
  1064. A partial match may be returned, e.g.: '' or '+' or '0x'.
  1065. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1066. standard function in ISO mode.
  1067. }
  1068. var
  1069. Base: Integer;
  1070. begin
  1071. s := '';
  1072. with TextRec(f) do begin
  1073. if not CheckRead(f) then Exit;
  1074. IgnoreSpaces(f);
  1075. if BufPos >= BufEnd then Exit;
  1076. if BufPtr^[BufPos] in ['+','-'] then
  1077. NextChar(f,s);
  1078. Base := 10;
  1079. if BufPos >= BufEnd then Exit;
  1080. if BufPtr^[BufPos] in ['$','x','X','%','&'] then
  1081. begin
  1082. case BufPtr^[BufPos] of
  1083. '$','x','X': Base := 16;
  1084. '%': Base := 2;
  1085. '&': Base := 8;
  1086. end;
  1087. NextChar(f,s);
  1088. end else if BufPtr^[BufPos] = '0' then
  1089. begin
  1090. NextChar(f,s);
  1091. if BufPos >= BufEnd then Exit;
  1092. if BufPtr^[BufPos] in ['x','X'] then
  1093. begin
  1094. Base := 16;
  1095. NextChar(f,s);
  1096. end;
  1097. end;
  1098. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1099. if (((Base = 2) and (BufPtr^[BufPos] in ['0'..'1']))
  1100. or ((Base = 8) and (BufPtr^[BufPos] in ['0'..'7']))
  1101. or ((Base = 10) and (BufPtr^[BufPos] in ['0'..'9']))
  1102. or ((Base = 16) and (BufPtr^[BufPos] in ['0'..'9','a'..'f','A'..'F']))) then
  1103. NextChar(f,s)
  1104. else Exit;
  1105. end;
  1106. end;
  1107. procedure ReadReal(var f:Text;var s:string);
  1108. {
  1109. Ignore leading blanks (incl. EOF) and return the first characters matching
  1110. a float number in the format recognized by the Val procedure:
  1111. [+-]?([0-9]+)?\.[0-9]+([eE][+-]?[0-9]+)?
  1112. or [+-]?[0-9]+\.([0-9]+)?([eE][+-]?[0-9]+)?
  1113. A partial match may be returned, e.g.: '' or '+' or '.' or '1e' or even '+.'.
  1114. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1115. standard function in ISO mode.
  1116. }
  1117. var digit: Boolean;
  1118. begin
  1119. s := '';
  1120. with TextRec(f) do begin
  1121. if not CheckRead(f) then Exit;
  1122. IgnoreSpaces(f);
  1123. if BufPos >= BufEnd then Exit;
  1124. if BufPtr^[BufPos] in ['+','-'] then
  1125. NextChar(f,s);
  1126. digit := false;
  1127. if BufPos >= BufEnd then Exit;
  1128. if BufPtr^[BufPos] in ['0'..'9'] then
  1129. begin
  1130. digit := true;
  1131. repeat
  1132. NextChar(f,s);
  1133. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1134. until not (BufPtr^[BufPos] in ['0'..'9']);
  1135. end;
  1136. if BufPtr^[BufPos] = '.' then
  1137. begin
  1138. NextChar(f,s);
  1139. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1140. if BufPtr^[BufPos] in ['0'..'9'] then
  1141. begin
  1142. digit := true;
  1143. repeat
  1144. NextChar(f,s);
  1145. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1146. until not (BufPtr^[BufPos] in ['0'..'9']);
  1147. end;
  1148. end;
  1149. {at least one digit is required on the left of the exponent}
  1150. if digit and (BufPtr^[BufPos] in ['e','E']) then
  1151. begin
  1152. NextChar(f,s);
  1153. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1154. if BufPtr^[BufPos] in ['+','-'] then
  1155. NextChar(f,s);
  1156. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1157. if BufPtr^[BufPos] in ['0'..'9'] then
  1158. NextChar(f,s)
  1159. else break;
  1160. end;
  1161. end;
  1162. end;
  1163. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  1164. begin
  1165. if TextRec(f).FlushFunc<>nil then
  1166. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1167. end;
  1168. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  1169. var prev: char;
  1170. Begin
  1171. If not CheckRead(f) then
  1172. exit;
  1173. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1174. { Flush if set }
  1175. begin
  1176. if (TextRec(f).FlushFunc<>nil) then
  1177. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1178. exit;
  1179. end;
  1180. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1181. Exit;
  1182. repeat
  1183. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1184. inc(TextRec(f).BufPos);
  1185. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1186. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1187. if prev = #10 then
  1188. exit;
  1189. {$ifdef MACOS}
  1190. if prev = #13 then
  1191. {StdInput on macos never have dos line ending, so this is safe.}
  1192. if TextRec(f).Handle = StdInputHandle then
  1193. exit;
  1194. {$endif MACOS}
  1195. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1196. begin
  1197. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1198. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1199. { Flush if set }
  1200. begin
  1201. if (TextRec(f).FlushFunc<>nil) then
  1202. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1203. exit;
  1204. end;
  1205. end;
  1206. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1207. Exit;
  1208. if (prev=#13) then
  1209. { is there also a #10 after it? }
  1210. begin
  1211. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1212. { yes, skip that one as well }
  1213. inc(TextRec(f).BufPos);
  1214. exit;
  1215. end;
  1216. until false;
  1217. End;
  1218. Procedure fpc_ReadLn_End_Iso(var f : Text);[Public,Alias:'FPC_READLN_END_ISO']; iocheck; compilerproc;
  1219. var prev: char;
  1220. Begin
  1221. If not CheckRead(f) then
  1222. exit;
  1223. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1224. { Flush if set }
  1225. begin
  1226. if (TextRec(f).FlushFunc<>nil) then
  1227. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1228. exit;
  1229. end;
  1230. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1231. begin
  1232. inc(TextRec(f).BufPos);
  1233. Exit;
  1234. end;
  1235. repeat
  1236. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1237. inc(TextRec(f).BufPos);
  1238. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1239. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1240. if prev = #10 then
  1241. exit;
  1242. {$ifdef MACOS}
  1243. if prev = #13 then
  1244. {StdInput on macos never have dos line ending, so this is safe.}
  1245. if TextRec(f).Handle = StdInputHandle then
  1246. exit;
  1247. {$endif MACOS}
  1248. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1249. begin
  1250. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1251. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1252. { Flush if set }
  1253. begin
  1254. if (TextRec(f).FlushFunc<>nil) then
  1255. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1256. exit;
  1257. end;
  1258. end;
  1259. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1260. begin
  1261. inc(TextRec(f).BufPos);
  1262. Exit;
  1263. end;
  1264. if (prev=#13) then
  1265. { is there also a #10 after it? }
  1266. begin
  1267. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1268. { yes, skip that one as well }
  1269. inc(TextRec(f).BufPos);
  1270. exit;
  1271. end;
  1272. until false;
  1273. End;
  1274. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  1275. var
  1276. sPos,len : Longint;
  1277. p,startp,maxp : pchar;
  1278. end_of_string:boolean;
  1279. Begin
  1280. ReadPCharLen:=0;
  1281. If not CheckRead(f) then
  1282. exit;
  1283. { Read maximal until Maxlen is reached }
  1284. sPos:=0;
  1285. end_of_string:=false;
  1286. repeat
  1287. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1288. begin
  1289. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1290. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1291. break;
  1292. end;
  1293. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1294. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  1295. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  1296. else
  1297. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1298. startp:=p;
  1299. { find stop character }
  1300. while p<maxp do
  1301. begin
  1302. { Optimization: Do a quick check for a control character first }
  1303. if (p^<' ') then
  1304. begin
  1305. if (p^ in [#10,#13]) or
  1306. (ctrlZmarkseof and (p^=#26)) then
  1307. begin
  1308. end_of_string:=true;
  1309. break;
  1310. end;
  1311. end;
  1312. inc(p);
  1313. end;
  1314. { calculate read bytes }
  1315. len:=p-startp;
  1316. inc(TextRec(f).BufPos,Len);
  1317. Move(startp^,s[sPos],Len);
  1318. inc(sPos,Len);
  1319. until (spos=MaxLen) or end_of_string;
  1320. ReadPCharLen:=spos;
  1321. End;
  1322. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  1323. Begin
  1324. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  1325. End;
  1326. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  1327. Begin
  1328. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  1329. End;
  1330. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  1331. var
  1332. len: longint;
  1333. Begin
  1334. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  1335. if zerobased and
  1336. (len > high(s)) then
  1337. len := high(s);
  1338. if (len <= high(s)) then
  1339. s[len] := #0;
  1340. End;
  1341. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1342. Procedure fpc_Read_Text_AnsiStr(var f : Text;out s : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); [public, alias: 'FPC_READ_TEXT_ANSISTR']; iocheck; compilerproc;
  1343. var
  1344. slen,len : SizeInt;
  1345. Begin
  1346. slen:=0;
  1347. Repeat
  1348. // SetLength will reallocate the length.
  1349. SetLength(s,slen+255);
  1350. len:=ReadPCharLen(f,pchar(Pointer(s)+slen),255);
  1351. inc(slen,len);
  1352. Until len<255;
  1353. // Set actual length
  1354. SetLength(s,Slen);
  1355. {$ifdef FPC_HAS_CPSTRING}
  1356. SetCodePage(s,TextRec(f).CodePage,false);
  1357. if cp<>TextRec(f).CodePage then
  1358. s:=fpc_AnsiStr_To_AnsiStr(s,cp);
  1359. {$endif FPC_HAS_CPSTRING}
  1360. End;
  1361. Procedure fpc_Read_Text_AnsiStr_Intern(var f : Text;out s : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); [external name 'FPC_READ_TEXT_ANSISTR'];
  1362. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1363. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1364. Procedure fpc_Read_Text_UnicodeStr(var f : Text;out us : UnicodeString); iocheck; compilerproc;
  1365. var
  1366. s: RawByteString;
  1367. Begin
  1368. // all standard input is assumed to be ansi-encoded
  1369. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1370. // Convert to unicodestring
  1371. {$ifdef FPC_HAS_CPSTRING}
  1372. widestringmanager.Ansi2UnicodeMoveProc(PAnsiChar(s),StringCodePage(s),us,Length(s));
  1373. {$else}
  1374. us:=s;
  1375. {$endif}
  1376. End;
  1377. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1378. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1379. Procedure fpc_Read_Text_WideStr(var f : Text;out ws : WideString); iocheck; compilerproc;
  1380. var
  1381. s: RawByteString;
  1382. Begin
  1383. // all standard input is assumed to be ansi-encoded
  1384. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1385. // Convert to widestring
  1386. {$ifdef FPC_HAS_CPSTRING}
  1387. widestringmanager.Ansi2WideMoveProc(PAnsiChar(s),StringCodePage(s),ws,Length(s));
  1388. {$else}
  1389. ws:=s;
  1390. {$endif}
  1391. End;
  1392. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1393. procedure fpc_Read_Text_Char(var f : Text; out c: char); [public, alias: 'FPC_READ_TEXT_CHAR']; iocheck; compilerproc;
  1394. Begin
  1395. c:=#0;
  1396. If not CheckRead(f) then
  1397. exit;
  1398. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1399. begin
  1400. c := #26;
  1401. exit;
  1402. end;
  1403. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1404. inc(TextRec(f).BufPos);
  1405. end;
  1406. procedure fpc_Read_Text_Char_intern(var f : Text; out c: char); iocheck; [external name 'FPC_READ_TEXT_CHAR'];
  1407. function fpc_GetBuf(var f : Text) : pchar; iocheck; compilerproc;
  1408. Begin
  1409. Result:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1410. If not CheckRead(f) then
  1411. exit;
  1412. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1413. exit;
  1414. Result:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1415. end;
  1416. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1417. procedure fpc_Read_Text_WideChar(var f : Text; out wc: widechar); iocheck;compilerproc;
  1418. var
  1419. ws: widestring;
  1420. i: longint;
  1421. { maximum code point length is 6 characters (with UTF-8) }
  1422. str: array[0..5] of char;
  1423. Begin
  1424. fillchar(str[0],sizeof(str),0);
  1425. for i:=low(str) to high(str) do
  1426. begin
  1427. fpc_Read_Text_Char_intern(f,str[i]);
  1428. case widestringmanager.CodePointLengthProc(@str[0],i+1) of
  1429. -1: { possibly incomplete code point, try with an extra character }
  1430. ;
  1431. 0: { null character }
  1432. begin
  1433. wc:=#0;
  1434. exit;
  1435. end;
  1436. else
  1437. begin
  1438. { valid code point -> convert to widestring}
  1439. {$ifdef FPC_HAS_CPSTRING}
  1440. widestringmanager.Ansi2WideMoveProc(@str[0],TextRec(f).CodePage,ws,i+1);
  1441. {$else}
  1442. widestringmanager.Ansi2WideMoveProc(@str[0],DefaultSystemCodePage,ws,i+1);
  1443. {$endif}
  1444. { has to be exactly one widechar }
  1445. if length(ws)=1 then
  1446. begin
  1447. wc:=ws[1];
  1448. exit
  1449. end
  1450. else
  1451. break;
  1452. end;
  1453. end;
  1454. end;
  1455. { invalid widechar input }
  1456. inoutres:=106;
  1457. end;
  1458. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1459. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: char); iocheck;compilerproc;
  1460. Begin
  1461. c:=' ';
  1462. If not CheckRead(f) then
  1463. exit;
  1464. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1465. begin
  1466. c:=' ';
  1467. exit;
  1468. end;
  1469. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1470. inc(TextRec(f).BufPos);
  1471. if c=#13 then
  1472. begin
  1473. c:=' ';
  1474. If not CheckRead(f) or
  1475. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1476. exit;
  1477. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1478. inc(TextRec(f).BufPos);
  1479. { ignore #26 following a new line }
  1480. If not CheckRead(f) or
  1481. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1482. exit;
  1483. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1484. inc(TextRec(f).BufPos);
  1485. end
  1486. else if c=#10 then
  1487. begin
  1488. c:=' ';
  1489. { ignore #26 following a new line }
  1490. If not CheckRead(f) or
  1491. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1492. exit;
  1493. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1494. inc(TextRec(f).BufPos);
  1495. end
  1496. else if c=#26 then
  1497. c:=' ';
  1498. end;
  1499. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1500. var
  1501. hs : String;
  1502. code : ValSInt;
  1503. Begin
  1504. l:=0;
  1505. If not CheckRead(f) then
  1506. exit;
  1507. hs:='';
  1508. if IgnoreSpaces(f) then
  1509. begin
  1510. { When spaces were found and we are now at EOF,
  1511. then we return 0 }
  1512. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1513. exit;
  1514. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1515. exit;
  1516. ReadNumeric(f,hs);
  1517. end;
  1518. if (hs = '') then
  1519. L := 0
  1520. else
  1521. begin
  1522. Val(hs,l,code);
  1523. if Code <> 0 then
  1524. InOutRes:=106;
  1525. end;
  1526. End;
  1527. Procedure fpc_Read_Text_SInt_Iso(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1528. var
  1529. hs : String;
  1530. code : ValSInt;
  1531. Begin
  1532. ReadInteger(f,hs);
  1533. Val(hs,l,code);
  1534. if Code <> 0 then
  1535. InOutRes:=106;
  1536. End;
  1537. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1538. var
  1539. hs : String;
  1540. code : ValSInt;
  1541. Begin
  1542. u:=0;
  1543. If not CheckRead(f) then
  1544. exit;
  1545. hs:='';
  1546. if IgnoreSpaces(f) then
  1547. begin
  1548. { When spaces were found and we are now at EOF,
  1549. then we return 0 }
  1550. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1551. exit;
  1552. ReadNumeric(f,hs);
  1553. end;
  1554. if (hs = '') then
  1555. u := 0
  1556. else
  1557. begin
  1558. val(hs,u,code);
  1559. If code<>0 Then
  1560. InOutRes:=106;
  1561. end;
  1562. End;
  1563. Procedure fpc_Read_Text_UInt_Iso(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1564. var
  1565. hs : String;
  1566. code : ValSInt;
  1567. Begin
  1568. ReadInteger(f,hs);
  1569. Val(hs,u,code);
  1570. If code<>0 Then
  1571. InOutRes:=106;
  1572. End;
  1573. {$ifndef FPUNONE}
  1574. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1575. var
  1576. hs : string;
  1577. code : Word;
  1578. begin
  1579. v:=0.0;
  1580. If not CheckRead(f) then
  1581. exit;
  1582. hs:='';
  1583. if IgnoreSpaces(f) then
  1584. begin
  1585. { When spaces were found and we are now at EOF,
  1586. then we return 0 }
  1587. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1588. exit;
  1589. ReadNumeric(f,hs);
  1590. end;
  1591. val(hs,v,code);
  1592. If code<>0 Then
  1593. InOutRes:=106;
  1594. end;
  1595. procedure fpc_Read_Text_Float_Iso(var f : Text; out v : ValReal); iocheck; compilerproc;
  1596. var
  1597. hs : string;
  1598. code : Word;
  1599. begin
  1600. ReadReal(f,hs);
  1601. Val(hs,v,code);
  1602. If code<>0 Then
  1603. InOutRes:=106;
  1604. end;
  1605. {$endif}
  1606. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1607. var s:string;
  1608. code:valsint;
  1609. begin
  1610. if not checkread(t) then
  1611. exit;
  1612. s:='';
  1613. if ignorespaces(t) then
  1614. begin
  1615. { When spaces were found and we are now at EOF, then we return 0 }
  1616. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1617. exit;
  1618. ReadNumeric(t,s);
  1619. end;
  1620. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1621. if code<>0 then
  1622. InOutRes:=106;
  1623. end;
  1624. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1625. var
  1626. hs : string;
  1627. code : ValSInt;
  1628. begin
  1629. {$ifdef FPUNONE}
  1630. v:=0;
  1631. {$else}
  1632. v:=0.0;
  1633. {$endif}
  1634. If not CheckRead(f) then
  1635. exit;
  1636. hs:='';
  1637. if IgnoreSpaces(f) then
  1638. begin
  1639. { When spaces were found and we are now at EOF,
  1640. then we return 0 }
  1641. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1642. exit;
  1643. ReadNumeric(f,hs);
  1644. end;
  1645. val(hs,v,code);
  1646. If code<>0 Then
  1647. InOutRes:=106;
  1648. end;
  1649. procedure fpc_Read_Text_Currency_Iso(var f : Text; out v : Currency); iocheck; compilerproc;
  1650. var
  1651. hs : string;
  1652. code : ValSInt;
  1653. begin
  1654. ReadReal(f,hs);
  1655. Val(hs,v,code);
  1656. If code<>0 Then
  1657. InOutRes:=106;
  1658. end;
  1659. {$ifndef cpu64}
  1660. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1661. var
  1662. hs : String;
  1663. code : longint;
  1664. Begin
  1665. q:=0;
  1666. If not CheckRead(f) then
  1667. exit;
  1668. hs:='';
  1669. if IgnoreSpaces(f) then
  1670. begin
  1671. { When spaces were found and we are now at EOF,
  1672. then we return 0 }
  1673. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1674. exit;
  1675. ReadNumeric(f,hs);
  1676. end;
  1677. val(hs,q,code);
  1678. If code<>0 Then
  1679. InOutRes:=106;
  1680. End;
  1681. procedure fpc_Read_Text_QWord_Iso(var f : text; out q : qword); iocheck; compilerproc;
  1682. var
  1683. hs : String;
  1684. code : longint;
  1685. Begin
  1686. ReadInteger(f,hs);
  1687. Val(hs,q,code);
  1688. If code<>0 Then
  1689. InOutRes:=106;
  1690. End;
  1691. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1692. var
  1693. hs : String;
  1694. code : Longint;
  1695. Begin
  1696. i:=0;
  1697. If not CheckRead(f) then
  1698. exit;
  1699. hs:='';
  1700. if IgnoreSpaces(f) then
  1701. begin
  1702. { When spaces were found and we are now at EOF,
  1703. then we return 0 }
  1704. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1705. exit;
  1706. ReadNumeric(f,hs);
  1707. end;
  1708. Val(hs,i,code);
  1709. If code<>0 Then
  1710. InOutRes:=106;
  1711. End;
  1712. procedure fpc_Read_Text_Int64_Iso(var f : text; out i : int64); iocheck; compilerproc;
  1713. var
  1714. hs : String;
  1715. code : Longint;
  1716. Begin
  1717. ReadInteger(f,hs);
  1718. Val(hs,i,code);
  1719. If code<>0 Then
  1720. InOutRes:=106;
  1721. End;
  1722. {$endif CPU64}
  1723. {$if defined(CPU16) or defined(CPU8)}
  1724. procedure fpc_Read_Text_LongWord(var f : text; out q : longword); iocheck; compilerproc;
  1725. var
  1726. hs : String;
  1727. code : longint;
  1728. Begin
  1729. q:=0;
  1730. If not CheckRead(f) then
  1731. exit;
  1732. hs:='';
  1733. if IgnoreSpaces(f) then
  1734. begin
  1735. { When spaces were found and we are now at EOF,
  1736. then we return 0 }
  1737. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1738. exit;
  1739. ReadNumeric(f,hs);
  1740. end;
  1741. val(hs,q,code);
  1742. If code<>0 Then
  1743. InOutRes:=106;
  1744. End;
  1745. procedure fpc_Read_Text_LongInt(var f : text; out i : longint); iocheck; compilerproc;
  1746. var
  1747. hs : String;
  1748. code : Longint;
  1749. Begin
  1750. i:=0;
  1751. If not CheckRead(f) then
  1752. exit;
  1753. hs:='';
  1754. if IgnoreSpaces(f) then
  1755. begin
  1756. { When spaces were found and we are now at EOF,
  1757. then we return 0 }
  1758. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1759. exit;
  1760. ReadNumeric(f,hs);
  1761. end;
  1762. Val(hs,i,code);
  1763. If code<>0 Then
  1764. InOutRes:=106;
  1765. End;
  1766. {$endif CPU16 or CPU8}
  1767. {*****************************************************************************
  1768. WriteStr/ReadStr
  1769. *****************************************************************************}
  1770. const
  1771. { pointer to target string }
  1772. StrPtrIndex = 1;
  1773. { temporary destination for writerstr, because the original value of the
  1774. destination may be used in the writestr expression }
  1775. TempWriteStrDestIndex = 9;
  1776. ShortStrLenIndex = 17;
  1777. { how many bytes of the string have been processed already (used for readstr) }
  1778. BytesReadIndex = 17;
  1779. procedure WriteStrShort(var t: textrec);
  1780. var
  1781. str: pshortstring;
  1782. newbytes,
  1783. oldlen: longint;
  1784. begin
  1785. if (t.bufpos=0) then
  1786. exit;
  1787. str:=pshortstring(ppointer(@t.userdata[TempWriteStrDestIndex])^);
  1788. newbytes:=t.BufPos;
  1789. oldlen:=length(str^);
  1790. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1791. begin
  1792. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1793. {$ifdef writestr_iolencheck}
  1794. // GPC only gives an io error if {$no-truncate-strings} is active
  1795. // FPC does not have this setting (it never gives errors when a
  1796. // a string expression is truncated)
  1797. { "disk full" }
  1798. inoutres:=101;
  1799. {$endif}
  1800. end;
  1801. setlength(str^,length(str^)+newbytes);
  1802. move(t.bufptr^,str^[oldlen+1],newbytes);
  1803. t.bufpos:=0;
  1804. end;
  1805. procedure WriteStrShortFlush(var t: textrec);
  1806. begin
  1807. { move written data from internal buffer to temporary string (don't move
  1808. directly from buffer to final string, because the temporary string may
  1809. already contain data in case the textbuf was smaller than the string
  1810. length) }
  1811. WriteStrShort(t);
  1812. { move written data to original string }
  1813. move(PPointer(@t.userdata[TempWriteStrDestIndex])^^,
  1814. PPointer(@t.userdata[StrPtrIndex])^^,
  1815. t.userdata[ShortStrLenIndex]+1);
  1816. { free temporary buffer }
  1817. freemem(PPointer(@t.userdata[TempWriteStrDestIndex])^);
  1818. end;
  1819. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1820. procedure WriteStrAnsi(var t: textrec);
  1821. var
  1822. str: pansistring;
  1823. oldlen: longint;
  1824. begin
  1825. if (t.bufpos=0) then
  1826. exit;
  1827. str:=pansistring(@t.userdata[TempWriteStrDestIndex]);
  1828. oldlen:=length(str^);
  1829. setlength(str^,oldlen+t.bufpos);
  1830. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1831. t.bufpos:=0;
  1832. end;
  1833. procedure WriteStrAnsiFlush(var t: textrec);
  1834. begin
  1835. { see comment in WriteStrShortFlush }
  1836. WriteStrAnsi(t);
  1837. pansistring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1838. pansistring(@t.userdata[TempWriteStrDestIndex])^;
  1839. { free memory/finalize temp }
  1840. pansistring(@t.userdata[TempWriteStrDestIndex])^:='';
  1841. end;
  1842. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1843. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1844. procedure WriteStrUnicode(var t: textrec);
  1845. var
  1846. temp: ansistring;
  1847. str: punicodestring;
  1848. begin
  1849. if (t.bufpos=0) then
  1850. exit;
  1851. str:=punicodestring(@t.userdata[TempWriteStrDestIndex]);
  1852. setlength(temp,t.bufpos);
  1853. move(t.bufptr^,temp[1],t.bufpos);
  1854. str^:=str^+temp;
  1855. t.bufpos:=0;
  1856. end;
  1857. procedure WriteStrUnicodeFlush(var t: textrec);
  1858. begin
  1859. { see comment in WriteStrShortFlush }
  1860. WriteStrUnicode(t);
  1861. punicodestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1862. punicodestring(@t.userdata[TempWriteStrDestIndex])^;
  1863. { free memory/finalize temp }
  1864. punicodestring(@t.userdata[TempWriteStrDestIndex])^:='';
  1865. end;
  1866. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1867. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1868. procedure WriteStrWide(var t: textrec);
  1869. var
  1870. temp: ansistring;
  1871. str: pwidestring;
  1872. begin
  1873. if (t.bufpos=0) then
  1874. exit;
  1875. str:=pwidestring(@t.userdata[TempWriteStrDestIndex]);
  1876. setlength(temp,t.bufpos);
  1877. move(t.bufptr^,temp[1],t.bufpos);
  1878. str^:=str^+temp;
  1879. t.bufpos:=0;
  1880. end;
  1881. procedure WriteStrWideFlush(var t: textrec);
  1882. begin
  1883. { see comment in WriteStrShortFlush }
  1884. WriteStrWide(t);
  1885. pwidestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1886. pwidestring(@t.userdata[TempWriteStrDestIndex])^;
  1887. { free memory/finalize temp }
  1888. finalize(pwidestring(@t.userdata[TempWriteStrDestIndex])^);
  1889. end;
  1890. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1891. procedure SetupWriteStrCommon(out t: textrec);
  1892. begin
  1893. // initialise
  1894. Assign(text(t),'');
  1895. t.mode:=fmOutput;
  1896. t.OpenFunc:=nil;
  1897. t.CloseFunc:=nil;
  1898. {$ifdef FPC_HAS_CPSTRING}
  1899. t.CodePage:=DefaultSystemCodePage;
  1900. {$endif}
  1901. end;
  1902. procedure fpc_SetupWriteStr_Shortstr(var ReadWriteStrText: text; var s: shortstring); compilerproc;
  1903. begin
  1904. setupwritestrcommon(TextRec(ReadWriteStrText));
  1905. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1906. { temporary destination (see comments for TempWriteStrDestIndex) }
  1907. getmem(PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^,high(s)+1);
  1908. setlength(pshortstring(ppointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^)^,0);
  1909. TextRec(ReadWriteStrText).userdata[ShortStrLenIndex]:=high(s);
  1910. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrShort;
  1911. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrShortFlush;
  1912. end;
  1913. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1914. procedure fpc_SetupWriteStr_Ansistr(var ReadWriteStrText: text; var s: ansistring); compilerproc;
  1915. begin
  1916. setupwritestrcommon(TextRec(ReadWriteStrText));
  1917. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1918. { temp destination ansistring, nil = empty string }
  1919. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  1920. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrAnsi;
  1921. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrAnsiFlush;
  1922. end;
  1923. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1924. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1925. procedure fpc_SetupWriteStr_Unicodestr(var ReadWriteStrText: text; var s: unicodestring); compilerproc;
  1926. begin
  1927. setupwritestrcommon(TextRec(ReadWriteStrText));
  1928. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1929. { temp destination unicodestring, nil = empty string }
  1930. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  1931. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrUnicode;
  1932. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrUnicodeFlush;
  1933. end;
  1934. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1935. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1936. procedure fpc_SetupWriteStr_Widestr(var ReadWriteStrText: text; var s: widestring); compilerproc;
  1937. begin
  1938. setupwritestrcommon(TextRec(ReadWriteStrText));
  1939. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1940. { temp destination widestring }
  1941. PWideString(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:='';
  1942. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrWide;
  1943. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrWideFlush;
  1944. end;
  1945. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1946. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1947. procedure ReadAnsiStrFinal(var t: textrec);
  1948. begin
  1949. { finalise the temp ansistring }
  1950. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1951. end;
  1952. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1953. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1954. var
  1955. newbytes: sizeint;
  1956. begin
  1957. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1958. if (t.BufSize <= newbytes) then
  1959. newbytes := t.BufSize;
  1960. if (newbytes > 0) then
  1961. begin
  1962. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1963. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1964. end;
  1965. t.BufEnd:=newbytes;
  1966. t.BufPos:=0;
  1967. end;
  1968. procedure ReadStrAnsi(var t: textrec);
  1969. var
  1970. str: pansistring;
  1971. begin
  1972. str:=pansistring(@t.userdata[StrPtrIndex]);
  1973. ReadStrCommon(t,@str^[1],length(str^));
  1974. end;
  1975. procedure SetupReadStrCommon(out t: textrec);
  1976. begin
  1977. // initialise
  1978. Assign(text(t),'');
  1979. t.mode:=fmInput;
  1980. t.OpenFunc:=nil;
  1981. t.CloseFunc:=nil;
  1982. {$ifdef FPC_HAS_CPSTRING}
  1983. t.CodePage:=DefaultSystemCodePage;
  1984. {$endif}
  1985. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1986. end;
  1987. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1988. procedure fpc_SetupReadStr_Ansistr(var ReadWriteStrText: text; const s: ansistring); [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1989. begin
  1990. setupreadstrcommon(TextRec(ReadWriteStrText));
  1991. { we need a reference, because 's' may be a temporary expression }
  1992. PAnsiString(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=s;
  1993. TextRec(ReadWriteStrText).InOutFunc:=@ReadStrAnsi;
  1994. { this is called at the end, by fpc_read_end }
  1995. TextRec(ReadWriteStrText).FlushFunc:=@ReadAnsiStrFinal;
  1996. end;
  1997. procedure fpc_SetupReadStr_Ansistr_Intern(var ReadWriteStrText: text; const s: ansistring); [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1998. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1999. procedure fpc_SetupReadStr_Shortstr(var ReadWriteStrText: text; const s: shortstring); compilerproc;
  2000. begin
  2001. { the reason we convert the short string to ansistring, is because the semantics of
  2002. readstr are defined as:
  2003. *********************
  2004. Apart from the restrictions imposed by requirements given in this clause,
  2005. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  2006. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  2007. char-type (or a subrange of char-type), the integer-type (or a subrange of
  2008. integer-type), the real-type, a fixed-string-type, or a
  2009. variable-string-type, shall be equivalent to
  2010. begin
  2011. rewrite(f);
  2012. writeln(f, e);
  2013. reset(f);
  2014. read(f, v 1 ,...,v n )
  2015. end
  2016. *********************
  2017. This means that any side effects caused by the evaluation of v 1 .. v n
  2018. must not affect the value of e (= our argument s) -> we need a copy of it.
  2019. An ansistring is the easiest way to get a threadsafe copy, and allows us
  2020. to use the other ansistring readstr helpers too.
  2021. }
  2022. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2023. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  2024. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  2025. runerror(217);
  2026. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2027. end;
  2028. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  2029. procedure fpc_SetupReadStr_Unicodestr(var ReadWriteStrText: text; const s: unicodestring); compilerproc;
  2030. begin
  2031. { we use an ansistring to avoid code duplication, and let the }
  2032. { assignment convert the widestring to an equivalent ansistring }
  2033. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  2034. end;
  2035. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  2036. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2037. procedure fpc_SetupReadStr_Widestr(var ReadWriteStrText: text; const s: widestring); compilerproc;
  2038. begin
  2039. { we use an ansistring to avoid code duplication, and let the }
  2040. { assignment convert the widestring to an equivalent ansistring }
  2041. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  2042. end;
  2043. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2044. {*****************************************************************************
  2045. Initializing
  2046. *****************************************************************************}
  2047. procedure OpenStdIO(var f:text;mode:longint;hdl:thandle);
  2048. begin
  2049. Assign(f,'');
  2050. TextRec(f).Handle:=hdl;
  2051. TextRec(f).Mode:=mode;
  2052. TextRec(f).Closefunc:=@FileCloseFunc;
  2053. case mode of
  2054. fmInput :
  2055. begin
  2056. TextRec(f).InOutFunc:=@FileReadFunc;
  2057. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  2058. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleInput);
  2059. {$endif}
  2060. end;
  2061. fmOutput :
  2062. begin
  2063. TextRec(f).InOutFunc:=@FileWriteFunc;
  2064. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  2065. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleOutput);
  2066. {$endif}
  2067. if Do_Isdevice(hdl) then
  2068. TextRec(f).FlushFunc:=@FileWriteFunc;
  2069. end;
  2070. else
  2071. HandleError(102);
  2072. end;
  2073. end;