text.inc 38 KB

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