text.inc 39 KB

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