text.inc 38 KB

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