text.inc 59 KB

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