text.inc 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. subroutines For TextFile handling
  12. ****************************************************************************}
  13. Procedure FileCloseFunc(Var t:TextRec);
  14. Begin
  15. Do_Close(t.Handle);
  16. t.Handle:=UnusedHandle;
  17. End;
  18. Procedure FileReadFunc(var t:TextRec);
  19. Begin
  20. t.BufEnd:=Do_Read(t.Handle,t.Bufptr,t.BufSize);
  21. t.BufPos:=0;
  22. End;
  23. Procedure FileWriteFunc(var t:TextRec);
  24. var
  25. i : longint;
  26. Begin
  27. { prevent unecessary system call }
  28. if t.BufPos=0 then
  29. exit;
  30. i:=Do_Write(t.Handle,t.Bufptr,t.BufPos);
  31. if i<>t.BufPos then
  32. InOutRes:=101;
  33. t.BufPos:=0;
  34. End;
  35. Procedure FileOpenFunc(var t:TextRec);
  36. var
  37. Flags : Longint;
  38. Begin
  39. Case t.mode Of
  40. fmInput : Flags:=$10000;
  41. fmOutput : Flags:=$11001;
  42. fmAppend : Flags:=$10101;
  43. else
  44. begin
  45. InOutRes:=102;
  46. exit;
  47. end;
  48. End;
  49. Do_Open(t,PChar(@t.Name),Flags);
  50. t.CloseFunc:=@FileCloseFunc;
  51. t.FlushFunc:=nil;
  52. if t.Mode=fmInput then
  53. t.InOutFunc:=@FileReadFunc
  54. else
  55. begin
  56. t.InOutFunc:=@FileWriteFunc;
  57. { Only install flushing if its a NOT a file, and only check if there
  58. was no error opening the file, because else we always get a bad
  59. file handle error 6 (PFV) }
  60. if (InOutRes=0) and
  61. Do_Isdevice(t.Handle) then
  62. t.FlushFunc:=@FileWriteFunc;
  63. end;
  64. End;
  65. Procedure Assign(out t:Text;const s:String);
  66. Begin
  67. FillChar(t,SizeOf(TextRec),0);
  68. { only set things that are not zero }
  69. TextRec(t).Handle:=UnusedHandle;
  70. TextRec(t).mode:=fmClosed;
  71. TextRec(t).BufSize:=TextRecBufSize;
  72. TextRec(t).Bufptr:=@TextRec(t).Buffer;
  73. TextRec(t).OpenFunc:=@FileOpenFunc;
  74. Case DefaultTextLineBreakStyle Of
  75. tlbsLF: TextRec(t).LineEnd := #10;
  76. tlbsCRLF: TextRec(t).LineEnd := #13#10;
  77. tlbsCR: TextRec(t).LineEnd := #13;
  78. End;
  79. Move(s[1],TextRec(t).Name,Length(s));
  80. End;
  81. Procedure Assign(out t:Text;p:pchar);
  82. begin
  83. Assign(t,StrPas(p));
  84. end;
  85. Procedure Assign(out t:Text;c:char);
  86. begin
  87. Assign(t,string(c));
  88. end;
  89. Procedure Close(var t : Text);[IOCheck];
  90. Begin
  91. if InOutRes<>0 then
  92. Exit;
  93. case TextRec(t).mode of
  94. fmInput,fmOutput,fmAppend:
  95. Begin
  96. { Write pending buffer }
  97. If Textrec(t).Mode=fmoutput then
  98. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  99. { 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);
  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);
  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); 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); 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; 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; 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. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  542. Procedure fpc_Write_Text_UnicodeStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  543. {
  544. Writes a UnicodeString to the Text file T
  545. }
  546. var
  547. SLen : longint;
  548. a: ansistring;
  549. begin
  550. If (pointer(S)=nil) or (InOutRes<>0) then
  551. exit;
  552. case TextRec(f).mode of
  553. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  554. begin
  555. SLen:=Length(s);
  556. If Len>SLen Then
  557. fpc_WriteBlanks(f,Len-SLen);
  558. a:=s;
  559. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  560. fpc_WriteBuffer(f,pchar(a)^,length(a));
  561. end;
  562. fmInput: InOutRes:=105
  563. else InOutRes:=103;
  564. end;
  565. end;
  566. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  567. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  568. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; 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; 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; 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; 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; 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; 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; 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; 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; 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_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  752. Begin
  753. If (InOutRes<>0) then
  754. exit;
  755. { Can't use array[boolean] because b can be >0 ! }
  756. if b then
  757. Write_Str(Len,t,'true')
  758. else
  759. Write_Str(Len,t,'false');
  760. End;
  761. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  762. Begin
  763. If (InOutRes<>0) then
  764. exit;
  765. if (TextRec(t).mode<>fmOutput) Then
  766. begin
  767. if TextRec(t).mode=fmClosed then
  768. InOutRes:=103
  769. else
  770. InOutRes:=105;
  771. exit;
  772. end;
  773. If Len>1 Then
  774. fpc_WriteBlanks(t,Len-1);
  775. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  776. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  777. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  778. Inc(TextRec(t).BufPos);
  779. End;
  780. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  781. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  782. var
  783. a : ansistring;
  784. Begin
  785. If (InOutRes<>0) then
  786. exit;
  787. if (TextRec(t).mode<>fmOutput) Then
  788. begin
  789. if TextRec(t).mode=fmClosed then
  790. InOutRes:=103
  791. else
  792. InOutRes:=105;
  793. exit;
  794. end;
  795. If Len>1 Then
  796. fpc_WriteBlanks(t,Len-1);
  797. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  798. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  799. { a widechar can be translated into more than a single ansichar }
  800. a:=c;
  801. fpc_WriteBuffer(t,pchar(a)^,length(a));
  802. End;
  803. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  804. {*****************************************************************************
  805. Read(Ln)
  806. *****************************************************************************}
  807. Function NextChar(var f:Text;var s:string):Boolean;
  808. begin
  809. NextChar:=false;
  810. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  811. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  812. begin
  813. if length(s)<high(s) then
  814. begin
  815. inc(s[0]);
  816. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  817. end;
  818. Inc(TextRec(f).BufPos);
  819. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  820. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  821. NextChar:=true;
  822. end;
  823. end;
  824. Function IgnoreSpaces(var f:Text):Boolean;
  825. {
  826. Removes all leading spaces,tab,eols from the input buffer, returns true if
  827. the buffer is empty
  828. }
  829. var
  830. s : string;
  831. begin
  832. s:='';
  833. IgnoreSpaces:=false;
  834. { Return false when already at EOF }
  835. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  836. exit;
  837. (* Check performed separately to avoid accessing memory outside buffer *)
  838. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  839. exit;
  840. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  841. begin
  842. if not NextChar(f,s) then
  843. exit;
  844. { EOF? }
  845. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  846. break;
  847. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  848. break;
  849. end;
  850. IgnoreSpaces:=true;
  851. end;
  852. procedure ReadNumeric(var f:Text;var s:string);
  853. {
  854. Read numeric input, if buffer is empty then return True
  855. }
  856. begin
  857. repeat
  858. if not NextChar(f,s) then
  859. exit;
  860. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  861. end;
  862. function CheckRead(var f:Text):Boolean;
  863. begin
  864. CheckRead:=False;
  865. { Check error and if file is open and load buf if empty }
  866. If (InOutRes<>0) then
  867. exit;
  868. if (TextRec(f).mode<>fmInput) Then
  869. begin
  870. case TextRec(f).mode of
  871. fmOutPut,fmAppend:
  872. InOutRes:=104;
  873. else
  874. InOutRes:=103;
  875. end;
  876. exit;
  877. end;
  878. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  879. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  880. CheckRead:=True;
  881. end;
  882. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  883. begin
  884. if TextRec(f).FlushFunc<>nil then
  885. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  886. end;
  887. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  888. var prev: char;
  889. Begin
  890. If not CheckRead(f) then
  891. exit;
  892. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  893. { Flush if set }
  894. begin
  895. if (TextRec(f).FlushFunc<>nil) then
  896. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  897. exit;
  898. end;
  899. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  900. Exit;
  901. repeat
  902. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  903. inc(TextRec(f).BufPos);
  904. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  905. { #13#10 = Dos), so if we've got #10, we can safely exit }
  906. if prev = #10 then
  907. exit;
  908. {$ifdef MACOS}
  909. if prev = #13 then
  910. {StdInput on macos never have dos line ending, so this is safe.}
  911. if TextRec(f).Handle = StdInputHandle then
  912. exit;
  913. {$endif MACOS}
  914. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  915. begin
  916. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  917. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  918. { Flush if set }
  919. begin
  920. if (TextRec(f).FlushFunc<>nil) then
  921. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  922. exit;
  923. end;
  924. end;
  925. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  926. Exit;
  927. if (prev=#13) then
  928. { is there also a #10 after it? }
  929. begin
  930. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  931. { yes, skip that one as well }
  932. inc(TextRec(f).BufPos);
  933. exit;
  934. end;
  935. until false;
  936. End;
  937. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  938. var
  939. sPos,len : Longint;
  940. p,startp,maxp : pchar;
  941. end_of_string:boolean;
  942. Begin
  943. ReadPCharLen:=0;
  944. If not CheckRead(f) then
  945. exit;
  946. { Read maximal until Maxlen is reached }
  947. sPos:=0;
  948. end_of_string:=false;
  949. repeat
  950. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  951. begin
  952. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  953. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  954. break;
  955. end;
  956. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  957. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  958. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  959. else
  960. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  961. startp:=p;
  962. { find stop character }
  963. while p<maxp do
  964. begin
  965. { Optimization: Do a quick check for a control character first }
  966. if (p^<' ') then
  967. begin
  968. if (p^ in [#10,#13]) or
  969. (ctrlZmarkseof and (p^=#26)) then
  970. begin
  971. end_of_string:=true;
  972. break;
  973. end;
  974. end;
  975. inc(p);
  976. end;
  977. { calculate read bytes }
  978. len:=p-startp;
  979. inc(TextRec(f).BufPos,Len);
  980. Move(startp^,s[sPos],Len);
  981. inc(sPos,Len);
  982. until (spos=MaxLen) or end_of_string;
  983. ReadPCharLen:=spos;
  984. End;
  985. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  986. Begin
  987. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  988. End;
  989. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  990. Begin
  991. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  992. End;
  993. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  994. var
  995. len: longint;
  996. Begin
  997. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  998. if zerobased and
  999. (len > high(s)) then
  1000. len := high(s);
  1001. if (len <= high(s)) then
  1002. s[len] := #0;
  1003. End;
  1004. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1005. Procedure fpc_Read_Text_AnsiStr(var f : Text;out s : AnsiString); iocheck; compilerproc;
  1006. var
  1007. slen,len : SizeInt;
  1008. Begin
  1009. slen:=0;
  1010. Repeat
  1011. // SetLength will reallocate the length.
  1012. SetLength(S,slen+255);
  1013. len:=ReadPCharLen(f,pchar(Pointer(S)+slen),255);
  1014. inc(slen,len);
  1015. Until len<255;
  1016. // Set actual length
  1017. SetLength(S,Slen);
  1018. End;
  1019. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1020. procedure fpc_Read_Text_Char(var f : Text; out c: char); iocheck;compilerproc;
  1021. Begin
  1022. c:=#0;
  1023. If not CheckRead(f) then
  1024. exit;
  1025. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1026. begin
  1027. c := #26;
  1028. exit;
  1029. end;
  1030. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1031. inc(TextRec(f).BufPos);
  1032. end;
  1033. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1034. var
  1035. hs : String;
  1036. code : longint;
  1037. Begin
  1038. l:=0;
  1039. If not CheckRead(f) then
  1040. exit;
  1041. hs:='';
  1042. if IgnoreSpaces(f) then
  1043. begin
  1044. { When spaces were found and we are now at EOF,
  1045. then we return 0 }
  1046. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1047. exit;
  1048. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1049. exit;
  1050. ReadNumeric(f,hs);
  1051. end;
  1052. if (hs = '') then
  1053. L := 0
  1054. else
  1055. begin
  1056. Val(hs,l,code);
  1057. if Code <> 0 then
  1058. InOutRes:=106;
  1059. end;
  1060. End;
  1061. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1062. var
  1063. hs : String;
  1064. code : longint;
  1065. Begin
  1066. u:=0;
  1067. If not CheckRead(f) then
  1068. exit;
  1069. hs:='';
  1070. if IgnoreSpaces(f) then
  1071. begin
  1072. { When spaces were found and we are now at EOF,
  1073. then we return 0 }
  1074. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1075. exit;
  1076. ReadNumeric(f,hs);
  1077. end;
  1078. if (hs = '') then
  1079. u := 0
  1080. else
  1081. begin
  1082. val(hs,u,code);
  1083. If code<>0 Then
  1084. InOutRes:=106;
  1085. end;
  1086. End;
  1087. {$ifndef FPUNONE}
  1088. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1089. var
  1090. hs : string;
  1091. code : Word;
  1092. begin
  1093. v:=0.0;
  1094. If not CheckRead(f) then
  1095. exit;
  1096. hs:='';
  1097. if IgnoreSpaces(f) then
  1098. begin
  1099. { When spaces were found and we are now at EOF,
  1100. then we return 0 }
  1101. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1102. exit;
  1103. ReadNumeric(f,hs);
  1104. end;
  1105. val(hs,v,code);
  1106. If code<>0 Then
  1107. InOutRes:=106;
  1108. end;
  1109. {$endif}
  1110. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1111. var s:string;
  1112. code:valsint;
  1113. begin
  1114. if not checkread(t) then
  1115. exit;
  1116. s:='';
  1117. if ignorespaces(t) then
  1118. begin
  1119. { When spaces were found and we are now at EOF, then we return 0 }
  1120. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1121. exit;
  1122. ReadNumeric(t,s);
  1123. end;
  1124. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1125. if code<>0 then
  1126. InOutRes:=106;
  1127. end;
  1128. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1129. var
  1130. hs : string;
  1131. code : Word;
  1132. begin
  1133. {$ifdef FPUNONE}
  1134. v:=0;
  1135. {$else}
  1136. v:=0.0;
  1137. {$endif}
  1138. If not CheckRead(f) then
  1139. exit;
  1140. hs:='';
  1141. if IgnoreSpaces(f) then
  1142. begin
  1143. { When spaces were found and we are now at EOF,
  1144. then we return 0 }
  1145. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1146. exit;
  1147. ReadNumeric(f,hs);
  1148. end;
  1149. val(hs,v,code);
  1150. If code<>0 Then
  1151. InOutRes:=106;
  1152. end;
  1153. {$ifndef cpu64}
  1154. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1155. var
  1156. hs : String;
  1157. code : longint;
  1158. Begin
  1159. q:=0;
  1160. If not CheckRead(f) then
  1161. exit;
  1162. hs:='';
  1163. if IgnoreSpaces(f) then
  1164. begin
  1165. { When spaces were found and we are now at EOF,
  1166. then we return 0 }
  1167. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1168. exit;
  1169. ReadNumeric(f,hs);
  1170. end;
  1171. val(hs,q,code);
  1172. If code<>0 Then
  1173. InOutRes:=106;
  1174. End;
  1175. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1176. var
  1177. hs : String;
  1178. code : Longint;
  1179. Begin
  1180. i:=0;
  1181. If not CheckRead(f) then
  1182. exit;
  1183. hs:='';
  1184. if IgnoreSpaces(f) then
  1185. begin
  1186. { When spaces were found and we are now at EOF,
  1187. then we return 0 }
  1188. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1189. exit;
  1190. ReadNumeric(f,hs);
  1191. end;
  1192. Val(hs,i,code);
  1193. If code<>0 Then
  1194. InOutRes:=106;
  1195. End;
  1196. {$endif CPU64}
  1197. {*****************************************************************************
  1198. WriteStr/ReadStr
  1199. *****************************************************************************}
  1200. const
  1201. StrPtrIndex = 1;
  1202. { leave space for 128 bit string pointers :) (used for writestr) }
  1203. ShortStrLenIndex = 17;
  1204. { how many bytes of the string have been processed already (used for readstr) }
  1205. BytesReadIndex = 17;
  1206. threadvar
  1207. ReadWriteStrText: textrec;
  1208. procedure WriteStrShort(var t: textrec);
  1209. var
  1210. str: pshortstring;
  1211. newbytes,
  1212. oldlen: longint;
  1213. begin
  1214. if (t.bufpos=0) then
  1215. exit;
  1216. str:=pshortstring(ppointer(@t.userdata[StrPtrIndex])^);
  1217. newbytes:=t.BufPos;
  1218. oldlen:=length(str^);
  1219. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1220. begin
  1221. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1222. {$ifdef writestr_iolencheck}
  1223. // GPC only gives an io error if {$no-truncate-strings} is active
  1224. // FPC does not have this setting (it never gives errors when a
  1225. // a string expression is truncated)
  1226. { "disk full" }
  1227. inoutres:=101;
  1228. {$endif}
  1229. end;
  1230. setlength(str^,length(str^)+newbytes);
  1231. move(t.bufptr^,str^[oldlen+1],newbytes);
  1232. t.bufpos:=0;
  1233. end;
  1234. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1235. procedure WriteStrAnsi(var t: textrec);
  1236. var
  1237. str: pansistring;
  1238. oldlen: longint;
  1239. begin
  1240. if (t.bufpos=0) then
  1241. exit;
  1242. str:=pansistring(ppointer(@t.userdata[StrPtrIndex])^);
  1243. oldlen:=length(str^);
  1244. setlength(str^,oldlen+t.bufpos);
  1245. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1246. t.bufpos:=0;
  1247. end;
  1248. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1249. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1250. procedure WriteStrWide(var t: textrec);
  1251. var
  1252. temp: ansistring;
  1253. str: pwidestring;
  1254. begin
  1255. if (t.bufpos=0) then
  1256. exit;
  1257. str:=pwidestring(ppointer(@t.userdata[StrPtrIndex])^);
  1258. setlength(temp,t.bufpos);
  1259. move(t.bufptr^,temp[1],t.bufpos);
  1260. str^:=str^+temp;
  1261. t.bufpos:=0;
  1262. end;
  1263. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1264. procedure SetupWriteStrCommon(out t: textrec);
  1265. begin
  1266. // initialise
  1267. Assign(text(t),'');
  1268. t.mode:=fmOutput;
  1269. t.OpenFunc:=nil;
  1270. t.CloseFunc:=nil;
  1271. end;
  1272. function fpc_SetupWriteStr_Shortstr(out s: shortstring): PText; compilerproc;
  1273. begin
  1274. setupwritestrcommon(ReadWriteStrText);
  1275. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1276. ReadWriteStrText.userdata[ShortStrLenIndex]:=high(s);
  1277. setlength(s,0);
  1278. ReadWriteStrText.InOutFunc:=@WriteStrShort;
  1279. ReadWriteStrText.FlushFunc:=@WriteStrShort;
  1280. result:=@ReadWriteStrText;
  1281. end;
  1282. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1283. function fpc_SetupWriteStr_Ansistr(out s: ansistring): PText; compilerproc;
  1284. begin
  1285. setupwritestrcommon(ReadWriteStrText);
  1286. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1287. // automatically done by out-semantics
  1288. // setlength(s,0);
  1289. ReadWriteStrText.InOutFunc:=@WriteStrAnsi;
  1290. ReadWriteStrText.FlushFunc:=@WriteStrAnsi;
  1291. result:=@ReadWriteStrText;
  1292. end;
  1293. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1294. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1295. function fpc_SetupWriteStr_Widestr(out s: widestring): PText; compilerproc;
  1296. begin
  1297. setupwritestrcommon(ReadWriteStrText);
  1298. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1299. // automatically done by out-semantics
  1300. // setlength(s,0);
  1301. ReadWriteStrText.InOutFunc:=@WriteStrWide;
  1302. ReadWriteStrText.FlushFunc:=@WriteStrWide;
  1303. result:=@ReadWriteStrText;
  1304. end;
  1305. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1306. procedure ReadAnsiStrFinal(var t: textrec);
  1307. begin
  1308. { finalise the temp ansistring }
  1309. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1310. end;
  1311. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1312. var
  1313. newbytes: sizeint;
  1314. begin
  1315. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1316. if (t.BufSize <= newbytes) then
  1317. newbytes := t.BufSize;
  1318. if (newbytes > 0) then
  1319. begin
  1320. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1321. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1322. end;
  1323. t.BufEnd:=newbytes;
  1324. t.BufPos:=0;
  1325. end;
  1326. procedure ReadStrAnsi(var t: textrec);
  1327. var
  1328. str: pansistring;
  1329. begin
  1330. str:=pansistring(@t.userdata[StrPtrIndex]);
  1331. ReadStrCommon(t,@str^[1],length(str^));
  1332. end;
  1333. procedure SetupReadStrCommon(out t: textrec);
  1334. begin
  1335. // initialise
  1336. Assign(text(t),'');
  1337. t.mode:=fmInput;
  1338. t.OpenFunc:=nil;
  1339. t.CloseFunc:=nil;
  1340. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1341. end;
  1342. function fpc_SetupReadStr_Ansistr(const s: ansistring): PText; [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1343. begin
  1344. setupreadstrcommon(ReadWriteStrText);
  1345. { we need a reference, because 's' may be a temporary expression }
  1346. PAnsiString(@ReadWriteStrText.userdata[StrPtrIndex])^:=s;
  1347. ReadWriteStrText.InOutFunc:=@ReadStrAnsi;
  1348. { this is called at the end, by fpc_read_end }
  1349. ReadWriteStrText.FlushFunc:=@ReadAnsiStrFinal;
  1350. result:=@ReadWriteStrText;
  1351. end;
  1352. function fpc_SetupReadStr_Ansistr_Intern(const s: ansistring): PText; [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1353. function fpc_SetupReadStr_Shortstr(const s: shortstring): PText; compilerproc;
  1354. begin
  1355. { the reason we convert the short string to ansistring, is because the semantics of
  1356. readstr are defined as:
  1357. *********************
  1358. Apart from the restrictions imposed by requirements given in this clause,
  1359. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  1360. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  1361. char-type (or a subrange of char-type), the integer-type (or a subrange of
  1362. integer-type), the real-type, a fixed-string-type, or a
  1363. variable-string-type, shall be equivalent to
  1364. begin
  1365. rewrite(f);
  1366. writeln(f, e);
  1367. reset(f);
  1368. read(f, v 1 ,...,v n )
  1369. end
  1370. *********************
  1371. This means that any side effects caused by the evaluation of v 1 .. v n
  1372. must not affect the value of e (= our argument s) -> we need a copy of it.
  1373. An ansistring is the easiest way to get a threadsafe copy, and allows us
  1374. to use the other ansistring readstr helpers too.
  1375. }
  1376. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1377. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1378. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  1379. runerror(217);
  1380. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1381. end;
  1382. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1383. function fpc_SetupReadStr_Widestr(const s: widestring): PText; compilerproc;
  1384. begin
  1385. { we use an ansistring to avoid code duplication, and let the }
  1386. { assignment convert the widestring to an equivalent ansistring }
  1387. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1388. end;
  1389. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1390. {*****************************************************************************
  1391. Initializing
  1392. *****************************************************************************}
  1393. procedure OpenStdIO(var f:text;mode,hdl:longint);
  1394. begin
  1395. Assign(f,'');
  1396. TextRec(f).Handle:=hdl;
  1397. TextRec(f).Mode:=mode;
  1398. TextRec(f).Closefunc:=@FileCloseFunc;
  1399. case mode of
  1400. fmInput :
  1401. TextRec(f).InOutFunc:=@FileReadFunc;
  1402. fmOutput :
  1403. begin
  1404. TextRec(f).InOutFunc:=@FileWriteFunc;
  1405. if Do_Isdevice(hdl) then
  1406. TextRec(f).FlushFunc:=@FileWriteFunc;
  1407. end;
  1408. else
  1409. HandleError(102);
  1410. end;
  1411. end;