text.inc 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  620. if TextRec(f).CodePage<>StringCodePage(S) then
  621. begin
  622. a:=fpc_AnsiStr_To_AnsiStr(S,TextRec(f).CodePage);
  623. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  624. end
  625. else
  626. {$endif}
  627. fpc_WriteBuffer(f,PAnsiChar(s)^,SLen);
  628. end;
  629. end;
  630. fmInput: InOutRes:=105
  631. else InOutRes:=103;
  632. end;
  633. end;
  634. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  635. Procedure fpc_Write_Text_UnicodeStr (Len : Longint; Var f : Text; const S : UnicodeString); iocheck; compilerproc;
  636. {
  637. Writes a UnicodeString to the Text file T
  638. }
  639. var
  640. SLen: longint;
  641. a: RawByteString;
  642. begin
  643. If (pointer(S)=nil) or (InOutRes<>0) then
  644. exit;
  645. case TextRec(f).mode of
  646. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  647. begin
  648. SLen:=Length(s);
  649. If Len>SLen Then
  650. fpc_WriteBlanks(f,Len-SLen);
  651. {$ifdef FPC_HAS_CPSTRING}
  652. WideStringManager.Unicode2AnsiMoveProc(PUnicodeChar(S),a,TextRec(f).CodePage,SLen);
  653. {$else}
  654. a:=s;
  655. {$endif FPC_HAS_CPSTRING}
  656. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  657. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  658. end;
  659. fmInput: InOutRes:=105
  660. else InOutRes:=103;
  661. end;
  662. end;
  663. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  664. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  665. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  666. {
  667. Writes a WideString to the Text file T
  668. }
  669. var
  670. SLen: longint;
  671. a: RawByteString;
  672. begin
  673. If (pointer(S)=nil) or (InOutRes<>0) then
  674. exit;
  675. case TextRec(f).mode of
  676. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  677. begin
  678. SLen:=Length(s);
  679. If Len>SLen Then
  680. fpc_WriteBlanks(f,Len-SLen);
  681. {$ifdef FPC_HAS_CPSTRING}
  682. widestringmanager.Wide2AnsiMoveProc(PWideChar(s), a, TextRec(f).CodePage, SLen);
  683. {$else}
  684. a:=s;
  685. {$endif}
  686. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  687. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  688. end;
  689. fmInput: InOutRes:=105
  690. else InOutRes:=103;
  691. end;
  692. end;
  693. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  694. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  695. var
  696. s : String;
  697. Begin
  698. If (InOutRes<>0) then
  699. exit;
  700. Str(l,s);
  701. Write_Str(Len,t,s);
  702. End;
  703. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  704. var
  705. s : String;
  706. Begin
  707. If (InOutRes<>0) then
  708. exit;
  709. Str(L,s);
  710. Write_Str(Len,t,s);
  711. End;
  712. Procedure fpc_Write_Text_SInt_Iso(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  713. var
  714. s : String;
  715. Begin
  716. If (InOutRes<>0) then
  717. exit;
  718. Str(l,s);
  719. { default value? }
  720. if len=-1 then
  721. len:=11
  722. else if len<length(s) then
  723. len:=length(s);
  724. Write_Str_Iso(Len,t,s);
  725. End;
  726. Procedure fpc_Write_Text_UInt_Iso(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  727. var
  728. s : String;
  729. Begin
  730. If (InOutRes<>0) then
  731. exit;
  732. Str(L,s);
  733. { default value? }
  734. if len=-1 then
  735. len:=11
  736. else if len<length(s) then
  737. len:=length(s);
  738. Write_Str_Iso(Len,t,s);
  739. End;
  740. {$ifndef CPU64}
  741. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; compilerproc;
  742. var
  743. s : string;
  744. begin
  745. if (InOutRes<>0) then
  746. exit;
  747. str(q,s);
  748. write_str(len,t,s);
  749. end;
  750. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; compilerproc;
  751. var
  752. s : string;
  753. begin
  754. if (InOutRes<>0) then
  755. exit;
  756. str(i,s);
  757. write_str(len,t,s);
  758. end;
  759. procedure fpc_write_text_qword_iso(len : longint;var t : text;q : qword); iocheck; compilerproc;
  760. var
  761. s : string;
  762. begin
  763. if (InOutRes<>0) then
  764. exit;
  765. str(q,s);
  766. { default value? }
  767. if len=-1 then
  768. len:=20
  769. else if len<length(s) then
  770. len:=length(s);
  771. write_str_iso(len,t,s);
  772. end;
  773. procedure fpc_write_text_int64_iso(len : longint;var t : text;i : int64); iocheck; compilerproc;
  774. var
  775. s : string;
  776. begin
  777. if (InOutRes<>0) then
  778. exit;
  779. str(i,s);
  780. { default value? }
  781. if len=-1 then
  782. len:=20
  783. else if len<length(s) then
  784. len:=length(s);
  785. write_str_iso(len,t,s);
  786. end;
  787. {$endif CPU64}
  788. {$ifndef FPUNONE}
  789. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  790. var
  791. s : String;
  792. Begin
  793. If (InOutRes<>0) then
  794. exit;
  795. Str_real(Len,fixkomma,r,treal_type(rt),s);
  796. Write_Str(Len,t,s);
  797. End;
  798. Procedure fpc_Write_Text_Float_iso(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  799. var
  800. s : String;
  801. Begin
  802. If (InOutRes<>0) then
  803. exit;
  804. Str_real_iso(Len,fixkomma,r,treal_type(rt),s);
  805. Write_Str(Len,t,s);
  806. End;
  807. {$endif}
  808. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; compilerproc;
  809. var
  810. s:string;
  811. begin
  812. if textrec(t).mode<>fmoutput then
  813. begin
  814. if textrec(t).mode=fminput then
  815. inoutres:=105
  816. else
  817. inoutres:=103;
  818. exit;
  819. end;
  820. inoutres := fpc_shortstr_enum_intern(ordinal, len, typinfo, ord2strindex, s);
  821. if (inoutres <> 0) then
  822. exit;
  823. fpc_writeBuffer(t,s[1],length(s));
  824. end;
  825. {$ifdef FPC_HAS_STR_CURRENCY}
  826. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; compilerproc;
  827. var
  828. s : String;
  829. Begin
  830. If (InOutRes<>0) then
  831. exit;
  832. str(c:Len:fixkomma,s);
  833. Write_Str(Len,t,s);
  834. End;
  835. {$endif FPC_HAS_STR_CURRENCY}
  836. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  837. Begin
  838. If (InOutRes<>0) then
  839. exit;
  840. { Can't use array[boolean] because b can be >0 ! }
  841. if b then
  842. Write_Str(Len,t,'TRUE')
  843. else
  844. Write_Str(Len,t,'FALSE');
  845. End;
  846. Procedure fpc_Write_Text_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  847. Begin
  848. If (InOutRes<>0) then
  849. exit;
  850. { Can't use array[boolean] because b can be >0 ! }
  851. { default value? }
  852. If Len=-1 then
  853. Len:=5;
  854. if b then
  855. Write_Str_Iso(Len,t,'true')
  856. else
  857. Write_Str_Iso(Len,t,'false');
  858. End;
  859. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  860. Begin
  861. If (InOutRes<>0) then
  862. exit;
  863. if (TextRec(t).mode<>fmOutput) Then
  864. begin
  865. if TextRec(t).mode=fmClosed then
  866. InOutRes:=103
  867. else
  868. InOutRes:=105;
  869. exit;
  870. end;
  871. If Len>1 Then
  872. fpc_WriteBlanks(t,Len-1);
  873. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  874. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  875. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  876. Inc(TextRec(t).BufPos);
  877. End;
  878. Procedure fpc_Write_Text_Char_Iso(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  879. Begin
  880. If (InOutRes<>0) then
  881. exit;
  882. if (TextRec(t).mode<>fmOutput) Then
  883. begin
  884. if TextRec(t).mode=fmClosed then
  885. InOutRes:=103
  886. else
  887. InOutRes:=105;
  888. exit;
  889. end;
  890. { default value? }
  891. If Len=-1 then
  892. Len:=1;
  893. If Len>1 Then
  894. fpc_WriteBlanks(t,Len-1)
  895. else If Len<1 Then
  896. exit;
  897. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  898. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  899. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  900. Inc(TextRec(t).BufPos);
  901. End;
  902. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  903. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  904. var
  905. a: RawByteString;
  906. Begin
  907. If (InOutRes<>0) then
  908. exit;
  909. if (TextRec(t).mode<>fmOutput) Then
  910. begin
  911. if TextRec(t).mode=fmClosed then
  912. InOutRes:=103
  913. else
  914. InOutRes:=105;
  915. exit;
  916. end;
  917. If Len>1 Then
  918. fpc_WriteBlanks(t,Len-1);
  919. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  920. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  921. { a widechar can be translated into more than a single ansichar }
  922. {$ifdef FPC_HAS_CPSTRING}
  923. widestringmanager.Wide2AnsiMoveProc(@c,a,TextRec(t).CodePage,1);
  924. {$else}
  925. a:=c;
  926. {$endif}
  927. fpc_WriteBuffer(t,PAnsiChar(a)^,Length(a));
  928. End;
  929. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  930. {*****************************************************************************
  931. Read(Ln)
  932. *****************************************************************************}
  933. Function NextChar(var f:Text;var s:string):Boolean;
  934. begin
  935. NextChar:=false;
  936. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  937. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  938. begin
  939. if length(s)<high(s) then
  940. begin
  941. inc(s[0]);
  942. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  943. end;
  944. Inc(TextRec(f).BufPos);
  945. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  946. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  947. NextChar:=true;
  948. end;
  949. end;
  950. Function IgnoreSpaces(var f:Text):Boolean;
  951. {
  952. Removes all leading spaces,tab,eols from the input buffer, returns true if
  953. the buffer is empty
  954. }
  955. var
  956. s : string;
  957. begin
  958. s:='';
  959. IgnoreSpaces:=false;
  960. { Return false when already at EOF }
  961. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  962. exit;
  963. (* Check performed separately to avoid accessing memory outside buffer *)
  964. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  965. exit;
  966. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  967. begin
  968. if not NextChar(f,s) then
  969. exit;
  970. { EOF? }
  971. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  972. break;
  973. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  974. break;
  975. end;
  976. IgnoreSpaces:=true;
  977. end;
  978. procedure ReadNumeric(var f:Text;var s:string);
  979. {
  980. Read numeric input, if buffer is empty then return True
  981. }
  982. begin
  983. repeat
  984. if not NextChar(f,s) then
  985. exit;
  986. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  987. end;
  988. function CheckRead(var f:Text):Boolean;
  989. begin
  990. CheckRead:=False;
  991. { Check error and if file is open and load buf if empty }
  992. If (InOutRes<>0) then
  993. exit;
  994. if (TextRec(f).mode<>fmInput) Then
  995. begin
  996. case TextRec(f).mode of
  997. fmOutPut,fmAppend:
  998. InOutRes:=104;
  999. else
  1000. InOutRes:=103;
  1001. end;
  1002. exit;
  1003. end;
  1004. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1005. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1006. CheckRead:=True;
  1007. end;
  1008. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  1009. begin
  1010. if TextRec(f).FlushFunc<>nil then
  1011. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1012. end;
  1013. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  1014. var prev: char;
  1015. Begin
  1016. If not CheckRead(f) then
  1017. exit;
  1018. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1019. { Flush if set }
  1020. begin
  1021. if (TextRec(f).FlushFunc<>nil) then
  1022. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1023. exit;
  1024. end;
  1025. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1026. Exit;
  1027. repeat
  1028. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1029. inc(TextRec(f).BufPos);
  1030. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1031. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1032. if prev = #10 then
  1033. exit;
  1034. {$ifdef MACOS}
  1035. if prev = #13 then
  1036. {StdInput on macos never have dos line ending, so this is safe.}
  1037. if TextRec(f).Handle = StdInputHandle then
  1038. exit;
  1039. {$endif MACOS}
  1040. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1041. begin
  1042. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1043. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1044. { Flush if set }
  1045. begin
  1046. if (TextRec(f).FlushFunc<>nil) then
  1047. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1048. exit;
  1049. end;
  1050. end;
  1051. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1052. Exit;
  1053. if (prev=#13) then
  1054. { is there also a #10 after it? }
  1055. begin
  1056. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1057. { yes, skip that one as well }
  1058. inc(TextRec(f).BufPos);
  1059. exit;
  1060. end;
  1061. until false;
  1062. End;
  1063. Procedure fpc_ReadLn_End_Iso(var f : Text);[Public,Alias:'FPC_READLN_END_ISO']; iocheck; compilerproc;
  1064. var prev: char;
  1065. Begin
  1066. If not CheckRead(f) then
  1067. exit;
  1068. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1069. { Flush if set }
  1070. begin
  1071. if (TextRec(f).FlushFunc<>nil) then
  1072. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1073. exit;
  1074. end;
  1075. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1076. begin
  1077. inc(TextRec(f).BufPos);
  1078. Exit;
  1079. end;
  1080. repeat
  1081. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1082. inc(TextRec(f).BufPos);
  1083. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1084. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1085. if prev = #10 then
  1086. exit;
  1087. {$ifdef MACOS}
  1088. if prev = #13 then
  1089. {StdInput on macos never have dos line ending, so this is safe.}
  1090. if TextRec(f).Handle = StdInputHandle then
  1091. exit;
  1092. {$endif MACOS}
  1093. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1094. begin
  1095. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1096. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1097. { Flush if set }
  1098. begin
  1099. if (TextRec(f).FlushFunc<>nil) then
  1100. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1101. exit;
  1102. end;
  1103. end;
  1104. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1105. begin
  1106. inc(TextRec(f).BufPos);
  1107. Exit;
  1108. end;
  1109. if (prev=#13) then
  1110. { is there also a #10 after it? }
  1111. begin
  1112. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1113. { yes, skip that one as well }
  1114. inc(TextRec(f).BufPos);
  1115. exit;
  1116. end;
  1117. until false;
  1118. End;
  1119. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  1120. var
  1121. sPos,len : Longint;
  1122. p,startp,maxp : pchar;
  1123. end_of_string:boolean;
  1124. Begin
  1125. ReadPCharLen:=0;
  1126. If not CheckRead(f) then
  1127. exit;
  1128. { Read maximal until Maxlen is reached }
  1129. sPos:=0;
  1130. end_of_string:=false;
  1131. repeat
  1132. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1133. begin
  1134. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1135. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1136. break;
  1137. end;
  1138. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1139. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  1140. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  1141. else
  1142. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1143. startp:=p;
  1144. { find stop character }
  1145. while p<maxp do
  1146. begin
  1147. { Optimization: Do a quick check for a control character first }
  1148. if (p^<' ') then
  1149. begin
  1150. if (p^ in [#10,#13]) or
  1151. (ctrlZmarkseof and (p^=#26)) then
  1152. begin
  1153. end_of_string:=true;
  1154. break;
  1155. end;
  1156. end;
  1157. inc(p);
  1158. end;
  1159. { calculate read bytes }
  1160. len:=p-startp;
  1161. inc(TextRec(f).BufPos,Len);
  1162. Move(startp^,s[sPos],Len);
  1163. inc(sPos,Len);
  1164. until (spos=MaxLen) or end_of_string;
  1165. ReadPCharLen:=spos;
  1166. End;
  1167. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  1168. Begin
  1169. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  1170. End;
  1171. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  1172. Begin
  1173. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  1174. End;
  1175. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  1176. var
  1177. len: longint;
  1178. Begin
  1179. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  1180. if zerobased and
  1181. (len > high(s)) then
  1182. len := high(s);
  1183. if (len <= high(s)) then
  1184. s[len] := #0;
  1185. End;
  1186. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1187. 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;
  1188. var
  1189. slen,len : SizeInt;
  1190. Begin
  1191. slen:=0;
  1192. Repeat
  1193. // SetLength will reallocate the length.
  1194. SetLength(s,slen+255);
  1195. len:=ReadPCharLen(f,pchar(Pointer(s)+slen),255);
  1196. inc(slen,len);
  1197. Until len<255;
  1198. // Set actual length
  1199. SetLength(s,Slen);
  1200. {$ifdef FPC_HAS_CPSTRING}
  1201. SetCodePage(s,TextRec(f).CodePage,false);
  1202. if cp<>TextRec(f).CodePage then
  1203. s:=fpc_AnsiStr_To_AnsiStr(s,cp);
  1204. {$endif FPC_HAS_CPSTRING}
  1205. End;
  1206. 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'];
  1207. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1208. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1209. Procedure fpc_Read_Text_UnicodeStr(var f : Text;out us : UnicodeString); iocheck; compilerproc;
  1210. var
  1211. s: RawByteString;
  1212. Begin
  1213. // all standard input is assumed to be ansi-encoded
  1214. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1215. // Convert to unicodestring
  1216. {$ifdef FPC_HAS_CPSTRING}
  1217. widestringmanager.Ansi2UnicodeMoveProc(PAnsiChar(s),StringCodePage(s),us,Length(s));
  1218. {$else}
  1219. us:=s;
  1220. {$endif}
  1221. End;
  1222. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1223. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1224. Procedure fpc_Read_Text_WideStr(var f : Text;out ws : WideString); iocheck; compilerproc;
  1225. var
  1226. s: RawByteString;
  1227. Begin
  1228. // all standard input is assumed to be ansi-encoded
  1229. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1230. // Convert to widestring
  1231. {$ifdef FPC_HAS_CPSTRING}
  1232. widestringmanager.Ansi2WideMoveProc(PAnsiChar(s),StringCodePage(s),ws,Length(s));
  1233. {$else}
  1234. ws:=s;
  1235. {$endif}
  1236. End;
  1237. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1238. procedure fpc_Read_Text_Char(var f : Text; out c: char); [public, alias: 'FPC_READ_TEXT_CHAR']; iocheck; compilerproc;
  1239. Begin
  1240. c:=#0;
  1241. If not CheckRead(f) then
  1242. exit;
  1243. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1244. begin
  1245. c := #26;
  1246. exit;
  1247. end;
  1248. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1249. inc(TextRec(f).BufPos);
  1250. end;
  1251. procedure fpc_Read_Text_Char_intern(var f : Text; out c: char); iocheck; [external name 'FPC_READ_TEXT_CHAR'];
  1252. function fpc_GetBuf(var f : Text) : pchar; iocheck; compilerproc;
  1253. Begin
  1254. Result:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1255. If not CheckRead(f) then
  1256. exit;
  1257. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1258. exit;
  1259. Result:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1260. end;
  1261. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1262. procedure fpc_Read_Text_WideChar(var f : Text; out wc: widechar); iocheck;compilerproc;
  1263. var
  1264. ws: widestring;
  1265. i: longint;
  1266. { maximum code point length is 6 characters (with UTF-8) }
  1267. str: array[0..5] of char;
  1268. Begin
  1269. fillchar(str[0],sizeof(str),0);
  1270. for i:=low(str) to high(str) do
  1271. begin
  1272. fpc_Read_Text_Char_intern(f,str[i]);
  1273. case widestringmanager.CodePointLengthProc(@str[0],i+1) of
  1274. -1: { possibly incomplete code point, try with an extra character }
  1275. ;
  1276. 0: { null character }
  1277. begin
  1278. wc:=#0;
  1279. exit;
  1280. end;
  1281. else
  1282. begin
  1283. { valid code point -> convert to widestring}
  1284. {$ifdef FPC_HAS_CPSTRING}
  1285. widestringmanager.Ansi2WideMoveProc(@str[0],TextRec(f).CodePage,ws,i+1);
  1286. {$else}
  1287. widestringmanager.Ansi2WideMoveProc(@str[0],DefaultSystemCodePage,ws,i+1);
  1288. {$endif}
  1289. { has to be exactly one widechar }
  1290. if length(ws)=1 then
  1291. begin
  1292. wc:=ws[1];
  1293. exit
  1294. end
  1295. else
  1296. break;
  1297. end;
  1298. end;
  1299. end;
  1300. { invalid widechar input }
  1301. inoutres:=106;
  1302. end;
  1303. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1304. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: char); iocheck;compilerproc;
  1305. Begin
  1306. c:=' ';
  1307. If not CheckRead(f) then
  1308. exit;
  1309. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1310. begin
  1311. c:=' ';
  1312. exit;
  1313. end;
  1314. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1315. inc(TextRec(f).BufPos);
  1316. if c=#13 then
  1317. begin
  1318. c:=' ';
  1319. If not CheckRead(f) or
  1320. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1321. exit;
  1322. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1323. inc(TextRec(f).BufPos);
  1324. { ignore #26 following a new line }
  1325. If not CheckRead(f) or
  1326. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1327. exit;
  1328. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1329. inc(TextRec(f).BufPos);
  1330. end
  1331. else if c=#10 then
  1332. begin
  1333. c:=' ';
  1334. { ignore #26 following a new line }
  1335. If not CheckRead(f) or
  1336. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1337. exit;
  1338. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1339. inc(TextRec(f).BufPos);
  1340. end
  1341. else if c=#26 then
  1342. c:=' ';
  1343. end;
  1344. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1345. var
  1346. hs : String;
  1347. code : ValSInt;
  1348. Begin
  1349. l:=0;
  1350. If not CheckRead(f) then
  1351. exit;
  1352. hs:='';
  1353. if IgnoreSpaces(f) then
  1354. begin
  1355. { When spaces were found and we are now at EOF,
  1356. then we return 0 }
  1357. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1358. exit;
  1359. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1360. exit;
  1361. ReadNumeric(f,hs);
  1362. end;
  1363. if (hs = '') then
  1364. L := 0
  1365. else
  1366. begin
  1367. Val(hs,l,code);
  1368. if Code <> 0 then
  1369. InOutRes:=106;
  1370. end;
  1371. End;
  1372. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1373. var
  1374. hs : String;
  1375. code : ValSInt;
  1376. Begin
  1377. u:=0;
  1378. If not CheckRead(f) then
  1379. exit;
  1380. hs:='';
  1381. if IgnoreSpaces(f) then
  1382. begin
  1383. { When spaces were found and we are now at EOF,
  1384. then we return 0 }
  1385. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1386. exit;
  1387. ReadNumeric(f,hs);
  1388. end;
  1389. if (hs = '') then
  1390. u := 0
  1391. else
  1392. begin
  1393. val(hs,u,code);
  1394. If code<>0 Then
  1395. InOutRes:=106;
  1396. end;
  1397. End;
  1398. {$ifndef FPUNONE}
  1399. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1400. var
  1401. hs : string;
  1402. code : Word;
  1403. begin
  1404. v:=0.0;
  1405. If not CheckRead(f) then
  1406. exit;
  1407. hs:='';
  1408. if IgnoreSpaces(f) then
  1409. begin
  1410. { When spaces were found and we are now at EOF,
  1411. then we return 0 }
  1412. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1413. exit;
  1414. ReadNumeric(f,hs);
  1415. end;
  1416. val(hs,v,code);
  1417. If code<>0 Then
  1418. InOutRes:=106;
  1419. end;
  1420. {$endif}
  1421. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1422. var s:string;
  1423. code:valsint;
  1424. begin
  1425. if not checkread(t) then
  1426. exit;
  1427. s:='';
  1428. if ignorespaces(t) then
  1429. begin
  1430. { When spaces were found and we are now at EOF, then we return 0 }
  1431. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1432. exit;
  1433. ReadNumeric(t,s);
  1434. end;
  1435. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1436. if code<>0 then
  1437. InOutRes:=106;
  1438. end;
  1439. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1440. var
  1441. hs : string;
  1442. code : ValSInt;
  1443. begin
  1444. {$ifdef FPUNONE}
  1445. v:=0;
  1446. {$else}
  1447. v:=0.0;
  1448. {$endif}
  1449. If not CheckRead(f) then
  1450. exit;
  1451. hs:='';
  1452. if IgnoreSpaces(f) then
  1453. begin
  1454. { When spaces were found and we are now at EOF,
  1455. then we return 0 }
  1456. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1457. exit;
  1458. ReadNumeric(f,hs);
  1459. end;
  1460. val(hs,v,code);
  1461. If code<>0 Then
  1462. InOutRes:=106;
  1463. end;
  1464. {$ifndef cpu64}
  1465. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1466. var
  1467. hs : String;
  1468. code : longint;
  1469. Begin
  1470. q:=0;
  1471. If not CheckRead(f) then
  1472. exit;
  1473. hs:='';
  1474. if IgnoreSpaces(f) then
  1475. begin
  1476. { When spaces were found and we are now at EOF,
  1477. then we return 0 }
  1478. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1479. exit;
  1480. ReadNumeric(f,hs);
  1481. end;
  1482. val(hs,q,code);
  1483. If code<>0 Then
  1484. InOutRes:=106;
  1485. End;
  1486. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1487. var
  1488. hs : String;
  1489. code : Longint;
  1490. Begin
  1491. i:=0;
  1492. If not CheckRead(f) then
  1493. exit;
  1494. hs:='';
  1495. if IgnoreSpaces(f) then
  1496. begin
  1497. { When spaces were found and we are now at EOF,
  1498. then we return 0 }
  1499. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1500. exit;
  1501. ReadNumeric(f,hs);
  1502. end;
  1503. Val(hs,i,code);
  1504. If code<>0 Then
  1505. InOutRes:=106;
  1506. End;
  1507. {$endif CPU64}
  1508. {*****************************************************************************
  1509. WriteStr/ReadStr
  1510. *****************************************************************************}
  1511. const
  1512. { pointer to target string }
  1513. StrPtrIndex = 1;
  1514. { temporary destination for writerstr, because the original value of the
  1515. destination may be used in the writestr expression }
  1516. TempWriteStrDestIndex = 9;
  1517. ShortStrLenIndex = 17;
  1518. { how many bytes of the string have been processed already (used for readstr) }
  1519. BytesReadIndex = 17;
  1520. {$ifdef FPC_HAS_FEATURE_THREADING}
  1521. ThreadVar
  1522. {$else FPC_HAS_FEATURE_THREADING}
  1523. Var
  1524. {$endif FPC_HAS_FEATURE_THREADING}
  1525. ReadWriteStrText: textrec;
  1526. procedure WriteStrShort(var t: textrec);
  1527. var
  1528. str: pshortstring;
  1529. newbytes,
  1530. oldlen: longint;
  1531. begin
  1532. if (t.bufpos=0) then
  1533. exit;
  1534. str:=pshortstring(ppointer(@t.userdata[TempWriteStrDestIndex])^);
  1535. newbytes:=t.BufPos;
  1536. oldlen:=length(str^);
  1537. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1538. begin
  1539. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1540. {$ifdef writestr_iolencheck}
  1541. // GPC only gives an io error if {$no-truncate-strings} is active
  1542. // FPC does not have this setting (it never gives errors when a
  1543. // a string expression is truncated)
  1544. { "disk full" }
  1545. inoutres:=101;
  1546. {$endif}
  1547. end;
  1548. setlength(str^,length(str^)+newbytes);
  1549. move(t.bufptr^,str^[oldlen+1],newbytes);
  1550. t.bufpos:=0;
  1551. end;
  1552. procedure WriteStrShortFlush(var t: textrec);
  1553. begin
  1554. { move written data from internal buffer to temporary string (don't move
  1555. directly from buffer to final string, because the temporary string may
  1556. already contain data in case the textbuf was smaller than the string
  1557. length) }
  1558. WriteStrShort(t);
  1559. { move written data to original string }
  1560. move(PPointer(@t.userdata[TempWriteStrDestIndex])^^,
  1561. PPointer(@t.userdata[StrPtrIndex])^^,
  1562. t.userdata[ShortStrLenIndex]+1);
  1563. { free temporary buffer }
  1564. freemem(PPointer(@t.userdata[TempWriteStrDestIndex])^);
  1565. end;
  1566. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1567. procedure WriteStrAnsi(var t: textrec);
  1568. var
  1569. str: pansistring;
  1570. oldlen: longint;
  1571. begin
  1572. if (t.bufpos=0) then
  1573. exit;
  1574. str:=pansistring(@t.userdata[TempWriteStrDestIndex]);
  1575. oldlen:=length(str^);
  1576. setlength(str^,oldlen+t.bufpos);
  1577. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1578. t.bufpos:=0;
  1579. end;
  1580. procedure WriteStrAnsiFlush(var t: textrec);
  1581. begin
  1582. { see comment in WriteStrShortFlush }
  1583. WriteStrAnsi(t);
  1584. pansistring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1585. pansistring(@t.userdata[TempWriteStrDestIndex])^;
  1586. { free memory/finalize temp }
  1587. pansistring(@t.userdata[TempWriteStrDestIndex])^:='';
  1588. end;
  1589. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1590. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1591. procedure WriteStrUnicode(var t: textrec);
  1592. var
  1593. temp: ansistring;
  1594. str: punicodestring;
  1595. begin
  1596. if (t.bufpos=0) then
  1597. exit;
  1598. str:=punicodestring(@t.userdata[TempWriteStrDestIndex]);
  1599. setlength(temp,t.bufpos);
  1600. move(t.bufptr^,temp[1],t.bufpos);
  1601. str^:=str^+temp;
  1602. t.bufpos:=0;
  1603. end;
  1604. procedure WriteStrUnicodeFlush(var t: textrec);
  1605. begin
  1606. { see comment in WriteStrShortFlush }
  1607. WriteStrUnicode(t);
  1608. punicodestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1609. punicodestring(@t.userdata[TempWriteStrDestIndex])^;
  1610. { free memory/finalize temp }
  1611. punicodestring(@t.userdata[TempWriteStrDestIndex])^:='';
  1612. end;
  1613. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1614. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1615. procedure WriteStrWide(var t: textrec);
  1616. var
  1617. temp: ansistring;
  1618. str: pwidestring;
  1619. begin
  1620. if (t.bufpos=0) then
  1621. exit;
  1622. str:=pwidestring(@t.userdata[TempWriteStrDestIndex]);
  1623. setlength(temp,t.bufpos);
  1624. move(t.bufptr^,temp[1],t.bufpos);
  1625. str^:=str^+temp;
  1626. t.bufpos:=0;
  1627. end;
  1628. procedure WriteStrWideFlush(var t: textrec);
  1629. begin
  1630. { see comment in WriteStrShortFlush }
  1631. WriteStrWide(t);
  1632. pwidestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1633. pwidestring(@t.userdata[TempWriteStrDestIndex])^;
  1634. { free memory/finalize temp }
  1635. finalize(pwidestring(@t.userdata[TempWriteStrDestIndex])^);
  1636. end;
  1637. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1638. procedure SetupWriteStrCommon(out t: textrec);
  1639. begin
  1640. // initialise
  1641. Assign(text(t),'');
  1642. t.mode:=fmOutput;
  1643. t.OpenFunc:=nil;
  1644. t.CloseFunc:=nil;
  1645. {$ifdef FPC_HAS_CPSTRING}
  1646. t.CodePage:=DefaultSystemCodePage;
  1647. {$endif}
  1648. end;
  1649. function fpc_SetupWriteStr_Shortstr(var s: shortstring): PText; compilerproc;
  1650. begin
  1651. setupwritestrcommon(ReadWriteStrText);
  1652. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1653. { temporary destination (see comments for TempWriteStrDestIndex) }
  1654. getmem(PPointer(@ReadWriteStrText.userdata[TempWriteStrDestIndex])^,high(s)+1);
  1655. setlength(pshortstring(ppointer(@ReadWriteStrText.userdata[TempWriteStrDestIndex])^)^,0);
  1656. ReadWriteStrText.userdata[ShortStrLenIndex]:=high(s);
  1657. ReadWriteStrText.InOutFunc:=@WriteStrShort;
  1658. ReadWriteStrText.FlushFunc:=@WriteStrShortFlush;
  1659. result:=@ReadWriteStrText;
  1660. end;
  1661. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1662. function fpc_SetupWriteStr_Ansistr(var s: ansistring): PText; compilerproc;
  1663. begin
  1664. setupwritestrcommon(ReadWriteStrText);
  1665. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1666. { temp destination ansistring, nil = empty string }
  1667. PPointer(@ReadWriteStrText.userdata[TempWriteStrDestIndex])^:=nil;
  1668. ReadWriteStrText.InOutFunc:=@WriteStrAnsi;
  1669. ReadWriteStrText.FlushFunc:=@WriteStrAnsiFlush;
  1670. result:=@ReadWriteStrText;
  1671. end;
  1672. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1673. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1674. function fpc_SetupWriteStr_Unicodestr(var s: unicodestring): PText; compilerproc;
  1675. begin
  1676. setupwritestrcommon(ReadWriteStrText);
  1677. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1678. { temp destination unicodestring, nil = empty string }
  1679. PPointer(@ReadWriteStrText.userdata[TempWriteStrDestIndex])^:=nil;
  1680. ReadWriteStrText.InOutFunc:=@WriteStrUnicode;
  1681. ReadWriteStrText.FlushFunc:=@WriteStrUnicodeFlush;
  1682. result:=@ReadWriteStrText;
  1683. end;
  1684. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1685. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1686. function fpc_SetupWriteStr_Widestr(var s: widestring): PText; compilerproc;
  1687. begin
  1688. setupwritestrcommon(ReadWriteStrText);
  1689. PPointer(@ReadWriteStrText.userdata[StrPtrIndex])^:=@s;
  1690. { temp destination widestring }
  1691. PWideString(@ReadWriteStrText.userdata[TempWriteStrDestIndex])^:='';
  1692. ReadWriteStrText.InOutFunc:=@WriteStrWide;
  1693. ReadWriteStrText.FlushFunc:=@WriteStrWideFlush;
  1694. result:=@ReadWriteStrText;
  1695. end;
  1696. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1697. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1698. procedure ReadAnsiStrFinal(var t: textrec);
  1699. begin
  1700. { finalise the temp ansistring }
  1701. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  1702. end;
  1703. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1704. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  1705. var
  1706. newbytes: sizeint;
  1707. begin
  1708. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  1709. if (t.BufSize <= newbytes) then
  1710. newbytes := t.BufSize;
  1711. if (newbytes > 0) then
  1712. begin
  1713. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  1714. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  1715. end;
  1716. t.BufEnd:=newbytes;
  1717. t.BufPos:=0;
  1718. end;
  1719. procedure ReadStrAnsi(var t: textrec);
  1720. var
  1721. str: pansistring;
  1722. begin
  1723. str:=pansistring(@t.userdata[StrPtrIndex]);
  1724. ReadStrCommon(t,@str^[1],length(str^));
  1725. end;
  1726. procedure SetupReadStrCommon(out t: textrec);
  1727. begin
  1728. // initialise
  1729. Assign(text(t),'');
  1730. t.mode:=fmInput;
  1731. t.OpenFunc:=nil;
  1732. t.CloseFunc:=nil;
  1733. {$ifdef FPC_HAS_CPSTRING}
  1734. t.CodePage:=DefaultSystemCodePage;
  1735. {$endif}
  1736. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  1737. end;
  1738. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1739. function fpc_SetupReadStr_Ansistr(const s: ansistring): PText; [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  1740. begin
  1741. setupreadstrcommon(ReadWriteStrText);
  1742. { we need a reference, because 's' may be a temporary expression }
  1743. PAnsiString(@ReadWriteStrText.userdata[StrPtrIndex])^:=s;
  1744. ReadWriteStrText.InOutFunc:=@ReadStrAnsi;
  1745. { this is called at the end, by fpc_read_end }
  1746. ReadWriteStrText.FlushFunc:=@ReadAnsiStrFinal;
  1747. result:=@ReadWriteStrText;
  1748. end;
  1749. function fpc_SetupReadStr_Ansistr_Intern(const s: ansistring): PText; [external name 'FPC_SETUPREADSTR_ANSISTR'];
  1750. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1751. function fpc_SetupReadStr_Shortstr(const s: shortstring): PText; compilerproc;
  1752. begin
  1753. { the reason we convert the short string to ansistring, is because the semantics of
  1754. readstr are defined as:
  1755. *********************
  1756. Apart from the restrictions imposed by requirements given in this clause,
  1757. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  1758. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  1759. char-type (or a subrange of char-type), the integer-type (or a subrange of
  1760. integer-type), the real-type, a fixed-string-type, or a
  1761. variable-string-type, shall be equivalent to
  1762. begin
  1763. rewrite(f);
  1764. writeln(f, e);
  1765. reset(f);
  1766. read(f, v 1 ,...,v n )
  1767. end
  1768. *********************
  1769. This means that any side effects caused by the evaluation of v 1 .. v n
  1770. must not affect the value of e (= our argument s) -> we need a copy of it.
  1771. An ansistring is the easiest way to get a threadsafe copy, and allows us
  1772. to use the other ansistring readstr helpers too.
  1773. }
  1774. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1775. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1776. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  1777. runerror(217);
  1778. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1779. end;
  1780. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1781. function fpc_SetupReadStr_Unicodestr(const s: unicodestring): PText; compilerproc;
  1782. begin
  1783. { we use an ansistring to avoid code duplication, and let the }
  1784. { assignment convert the widestring to an equivalent ansistring }
  1785. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1786. end;
  1787. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1788. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1789. function fpc_SetupReadStr_Widestr(const s: widestring): PText; compilerproc;
  1790. begin
  1791. { we use an ansistring to avoid code duplication, and let the }
  1792. { assignment convert the widestring to an equivalent ansistring }
  1793. result:=fpc_SetupReadStr_Ansistr_Intern(s);
  1794. end;
  1795. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1796. {*****************************************************************************
  1797. Initializing
  1798. *****************************************************************************}
  1799. procedure OpenStdIO(var f:text;mode:longint;hdl:thandle);
  1800. begin
  1801. Assign(f,'');
  1802. TextRec(f).Handle:=hdl;
  1803. TextRec(f).Mode:=mode;
  1804. TextRec(f).Closefunc:=@FileCloseFunc;
  1805. case mode of
  1806. fmInput :
  1807. begin
  1808. TextRec(f).InOutFunc:=@FileReadFunc;
  1809. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1810. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleInput);
  1811. {$endif}
  1812. end;
  1813. fmOutput :
  1814. begin
  1815. TextRec(f).InOutFunc:=@FileWriteFunc;
  1816. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  1817. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleOutput);
  1818. {$endif}
  1819. if Do_Isdevice(hdl) then
  1820. TextRec(f).FlushFunc:=@FileWriteFunc;
  1821. end;
  1822. else
  1823. HandleError(102);
  1824. end;
  1825. end;