text.inc 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. subroutines For TextFile handling
  12. ****************************************************************************}
  13. Procedure FileCloseFunc(Var t:TextRec);
  14. Begin
  15. Do_Close(t.Handle);
  16. t.Handle:=UnusedHandle;
  17. End;
  18. Procedure FileReadFunc(var t:TextRec);
  19. Begin
  20. t.BufEnd:=Do_Read(t.Handle,t.Bufptr,t.BufSize);
  21. t.BufPos:=0;
  22. End;
  23. Procedure FileWriteFunc(var t:TextRec);
  24. var
  25. i : longint;
  26. Begin
  27. { prevent unecessary system call }
  28. if t.BufPos=0 then
  29. exit;
  30. i:=Do_Write(t.Handle,t.Bufptr,t.BufPos);
  31. if i<>t.BufPos then
  32. InOutRes:=101;
  33. t.BufPos:=0;
  34. End;
  35. Procedure FileOpenFunc(var t:TextRec);
  36. var
  37. Flags : Longint;
  38. Begin
  39. Case t.mode Of
  40. fmInput : Flags:=$10000;
  41. fmOutput : Flags:=$11001;
  42. fmAppend : Flags:=$10101;
  43. else
  44. begin
  45. InOutRes:=102;
  46. exit;
  47. end;
  48. End;
  49. Do_Open(t,PChar(@t.Name),Flags);
  50. t.CloseFunc:=@FileCloseFunc;
  51. t.FlushFunc:=nil;
  52. if t.Mode=fmInput then
  53. t.InOutFunc:=@FileReadFunc
  54. else
  55. begin
  56. t.InOutFunc:=@FileWriteFunc;
  57. { Only install flushing if its a NOT a file, and only check if there
  58. was no error opening the file, because else we always get a bad
  59. file handle error 6 (PFV) }
  60. if (InOutRes=0) and
  61. Do_Isdevice(t.Handle) then
  62. t.FlushFunc:=@FileWriteFunc;
  63. end;
  64. End;
  65. Procedure Assign({$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, 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(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_UnicodeStr (Len : Longint; Var f : Text; const S : WideString); iocheck; [Public,alias:'FPC_WRITE_TEXT_UNICODESTR']; compilerproc;
  542. {
  543. Writes a UnicodeString 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. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  566. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; [Public,alias:'FPC_WRITE_TEXT_WIDESTR']; compilerproc;
  567. {
  568. Writes a WideString to the Text file T
  569. }
  570. var
  571. SLen : longint;
  572. a: ansistring;
  573. begin
  574. If (pointer(S)=nil) or (InOutRes<>0) then
  575. exit;
  576. case TextRec(f).mode of
  577. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  578. begin
  579. SLen:=Length(s);
  580. If Len>SLen Then
  581. fpc_WriteBlanks(f,Len-SLen);
  582. a:=s;
  583. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  584. fpc_WriteBuffer(f,pchar(a)^,length(a));
  585. end;
  586. fmInput: InOutRes:=105
  587. else InOutRes:=103;
  588. end;
  589. end;
  590. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  591. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SINT']; compilerproc;
  592. var
  593. s : String;
  594. Begin
  595. If (InOutRes<>0) then
  596. exit;
  597. Str(l,s);
  598. Write_Str(Len,t,s);
  599. End;
  600. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; [Public,Alias:'FPC_WRITE_TEXT_UINT']; compilerproc;
  601. var
  602. s : String;
  603. Begin
  604. If (InOutRes<>0) then
  605. exit;
  606. Str(L,s);
  607. Write_Str(Len,t,s);
  608. End;
  609. {$ifndef CPU64}
  610. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; [public,alias:'FPC_WRITE_TEXT_QWORD']; compilerproc;
  611. var
  612. s : string;
  613. begin
  614. if (InOutRes<>0) then
  615. exit;
  616. str(q,s);
  617. write_str(len,t,s);
  618. end;
  619. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; [public,alias:'FPC_WRITE_TEXT_INT64']; compilerproc;
  620. var
  621. s : string;
  622. begin
  623. if (InOutRes<>0) then
  624. exit;
  625. str(i,s);
  626. write_str(len,t,s);
  627. end;
  628. {$endif CPU64}
  629. {$ifndef FPUNONE}
  630. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; [Public,Alias:'FPC_WRITE_TEXT_FLOAT']; compilerproc;
  631. var
  632. s : String;
  633. Begin
  634. If (InOutRes<>0) then
  635. exit;
  636. Str_real(Len,fixkomma,r,treal_type(rt),s);
  637. Write_Str(Len,t,s);
  638. End;
  639. {$endif}
  640. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; [Public,Alias:'FPC_WRITE_TEXT_ENUM']; compilerproc;
  641. type Ptypeinfo=^Ttypeinfo;
  642. Ttypeinfo=packed record
  643. kind:byte;
  644. name:shortstring;
  645. end;
  646. Penuminfo=^Tenuminfo;
  647. Tenuminfo={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  648. ordtype:byte;
  649. minvalue, maxvalue:longint;
  650. basetype:pointer;
  651. namelist:shortstring;
  652. end;
  653. Tsorted_array={$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  654. o:longint;
  655. s:Pstring;
  656. end;
  657. var
  658. p:Pstring;
  659. l,h,m,offset:cardinal;
  660. sorted_array:^Tsorted_array;
  661. s:string;
  662. begin
  663. if textrec(t).mode<>fmoutput then
  664. begin
  665. if textrec(t).mode=fminput then
  666. inoutres:=105
  667. else
  668. inoutres:=103;
  669. exit;
  670. end;
  671. if Pcardinal(ord2strindex)^=0 then
  672. begin
  673. {The compiler did generate a lookup table.}
  674. offset:=2+length(Ptypeinfo(typinfo)^.name);
  675. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  676. offset:=align(offset, sizeof(ptrint));
  677. {$endif}
  678. with Penuminfo(Pbyte(typinfo)+offset)^ do
  679. begin
  680. if (ordinal<minvalue) or (ordinal>maxvalue) then
  681. begin
  682. inoutres:=107; {Invalid ordinal value for this enum.}
  683. exit;
  684. end;
  685. dec(ordinal,minvalue);
  686. end;
  687. {Get the address of the string.}
  688. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  689. p:=Pshortstring((PPpointer(ord2strindex+align(sizeof(longint), sizeof(ptrint)))+ordinal)^);
  690. {$else}
  691. p:=Pshortstring((PPpointer(ord2strindex+sizeof(longint))+ordinal)^);
  692. {$endif}
  693. if p=nil then
  694. begin
  695. inoutres:=107; {Invalid ordinal value for this enum.}
  696. exit;
  697. end;
  698. s:=p^;
  699. end
  700. else
  701. begin
  702. {The compiler did generate a sorted array of (ordvalue,Pstring) tuples.}
  703. sorted_array:=pointer(Pcardinal(ord2strindex)+2);
  704. {Use a binary search to get the string.}
  705. l:=0;
  706. h:=(Pcardinal(ord2strindex)+1)^-1;
  707. repeat
  708. m:=(l+h) div 2;
  709. if ordinal>sorted_array[m].o then
  710. l:=m+1
  711. else if ordinal<sorted_array[m].o then
  712. h:=m-1
  713. else
  714. break;
  715. if l>h then
  716. begin
  717. inoutres:=107; {Invalid ordinal value for this enum.}
  718. exit;
  719. end;
  720. until false;
  721. s:=sorted_array[m].s^;
  722. end;
  723. fpc_writeBuffer(t,s[1],length(s));
  724. {Pad the string with spaces if necessary.}
  725. if len>length(s) then
  726. fpc_writeblanks(t,len-length(s));
  727. end;
  728. {$ifdef FPC_HAS_STR_CURRENCY}
  729. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; [Public,Alias:'FPC_WRITE_TEXT_CURRENCY']; compilerproc;
  730. var
  731. s : String;
  732. Begin
  733. If (InOutRes<>0) then
  734. exit;
  735. str(c:Len:fixkomma,s);
  736. Write_Str(Len,t,s);
  737. End;
  738. {$endif FPC_HAS_STR_CURRENCY}
  739. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; [Public,Alias:'FPC_WRITE_TEXT_BOOLEAN']; compilerproc;
  740. Begin
  741. If (InOutRes<>0) then
  742. exit;
  743. { Can't use array[boolean] because b can be >0 ! }
  744. if b then
  745. Write_Str(Len,t,'TRUE')
  746. else
  747. Write_Str(Len,t,'FALSE');
  748. End;
  749. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; [Public,Alias:'FPC_WRITE_TEXT_CHAR']; compilerproc;
  750. Begin
  751. If (InOutRes<>0) then
  752. exit;
  753. if (TextRec(t).mode<>fmOutput) Then
  754. begin
  755. if TextRec(t).mode=fmClosed then
  756. InOutRes:=103
  757. else
  758. InOutRes:=105;
  759. exit;
  760. end;
  761. If Len>1 Then
  762. fpc_WriteBlanks(t,Len-1);
  763. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  764. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  765. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  766. Inc(TextRec(t).BufPos);
  767. End;
  768. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; [Public,Alias:'FPC_WRITE_TEXT_WIDECHAR']; compilerproc;
  769. var
  770. a : ansistring;
  771. Begin
  772. If (InOutRes<>0) then
  773. exit;
  774. if (TextRec(t).mode<>fmOutput) Then
  775. begin
  776. if TextRec(t).mode=fmClosed then
  777. InOutRes:=103
  778. else
  779. InOutRes:=105;
  780. exit;
  781. end;
  782. If Len>1 Then
  783. fpc_WriteBlanks(t,Len-1);
  784. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  785. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  786. { a widechar can be translated into more than a single ansichar }
  787. a:=c;
  788. fpc_WriteBuffer(t,pchar(a)^,length(a));
  789. End;
  790. {*****************************************************************************
  791. Read(Ln)
  792. *****************************************************************************}
  793. Function NextChar(var f:Text;var s:string):Boolean;
  794. begin
  795. NextChar:=false;
  796. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  797. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  798. begin
  799. if length(s)<high(s) then
  800. begin
  801. inc(s[0]);
  802. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  803. end;
  804. Inc(TextRec(f).BufPos);
  805. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  806. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  807. NextChar:=true;
  808. end;
  809. end;
  810. Function IgnoreSpaces(var f:Text):Boolean;
  811. {
  812. Removes all leading spaces,tab,eols from the input buffer, returns true if
  813. the buffer is empty
  814. }
  815. var
  816. s : string;
  817. begin
  818. s:='';
  819. IgnoreSpaces:=false;
  820. { Return false when already at EOF }
  821. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  822. exit;
  823. (* Check performed separately to avoid accessing memory outside buffer *)
  824. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  825. exit;
  826. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  827. begin
  828. if not NextChar(f,s) then
  829. exit;
  830. { EOF? }
  831. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  832. break;
  833. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  834. break;
  835. end;
  836. IgnoreSpaces:=true;
  837. end;
  838. procedure ReadNumeric(var f:Text;var s:string);
  839. {
  840. Read numeric input, if buffer is empty then return True
  841. }
  842. begin
  843. repeat
  844. if not NextChar(f,s) then
  845. exit;
  846. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  847. end;
  848. function CheckRead(var f:Text):Boolean;
  849. begin
  850. CheckRead:=False;
  851. { Check error and if file is open and load buf if empty }
  852. If (InOutRes<>0) then
  853. exit;
  854. if (TextRec(f).mode<>fmInput) Then
  855. begin
  856. case TextRec(f).mode of
  857. fmOutPut,fmAppend:
  858. InOutRes:=104;
  859. else
  860. InOutRes:=103;
  861. end;
  862. exit;
  863. end;
  864. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  865. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  866. CheckRead:=True;
  867. end;
  868. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  869. begin
  870. if TextRec(f).FlushFunc<>nil then
  871. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  872. end;
  873. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  874. var prev: char;
  875. Begin
  876. If not CheckRead(f) then
  877. exit;
  878. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  879. { Flush if set }
  880. begin
  881. if (TextRec(f).FlushFunc<>nil) then
  882. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  883. exit;
  884. end;
  885. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  886. Exit;
  887. repeat
  888. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  889. inc(TextRec(f).BufPos);
  890. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  891. { #13#10 = Dos), so if we've got #10, we can safely exit }
  892. if prev = #10 then
  893. exit;
  894. {$ifdef MACOS}
  895. if prev = #13 then
  896. {StdInput on macos never have dos line ending, so this is safe.}
  897. if TextRec(f).Handle = StdInputHandle then
  898. exit;
  899. {$endif MACOS}
  900. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  901. begin
  902. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  903. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  904. { Flush if set }
  905. begin
  906. if (TextRec(f).FlushFunc<>nil) then
  907. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  908. exit;
  909. end;
  910. end;
  911. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  912. Exit;
  913. if (prev=#13) then
  914. { is there also a #10 after it? }
  915. begin
  916. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  917. { yes, skip that one as well }
  918. inc(TextRec(f).BufPos);
  919. exit;
  920. end;
  921. until false;
  922. End;
  923. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  924. var
  925. sPos,len : Longint;
  926. p,startp,maxp : pchar;
  927. end_of_string:boolean;
  928. Begin
  929. ReadPCharLen:=0;
  930. If not CheckRead(f) then
  931. exit;
  932. { Read maximal until Maxlen is reached }
  933. sPos:=0;
  934. end_of_string:=false;
  935. repeat
  936. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  937. begin
  938. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  939. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  940. break;
  941. end;
  942. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  943. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  944. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  945. else
  946. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  947. startp:=p;
  948. { find stop character }
  949. while p<maxp do
  950. begin
  951. { Optimization: Do a quick check for a control character first }
  952. if (p^<' ') then
  953. begin
  954. if (p^ in [#10,#13]) or
  955. (ctrlZmarkseof and (p^=#26)) then
  956. begin
  957. end_of_string:=true;
  958. break;
  959. end;
  960. end;
  961. inc(p);
  962. end;
  963. { calculate read bytes }
  964. len:=p-startp;
  965. inc(TextRec(f).BufPos,Len);
  966. Move(startp^,s[sPos],Len);
  967. inc(sPos,Len);
  968. until (spos=MaxLen) or end_of_string;
  969. ReadPCharLen:=spos;
  970. End;
  971. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; [Public,Alias:'FPC_READ_TEXT_SHORTSTR']; compilerproc;
  972. Begin
  973. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  974. End;
  975. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; [Public,Alias:'FPC_READ_TEXT_PCHAR_AS_POINTER']; compilerproc;
  976. Begin
  977. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  978. End;
  979. 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;
  980. var
  981. len: longint;
  982. Begin
  983. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  984. if zerobased and
  985. (len > high(s)) then
  986. len := high(s);
  987. if (len <= high(s)) then
  988. s[len] := #0;
  989. End;
  990. Procedure fpc_Read_Text_AnsiStr(var f : Text;out s : AnsiString); iocheck; [Public,Alias:'FPC_READ_TEXT_ANSISTR']; compilerproc;
  991. var
  992. slen,len : SizeInt;
  993. Begin
  994. slen:=0;
  995. Repeat
  996. // SetLength will reallocate the length.
  997. SetLength(S,slen+255);
  998. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  999. inc(slen,len);
  1000. Until len<255;
  1001. // Set actual length
  1002. SetLength(S,Slen);
  1003. End;
  1004. procedure fpc_Read_Text_Char(var f : Text; out c: char); iocheck; [Public,Alias:'FPC_READ_TEXT_CHAR'];compilerproc;
  1005. Begin
  1006. c:=#0;
  1007. If not CheckRead(f) then
  1008. exit;
  1009. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1010. begin
  1011. c := #26;
  1012. exit;
  1013. end;
  1014. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1015. inc(TextRec(f).BufPos);
  1016. end;
  1017. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; [Public,Alias:'FPC_READ_TEXT_SINT']; compilerproc;
  1018. var
  1019. hs : String;
  1020. code : longint;
  1021. Begin
  1022. l:=0;
  1023. If not CheckRead(f) then
  1024. exit;
  1025. hs:='';
  1026. if IgnoreSpaces(f) then
  1027. begin
  1028. { When spaces were found and we are now at EOF,
  1029. then we return 0 }
  1030. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1031. exit;
  1032. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1033. exit;
  1034. ReadNumeric(f,hs);
  1035. end;
  1036. if (hs = '') then
  1037. L := 0
  1038. else
  1039. begin
  1040. Val(hs,l,code);
  1041. if Code <> 0 then
  1042. InOutRes:=106;
  1043. end;
  1044. End;
  1045. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; [Public,Alias:'FPC_READ_TEXT_UINT']; compilerproc;
  1046. var
  1047. hs : String;
  1048. code : longint;
  1049. Begin
  1050. u:=0;
  1051. If not CheckRead(f) then
  1052. exit;
  1053. hs:='';
  1054. if IgnoreSpaces(f) then
  1055. begin
  1056. { When spaces were found and we are now at EOF,
  1057. then we return 0 }
  1058. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1059. exit;
  1060. ReadNumeric(f,hs);
  1061. end;
  1062. if (hs = '') then
  1063. u := 0
  1064. else
  1065. begin
  1066. val(hs,u,code);
  1067. If code<>0 Then
  1068. InOutRes:=106;
  1069. end;
  1070. End;
  1071. {$ifndef FPUNONE}
  1072. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; [Public,Alias:'FPC_READ_TEXT_FLOAT']; compilerproc;
  1073. var
  1074. hs : string;
  1075. code : Word;
  1076. begin
  1077. v:=0.0;
  1078. If not CheckRead(f) then
  1079. exit;
  1080. hs:='';
  1081. if IgnoreSpaces(f) then
  1082. begin
  1083. { When spaces were found and we are now at EOF,
  1084. then we return 0 }
  1085. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1086. exit;
  1087. ReadNumeric(f,hs);
  1088. end;
  1089. val(hs,v,code);
  1090. If code<>0 Then
  1091. InOutRes:=106;
  1092. end;
  1093. {$endif}
  1094. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck; [Public,Alias:'FPC_READ_TEXT_ENUM'];compilerproc;
  1095. var s:string;
  1096. code:valsint;
  1097. begin
  1098. if not checkread(t) then
  1099. exit;
  1100. s:='';
  1101. if ignorespaces(t) then
  1102. begin
  1103. { When spaces were found and we are now at EOF, then we return 0 }
  1104. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1105. exit;
  1106. ReadNumeric(t,s);
  1107. end;
  1108. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1109. if code<>0 then
  1110. InOutRes:=106;
  1111. end;
  1112. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; [Public,Alias:'FPC_READ_TEXT_CURRENCY']; compilerproc;
  1113. var
  1114. hs : string;
  1115. code : Word;
  1116. begin
  1117. {$ifdef FPUNONE}
  1118. v:=0;
  1119. {$else}
  1120. v:=0.0;
  1121. {$endif}
  1122. If not CheckRead(f) then
  1123. exit;
  1124. hs:='';
  1125. if IgnoreSpaces(f) then
  1126. begin
  1127. { When spaces were found and we are now at EOF,
  1128. then we return 0 }
  1129. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1130. exit;
  1131. ReadNumeric(f,hs);
  1132. end;
  1133. val(hs,v,code);
  1134. If code<>0 Then
  1135. InOutRes:=106;
  1136. end;
  1137. {$ifndef cpu64}
  1138. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; [public,alias:'FPC_READ_TEXT_QWORD']; compilerproc;
  1139. var
  1140. hs : String;
  1141. code : longint;
  1142. Begin
  1143. q:=0;
  1144. If not CheckRead(f) then
  1145. exit;
  1146. hs:='';
  1147. if IgnoreSpaces(f) then
  1148. begin
  1149. { When spaces were found and we are now at EOF,
  1150. then we return 0 }
  1151. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1152. exit;
  1153. ReadNumeric(f,hs);
  1154. end;
  1155. val(hs,q,code);
  1156. If code<>0 Then
  1157. InOutRes:=106;
  1158. End;
  1159. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; [public,alias:'FPC_READ_TEXT_INT64']; compilerproc;
  1160. var
  1161. hs : String;
  1162. code : Longint;
  1163. Begin
  1164. i:=0;
  1165. If not CheckRead(f) then
  1166. exit;
  1167. hs:='';
  1168. if IgnoreSpaces(f) then
  1169. begin
  1170. { When spaces were found and we are now at EOF,
  1171. then we return 0 }
  1172. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1173. exit;
  1174. ReadNumeric(f,hs);
  1175. end;
  1176. Val(hs,i,code);
  1177. If code<>0 Then
  1178. InOutRes:=106;
  1179. End;
  1180. {$endif CPU64}
  1181. {*****************************************************************************
  1182. WriteStr/ReadStr
  1183. *****************************************************************************}
  1184. const
  1185. StrPtrIndex = 1;
  1186. { leave space for 128 bit string pointers :) (used for writestr) }
  1187. ShortStrLenIndex = 17;
  1188. { how many bytes of the string have been processed already (used for readstr) }
  1189. BytesReadIndex = 17;
  1190. threadvar
  1191. ReadWriteStrText: textrec;
  1192. procedure WriteStrShort(var t: textrec);
  1193. var
  1194. str: pshortstring;
  1195. newbytes,
  1196. oldlen: longint;
  1197. begin
  1198. if (t.bufpos=0) then
  1199. exit;
  1200. str:=pshortstring(ppointer(@t.userdata[StrPtrIndex])^);
  1201. newbytes:=t.BufPos;
  1202. oldlen:=length(str^);
  1203. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1204. begin
  1205. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1206. {$ifdef writestr_iolencheck}
  1207. // GPC only gives an io error if {$no-truncate-strings} is active
  1208. // FPC does not have this setting (it never gives errors when a
  1209. // a string expression is truncated)
  1210. { "disk full" }
  1211. inoutres:=101;
  1212. {$endif}
  1213. end;
  1214. setlength(str^,length(str^)+newbytes);
  1215. move(t.bufptr^,str^[oldlen+1],newbytes);
  1216. t.bufpos:=0;
  1217. end;
  1218. procedure WriteStrAnsi(var t: textrec);
  1219. var
  1220. str: pansistring;
  1221. oldlen: longint;
  1222. begin
  1223. if (t.bufpos=0) then
  1224. exit;
  1225. str:=pansistring(ppointer(@t.userdata[StrPtrIndex])^);
  1226. oldlen:=length(str^);
  1227. setlength(str^,oldlen+t.bufpos);
  1228. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1229. t.bufpos:=0;
  1230. end;
  1231. procedure WriteStrWide(var t: textrec);
  1232. var
  1233. temp: ansistring;
  1234. str: pwidestring;
  1235. begin
  1236. if (t.bufpos=0) then
  1237. exit;
  1238. str:=pwidestring(ppointer(@t.userdata[StrPtrIndex])^);
  1239. setlength(temp,t.bufpos);
  1240. move(t.bufptr^,temp[1],t.bufpos);
  1241. str^:=str^+temp;
  1242. t.bufpos:=0;
  1243. end;
  1244. procedure SetupWriteStrCommon(out t: textrec);
  1245. begin
  1246. // initialise
  1247. Assign(text(t),'');
  1248. t.mode:=fmOutput;
  1249. t.OpenFunc:=nil;
  1250. t.CloseFunc:=nil;
  1251. end;
  1252. function fpc_SetupWriteStr_Shortstr(out s: shortstring): PText; compilerproc;
  1253. begin
  1254. setupwritestrcommon(ReadWriteStrText);
  1255. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1256. ReadWriteStrText.userdata[ShortStrLenIndex]:=high(s);
  1257. setlength(s,0);
  1258. ReadWriteStrText.InOutFunc:=@WriteStrShort;
  1259. ReadWriteStrText.FlushFunc:=@WriteStrShort;
  1260. result:=@ReadWriteStrText;
  1261. end;
  1262. function fpc_SetupWriteStr_Ansistr(out s: ansistring): PText; compilerproc;
  1263. begin
  1264. setupwritestrcommon(ReadWriteStrText);
  1265. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1266. // automatically done by out-semantics
  1267. // setlength(s,0);
  1268. ReadWriteStrText.InOutFunc:=@WriteStrAnsi;
  1269. ReadWriteStrText.FlushFunc:=@WriteStrAnsi;
  1270. result:=@ReadWriteStrText;
  1271. end;
  1272. function fpc_SetupWriteStr_Widestr(out s: widestring): PText; compilerproc;
  1273. begin
  1274. setupwritestrcommon(ReadWriteStrText);
  1275. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1276. // automatically done by out-semantics
  1277. // setlength(s,0);
  1278. ReadWriteStrText.InOutFunc:=@WriteStrWide;
  1279. ReadWriteStrText.FlushFunc:=@WriteStrWide;
  1280. result:=@ReadWriteStrText;
  1281. end;
  1282. procedure ReadAnsiStrFinal(var t: textrec);
  1283. begin
  1284. { finalise the temp ansistring }
  1285. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1286. end;
  1287. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1288. var
  1289. newbytes: sizeint;
  1290. begin
  1291. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1292. if (t.BufSize <= newbytes) then
  1293. newbytes := t.BufSize;
  1294. if (newbytes > 0) then
  1295. begin
  1296. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1297. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1298. end;
  1299. t.BufEnd:=newbytes;
  1300. t.BufPos:=0;
  1301. end;
  1302. procedure ReadStrAnsi(var t: textrec);
  1303. var
  1304. str: pansistring;
  1305. begin
  1306. str:=pansistring(@t.userdata[StrPtrIndex]);
  1307. ReadStrCommon(t,@str^[1],length(str^));
  1308. end;
  1309. procedure SetupReadStrCommon(out t: textrec);
  1310. begin
  1311. // initialise
  1312. Assign(text(t),'');
  1313. t.mode:=fmInput;
  1314. t.OpenFunc:=nil;
  1315. t.CloseFunc:=nil;
  1316. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1317. end;
  1318. function fpc_SetupReadStr_Ansistr(const s: ansistring): PText; [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1319. begin
  1320. setupreadstrcommon(ReadWriteStrText);
  1321. { we need a reference, because 's' may be a temporary expression }
  1322. PAnsiString(@ReadWriteStrText.userdata[StrPtrIndex])^:=s;
  1323. ReadWriteStrText.InOutFunc:=@ReadStrAnsi;
  1324. { this is called at the end, by fpc_read_end }
  1325. ReadWriteStrText.FlushFunc:=@ReadAnsiStrFinal;
  1326. result:=@ReadWriteStrText;
  1327. end;
  1328. function fpc_SetupReadStr_Ansistr_Intern(const s: ansistring): PText; [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1329. function fpc_SetupReadStr_Shortstr(const s: shortstring): PText; compilerproc;
  1330. begin
  1331. { the reason we convert the short string to ansistring, is because the semantics of
  1332. readstr are defined as:
  1333. *********************
  1334. Apart from the restrictions imposed by requirements given in this clause,
  1335. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  1336. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  1337. char-type (or a subrange of char-type), the integer-type (or a subrange of
  1338. integer-type), the real-type, a fixed-string-type, or a
  1339. variable-string-type, shall be equivalent to
  1340. begin
  1341. rewrite(f);
  1342. writeln(f, e);
  1343. reset(f);
  1344. read(f, v 1 ,...,v n )
  1345. end
  1346. *********************
  1347. This means that any side effects caused by the evaluation of v 1 .. v n
  1348. must not affect the value of e (= our argument s) -> we need a copy of it.
  1349. An ansistring is the easiest way to get a threadsafe copy, and allows us
  1350. to use the other ansistring readstr helpers too.
  1351. }
  1352. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1353. end;
  1354. function fpc_SetupReadStr_Widestr(const s: widestring): PText; compilerproc;
  1355. begin
  1356. { we use an ansistring to avoid code duplication, and let the }
  1357. { assignment convert the widestring to an equivalent ansistring }
  1358. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1359. end;
  1360. {*****************************************************************************
  1361. Initializing
  1362. *****************************************************************************}
  1363. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1364. begin
  1365. Assign(f,'');
  1366. TextRec(f).Handle:=hdl;
  1367. TextRec(f).Mode:=mode;
  1368. TextRec(f).Closefunc:=@FileCloseFunc;
  1369. case mode of
  1370. fmInput :
  1371. TextRec(f).InOutFunc:=@FileReadFunc;
  1372. fmOutput :
  1373. begin
  1374. TextRec(f).InOutFunc:=@FileWriteFunc;
  1375. if Do_Isdevice(hdl) then
  1376. TextRec(f).FlushFunc:=@FileWriteFunc;
  1377. end;
  1378. else
  1379. HandleError(102);
  1380. end;
  1381. end;