text.inc 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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. {$ifndef FPUNONE}
  789. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  790. var
  791. s : String;
  792. Begin
  793. If (InOutRes<>0) then
  794. exit;
  795. Str_real(Len,fixkomma,r,treal_type(rt),s);
  796. Write_Str(Len,t,s);
  797. End;
  798. Procedure fpc_Write_Text_Float_iso(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  799. var
  800. s : String;
  801. Begin
  802. If (InOutRes<>0) then
  803. exit;
  804. Str_real_iso(Len,fixkomma,r,treal_type(rt),s);
  805. Write_Str(Len,t,s);
  806. End;
  807. {$endif}
  808. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; compilerproc;
  809. var
  810. s:string;
  811. begin
  812. if textrec(t).mode<>fmoutput then
  813. begin
  814. if textrec(t).mode=fminput then
  815. inoutres:=105
  816. else
  817. inoutres:=103;
  818. exit;
  819. end;
  820. inoutres := fpc_shortstr_enum_intern(ordinal, len, typinfo, ord2strindex, s);
  821. if (inoutres <> 0) then
  822. exit;
  823. fpc_writeBuffer(t,s[1],length(s));
  824. end;
  825. {$ifdef FPC_HAS_STR_CURRENCY}
  826. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; compilerproc;
  827. var
  828. s : String;
  829. Begin
  830. If (InOutRes<>0) then
  831. exit;
  832. str(c:Len:fixkomma,s);
  833. Write_Str(Len,t,s);
  834. End;
  835. {$endif FPC_HAS_STR_CURRENCY}
  836. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  837. Begin
  838. If (InOutRes<>0) then
  839. exit;
  840. { Can't use array[boolean] because b can be >0 ! }
  841. if b then
  842. Write_Str(Len,t,'TRUE')
  843. else
  844. Write_Str(Len,t,'FALSE');
  845. End;
  846. Procedure fpc_Write_Text_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  847. Begin
  848. If (InOutRes<>0) then
  849. exit;
  850. { Can't use array[boolean] because b can be >0 ! }
  851. { default value? }
  852. If Len=-1 then
  853. Len:=5;
  854. if b then
  855. Write_Str_Iso(Len,t,'true')
  856. else
  857. Write_Str_Iso(Len,t,'false');
  858. End;
  859. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  860. Begin
  861. If (InOutRes<>0) then
  862. exit;
  863. if (TextRec(t).mode<>fmOutput) Then
  864. begin
  865. if TextRec(t).mode=fmClosed then
  866. InOutRes:=103
  867. else
  868. InOutRes:=105;
  869. exit;
  870. end;
  871. If Len>1 Then
  872. fpc_WriteBlanks(t,Len-1);
  873. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  874. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  875. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  876. Inc(TextRec(t).BufPos);
  877. End;
  878. Procedure fpc_Write_Text_Char_Iso(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  879. Begin
  880. If (InOutRes<>0) then
  881. exit;
  882. if (TextRec(t).mode<>fmOutput) Then
  883. begin
  884. if TextRec(t).mode=fmClosed then
  885. InOutRes:=103
  886. else
  887. InOutRes:=105;
  888. exit;
  889. end;
  890. { default value? }
  891. If Len=-1 then
  892. Len:=1;
  893. If Len>1 Then
  894. fpc_WriteBlanks(t,Len-1)
  895. else If Len<1 Then
  896. exit;
  897. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  898. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  899. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  900. Inc(TextRec(t).BufPos);
  901. End;
  902. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  903. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  904. var
  905. a: RawByteString;
  906. Begin
  907. If (InOutRes<>0) then
  908. exit;
  909. if (TextRec(t).mode<>fmOutput) Then
  910. begin
  911. if TextRec(t).mode=fmClosed then
  912. InOutRes:=103
  913. else
  914. InOutRes:=105;
  915. exit;
  916. end;
  917. If Len>1 Then
  918. fpc_WriteBlanks(t,Len-1);
  919. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  920. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  921. { a widechar can be translated into more than a single ansichar }
  922. {$ifdef FPC_HAS_CPSTRING}
  923. widestringmanager.Wide2AnsiMoveProc(@c,a,TextRec(t).CodePage,1);
  924. {$else}
  925. a:=c;
  926. {$endif}
  927. fpc_WriteBuffer(t,PAnsiChar(a)^,Length(a));
  928. End;
  929. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  930. {*****************************************************************************
  931. Read(Ln)
  932. *****************************************************************************}
  933. Function NextChar(var f:Text;var s:string):Boolean;
  934. begin
  935. NextChar:=false;
  936. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  937. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  938. begin
  939. if length(s)<high(s) then
  940. begin
  941. inc(s[0]);
  942. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  943. end;
  944. Inc(TextRec(f).BufPos);
  945. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  946. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  947. NextChar:=true;
  948. end;
  949. end;
  950. Function IgnoreSpaces(var f:Text):Boolean;
  951. {
  952. Removes all leading spaces,tab,eols from the input buffer, returns true if
  953. the buffer is empty
  954. }
  955. var
  956. s : string;
  957. begin
  958. s:='';
  959. IgnoreSpaces:=false;
  960. { Return false when already at EOF }
  961. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  962. exit;
  963. (* Check performed separately to avoid accessing memory outside buffer *)
  964. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  965. exit;
  966. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  967. begin
  968. if not NextChar(f,s) then
  969. exit;
  970. { EOF? }
  971. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  972. break;
  973. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  974. break;
  975. end;
  976. IgnoreSpaces:=true;
  977. end;
  978. procedure ReadNumeric(var f:Text;var s:string);
  979. {
  980. Read numeric input, if buffer is empty then return True
  981. }
  982. begin
  983. repeat
  984. if not NextChar(f,s) then
  985. exit;
  986. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  987. end;
  988. function CheckRead(var f:Text):Boolean;
  989. begin
  990. CheckRead:=False;
  991. { Check error and if file is open and load buf if empty }
  992. If (InOutRes<>0) then
  993. exit;
  994. if (TextRec(f).mode<>fmInput) Then
  995. begin
  996. case TextRec(f).mode of
  997. fmOutPut,fmAppend:
  998. InOutRes:=104;
  999. else
  1000. InOutRes:=103;
  1001. end;
  1002. exit;
  1003. end;
  1004. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1005. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1006. CheckRead:=True;
  1007. end;
  1008. procedure ReadInteger(var f:Text;var s:string);
  1009. {
  1010. Ignore leading blanks (incl. EOF) and return the first characters matching
  1011. an integer in the format recognized by the Val procedure:
  1012. [+-]?[0-9]+
  1013. or [+-]?(0x|0X|x|X)[0-9A-Za-z]+
  1014. or [+-]?&[0-7]+
  1015. or [+-]?%[0-1]+
  1016. A partial match may be returned, e.g.: '' or '+' or '0x'.
  1017. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1018. standard function in ISO mode.
  1019. }
  1020. var
  1021. Base: Integer;
  1022. begin
  1023. s := '';
  1024. with TextRec(f) do begin
  1025. if not CheckRead(f) then Exit;
  1026. IgnoreSpaces(f);
  1027. if BufPos >= BufEnd then Exit;
  1028. if BufPtr^[BufPos] in ['+','-'] then
  1029. NextChar(f,s);
  1030. Base := 10;
  1031. if BufPos >= BufEnd then Exit;
  1032. if BufPtr^[BufPos] in ['$','x','X','%','&'] then
  1033. begin
  1034. case BufPtr^[BufPos] of
  1035. '$','x','X': Base := 16;
  1036. '%': Base := 2;
  1037. '&': Base := 8;
  1038. end;
  1039. NextChar(f,s);
  1040. end else if BufPtr^[BufPos] = '0' then
  1041. begin
  1042. NextChar(f,s);
  1043. if BufPos >= BufEnd then Exit;
  1044. if BufPtr^[BufPos] in ['x','X'] then
  1045. begin
  1046. Base := 16;
  1047. NextChar(f,s);
  1048. end;
  1049. end;
  1050. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1051. if (((Base = 2) and (BufPtr^[BufPos] in ['0'..'1']))
  1052. or ((Base = 8) and (BufPtr^[BufPos] in ['0'..'7']))
  1053. or ((Base = 10) and (BufPtr^[BufPos] in ['0'..'9']))
  1054. or ((Base = 16) and (BufPtr^[BufPos] in ['0'..'9','a'..'f','A'..'F']))) then
  1055. NextChar(f,s)
  1056. else Exit;
  1057. end;
  1058. end;
  1059. procedure ReadReal(var f:Text;var s:string);
  1060. {
  1061. Ignore leading blanks (incl. EOF) and return the first characters matching
  1062. a float number in the format recognized by the Val procedure:
  1063. [+-]?([0-9]+)?\.[0-9]+([eE][+-]?[0-9]+)?
  1064. or [+-]?[0-9]+\.([0-9]+)?([eE][+-]?[0-9]+)?
  1065. A partial match may be returned, e.g.: '' or '+' or '.' or '1e' or even '+.'.
  1066. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1067. standard function in ISO mode.
  1068. }
  1069. var digit: Boolean;
  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. digit := false;
  1079. if BufPos >= BufEnd then Exit;
  1080. if BufPtr^[BufPos] in ['0'..'9'] then
  1081. begin
  1082. digit := true;
  1083. repeat
  1084. NextChar(f,s);
  1085. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1086. until not (BufPtr^[BufPos] in ['0'..'9']);
  1087. end;
  1088. if BufPtr^[BufPos] = '.' then
  1089. begin
  1090. NextChar(f,s);
  1091. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1092. if BufPtr^[BufPos] in ['0'..'9'] then
  1093. begin
  1094. digit := true;
  1095. repeat
  1096. NextChar(f,s);
  1097. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1098. until not (BufPtr^[BufPos] in ['0'..'9']);
  1099. end;
  1100. end;
  1101. {at least one digit is required on the left of the exponent}
  1102. if digit and (BufPtr^[BufPos] in ['e','E']) then
  1103. begin
  1104. NextChar(f,s);
  1105. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1106. if BufPtr^[BufPos] in ['+','-'] then
  1107. NextChar(f,s);
  1108. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1109. if BufPtr^[BufPos] in ['0'..'9'] then
  1110. NextChar(f,s)
  1111. else break;
  1112. end;
  1113. end;
  1114. end;
  1115. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  1116. begin
  1117. if TextRec(f).FlushFunc<>nil then
  1118. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1119. end;
  1120. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  1121. var prev: char;
  1122. Begin
  1123. If not CheckRead(f) then
  1124. exit;
  1125. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1126. { Flush if set }
  1127. begin
  1128. if (TextRec(f).FlushFunc<>nil) then
  1129. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1130. exit;
  1131. end;
  1132. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1133. Exit;
  1134. repeat
  1135. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1136. inc(TextRec(f).BufPos);
  1137. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1138. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1139. if prev = #10 then
  1140. exit;
  1141. {$ifdef MACOS}
  1142. if prev = #13 then
  1143. {StdInput on macos never have dos line ending, so this is safe.}
  1144. if TextRec(f).Handle = StdInputHandle then
  1145. exit;
  1146. {$endif MACOS}
  1147. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1148. begin
  1149. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1150. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1151. { Flush if set }
  1152. begin
  1153. if (TextRec(f).FlushFunc<>nil) then
  1154. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1155. exit;
  1156. end;
  1157. end;
  1158. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1159. Exit;
  1160. if (prev=#13) then
  1161. { is there also a #10 after it? }
  1162. begin
  1163. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1164. { yes, skip that one as well }
  1165. inc(TextRec(f).BufPos);
  1166. exit;
  1167. end;
  1168. until false;
  1169. End;
  1170. Procedure fpc_ReadLn_End_Iso(var f : Text);[Public,Alias:'FPC_READLN_END_ISO']; iocheck; compilerproc;
  1171. var prev: char;
  1172. Begin
  1173. If not CheckRead(f) then
  1174. exit;
  1175. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1176. { Flush if set }
  1177. begin
  1178. if (TextRec(f).FlushFunc<>nil) then
  1179. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1180. exit;
  1181. end;
  1182. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1183. begin
  1184. inc(TextRec(f).BufPos);
  1185. Exit;
  1186. end;
  1187. repeat
  1188. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1189. inc(TextRec(f).BufPos);
  1190. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1191. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1192. if prev = #10 then
  1193. exit;
  1194. {$ifdef MACOS}
  1195. if prev = #13 then
  1196. {StdInput on macos never have dos line ending, so this is safe.}
  1197. if TextRec(f).Handle = StdInputHandle then
  1198. exit;
  1199. {$endif MACOS}
  1200. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1201. begin
  1202. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1203. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1204. { Flush if set }
  1205. begin
  1206. if (TextRec(f).FlushFunc<>nil) then
  1207. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1208. exit;
  1209. end;
  1210. end;
  1211. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1212. begin
  1213. inc(TextRec(f).BufPos);
  1214. Exit;
  1215. end;
  1216. if (prev=#13) then
  1217. { is there also a #10 after it? }
  1218. begin
  1219. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1220. { yes, skip that one as well }
  1221. inc(TextRec(f).BufPos);
  1222. exit;
  1223. end;
  1224. until false;
  1225. End;
  1226. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  1227. var
  1228. sPos,len : Longint;
  1229. p,startp,maxp : pchar;
  1230. end_of_string:boolean;
  1231. Begin
  1232. ReadPCharLen:=0;
  1233. If not CheckRead(f) then
  1234. exit;
  1235. { Read maximal until Maxlen is reached }
  1236. sPos:=0;
  1237. end_of_string:=false;
  1238. repeat
  1239. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1240. begin
  1241. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1242. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1243. break;
  1244. end;
  1245. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1246. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  1247. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  1248. else
  1249. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1250. startp:=p;
  1251. { find stop character }
  1252. while p<maxp do
  1253. begin
  1254. { Optimization: Do a quick check for a control character first }
  1255. if (p^<' ') then
  1256. begin
  1257. if (p^ in [#10,#13]) or
  1258. (ctrlZmarkseof and (p^=#26)) then
  1259. begin
  1260. end_of_string:=true;
  1261. break;
  1262. end;
  1263. end;
  1264. inc(p);
  1265. end;
  1266. { calculate read bytes }
  1267. len:=p-startp;
  1268. inc(TextRec(f).BufPos,Len);
  1269. Move(startp^,s[sPos],Len);
  1270. inc(sPos,Len);
  1271. until (spos=MaxLen) or end_of_string;
  1272. ReadPCharLen:=spos;
  1273. End;
  1274. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  1275. Begin
  1276. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  1277. End;
  1278. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  1279. Begin
  1280. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  1281. End;
  1282. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  1283. var
  1284. len: longint;
  1285. Begin
  1286. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  1287. if zerobased and
  1288. (len > high(s)) then
  1289. len := high(s);
  1290. if (len <= high(s)) then
  1291. s[len] := #0;
  1292. End;
  1293. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1294. 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;
  1295. var
  1296. slen,len : SizeInt;
  1297. Begin
  1298. slen:=0;
  1299. Repeat
  1300. // SetLength will reallocate the length.
  1301. SetLength(s,slen+255);
  1302. len:=ReadPCharLen(f,pchar(Pointer(s)+slen),255);
  1303. inc(slen,len);
  1304. Until len<255;
  1305. // Set actual length
  1306. SetLength(s,Slen);
  1307. {$ifdef FPC_HAS_CPSTRING}
  1308. SetCodePage(s,TextRec(f).CodePage,false);
  1309. if cp<>TextRec(f).CodePage then
  1310. s:=fpc_AnsiStr_To_AnsiStr(s,cp);
  1311. {$endif FPC_HAS_CPSTRING}
  1312. End;
  1313. 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'];
  1314. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1315. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1316. Procedure fpc_Read_Text_UnicodeStr(var f : Text;out us : UnicodeString); iocheck; compilerproc;
  1317. var
  1318. s: RawByteString;
  1319. Begin
  1320. // all standard input is assumed to be ansi-encoded
  1321. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1322. // Convert to unicodestring
  1323. {$ifdef FPC_HAS_CPSTRING}
  1324. widestringmanager.Ansi2UnicodeMoveProc(PAnsiChar(s),StringCodePage(s),us,Length(s));
  1325. {$else}
  1326. us:=s;
  1327. {$endif}
  1328. End;
  1329. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1330. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1331. Procedure fpc_Read_Text_WideStr(var f : Text;out ws : WideString); iocheck; compilerproc;
  1332. var
  1333. s: RawByteString;
  1334. Begin
  1335. // all standard input is assumed to be ansi-encoded
  1336. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1337. // Convert to widestring
  1338. {$ifdef FPC_HAS_CPSTRING}
  1339. widestringmanager.Ansi2WideMoveProc(PAnsiChar(s),StringCodePage(s),ws,Length(s));
  1340. {$else}
  1341. ws:=s;
  1342. {$endif}
  1343. End;
  1344. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1345. procedure fpc_Read_Text_Char(var f : Text; out c: char); [public, alias: 'FPC_READ_TEXT_CHAR']; iocheck; compilerproc;
  1346. Begin
  1347. c:=#0;
  1348. If not CheckRead(f) then
  1349. exit;
  1350. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1351. begin
  1352. c := #26;
  1353. exit;
  1354. end;
  1355. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1356. inc(TextRec(f).BufPos);
  1357. end;
  1358. procedure fpc_Read_Text_Char_intern(var f : Text; out c: char); iocheck; [external name 'FPC_READ_TEXT_CHAR'];
  1359. function fpc_GetBuf(var f : Text) : pchar; iocheck; compilerproc;
  1360. Begin
  1361. Result:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1362. If not CheckRead(f) then
  1363. exit;
  1364. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1365. exit;
  1366. Result:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1367. end;
  1368. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1369. procedure fpc_Read_Text_WideChar(var f : Text; out wc: widechar); iocheck;compilerproc;
  1370. var
  1371. ws: widestring;
  1372. i: longint;
  1373. { maximum code point length is 6 characters (with UTF-8) }
  1374. str: array[0..5] of char;
  1375. Begin
  1376. fillchar(str[0],sizeof(str),0);
  1377. for i:=low(str) to high(str) do
  1378. begin
  1379. fpc_Read_Text_Char_intern(f,str[i]);
  1380. case widestringmanager.CodePointLengthProc(@str[0],i+1) of
  1381. -1: { possibly incomplete code point, try with an extra character }
  1382. ;
  1383. 0: { null character }
  1384. begin
  1385. wc:=#0;
  1386. exit;
  1387. end;
  1388. else
  1389. begin
  1390. { valid code point -> convert to widestring}
  1391. {$ifdef FPC_HAS_CPSTRING}
  1392. widestringmanager.Ansi2WideMoveProc(@str[0],TextRec(f).CodePage,ws,i+1);
  1393. {$else}
  1394. widestringmanager.Ansi2WideMoveProc(@str[0],DefaultSystemCodePage,ws,i+1);
  1395. {$endif}
  1396. { has to be exactly one widechar }
  1397. if length(ws)=1 then
  1398. begin
  1399. wc:=ws[1];
  1400. exit
  1401. end
  1402. else
  1403. break;
  1404. end;
  1405. end;
  1406. end;
  1407. { invalid widechar input }
  1408. inoutres:=106;
  1409. end;
  1410. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1411. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: char); iocheck;compilerproc;
  1412. Begin
  1413. c:=' ';
  1414. If not CheckRead(f) then
  1415. exit;
  1416. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1417. begin
  1418. c:=' ';
  1419. exit;
  1420. end;
  1421. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1422. inc(TextRec(f).BufPos);
  1423. if c=#13 then
  1424. begin
  1425. c:=' ';
  1426. If not CheckRead(f) or
  1427. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1428. exit;
  1429. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1430. inc(TextRec(f).BufPos);
  1431. { ignore #26 following a new line }
  1432. If not CheckRead(f) or
  1433. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1434. exit;
  1435. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1436. inc(TextRec(f).BufPos);
  1437. end
  1438. else if c=#10 then
  1439. begin
  1440. c:=' ';
  1441. { ignore #26 following a new line }
  1442. If not CheckRead(f) or
  1443. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1444. exit;
  1445. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1446. inc(TextRec(f).BufPos);
  1447. end
  1448. else if c=#26 then
  1449. c:=' ';
  1450. end;
  1451. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1452. var
  1453. hs : String;
  1454. code : ValSInt;
  1455. Begin
  1456. l:=0;
  1457. If not CheckRead(f) then
  1458. exit;
  1459. hs:='';
  1460. if IgnoreSpaces(f) then
  1461. begin
  1462. { When spaces were found and we are now at EOF,
  1463. then we return 0 }
  1464. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1465. exit;
  1466. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1467. exit;
  1468. ReadNumeric(f,hs);
  1469. end;
  1470. if (hs = '') then
  1471. L := 0
  1472. else
  1473. begin
  1474. Val(hs,l,code);
  1475. if Code <> 0 then
  1476. InOutRes:=106;
  1477. end;
  1478. End;
  1479. Procedure fpc_Read_Text_SInt_Iso(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1480. var
  1481. hs : String;
  1482. code : ValSInt;
  1483. Begin
  1484. ReadInteger(f,hs);
  1485. Val(hs,l,code);
  1486. if Code <> 0 then
  1487. InOutRes:=106;
  1488. End;
  1489. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1490. var
  1491. hs : String;
  1492. code : ValSInt;
  1493. Begin
  1494. u:=0;
  1495. If not CheckRead(f) then
  1496. exit;
  1497. hs:='';
  1498. if IgnoreSpaces(f) then
  1499. begin
  1500. { When spaces were found and we are now at EOF,
  1501. then we return 0 }
  1502. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1503. exit;
  1504. ReadNumeric(f,hs);
  1505. end;
  1506. if (hs = '') then
  1507. u := 0
  1508. else
  1509. begin
  1510. val(hs,u,code);
  1511. If code<>0 Then
  1512. InOutRes:=106;
  1513. end;
  1514. End;
  1515. Procedure fpc_Read_Text_UInt_Iso(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1516. var
  1517. hs : String;
  1518. code : ValSInt;
  1519. Begin
  1520. ReadInteger(f,hs);
  1521. Val(hs,u,code);
  1522. If code<>0 Then
  1523. InOutRes:=106;
  1524. End;
  1525. {$ifndef FPUNONE}
  1526. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1527. var
  1528. hs : string;
  1529. code : Word;
  1530. begin
  1531. v:=0.0;
  1532. If not CheckRead(f) then
  1533. exit;
  1534. hs:='';
  1535. if IgnoreSpaces(f) then
  1536. begin
  1537. { When spaces were found and we are now at EOF,
  1538. then we return 0 }
  1539. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1540. exit;
  1541. ReadNumeric(f,hs);
  1542. end;
  1543. val(hs,v,code);
  1544. If code<>0 Then
  1545. InOutRes:=106;
  1546. end;
  1547. procedure fpc_Read_Text_Float_Iso(var f : Text; out v : ValReal); iocheck; compilerproc;
  1548. var
  1549. hs : string;
  1550. code : Word;
  1551. begin
  1552. ReadReal(f,hs);
  1553. Val(hs,v,code);
  1554. If code<>0 Then
  1555. InOutRes:=106;
  1556. end;
  1557. {$endif}
  1558. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1559. var s:string;
  1560. code:valsint;
  1561. begin
  1562. if not checkread(t) then
  1563. exit;
  1564. s:='';
  1565. if ignorespaces(t) then
  1566. begin
  1567. { When spaces were found and we are now at EOF, then we return 0 }
  1568. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1569. exit;
  1570. ReadNumeric(t,s);
  1571. end;
  1572. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1573. if code<>0 then
  1574. InOutRes:=106;
  1575. end;
  1576. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1577. var
  1578. hs : string;
  1579. code : ValSInt;
  1580. begin
  1581. {$ifdef FPUNONE}
  1582. v:=0;
  1583. {$else}
  1584. v:=0.0;
  1585. {$endif}
  1586. If not CheckRead(f) then
  1587. exit;
  1588. hs:='';
  1589. if IgnoreSpaces(f) then
  1590. begin
  1591. { When spaces were found and we are now at EOF,
  1592. then we return 0 }
  1593. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1594. exit;
  1595. ReadNumeric(f,hs);
  1596. end;
  1597. val(hs,v,code);
  1598. If code<>0 Then
  1599. InOutRes:=106;
  1600. end;
  1601. procedure fpc_Read_Text_Currency_Iso(var f : Text; out v : Currency); iocheck; compilerproc;
  1602. var
  1603. hs : string;
  1604. code : ValSInt;
  1605. begin
  1606. ReadReal(f,hs);
  1607. Val(hs,v,code);
  1608. If code<>0 Then
  1609. InOutRes:=106;
  1610. end;
  1611. {$ifndef cpu64}
  1612. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1613. var
  1614. hs : String;
  1615. code : longint;
  1616. Begin
  1617. q:=0;
  1618. If not CheckRead(f) then
  1619. exit;
  1620. hs:='';
  1621. if IgnoreSpaces(f) then
  1622. begin
  1623. { When spaces were found and we are now at EOF,
  1624. then we return 0 }
  1625. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1626. exit;
  1627. ReadNumeric(f,hs);
  1628. end;
  1629. val(hs,q,code);
  1630. If code<>0 Then
  1631. InOutRes:=106;
  1632. End;
  1633. procedure fpc_Read_Text_QWord_Iso(var f : text; out q : qword); iocheck; compilerproc;
  1634. var
  1635. hs : String;
  1636. code : longint;
  1637. Begin
  1638. ReadInteger(f,hs);
  1639. Val(hs,q,code);
  1640. If code<>0 Then
  1641. InOutRes:=106;
  1642. End;
  1643. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1644. var
  1645. hs : String;
  1646. code : Longint;
  1647. Begin
  1648. i:=0;
  1649. If not CheckRead(f) then
  1650. exit;
  1651. hs:='';
  1652. if IgnoreSpaces(f) then
  1653. begin
  1654. { When spaces were found and we are now at EOF,
  1655. then we return 0 }
  1656. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1657. exit;
  1658. ReadNumeric(f,hs);
  1659. end;
  1660. Val(hs,i,code);
  1661. If code<>0 Then
  1662. InOutRes:=106;
  1663. End;
  1664. procedure fpc_Read_Text_Int64_Iso(var f : text; out i : int64); iocheck; compilerproc;
  1665. var
  1666. hs : String;
  1667. code : Longint;
  1668. Begin
  1669. ReadInteger(f,hs);
  1670. Val(hs,i,code);
  1671. If code<>0 Then
  1672. InOutRes:=106;
  1673. End;
  1674. {$endif CPU64}
  1675. {*****************************************************************************
  1676. WriteStr/ReadStr
  1677. *****************************************************************************}
  1678. const
  1679. { pointer to target string }
  1680. StrPtrIndex = 1;
  1681. { temporary destination for writerstr, because the original value of the
  1682. destination may be used in the writestr expression }
  1683. TempWriteStrDestIndex = 9;
  1684. ShortStrLenIndex = 17;
  1685. { how many bytes of the string have been processed already (used for readstr) }
  1686. BytesReadIndex = 17;
  1687. procedure WriteStrShort(var t: textrec);
  1688. var
  1689. str: pshortstring;
  1690. newbytes,
  1691. oldlen: longint;
  1692. begin
  1693. if (t.bufpos=0) then
  1694. exit;
  1695. str:=pshortstring(ppointer(@t.userdata[TempWriteStrDestIndex])^);
  1696. newbytes:=t.BufPos;
  1697. oldlen:=length(str^);
  1698. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1699. begin
  1700. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1701. {$ifdef writestr_iolencheck}
  1702. // GPC only gives an io error if {$no-truncate-strings} is active
  1703. // FPC does not have this setting (it never gives errors when a
  1704. // a string expression is truncated)
  1705. { "disk full" }
  1706. inoutres:=101;
  1707. {$endif}
  1708. end;
  1709. setlength(str^,length(str^)+newbytes);
  1710. move(t.bufptr^,str^[oldlen+1],newbytes);
  1711. t.bufpos:=0;
  1712. end;
  1713. procedure WriteStrShortFlush(var t: textrec);
  1714. begin
  1715. { move written data from internal buffer to temporary string (don't move
  1716. directly from buffer to final string, because the temporary string may
  1717. already contain data in case the textbuf was smaller than the string
  1718. length) }
  1719. WriteStrShort(t);
  1720. { move written data to original string }
  1721. move(PPointer(@t.userdata[TempWriteStrDestIndex])^^,
  1722. PPointer(@t.userdata[StrPtrIndex])^^,
  1723. t.userdata[ShortStrLenIndex]+1);
  1724. { free temporary buffer }
  1725. freemem(PPointer(@t.userdata[TempWriteStrDestIndex])^);
  1726. end;
  1727. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1728. procedure WriteStrAnsi(var t: textrec);
  1729. var
  1730. str: pansistring;
  1731. oldlen: longint;
  1732. begin
  1733. if (t.bufpos=0) then
  1734. exit;
  1735. str:=pansistring(@t.userdata[TempWriteStrDestIndex]);
  1736. oldlen:=length(str^);
  1737. setlength(str^,oldlen+t.bufpos);
  1738. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1739. t.bufpos:=0;
  1740. end;
  1741. procedure WriteStrAnsiFlush(var t: textrec);
  1742. begin
  1743. { see comment in WriteStrShortFlush }
  1744. WriteStrAnsi(t);
  1745. pansistring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1746. pansistring(@t.userdata[TempWriteStrDestIndex])^;
  1747. { free memory/finalize temp }
  1748. pansistring(@t.userdata[TempWriteStrDestIndex])^:='';
  1749. end;
  1750. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1751. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1752. procedure WriteStrUnicode(var t: textrec);
  1753. var
  1754. temp: ansistring;
  1755. str: punicodestring;
  1756. begin
  1757. if (t.bufpos=0) then
  1758. exit;
  1759. str:=punicodestring(@t.userdata[TempWriteStrDestIndex]);
  1760. setlength(temp,t.bufpos);
  1761. move(t.bufptr^,temp[1],t.bufpos);
  1762. str^:=str^+temp;
  1763. t.bufpos:=0;
  1764. end;
  1765. procedure WriteStrUnicodeFlush(var t: textrec);
  1766. begin
  1767. { see comment in WriteStrShortFlush }
  1768. WriteStrUnicode(t);
  1769. punicodestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1770. punicodestring(@t.userdata[TempWriteStrDestIndex])^;
  1771. { free memory/finalize temp }
  1772. punicodestring(@t.userdata[TempWriteStrDestIndex])^:='';
  1773. end;
  1774. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1775. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1776. procedure WriteStrWide(var t: textrec);
  1777. var
  1778. temp: ansistring;
  1779. str: pwidestring;
  1780. begin
  1781. if (t.bufpos=0) then
  1782. exit;
  1783. str:=pwidestring(@t.userdata[TempWriteStrDestIndex]);
  1784. setlength(temp,t.bufpos);
  1785. move(t.bufptr^,temp[1],t.bufpos);
  1786. str^:=str^+temp;
  1787. t.bufpos:=0;
  1788. end;
  1789. procedure WriteStrWideFlush(var t: textrec);
  1790. begin
  1791. { see comment in WriteStrShortFlush }
  1792. WriteStrWide(t);
  1793. pwidestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1794. pwidestring(@t.userdata[TempWriteStrDestIndex])^;
  1795. { free memory/finalize temp }
  1796. finalize(pwidestring(@t.userdata[TempWriteStrDestIndex])^);
  1797. end;
  1798. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1799. procedure SetupWriteStrCommon(out t: textrec);
  1800. begin
  1801. // initialise
  1802. Assign(text(t),'');
  1803. t.mode:=fmOutput;
  1804. t.OpenFunc:=nil;
  1805. t.CloseFunc:=nil;
  1806. {$ifdef FPC_HAS_CPSTRING}
  1807. t.CodePage:=DefaultSystemCodePage;
  1808. {$endif}
  1809. end;
  1810. procedure fpc_SetupWriteStr_Shortstr(var ReadWriteStrText: text; var s: shortstring); compilerproc;
  1811. begin
  1812. setupwritestrcommon(TextRec(ReadWriteStrText));
  1813. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1814. { temporary destination (see comments for TempWriteStrDestIndex) }
  1815. getmem(PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^,high(s)+1);
  1816. setlength(pshortstring(ppointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^)^,0);
  1817. TextRec(ReadWriteStrText).userdata[ShortStrLenIndex]:=high(s);
  1818. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrShort;
  1819. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrShortFlush;
  1820. end;
  1821. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1822. procedure fpc_SetupWriteStr_Ansistr(var ReadWriteStrText: text; var s: ansistring); compilerproc;
  1823. begin
  1824. setupwritestrcommon(TextRec(ReadWriteStrText));
  1825. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1826. { temp destination ansistring, nil = empty string }
  1827. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  1828. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrAnsi;
  1829. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrAnsiFlush;
  1830. end;
  1831. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1832. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1833. procedure fpc_SetupWriteStr_Unicodestr(var ReadWriteStrText: text; var s: unicodestring); compilerproc;
  1834. begin
  1835. setupwritestrcommon(TextRec(ReadWriteStrText));
  1836. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1837. { temp destination unicodestring, nil = empty string }
  1838. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  1839. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrUnicode;
  1840. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrUnicodeFlush;
  1841. end;
  1842. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1843. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1844. procedure fpc_SetupWriteStr_Widestr(var ReadWriteStrText: text; var s: widestring); compilerproc;
  1845. begin
  1846. setupwritestrcommon(TextRec(ReadWriteStrText));
  1847. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  1848. { temp destination widestring }
  1849. PWideString(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:='';
  1850. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrWide;
  1851. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrWideFlush;
  1852. end;
  1853. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1854. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1855. procedure ReadAnsiStrFinal(var t: textrec);
  1856. begin
  1857. { finalise the temp ansistring }
  1858. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1859. end;
  1860. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1861. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1862. var
  1863. newbytes: sizeint;
  1864. begin
  1865. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1866. if (t.BufSize <= newbytes) then
  1867. newbytes := t.BufSize;
  1868. if (newbytes > 0) then
  1869. begin
  1870. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1871. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1872. end;
  1873. t.BufEnd:=newbytes;
  1874. t.BufPos:=0;
  1875. end;
  1876. procedure ReadStrAnsi(var t: textrec);
  1877. var
  1878. str: pansistring;
  1879. begin
  1880. str:=pansistring(@t.userdata[StrPtrIndex]);
  1881. ReadStrCommon(t,@str^[1],length(str^));
  1882. end;
  1883. procedure SetupReadStrCommon(out t: textrec);
  1884. begin
  1885. // initialise
  1886. Assign(text(t),'');
  1887. t.mode:=fmInput;
  1888. t.OpenFunc:=nil;
  1889. t.CloseFunc:=nil;
  1890. {$ifdef FPC_HAS_CPSTRING}
  1891. t.CodePage:=DefaultSystemCodePage;
  1892. {$endif}
  1893. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1894. end;
  1895. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1896. procedure fpc_SetupReadStr_Ansistr(var ReadWriteStrText: text; const s: ansistring); [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1897. begin
  1898. setupreadstrcommon(TextRec(ReadWriteStrText));
  1899. { we need a reference, because 's' may be a temporary expression }
  1900. PAnsiString(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=s;
  1901. TextRec(ReadWriteStrText).InOutFunc:=@ReadStrAnsi;
  1902. { this is called at the end, by fpc_read_end }
  1903. TextRec(ReadWriteStrText).FlushFunc:=@ReadAnsiStrFinal;
  1904. end;
  1905. procedure fpc_SetupReadStr_Ansistr_Intern(var ReadWriteStrText: text; const s: ansistring); [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1906. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1907. procedure fpc_SetupReadStr_Shortstr(var ReadWriteStrText: text; const s: shortstring); compilerproc;
  1908. begin
  1909. { the reason we convert the short string to ansistring, is because the semantics of
  1910. readstr are defined as:
  1911. *********************
  1912. Apart from the restrictions imposed by requirements given in this clause,
  1913. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  1914. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  1915. char-type (or a subrange of char-type), the integer-type (or a subrange of
  1916. integer-type), the real-type, a fixed-string-type, or a
  1917. variable-string-type, shall be equivalent to
  1918. begin
  1919. rewrite(f);
  1920. writeln(f, e);
  1921. reset(f);
  1922. read(f, v 1 ,...,v n )
  1923. end
  1924. *********************
  1925. This means that any side effects caused by the evaluation of v 1 .. v n
  1926. must not affect the value of e (= our argument s) -> we need a copy of it.
  1927. An ansistring is the easiest way to get a threadsafe copy, and allows us
  1928. to use the other ansistring readstr helpers too.
  1929. }
  1930. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1931. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  1932. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  1933. runerror(217);
  1934. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1935. end;
  1936. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1937. procedure fpc_SetupReadStr_Unicodestr(var ReadWriteStrText: text; const s: unicodestring); compilerproc;
  1938. begin
  1939. { we use an ansistring to avoid code duplication, and let the }
  1940. { assignment convert the widestring to an equivalent ansistring }
  1941. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  1942. end;
  1943. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1944. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1945. procedure fpc_SetupReadStr_Widestr(var ReadWriteStrText: text; const s: widestring); compilerproc;
  1946. begin
  1947. { we use an ansistring to avoid code duplication, and let the }
  1948. { assignment convert the widestring to an equivalent ansistring }
  1949. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  1950. end;
  1951. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1952. {*****************************************************************************
  1953. Initializing
  1954. *****************************************************************************}
  1955. procedure OpenStdIO(var f:text;mode:longint;hdl:thandle);
  1956. begin
  1957. Assign(f,'');
  1958. TextRec(f).Handle:=hdl;
  1959. TextRec(f).Mode:=mode;
  1960. TextRec(f).Closefunc:=@FileCloseFunc;
  1961. case mode of
  1962. fmInput :
  1963. begin
  1964. TextRec(f).InOutFunc:=@FileReadFunc;
  1965. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1966. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleInput);
  1967. {$endif}
  1968. end;
  1969. fmOutput :
  1970. begin
  1971. TextRec(f).InOutFunc:=@FileWriteFunc;
  1972. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1973. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleOutput);
  1974. {$endif}
  1975. if Do_Isdevice(hdl) then
  1976. TextRec(f).FlushFunc:=@FileWriteFunc;
  1977. end;
  1978. else
  1979. HandleError(102);
  1980. end;
  1981. end;