text.inc 38 KB

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