text.inc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  100. { Only close functions not connected to stdout.}
  101. If ((TextRec(t).Handle<>StdInputHandle) and
  102. (TextRec(t).Handle<>StdOutputHandle) and
  103. (TextRec(t).Handle<>StdErrorHandle)) Then
  104. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  105. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  106. TextRec(t).mode := fmClosed;
  107. { Reset buffer for safety }
  108. TextRec(t).BufPos:=0;
  109. TextRec(t).BufEnd:=0;
  110. End
  111. else inOutRes := 103;
  112. End;
  113. End;
  114. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  115. Begin
  116. Case TextRec(t).mode Of {This gives the fastest code}
  117. fmInput,fmOutput,fmInOut : Close(t);
  118. fmClosed : ;
  119. else
  120. Begin
  121. InOutRes:=102;
  122. exit;
  123. End;
  124. End;
  125. TextRec(t).mode:=mode;
  126. TextRec(t).bufpos:=0;
  127. TextRec(t).bufend:=0;
  128. {$ifdef FPC_HAS_CPSTRING}
  129. { if no codepage is yet assigned then assign default ansi codepage }
  130. if TextRec(t).CodePage=CP_ACP then
  131. TextRec(t).CodePage:=DefaultSystemCodePage;
  132. {$endif}
  133. FileFunc(TextRec(t).OpenFunc)(TextRec(t));
  134. { reset the mode to closed when an error has occured }
  135. if InOutRes<>0 then
  136. TextRec(t).mode:=fmClosed;
  137. End;
  138. Procedure Rewrite(var t : Text);[IOCheck];
  139. Begin
  140. If InOutRes<>0 then
  141. exit;
  142. OpenText(t,fmOutput,1);
  143. End;
  144. Procedure Reset(var t : Text);[IOCheck];
  145. Begin
  146. If InOutRes<>0 then
  147. exit;
  148. OpenText(t,fmInput,0);
  149. End;
  150. Procedure Append(var t : Text);[IOCheck];
  151. Begin
  152. If InOutRes<>0 then
  153. exit;
  154. OpenText(t,fmAppend,1);
  155. End;
  156. Procedure Flush(var t : Text);[IOCheck];
  157. Begin
  158. If InOutRes<>0 then
  159. exit;
  160. if TextRec(t).mode<>fmOutput then
  161. begin
  162. if TextRec(t).mode=fmInput then
  163. InOutRes:=105
  164. else
  165. InOutRes:=103;
  166. exit;
  167. end;
  168. { Not the flushfunc but the inoutfunc should be used, because that
  169. writes the data, flushfunc doesn't need to be assigned }
  170. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  171. End;
  172. Procedure Erase(var t:Text);[IOCheck];
  173. Begin
  174. If InOutRes <> 0 then
  175. exit;
  176. If TextRec(t).mode=fmClosed Then
  177. Do_Erase(PChar(@TextRec(t).Name));
  178. End;
  179. Procedure Rename(var t : text;p:pchar);[IOCheck];
  180. Begin
  181. If InOutRes <> 0 then
  182. exit;
  183. If TextRec(t).mode=fmClosed Then
  184. Begin
  185. Do_Rename(PChar(@TextRec(t).Name),p);
  186. { check error code of do_rename }
  187. If InOutRes = 0 then
  188. Move(p^,TextRec(t).Name,StrLen(p)+1);
  189. End;
  190. End;
  191. Procedure Rename(var t : Text;const s : string);[IOCheck];
  192. var
  193. p : array[0..255] Of Char;
  194. Begin
  195. If InOutRes <> 0 then
  196. exit;
  197. Move(s[1],p,Length(s));
  198. p[Length(s)]:=#0;
  199. Rename(t,Pchar(@p));
  200. End;
  201. Procedure Rename(var t : Text;c : char);[IOCheck];
  202. var
  203. p : array[0..1] Of Char;
  204. Begin
  205. If InOutRes <> 0 then
  206. exit;
  207. p[0]:=c;
  208. p[1]:=#0;
  209. Rename(t,Pchar(@p));
  210. End;
  211. Function Eof(Var t: Text): Boolean;[IOCheck];
  212. Begin
  213. If (InOutRes<>0) then
  214. exit(true);
  215. if (TextRec(t).mode<>fmInput) Then
  216. begin
  217. if TextRec(t).mode=fmOutput then
  218. InOutRes:=104
  219. else
  220. InOutRes:=103;
  221. exit(true);
  222. end;
  223. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  224. begin
  225. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  226. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  227. exit(true);
  228. end;
  229. Eof:=CtrlZMarksEOF and (TextRec(t).Bufptr^[TextRec(t).BufPos]=#26);
  230. end;
  231. Function Eof:Boolean;
  232. Begin
  233. Eof:=Eof(Input);
  234. End;
  235. Function SeekEof (Var t : Text) : Boolean;
  236. var
  237. oldfilepos : Int64;
  238. oldbufpos, oldbufend : SizeInt;
  239. reads: longint;
  240. isdevice: boolean;
  241. Begin
  242. If (InOutRes<>0) then
  243. exit(true);
  244. if (TextRec(t).mode<>fmInput) Then
  245. begin
  246. if TextRec(t).mode=fmOutPut then
  247. InOutRes:=104
  248. else
  249. InOutRes:=103;
  250. exit(true);
  251. end;
  252. { try to save the current position in the file, seekeof() should not move }
  253. { the current file position (JM) }
  254. oldbufpos := TextRec(t).BufPos;
  255. oldbufend := TextRec(t).BufEnd;
  256. reads := 0;
  257. oldfilepos := -1;
  258. isdevice := Do_IsDevice(TextRec(t).handle);
  259. repeat
  260. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  261. begin
  262. { signal that the we will have to do a seek }
  263. inc(reads);
  264. if not isdevice and
  265. (reads = 1) then
  266. begin
  267. oldfilepos := Do_FilePos(TextRec(t).handle) - TextRec(t).BufEnd;
  268. InOutRes:=0;
  269. end;
  270. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  271. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  272. begin
  273. { if we only did a read in which we didn't read anything, the }
  274. { old buffer is still valid and we can simply restore the }
  275. { pointers (JM) }
  276. dec(reads);
  277. SeekEof := true;
  278. break;
  279. end;
  280. end;
  281. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  282. #26 :
  283. if CtrlZMarksEOF then
  284. begin
  285. SeekEof := true;
  286. break;
  287. end;
  288. #10,#13,#9,' ' :
  289. ;
  290. else
  291. begin
  292. SeekEof := false;
  293. break;
  294. end;
  295. end;
  296. inc(TextRec(t).BufPos);
  297. until false;
  298. { restore file position if not working with a device }
  299. if not isdevice then
  300. { if we didn't modify the buffer, simply restore the BufPos and BufEnd }
  301. { (the latter because it's now probably set to zero because nothing was }
  302. { was read anymore) }
  303. if (reads = 0) then
  304. begin
  305. TextRec(t).BufPos:=oldbufpos;
  306. TextRec(t).BufEnd:=oldbufend;
  307. end
  308. { otherwise return to the old filepos and reset the buffer }
  309. else
  310. begin
  311. do_seek(TextRec(t).handle,oldfilepos);
  312. InOutRes:=0;
  313. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  314. TextRec(t).BufPos:=oldbufpos;
  315. end;
  316. End;
  317. Function SeekEof : Boolean;
  318. Begin
  319. SeekEof:=SeekEof(Input);
  320. End;
  321. Function Eoln(var t:Text) : Boolean;
  322. Begin
  323. If (InOutRes<>0) then
  324. exit(true);
  325. if (TextRec(t).mode<>fmInput) Then
  326. begin
  327. if TextRec(t).mode=fmOutPut then
  328. InOutRes:=104
  329. else
  330. InOutRes:=103;
  331. exit(true);
  332. end;
  333. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  334. begin
  335. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  336. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  337. exit(true);
  338. end;
  339. if CtrlZMarksEOF and (TextRec (T).BufPtr^[TextRec (T).BufPos] = #26) then
  340. exit (true);
  341. Eoln:=(TextRec(t).Bufptr^[TextRec(t).BufPos] in [#10,#13]);
  342. End;
  343. Function Eoln : Boolean;
  344. Begin
  345. Eoln:=Eoln(Input);
  346. End;
  347. Function SeekEoln (Var t : Text) : Boolean;
  348. Begin
  349. If (InOutRes<>0) then
  350. exit(true);
  351. if (TextRec(t).mode<>fmInput) Then
  352. begin
  353. if TextRec(t).mode=fmOutput then
  354. InOutRes:=104
  355. else
  356. InOutRes:=103;
  357. exit(true);
  358. end;
  359. repeat
  360. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  361. begin
  362. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  363. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  364. exit(true);
  365. end;
  366. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  367. #26: if CtrlZMarksEOF then
  368. exit (true);
  369. #10,#13 : exit(true);
  370. #9,' ' : ;
  371. else
  372. exit(false);
  373. end;
  374. inc(TextRec(t).BufPos);
  375. until false;
  376. End;
  377. Function SeekEoln : Boolean;
  378. Begin
  379. SeekEoln:=SeekEoln(Input);
  380. End;
  381. Procedure SetTextBuf(Var F : Text; Var Buf; Size : SizeInt);
  382. Begin
  383. TextRec(f).BufPtr:=@Buf;
  384. TextRec(f).BufSize:=Size;
  385. TextRec(f).BufPos:=0;
  386. TextRec(f).BufEnd:=0;
  387. End;
  388. Procedure SetTextLineEnding(Var f:Text; Ending:string);
  389. Begin
  390. TextRec(F).LineEnd:=Ending;
  391. End;
  392. function GetTextCodePage(var T: Text): TSystemCodePage;
  393. begin
  394. {$ifdef FPC_HAS_CPSTRING}
  395. GetTextCodePage:=TextRec(T).CodePage;
  396. {$else}
  397. GetTextCodePage:=0;
  398. {$endif}
  399. end;
  400. procedure SetTextCodePage(var T: Text; CodePage: TSystemCodePage);
  401. begin
  402. {$ifdef FPC_HAS_CPSTRING}
  403. TextRec(T).CodePage:=CodePage;
  404. {$endif}
  405. end;
  406. Function fpc_get_input:PText;compilerproc;
  407. begin
  408. fpc_get_input:=@Input;
  409. end;
  410. Function fpc_get_output:PText;compilerproc;
  411. begin
  412. fpc_get_output:=@Output;
  413. end;
  414. {*****************************************************************************
  415. Write(Ln)
  416. *****************************************************************************}
  417. Procedure fpc_WriteBuffer(var f:Text;const b;len:SizeInt);
  418. var
  419. p : pchar;
  420. left,
  421. idx : SizeInt;
  422. begin
  423. p:=pchar(@b);
  424. idx:=0;
  425. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  426. while len>left do
  427. begin
  428. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],left);
  429. dec(len,left);
  430. inc(idx,left);
  431. inc(TextRec(f).BufPos,left);
  432. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  433. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  434. end;
  435. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],len);
  436. inc(TextRec(f).BufPos,len);
  437. end;
  438. Procedure fpc_WriteBlanks(var f:Text;len:longint);
  439. var
  440. left : longint;
  441. begin
  442. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  443. while len>left do
  444. begin
  445. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],left,' ');
  446. dec(len,left);
  447. inc(TextRec(f).BufPos,left);
  448. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  449. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  450. end;
  451. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],len,' ');
  452. inc(TextRec(f).BufPos,len);
  453. end;
  454. Procedure fpc_Write_End(var f:Text); iocheck; compilerproc;
  455. begin
  456. if TextRec(f).FlushFunc<>nil then
  457. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  458. end;
  459. Procedure fpc_Writeln_End(var f:Text); iocheck; compilerproc;
  460. begin
  461. If InOutRes <> 0 then exit;
  462. case TextRec(f).mode of
  463. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  464. begin
  465. { Write EOL }
  466. fpc_WriteBuffer(f,TextRec(f).LineEnd[1],length(TextRec(f).LineEnd));
  467. { Flush }
  468. if TextRec(f).FlushFunc<>nil then
  469. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  470. end;
  471. fmInput: InOutRes:=105
  472. else InOutRes:=103;
  473. end;
  474. end;
  475. Procedure fpc_Write_Text_ShortStr(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR']; compilerproc;
  476. Begin
  477. If (InOutRes<>0) then
  478. exit;
  479. case TextRec(f).mode of
  480. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  481. begin
  482. If Len>Length(s) Then
  483. fpc_WriteBlanks(f,Len-Length(s));
  484. fpc_WriteBuffer(f,s[1],Length(s));
  485. end;
  486. fmInput: InOutRes:=105
  487. else InOutRes:=103;
  488. end;
  489. End;
  490. Procedure fpc_Write_Text_ShortStr_Iso(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR_ISO']; compilerproc;
  491. Begin
  492. If (InOutRes<>0) then
  493. exit;
  494. case TextRec(f).mode of
  495. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  496. begin
  497. { default value? }
  498. If Len=-1 then
  499. Len:=length(s);
  500. If Len>Length(s) Then
  501. begin
  502. fpc_WriteBlanks(f,Len-Length(s));
  503. fpc_WriteBuffer(f,s[1],Length(s));
  504. end
  505. else
  506. fpc_WriteBuffer(f,s[1],Len);
  507. end;
  508. fmInput: InOutRes:=105
  509. else InOutRes:=103;
  510. end;
  511. End;
  512. { provide local access to write_str }
  513. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  514. { provide local access to write_str_iso }
  515. procedure Write_Str_Iso(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR_ISO'];
  516. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  517. var
  518. ArrayLen : longint;
  519. p : pchar;
  520. Begin
  521. If (InOutRes<>0) then
  522. exit;
  523. case TextRec(f).mode of
  524. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  525. begin
  526. p:=pchar(@s);
  527. if zerobased then
  528. begin
  529. { can't use StrLen, since that one could try to read past the end }
  530. { of the heap (JM) }
  531. ArrayLen:=IndexByte(p^,high(s)+1,0);
  532. { IndexByte returns -1 if not found (JM) }
  533. if ArrayLen = -1 then
  534. ArrayLen := high(s)+1;
  535. end
  536. else
  537. ArrayLen := high(s)+1;
  538. If Len>ArrayLen Then
  539. fpc_WriteBlanks(f,Len-ArrayLen);
  540. fpc_WriteBuffer(f,p^,ArrayLen);
  541. end;
  542. fmInput: InOutRes:=105
  543. else InOutRes:=103;
  544. end;
  545. End;
  546. Procedure fpc_Write_Text_Pchar_as_Array_Iso(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  547. var
  548. ArrayLen : longint;
  549. p : pchar;
  550. Begin
  551. If (InOutRes<>0) then
  552. exit;
  553. case TextRec(f).mode of
  554. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  555. begin
  556. p:=pchar(@s);
  557. if zerobased then
  558. begin
  559. { can't use StrLen, since that one could try to read past the end }
  560. { of the heap (JM) }
  561. ArrayLen:=IndexByte(p^,high(s)+1,0);
  562. { IndexByte returns -1 if not found (JM) }
  563. if ArrayLen = -1 then
  564. ArrayLen := high(s)+1;
  565. end
  566. else
  567. ArrayLen := high(s)+1;
  568. { default value? }
  569. If Len=-1 then
  570. Len:=ArrayLen;
  571. If Len>ArrayLen Then
  572. begin
  573. fpc_WriteBlanks(f,Len-ArrayLen);
  574. fpc_WriteBuffer(f,p^,ArrayLen);
  575. end
  576. else
  577. fpc_WriteBuffer(f,p^,Len);
  578. end;
  579. fmInput: InOutRes:=105
  580. else InOutRes:=103;
  581. end;
  582. End;
  583. Procedure fpc_Write_Text_PChar_As_Pointer(Len : Longint;var f : Text;p : PChar); iocheck; compilerproc;
  584. var
  585. PCharLen : longint;
  586. Begin
  587. If (p=nil) or (InOutRes<>0) then
  588. exit;
  589. case TextRec(f).mode of
  590. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  591. begin
  592. PCharLen:=StrLen(p);
  593. If Len>PCharLen Then
  594. fpc_WriteBlanks(f,Len-PCharLen);
  595. fpc_WriteBuffer(f,p^,PCharLen);
  596. end;
  597. fmInput: InOutRes:=105
  598. else InOutRes:=103;
  599. end;
  600. End;
  601. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; const S : RawByteString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; compilerproc;
  602. {
  603. Writes a AnsiString to the Text file T
  604. }
  605. var
  606. SLen: longint;
  607. a: RawByteString;
  608. begin
  609. If (InOutRes<>0) then
  610. exit;
  611. case TextRec(f).mode of
  612. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  613. begin
  614. SLen:=Length(s);
  615. If Len>SLen Then
  616. fpc_WriteBlanks(f,Len-SLen);
  617. if slen > 0 then
  618. begin
  619. {$ifdef FPC_HAS_CPSTRING}
  620. a:=fpc_AnsiStr_To_AnsiStr(S,TextRec(f).CodePage);
  621. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  622. {$else}
  623. fpc_WriteBuffer(f,PAnsiChar(s)^,SLen);
  624. {$endif}
  625. end;
  626. end;
  627. fmInput: InOutRes:=105
  628. else InOutRes:=103;
  629. end;
  630. end;
  631. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  632. Procedure fpc_Write_Text_UnicodeStr (Len : Longint; Var f : Text; const S : UnicodeString); iocheck; compilerproc;
  633. {
  634. Writes a UnicodeString to the Text file T
  635. }
  636. var
  637. SLen: longint;
  638. a: RawByteString;
  639. begin
  640. If (pointer(S)=nil) or (InOutRes<>0) then
  641. exit;
  642. case TextRec(f).mode of
  643. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  644. begin
  645. SLen:=Length(s);
  646. If Len>SLen Then
  647. fpc_WriteBlanks(f,Len-SLen);
  648. {$ifdef FPC_HAS_CPSTRING}
  649. WideStringManager.Unicode2AnsiMoveProc(PUnicodeChar(S),a,TextRec(f).CodePage,SLen);
  650. {$else}
  651. a:=s;
  652. {$endif FPC_HAS_CPSTRING}
  653. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  654. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  655. end;
  656. fmInput: InOutRes:=105
  657. else InOutRes:=103;
  658. end;
  659. end;
  660. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  661. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  662. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  663. {
  664. Writes a WideString to the Text file T
  665. }
  666. var
  667. SLen: longint;
  668. a: RawByteString;
  669. begin
  670. If (pointer(S)=nil) or (InOutRes<>0) then
  671. exit;
  672. case TextRec(f).mode of
  673. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  674. begin
  675. SLen:=Length(s);
  676. If Len>SLen Then
  677. fpc_WriteBlanks(f,Len-SLen);
  678. {$ifdef FPC_HAS_CPSTRING}
  679. widestringmanager.Wide2AnsiMoveProc(PWideChar(s), a, TextRec(f).CodePage, SLen);
  680. {$else}
  681. a:=s;
  682. {$endif}
  683. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  684. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  685. end;
  686. fmInput: InOutRes:=105
  687. else InOutRes:=103;
  688. end;
  689. end;
  690. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  691. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  692. var
  693. s : String;
  694. Begin
  695. If (InOutRes<>0) then
  696. exit;
  697. Str(l,s);
  698. Write_Str(Len,t,s);
  699. End;
  700. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  701. var
  702. s : String;
  703. Begin
  704. If (InOutRes<>0) then
  705. exit;
  706. Str(L,s);
  707. Write_Str(Len,t,s);
  708. End;
  709. Procedure fpc_Write_Text_SInt_Iso(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  710. var
  711. s : String;
  712. Begin
  713. If (InOutRes<>0) then
  714. exit;
  715. Str(l,s);
  716. { default value? }
  717. if len=-1 then
  718. len:=11
  719. else if len<length(s) then
  720. len:=length(s);
  721. Write_Str_Iso(Len,t,s);
  722. End;
  723. Procedure fpc_Write_Text_UInt_Iso(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  724. var
  725. s : String;
  726. Begin
  727. If (InOutRes<>0) then
  728. exit;
  729. Str(L,s);
  730. { default value? }
  731. if len=-1 then
  732. len:=11
  733. else if len<length(s) then
  734. len:=length(s);
  735. Write_Str_Iso(Len,t,s);
  736. End;
  737. {$ifndef CPU64}
  738. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; compilerproc;
  739. var
  740. s : string;
  741. begin
  742. if (InOutRes<>0) then
  743. exit;
  744. str(q,s);
  745. write_str(len,t,s);
  746. end;
  747. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; compilerproc;
  748. var
  749. s : string;
  750. begin
  751. if (InOutRes<>0) then
  752. exit;
  753. str(i,s);
  754. write_str(len,t,s);
  755. end;
  756. procedure fpc_write_text_qword_iso(len : longint;var t : text;q : qword); iocheck; compilerproc;
  757. var
  758. s : string;
  759. begin
  760. if (InOutRes<>0) then
  761. exit;
  762. str(q,s);
  763. { default value? }
  764. if len=-1 then
  765. len:=20
  766. else if len<length(s) then
  767. len:=length(s);
  768. write_str_iso(len,t,s);
  769. end;
  770. procedure fpc_write_text_int64_iso(len : longint;var t : text;i : int64); iocheck; compilerproc;
  771. var
  772. s : string;
  773. begin
  774. if (InOutRes<>0) then
  775. exit;
  776. str(i,s);
  777. { default value? }
  778. if len=-1 then
  779. len:=20
  780. else if len<length(s) then
  781. len:=length(s);
  782. write_str_iso(len,t,s);
  783. end;
  784. {$endif CPU64}
  785. {$ifndef FPUNONE}
  786. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  787. var
  788. s : String;
  789. Begin
  790. If (InOutRes<>0) then
  791. exit;
  792. Str_real(Len,fixkomma,r,treal_type(rt),s);
  793. Write_Str(Len,t,s);
  794. End;
  795. Procedure fpc_Write_Text_Float_iso(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  796. var
  797. s : String;
  798. Begin
  799. If (InOutRes<>0) then
  800. exit;
  801. Str_real_iso(Len,fixkomma,r,treal_type(rt),s);
  802. Write_Str(Len,t,s);
  803. End;
  804. {$endif}
  805. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; compilerproc;
  806. var
  807. s:string;
  808. begin
  809. if textrec(t).mode<>fmoutput then
  810. begin
  811. if textrec(t).mode=fminput then
  812. inoutres:=105
  813. else
  814. inoutres:=103;
  815. exit;
  816. end;
  817. inoutres := fpc_shortstr_enum_intern(ordinal, len, typinfo, ord2strindex, s);
  818. if (inoutres <> 0) then
  819. exit;
  820. fpc_writeBuffer(t,s[1],length(s));
  821. end;
  822. {$ifdef FPC_HAS_STR_CURRENCY}
  823. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; compilerproc;
  824. var
  825. s : String;
  826. Begin
  827. If (InOutRes<>0) then
  828. exit;
  829. str(c:Len:fixkomma,s);
  830. Write_Str(Len,t,s);
  831. End;
  832. {$endif FPC_HAS_STR_CURRENCY}
  833. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  834. Begin
  835. If (InOutRes<>0) then
  836. exit;
  837. { Can't use array[boolean] because b can be >0 ! }
  838. if b then
  839. Write_Str(Len,t,'TRUE')
  840. else
  841. Write_Str(Len,t,'FALSE');
  842. End;
  843. Procedure fpc_Write_Text_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  844. Begin
  845. If (InOutRes<>0) then
  846. exit;
  847. { Can't use array[boolean] because b can be >0 ! }
  848. { default value? }
  849. If Len=-1 then
  850. Len:=5;
  851. if b then
  852. Write_Str_Iso(Len,t,'true')
  853. else
  854. Write_Str_Iso(Len,t,'false');
  855. End;
  856. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  857. Begin
  858. If (InOutRes<>0) then
  859. exit;
  860. if (TextRec(t).mode<>fmOutput) Then
  861. begin
  862. if TextRec(t).mode=fmClosed then
  863. InOutRes:=103
  864. else
  865. InOutRes:=105;
  866. exit;
  867. end;
  868. If Len>1 Then
  869. fpc_WriteBlanks(t,Len-1);
  870. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  871. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  872. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  873. Inc(TextRec(t).BufPos);
  874. End;
  875. Procedure fpc_Write_Text_Char_Iso(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  876. Begin
  877. If (InOutRes<>0) then
  878. exit;
  879. if (TextRec(t).mode<>fmOutput) Then
  880. begin
  881. if TextRec(t).mode=fmClosed then
  882. InOutRes:=103
  883. else
  884. InOutRes:=105;
  885. exit;
  886. end;
  887. { default value? }
  888. If Len=-1 then
  889. Len:=1;
  890. If Len>1 Then
  891. fpc_WriteBlanks(t,Len-1)
  892. else If Len<1 Then
  893. exit;
  894. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  895. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  896. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  897. Inc(TextRec(t).BufPos);
  898. End;
  899. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  900. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  901. var
  902. a: RawByteString;
  903. Begin
  904. If (InOutRes<>0) then
  905. exit;
  906. if (TextRec(t).mode<>fmOutput) Then
  907. begin
  908. if TextRec(t).mode=fmClosed then
  909. InOutRes:=103
  910. else
  911. InOutRes:=105;
  912. exit;
  913. end;
  914. If Len>1 Then
  915. fpc_WriteBlanks(t,Len-1);
  916. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  917. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  918. { a widechar can be translated into more than a single ansichar }
  919. {$ifdef FPC_HAS_CPSTRING}
  920. widestringmanager.Wide2AnsiMoveProc(@c,a,TextRec(t).CodePage,1);
  921. {$else}
  922. a:=c;
  923. {$endif}
  924. fpc_WriteBuffer(t,PAnsiChar(a)^,Length(a));
  925. End;
  926. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  927. {*****************************************************************************
  928. Read(Ln)
  929. *****************************************************************************}
  930. Function NextChar(var f:Text;var s:string):Boolean;
  931. begin
  932. NextChar:=false;
  933. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  934. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  935. begin
  936. if length(s)<high(s) then
  937. begin
  938. inc(s[0]);
  939. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  940. end;
  941. Inc(TextRec(f).BufPos);
  942. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  943. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  944. NextChar:=true;
  945. end;
  946. end;
  947. Function IgnoreSpaces(var f:Text):Boolean;
  948. {
  949. Removes all leading spaces,tab,eols from the input buffer, returns true if
  950. the buffer is empty
  951. }
  952. var
  953. s : string;
  954. begin
  955. s:='';
  956. IgnoreSpaces:=false;
  957. { Return false when already at EOF }
  958. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  959. exit;
  960. (* Check performed separately to avoid accessing memory outside buffer *)
  961. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  962. exit;
  963. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  964. begin
  965. if not NextChar(f,s) then
  966. exit;
  967. { EOF? }
  968. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  969. break;
  970. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  971. break;
  972. end;
  973. IgnoreSpaces:=true;
  974. end;
  975. procedure ReadNumeric(var f:Text;var s:string);
  976. {
  977. Read numeric input, if buffer is empty then return True
  978. }
  979. begin
  980. repeat
  981. if not NextChar(f,s) then
  982. exit;
  983. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  984. end;
  985. function CheckRead(var f:Text):Boolean;
  986. begin
  987. CheckRead:=False;
  988. { Check error and if file is open and load buf if empty }
  989. If (InOutRes<>0) then
  990. exit;
  991. if (TextRec(f).mode<>fmInput) Then
  992. begin
  993. case TextRec(f).mode of
  994. fmOutPut,fmAppend:
  995. InOutRes:=104;
  996. else
  997. InOutRes:=103;
  998. end;
  999. exit;
  1000. end;
  1001. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1002. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1003. CheckRead:=True;
  1004. end;
  1005. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  1006. begin
  1007. if TextRec(f).FlushFunc<>nil then
  1008. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1009. end;
  1010. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  1011. var prev: char;
  1012. Begin
  1013. If not CheckRead(f) then
  1014. exit;
  1015. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1016. { Flush if set }
  1017. begin
  1018. if (TextRec(f).FlushFunc<>nil) then
  1019. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1020. exit;
  1021. end;
  1022. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1023. Exit;
  1024. repeat
  1025. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1026. inc(TextRec(f).BufPos);
  1027. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1028. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1029. if prev = #10 then
  1030. exit;
  1031. {$ifdef MACOS}
  1032. if prev = #13 then
  1033. {StdInput on macos never have dos line ending, so this is safe.}
  1034. if TextRec(f).Handle = StdInputHandle then
  1035. exit;
  1036. {$endif MACOS}
  1037. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1038. begin
  1039. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1040. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1041. { Flush if set }
  1042. begin
  1043. if (TextRec(f).FlushFunc<>nil) then
  1044. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1045. exit;
  1046. end;
  1047. end;
  1048. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1049. Exit;
  1050. if (prev=#13) then
  1051. { is there also a #10 after it? }
  1052. begin
  1053. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1054. { yes, skip that one as well }
  1055. inc(TextRec(f).BufPos);
  1056. exit;
  1057. end;
  1058. until false;
  1059. End;
  1060. Procedure fpc_ReadLn_End_Iso(var f : Text);[Public,Alias:'FPC_READLN_END_ISO']; iocheck; compilerproc;
  1061. var prev: char;
  1062. Begin
  1063. If not CheckRead(f) then
  1064. exit;
  1065. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1066. { Flush if set }
  1067. begin
  1068. if (TextRec(f).FlushFunc<>nil) then
  1069. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1070. exit;
  1071. end;
  1072. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1073. begin
  1074. inc(TextRec(f).BufPos);
  1075. Exit;
  1076. end;
  1077. repeat
  1078. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1079. inc(TextRec(f).BufPos);
  1080. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1081. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1082. if prev = #10 then
  1083. exit;
  1084. {$ifdef MACOS}
  1085. if prev = #13 then
  1086. {StdInput on macos never have dos line ending, so this is safe.}
  1087. if TextRec(f).Handle = StdInputHandle then
  1088. exit;
  1089. {$endif MACOS}
  1090. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1091. begin
  1092. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1093. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1094. { Flush if set }
  1095. begin
  1096. if (TextRec(f).FlushFunc<>nil) then
  1097. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1098. exit;
  1099. end;
  1100. end;
  1101. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1102. begin
  1103. inc(TextRec(f).BufPos);
  1104. Exit;
  1105. end;
  1106. if (prev=#13) then
  1107. { is there also a #10 after it? }
  1108. begin
  1109. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1110. { yes, skip that one as well }
  1111. inc(TextRec(f).BufPos);
  1112. exit;
  1113. end;
  1114. until false;
  1115. End;
  1116. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  1117. var
  1118. sPos,len : Longint;
  1119. p,startp,maxp : pchar;
  1120. end_of_string:boolean;
  1121. Begin
  1122. ReadPCharLen:=0;
  1123. If not CheckRead(f) then
  1124. exit;
  1125. { Read maximal until Maxlen is reached }
  1126. sPos:=0;
  1127. end_of_string:=false;
  1128. repeat
  1129. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1130. begin
  1131. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1132. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1133. break;
  1134. end;
  1135. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1136. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  1137. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  1138. else
  1139. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1140. startp:=p;
  1141. { find stop character }
  1142. while p<maxp do
  1143. begin
  1144. { Optimization: Do a quick check for a control character first }
  1145. if (p^<' ') then
  1146. begin
  1147. if (p^ in [#10,#13]) or
  1148. (ctrlZmarkseof and (p^=#26)) then
  1149. begin
  1150. end_of_string:=true;
  1151. break;
  1152. end;
  1153. end;
  1154. inc(p);
  1155. end;
  1156. { calculate read bytes }
  1157. len:=p-startp;
  1158. inc(TextRec(f).BufPos,Len);
  1159. Move(startp^,s[sPos],Len);
  1160. inc(sPos,Len);
  1161. until (spos=MaxLen) or end_of_string;
  1162. ReadPCharLen:=spos;
  1163. End;
  1164. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  1165. Begin
  1166. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  1167. End;
  1168. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  1169. Begin
  1170. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  1171. End;
  1172. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  1173. var
  1174. len: longint;
  1175. Begin
  1176. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  1177. if zerobased and
  1178. (len > high(s)) then
  1179. len := high(s);
  1180. if (len <= high(s)) then
  1181. s[len] := #0;
  1182. End;
  1183. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1184. Procedure fpc_Read_Text_AnsiStr(var f : Text;out s : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); [public, alias: 'FPC_READ_TEXT_ANSISTR']; iocheck; compilerproc;
  1185. var
  1186. slen,len : SizeInt;
  1187. Begin
  1188. slen:=0;
  1189. Repeat
  1190. // SetLength will reallocate the length.
  1191. SetLength(s,slen+255);
  1192. len:=ReadPCharLen(f,pchar(Pointer(s)+slen),255);
  1193. inc(slen,len);
  1194. Until len<255;
  1195. // Set actual length
  1196. SetLength(s,Slen);
  1197. {$ifdef FPC_HAS_CPSTRING}
  1198. SetCodePage(s,TextRec(f).CodePage,false);
  1199. s:=fpc_AnsiStr_To_AnsiStr(s,cp);
  1200. {$endif FPC_HAS_CPSTRING}
  1201. End;
  1202. Procedure fpc_Read_Text_AnsiStr_Intern(var f : Text;out s : RawByteString{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}); [external name 'FPC_READ_TEXT_ANSISTR'];
  1203. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1204. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1205. Procedure fpc_Read_Text_UnicodeStr(var f : Text;out us : UnicodeString); iocheck; compilerproc;
  1206. var
  1207. s: RawByteString;
  1208. Begin
  1209. // all standard input is assumed to be ansi-encoded
  1210. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1211. // Convert to unicodestring
  1212. {$ifdef FPC_HAS_CPSTRING}
  1213. widestringmanager.Ansi2UnicodeMoveProc(PAnsiChar(s),StringCodePage(s),us,Length(s));
  1214. {$else}
  1215. us:=s;
  1216. {$endif}
  1217. End;
  1218. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1219. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1220. Procedure fpc_Read_Text_WideStr(var f : Text;out ws : WideString); iocheck; compilerproc;
  1221. var
  1222. s: RawByteString;
  1223. Begin
  1224. // all standard input is assumed to be ansi-encoded
  1225. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1226. // Convert to widestring
  1227. {$ifdef FPC_HAS_CPSTRING}
  1228. widestringmanager.Ansi2WideMoveProc(PAnsiChar(s),StringCodePage(s),ws,Length(s));
  1229. {$else}
  1230. ws:=s;
  1231. {$endif}
  1232. End;
  1233. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1234. procedure fpc_Read_Text_Char(var f : Text; out c: char); [public, alias: 'FPC_READ_TEXT_CHAR']; iocheck;compilerproc;
  1235. Begin
  1236. c:=#0;
  1237. If not CheckRead(f) then
  1238. exit;
  1239. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1240. begin
  1241. c := #26;
  1242. exit;
  1243. end;
  1244. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1245. inc(TextRec(f).BufPos);
  1246. end;
  1247. procedure fpc_Read_Text_Char_intern(var f : Text; out c: char); iocheck; [external name 'FPC_READ_TEXT_CHAR'];
  1248. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1249. procedure fpc_Read_Text_WideChar(var f : Text; out wc: widechar); iocheck;compilerproc;
  1250. var
  1251. ws: widestring;
  1252. i: longint;
  1253. { maximum code point length is 6 characters (with UTF-8) }
  1254. str: array[0..5] of char;
  1255. Begin
  1256. fillchar(str[0],sizeof(str),0);
  1257. for i:=low(str) to high(str) do
  1258. begin
  1259. fpc_Read_Text_Char_intern(f,str[i]);
  1260. case widestringmanager.CodePointLengthProc(@str[0],i+1) of
  1261. -1: { possibly incomplete code point, try with an extra character }
  1262. ;
  1263. 0: { null character }
  1264. begin
  1265. wc:=#0;
  1266. exit;
  1267. end;
  1268. else
  1269. begin
  1270. { valid code point -> convert to widestring}
  1271. {$ifdef FPC_HAS_CPSTRING}
  1272. widestringmanager.Ansi2WideMoveProc(@str[0],TextRec(f).CodePage,ws,i+1);
  1273. {$else}
  1274. widestringmanager.Ansi2WideMoveProc(@str[0],DefaultSystemCodePage,ws,i+1);
  1275. {$endif}
  1276. { has to be exactly one widechar }
  1277. if length(ws)=1 then
  1278. begin
  1279. wc:=ws[1];
  1280. exit
  1281. end
  1282. else
  1283. break;
  1284. end;
  1285. end;
  1286. end;
  1287. { invalid widechar input }
  1288. inoutres:=106;
  1289. end;
  1290. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1291. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: char); iocheck;compilerproc;
  1292. Begin
  1293. c:=' ';
  1294. If not CheckRead(f) then
  1295. exit;
  1296. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1297. begin
  1298. c:=' ';
  1299. exit;
  1300. end;
  1301. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1302. inc(TextRec(f).BufPos);
  1303. if c=#13 then
  1304. begin
  1305. c:=' ';
  1306. If not CheckRead(f) or
  1307. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1308. exit;
  1309. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1310. inc(TextRec(f).BufPos);
  1311. { ignore #26 following a new line }
  1312. If not CheckRead(f) or
  1313. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1314. exit;
  1315. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1316. inc(TextRec(f).BufPos);
  1317. end
  1318. else if c=#10 then
  1319. begin
  1320. c:=' ';
  1321. { ignore #26 following a new line }
  1322. If not CheckRead(f) or
  1323. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1324. exit;
  1325. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1326. inc(TextRec(f).BufPos);
  1327. end
  1328. else if c=#26 then
  1329. c:=' ';
  1330. end;
  1331. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1332. var
  1333. hs : String;
  1334. code : longint;
  1335. Begin
  1336. l:=0;
  1337. If not CheckRead(f) then
  1338. exit;
  1339. hs:='';
  1340. if IgnoreSpaces(f) then
  1341. begin
  1342. { When spaces were found and we are now at EOF,
  1343. then we return 0 }
  1344. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1345. exit;
  1346. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1347. exit;
  1348. ReadNumeric(f,hs);
  1349. end;
  1350. if (hs = '') then
  1351. L := 0
  1352. else
  1353. begin
  1354. Val(hs,l,code);
  1355. if Code <> 0 then
  1356. InOutRes:=106;
  1357. end;
  1358. End;
  1359. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1360. var
  1361. hs : String;
  1362. code : longint;
  1363. Begin
  1364. u:=0;
  1365. If not CheckRead(f) then
  1366. exit;
  1367. hs:='';
  1368. if IgnoreSpaces(f) then
  1369. begin
  1370. { When spaces were found and we are now at EOF,
  1371. then we return 0 }
  1372. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1373. exit;
  1374. ReadNumeric(f,hs);
  1375. end;
  1376. if (hs = '') then
  1377. u := 0
  1378. else
  1379. begin
  1380. val(hs,u,code);
  1381. If code<>0 Then
  1382. InOutRes:=106;
  1383. end;
  1384. End;
  1385. {$ifndef FPUNONE}
  1386. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1387. var
  1388. hs : string;
  1389. code : Word;
  1390. begin
  1391. v:=0.0;
  1392. If not CheckRead(f) then
  1393. exit;
  1394. hs:='';
  1395. if IgnoreSpaces(f) then
  1396. begin
  1397. { When spaces were found and we are now at EOF,
  1398. then we return 0 }
  1399. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1400. exit;
  1401. ReadNumeric(f,hs);
  1402. end;
  1403. val(hs,v,code);
  1404. If code<>0 Then
  1405. InOutRes:=106;
  1406. end;
  1407. {$endif}
  1408. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1409. var s:string;
  1410. code:valsint;
  1411. begin
  1412. if not checkread(t) then
  1413. exit;
  1414. s:='';
  1415. if ignorespaces(t) then
  1416. begin
  1417. { When spaces were found and we are now at EOF, then we return 0 }
  1418. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1419. exit;
  1420. ReadNumeric(t,s);
  1421. end;
  1422. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1423. if code<>0 then
  1424. InOutRes:=106;
  1425. end;
  1426. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1427. var
  1428. hs : string;
  1429. code : Word;
  1430. begin
  1431. {$ifdef FPUNONE}
  1432. v:=0;
  1433. {$else}
  1434. v:=0.0;
  1435. {$endif}
  1436. If not CheckRead(f) then
  1437. exit;
  1438. hs:='';
  1439. if IgnoreSpaces(f) then
  1440. begin
  1441. { When spaces were found and we are now at EOF,
  1442. then we return 0 }
  1443. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1444. exit;
  1445. ReadNumeric(f,hs);
  1446. end;
  1447. val(hs,v,code);
  1448. If code<>0 Then
  1449. InOutRes:=106;
  1450. end;
  1451. {$ifndef cpu64}
  1452. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1453. var
  1454. hs : String;
  1455. code : longint;
  1456. Begin
  1457. q:=0;
  1458. If not CheckRead(f) then
  1459. exit;
  1460. hs:='';
  1461. if IgnoreSpaces(f) then
  1462. begin
  1463. { When spaces were found and we are now at EOF,
  1464. then we return 0 }
  1465. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1466. exit;
  1467. ReadNumeric(f,hs);
  1468. end;
  1469. val(hs,q,code);
  1470. If code<>0 Then
  1471. InOutRes:=106;
  1472. End;
  1473. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1474. var
  1475. hs : String;
  1476. code : Longint;
  1477. Begin
  1478. i:=0;
  1479. If not CheckRead(f) then
  1480. exit;
  1481. hs:='';
  1482. if IgnoreSpaces(f) then
  1483. begin
  1484. { When spaces were found and we are now at EOF,
  1485. then we return 0 }
  1486. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1487. exit;
  1488. ReadNumeric(f,hs);
  1489. end;
  1490. Val(hs,i,code);
  1491. If code<>0 Then
  1492. InOutRes:=106;
  1493. End;
  1494. {$endif CPU64}
  1495. {*****************************************************************************
  1496. WriteStr/ReadStr
  1497. *****************************************************************************}
  1498. const
  1499. StrPtrIndex = 1;
  1500. { leave space for 128 bit string pointers :) (used for writestr) }
  1501. ShortStrLenIndex = 17;
  1502. { how many bytes of the string have been processed already (used for readstr) }
  1503. BytesReadIndex = 17;
  1504. threadvar
  1505. ReadWriteStrText: textrec;
  1506. procedure WriteStrShort(var t: textrec);
  1507. var
  1508. str: pshortstring;
  1509. newbytes,
  1510. oldlen: longint;
  1511. begin
  1512. if (t.bufpos=0) then
  1513. exit;
  1514. str:=pshortstring(ppointer(@t.userdata[StrPtrIndex])^);
  1515. newbytes:=t.BufPos;
  1516. oldlen:=length(str^);
  1517. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1518. begin
  1519. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1520. {$ifdef writestr_iolencheck}
  1521. // GPC only gives an io error if {$no-truncate-strings} is active
  1522. // FPC does not have this setting (it never gives errors when a
  1523. // a string expression is truncated)
  1524. { "disk full" }
  1525. inoutres:=101;
  1526. {$endif}
  1527. end;
  1528. setlength(str^,length(str^)+newbytes);
  1529. move(t.bufptr^,str^[oldlen+1],newbytes);
  1530. t.bufpos:=0;
  1531. end;
  1532. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1533. procedure WriteStrAnsi(var t: textrec);
  1534. var
  1535. str: pansistring;
  1536. oldlen: longint;
  1537. begin
  1538. if (t.bufpos=0) then
  1539. exit;
  1540. str:=pansistring(ppointer(@t.userdata[StrPtrIndex])^);
  1541. oldlen:=length(str^);
  1542. setlength(str^,oldlen+t.bufpos);
  1543. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1544. t.bufpos:=0;
  1545. end;
  1546. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1547. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1548. procedure WriteStrUnicode(var t: textrec);
  1549. var
  1550. temp: ansistring;
  1551. str: punicodestring;
  1552. begin
  1553. if (t.bufpos=0) then
  1554. exit;
  1555. str:=punicodestring(ppointer(@t.userdata[StrPtrIndex])^);
  1556. setlength(temp,t.bufpos);
  1557. move(t.bufptr^,temp[1],t.bufpos);
  1558. str^:=str^+temp;
  1559. t.bufpos:=0;
  1560. end;
  1561. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1562. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1563. procedure WriteStrWide(var t: textrec);
  1564. var
  1565. temp: ansistring;
  1566. str: pwidestring;
  1567. begin
  1568. if (t.bufpos=0) then
  1569. exit;
  1570. str:=pwidestring(ppointer(@t.userdata[StrPtrIndex])^);
  1571. setlength(temp,t.bufpos);
  1572. move(t.bufptr^,temp[1],t.bufpos);
  1573. str^:=str^+temp;
  1574. t.bufpos:=0;
  1575. end;
  1576. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1577. procedure SetupWriteStrCommon(out t: textrec);
  1578. begin
  1579. // initialise
  1580. Assign(text(t),'');
  1581. t.mode:=fmOutput;
  1582. t.OpenFunc:=nil;
  1583. t.CloseFunc:=nil;
  1584. end;
  1585. function fpc_SetupWriteStr_Shortstr(out s: shortstring): PText; compilerproc;
  1586. begin
  1587. setupwritestrcommon(ReadWriteStrText);
  1588. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1589. ReadWriteStrText.userdata[ShortStrLenIndex]:=high(s);
  1590. setlength(s,0);
  1591. ReadWriteStrText.InOutFunc:=@WriteStrShort;
  1592. ReadWriteStrText.FlushFunc:=@WriteStrShort;
  1593. result:=@ReadWriteStrText;
  1594. end;
  1595. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1596. function fpc_SetupWriteStr_Ansistr(out s: ansistring): PText; compilerproc;
  1597. begin
  1598. setupwritestrcommon(ReadWriteStrText);
  1599. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1600. // automatically done by out-semantics
  1601. // setlength(s,0);
  1602. ReadWriteStrText.InOutFunc:=@WriteStrAnsi;
  1603. ReadWriteStrText.FlushFunc:=@WriteStrAnsi;
  1604. result:=@ReadWriteStrText;
  1605. end;
  1606. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1607. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1608. function fpc_SetupWriteStr_Unicodestr(out s: unicodestring): PText; compilerproc;
  1609. begin
  1610. setupwritestrcommon(ReadWriteStrText);
  1611. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1612. // automatically done by out-semantics
  1613. // setlength(s,0);
  1614. ReadWriteStrText.InOutFunc:=@WriteStrUnicode;
  1615. ReadWriteStrText.FlushFunc:=@WriteStrUnicode;
  1616. result:=@ReadWriteStrText;
  1617. end;
  1618. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1619. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1620. function fpc_SetupWriteStr_Widestr(out s: widestring): PText; compilerproc;
  1621. begin
  1622. setupwritestrcommon(ReadWriteStrText);
  1623. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1624. // automatically done by out-semantics
  1625. // setlength(s,0);
  1626. ReadWriteStrText.InOutFunc:=@WriteStrWide;
  1627. ReadWriteStrText.FlushFunc:=@WriteStrWide;
  1628. result:=@ReadWriteStrText;
  1629. end;
  1630. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1631. procedure ReadAnsiStrFinal(var t: textrec);
  1632. begin
  1633. { finalise the temp ansistring }
  1634. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1635. end;
  1636. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1637. var
  1638. newbytes: sizeint;
  1639. begin
  1640. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1641. if (t.BufSize <= newbytes) then
  1642. newbytes := t.BufSize;
  1643. if (newbytes > 0) then
  1644. begin
  1645. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1646. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1647. end;
  1648. t.BufEnd:=newbytes;
  1649. t.BufPos:=0;
  1650. end;
  1651. procedure ReadStrAnsi(var t: textrec);
  1652. var
  1653. str: pansistring;
  1654. begin
  1655. str:=pansistring(@t.userdata[StrPtrIndex]);
  1656. ReadStrCommon(t,@str^[1],length(str^));
  1657. end;
  1658. procedure SetupReadStrCommon(out t: textrec);
  1659. begin
  1660. // initialise
  1661. Assign(text(t),'');
  1662. t.mode:=fmInput;
  1663. t.OpenFunc:=nil;
  1664. t.CloseFunc:=nil;
  1665. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1666. end;
  1667. function fpc_SetupReadStr_Ansistr(const s: ansistring): PText; [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1668. begin
  1669. setupreadstrcommon(ReadWriteStrText);
  1670. { we need a reference, because 's' may be a temporary expression }
  1671. PAnsiString(@ReadWriteStrText.userdata[StrPtrIndex])^:=s;
  1672. ReadWriteStrText.InOutFunc:=@ReadStrAnsi;
  1673. { this is called at the end, by fpc_read_end }
  1674. ReadWriteStrText.FlushFunc:=@ReadAnsiStrFinal;
  1675. result:=@ReadWriteStrText;
  1676. end;
  1677. function fpc_SetupReadStr_Ansistr_Intern(const s: ansistring): PText; [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1678. function fpc_SetupReadStr_Shortstr(const s: shortstring): PText; compilerproc;
  1679. begin
  1680. { the reason we convert the short string to ansistring, is because the semantics of
  1681. readstr are defined as:
  1682. *********************
  1683. Apart from the restrictions imposed by requirements given in this clause,
  1684. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  1685. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  1686. char-type (or a subrange of char-type), the integer-type (or a subrange of
  1687. integer-type), the real-type, a fixed-string-type, or a
  1688. variable-string-type, shall be equivalent to
  1689. begin
  1690. rewrite(f);
  1691. writeln(f, e);
  1692. reset(f);
  1693. read(f, v 1 ,...,v n )
  1694. end
  1695. *********************
  1696. This means that any side effects caused by the evaluation of v 1 .. v n
  1697. must not affect the value of e (= our argument s) -> we need a copy of it.
  1698. An ansistring is the easiest way to get a threadsafe copy, and allows us
  1699. to use the other ansistring readstr helpers too.
  1700. }
  1701. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1702. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1703. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  1704. runerror(217);
  1705. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1706. end;
  1707. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1708. function fpc_SetupReadStr_Unicodestr(const s: unicodestring): PText; compilerproc;
  1709. begin
  1710. { we use an ansistring to avoid code duplication, and let the }
  1711. { assignment convert the widestring to an equivalent ansistring }
  1712. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1713. end;
  1714. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1715. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1716. function fpc_SetupReadStr_Widestr(const s: widestring): PText; compilerproc;
  1717. begin
  1718. { we use an ansistring to avoid code duplication, and let the }
  1719. { assignment convert the widestring to an equivalent ansistring }
  1720. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1721. end;
  1722. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1723. {*****************************************************************************
  1724. Initializing
  1725. *****************************************************************************}
  1726. procedure OpenStdIO(var f:text;mode:longint;hdl:thandle);
  1727. begin
  1728. Assign(f,'');
  1729. TextRec(f).Handle:=hdl;
  1730. TextRec(f).Mode:=mode;
  1731. TextRec(f).Closefunc:=@FileCloseFunc;
  1732. case mode of
  1733. fmInput :
  1734. begin
  1735. TextRec(f).InOutFunc:=@FileReadFunc;
  1736. {$ifndef ver2_4}
  1737. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleInput);
  1738. {$endif ver2_4}
  1739. end;
  1740. fmOutput :
  1741. begin
  1742. TextRec(f).InOutFunc:=@FileWriteFunc;
  1743. {$ifndef ver2_4}
  1744. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleOutput);
  1745. {$endif ver2_4}
  1746. if Do_Isdevice(hdl) then
  1747. TextRec(f).FlushFunc:=@FileWriteFunc;
  1748. end;
  1749. else
  1750. HandleError(102);
  1751. end;
  1752. end;