text.inc 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  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,PFileTextRecChar(@t.Name),Flags,False);
  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 InitText(Var t : Text);
  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. end;
  80. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  81. Procedure Assign(out t:Text;const s : UnicodeString);
  82. begin
  83. InitText(t);
  84. {$ifdef FPC_ANSI_TEXTFILEREC}
  85. TextRec(t).Name:=ToSingleByteFileSystemEncodedFileName(S);
  86. {$else FPC_ANSI_TEXTFILEREC}
  87. TextRec(t).Name:=S;
  88. {$endif FPC_ANSI_TEXTFILEREC}
  89. { null terminate, since the name array is regularly used as p(wide)char }
  90. TextRec(t).Name[high(TextRec(t).Name)]:=#0;
  91. end;
  92. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  93. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  94. Procedure Assign(out t:Text;const s: RawByteString);
  95. Begin
  96. InitText(t);
  97. {$ifdef FPC_ANSI_TEXTFILEREC}
  98. { ensure the characters in the record's filename are encoded correctly }
  99. TextRec(t).Name:=ToSingleByteFileSystemEncodedFileName(S);
  100. {$else FPC_ANSI_TEXTFILEREC}
  101. TextRec(t).Name:=S;
  102. {$endif FPC_ANSI_TEXTFILEREC}
  103. { null terminate, since the name array is regularly used as p(wide)char }
  104. TextRec(t).Name[high(TextRec(t).Name)]:=#0;
  105. End;
  106. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  107. Procedure Assign(out t:Text;const s: ShortString);
  108. Begin
  109. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  110. Assign(t,AnsiString(s));
  111. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  112. InitText(t);
  113. { warning: no encoding support }
  114. TextRec(t).Name:=s;
  115. { null terminate, since the name array is regularly used as p(wide)char }
  116. TextRec(t).Name[high(TextRec(t).Name)]:=#0;
  117. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  118. End;
  119. Procedure Assign(out t:Text;const p: PAnsiChar);
  120. Begin
  121. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  122. Assign(t,AnsiString(p));
  123. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  124. { no use in making this the one that does the work, since the name field is
  125. limited to 255 characters anyway }
  126. Assign(t,strpas(p));
  127. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  128. End;
  129. Procedure Assign(out t:Text;const c: AnsiChar);
  130. Begin
  131. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  132. Assign(t,AnsiString(c));
  133. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  134. Assign(t,ShortString(c));
  135. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  136. End;
  137. Procedure Close(var t : Text);[IOCheck];
  138. Begin
  139. if InOutRes<>0 then
  140. Exit;
  141. case TextRec(t).mode of
  142. fmInput,fmOutput,fmAppend:
  143. Begin
  144. { Write pending buffer }
  145. If Textrec(t).Mode=fmoutput then
  146. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  147. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  148. { Only close functions not connected to stdout.}
  149. If ((TextRec(t).Handle<>StdInputHandle) and
  150. (TextRec(t).Handle<>StdOutputHandle) and
  151. (TextRec(t).Handle<>StdErrorHandle)) Then
  152. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  153. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  154. TextRec(t).mode := fmClosed;
  155. { Reset buffer for safety }
  156. TextRec(t).BufPos:=0;
  157. TextRec(t).BufEnd:=0;
  158. End
  159. else inOutRes := 103;
  160. End;
  161. End;
  162. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  163. Begin
  164. Case TextRec(t).mode Of {This gives the fastest code}
  165. fmInput,fmOutput,fmInOut : Close(t);
  166. fmClosed : ;
  167. else
  168. Begin
  169. InOutRes:=102;
  170. exit;
  171. End;
  172. End;
  173. TextRec(t).mode:=mode;
  174. TextRec(t).bufpos:=0;
  175. TextRec(t).bufend:=0;
  176. {$ifdef FPC_HAS_CPSTRING}
  177. { if no codepage is yet assigned then assign default ansi codepage }
  178. TextRec(t).CodePage:=TranslatePlaceholderCP(TextRec(t).CodePage);
  179. {$endif}
  180. FileFunc(TextRec(t).OpenFunc)(TextRec(t));
  181. { reset the mode to closed when an error has occured }
  182. if InOutRes<>0 then
  183. TextRec(t).mode:=fmClosed;
  184. End;
  185. Procedure Rewrite(var t : Text);[IOCheck];
  186. Begin
  187. If InOutRes<>0 then
  188. exit;
  189. OpenText(t,fmOutput,1);
  190. End;
  191. Procedure Reset(var t : Text);[IOCheck];
  192. Begin
  193. If InOutRes<>0 then
  194. exit;
  195. OpenText(t,fmInput,0);
  196. End;
  197. Procedure Append(var t : Text);[IOCheck];
  198. Begin
  199. If InOutRes<>0 then
  200. exit;
  201. OpenText(t,fmAppend,1);
  202. End;
  203. Procedure Flush(var t : Text);[IOCheck];
  204. Begin
  205. If InOutRes<>0 then
  206. exit;
  207. if TextRec(t).mode<>fmOutput then
  208. begin
  209. if TextRec(t).mode=fmInput then
  210. InOutRes:=105
  211. else
  212. InOutRes:=103;
  213. exit;
  214. end;
  215. { Not the flushfunc but the inoutfunc should be used, because that
  216. writes the data, flushfunc doesn't need to be assigned }
  217. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  218. End;
  219. Procedure Erase(var t:Text);[IOCheck];
  220. Begin
  221. If InOutRes <> 0 then
  222. exit;
  223. If TextRec(t).mode=fmClosed Then
  224. Do_Erase(PFileTextRecChar(@TextRec(t).Name),false);
  225. End;
  226. Procedure Rename(var t : Text;const s : unicodestring);[IOCheck];
  227. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  228. var
  229. fs: RawByteString;
  230. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  231. Begin
  232. If (InOutRes<>0) or
  233. (TextRec(t).mode<>fmClosed) then
  234. exit;
  235. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  236. { it's slightly faster to convert the unicodestring here to rawbytestring
  237. than doing it in do_rename(), because here we still know the length }
  238. fs:=ToSingleByteFileSystemEncodedFileName(s);
  239. Do_Rename(PFileTextRecChar(@TextRec(t).Name),PAnsiChar(fs),false,true);
  240. If InOutRes=0 then
  241. TextRec(t).Name:=fs
  242. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  243. Do_Rename(PFileTextRecChar(@TextRec(t).Name),PUnicodeChar(S),false,false);
  244. If InOutRes=0 then
  245. {$ifdef FPC_ANSI_TEXTTextRec}
  246. TextRec(t).Name:=ToSingleByteFileSystemEncodedFileName(s);
  247. {$else FPC_ANSI_TEXTFILEREC}
  248. TextRec(t).Name:=s
  249. {$endif FPC_ANSI_TEXTFILEREC}
  250. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  251. End;
  252. Procedure Rename(var t : Text;const s : rawbytestring);[IOCheck];
  253. var
  254. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  255. fs: RawByteString;
  256. pdst: PAnsiChar;
  257. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  258. fs: UnicodeString;
  259. pdst: PUnicodeChar;
  260. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  261. dstchangeable: boolean;
  262. Begin
  263. If (InOutRes<>0) or
  264. (TextRec(t).mode<>fmClosed) then
  265. exit;
  266. {$ifdef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  267. dstchangeable:=false;
  268. pdst:=PAnsiChar(s);
  269. if TranslatePlaceholderCP(StringCodePage(s))<>DefaultFileSystemCodePage then
  270. begin
  271. fs:=ToSingleByteFileSystemEncodedFileName(s);
  272. pdst:=PAnsiChar(fs);
  273. dstchangeable:=true;
  274. end
  275. else
  276. fs:=s;
  277. {$else FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  278. { it's slightly faster to convert the rawbytestring here to unicodestring
  279. than doing it in do_rename, because here we still know the length }
  280. fs:=unicodestring(s);
  281. pdst:=PUnicodeChar(fs);
  282. dstchangeable:=true;
  283. {$endif FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  284. Do_Rename(PFileTextRecChar(@TextRec(t).Name),pdst,false,dstchangeable);
  285. If InOutRes=0 then
  286. {$if defined(FPC_ANSI_TEXTTextRec) and not defined(FPCRTL_FILESYSTEM_SINGLE_BYTE_API)}
  287. TextRec(t).Name:=ToSingleByteFileSystemEncodedFileName(fs)
  288. {$else FPC_ANSI_TEXTTextRec and not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  289. TextRec(t).Name:=fs
  290. {$endif FPC_ANSI_TEXTTextRec and not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  291. End;
  292. Procedure Rename(var t : Text;const s : ShortString);[IOCheck];
  293. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  294. Begin
  295. Rename(t,AnsiString(s));
  296. End;
  297. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  298. var
  299. p : array[0..255] Of Char;
  300. Begin
  301. Move(s[1],p,Length(s));
  302. p[Length(s)]:=#0;
  303. Rename(t,Pchar(@p));
  304. End;
  305. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  306. Procedure Rename(var t:Text;const p:PAnsiChar);
  307. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  308. Begin
  309. Rename(t,AnsiString(p));
  310. End;
  311. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  312. var
  313. len: SizeInt
  314. Begin
  315. If InOutRes<>0 then
  316. exit;
  317. If TextRec(t).mode=fmClosed Then
  318. Begin
  319. Do_Rename(PFileTextRecChar(@TextRec(t).Name),p,false);
  320. { check error code of do_rename }
  321. If InOutRes=0 then
  322. begin
  323. len:=min(StrLen(p),high(TextRec(t).Name));
  324. Move(p^,TextRec(t).Name,len);
  325. TextRec(t).Name[len]:=#0;
  326. end;
  327. End;
  328. End;
  329. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  330. Procedure Rename(var t : Text;const c : AnsiChar);[IOCheck];
  331. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  332. Begin
  333. Rename(t,AnsiString(c));
  334. End;
  335. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  336. var
  337. p : array[0..1] Of AnsiChar;
  338. Begin
  339. p[0]:=c;
  340. p[1]:=#0;
  341. Rename(t,PAnsiChar(@p));
  342. End;
  343. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  344. Function Eof(Var t: Text): Boolean;[IOCheck];
  345. Begin
  346. If (InOutRes<>0) then
  347. exit(true);
  348. if (TextRec(t).mode<>fmInput) Then
  349. begin
  350. if TextRec(t).mode=fmOutput then
  351. InOutRes:=104
  352. else
  353. InOutRes:=103;
  354. exit(true);
  355. end;
  356. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  357. begin
  358. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  359. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  360. exit(true);
  361. end;
  362. Eof:=CtrlZMarksEOF and (TextRec(t).Bufptr^[TextRec(t).BufPos]=#26);
  363. end;
  364. Function Eof:Boolean;
  365. Begin
  366. Eof:=Eof(Input);
  367. End;
  368. Function SeekEof (Var t : Text) : Boolean;
  369. var
  370. oldfilepos : Int64;
  371. oldbufpos, oldbufend : SizeInt;
  372. reads: longint;
  373. isdevice: boolean;
  374. Begin
  375. If (InOutRes<>0) then
  376. exit(true);
  377. if (TextRec(t).mode<>fmInput) Then
  378. begin
  379. if TextRec(t).mode=fmOutPut then
  380. InOutRes:=104
  381. else
  382. InOutRes:=103;
  383. exit(true);
  384. end;
  385. { try to save the current position in the file, seekeof() should not move }
  386. { the current file position (JM) }
  387. oldbufpos := TextRec(t).BufPos;
  388. oldbufend := TextRec(t).BufEnd;
  389. reads := 0;
  390. oldfilepos := -1;
  391. isdevice := Do_IsDevice(TextRec(t).handle);
  392. repeat
  393. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  394. begin
  395. { signal that the we will have to do a seek }
  396. inc(reads);
  397. if not isdevice and
  398. (reads = 1) then
  399. begin
  400. oldfilepos := Do_FilePos(TextRec(t).handle) - TextRec(t).BufEnd;
  401. InOutRes:=0;
  402. end;
  403. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  404. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  405. begin
  406. { if we only did a read in which we didn't read anything, the }
  407. { old buffer is still valid and we can simply restore the }
  408. { pointers (JM) }
  409. dec(reads);
  410. SeekEof := true;
  411. break;
  412. end;
  413. end;
  414. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  415. #26 :
  416. if CtrlZMarksEOF then
  417. begin
  418. SeekEof := true;
  419. break;
  420. end;
  421. #10,#13,#9,' ' :
  422. ;
  423. else
  424. begin
  425. SeekEof := false;
  426. break;
  427. end;
  428. end;
  429. inc(TextRec(t).BufPos);
  430. until false;
  431. { restore file position if not working with a device }
  432. if not isdevice then
  433. { if we didn't modify the buffer, simply restore the BufPos and BufEnd }
  434. { (the latter because it's now probably set to zero because nothing was }
  435. { was read anymore) }
  436. if (reads = 0) then
  437. begin
  438. TextRec(t).BufPos:=oldbufpos;
  439. TextRec(t).BufEnd:=oldbufend;
  440. end
  441. { otherwise return to the old filepos and reset the buffer }
  442. else
  443. begin
  444. do_seek(TextRec(t).handle,oldfilepos);
  445. InOutRes:=0;
  446. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  447. TextRec(t).BufPos:=oldbufpos;
  448. end;
  449. End;
  450. Function SeekEof : Boolean;
  451. Begin
  452. SeekEof:=SeekEof(Input);
  453. End;
  454. Function Eoln(var t:Text) : Boolean;
  455. Begin
  456. If (InOutRes<>0) then
  457. exit(true);
  458. if (TextRec(t).mode<>fmInput) Then
  459. begin
  460. if TextRec(t).mode=fmOutPut then
  461. InOutRes:=104
  462. else
  463. InOutRes:=103;
  464. exit(true);
  465. end;
  466. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  467. begin
  468. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  469. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  470. exit(true);
  471. end;
  472. if CtrlZMarksEOF and (TextRec (T).BufPtr^[TextRec (T).BufPos] = #26) then
  473. exit (true);
  474. Eoln:=(TextRec(t).Bufptr^[TextRec(t).BufPos] in [#10,#13]);
  475. End;
  476. Function Eoln : Boolean;
  477. Begin
  478. Eoln:=Eoln(Input);
  479. End;
  480. Function SeekEoln (Var t : Text) : Boolean;
  481. Begin
  482. If (InOutRes<>0) then
  483. exit(true);
  484. if (TextRec(t).mode<>fmInput) Then
  485. begin
  486. if TextRec(t).mode=fmOutput then
  487. InOutRes:=104
  488. else
  489. InOutRes:=103;
  490. exit(true);
  491. end;
  492. repeat
  493. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  494. begin
  495. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  496. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  497. exit(true);
  498. end;
  499. case TextRec(t).Bufptr^[TextRec(t).BufPos] of
  500. #26: if CtrlZMarksEOF then
  501. exit (true);
  502. #10,#13 : exit(true);
  503. #9,' ' : ;
  504. else
  505. exit(false);
  506. end;
  507. inc(TextRec(t).BufPos);
  508. until false;
  509. End;
  510. Function SeekEoln : Boolean;
  511. Begin
  512. SeekEoln:=SeekEoln(Input);
  513. End;
  514. Procedure SetTextBuf(Var F : Text; Var Buf; Size : SizeInt);
  515. Begin
  516. TextRec(f).BufPtr:=@Buf;
  517. TextRec(f).BufSize:=Size;
  518. TextRec(f).BufPos:=0;
  519. TextRec(f).BufEnd:=0;
  520. End;
  521. Procedure SetTextLineEnding(Var f:Text; Ending:string);
  522. Begin
  523. TextRec(F).LineEnd:=Ending;
  524. End;
  525. function GetTextCodePage(var T: Text): TSystemCodePage;
  526. begin
  527. {$ifdef FPC_HAS_CPSTRING}
  528. GetTextCodePage:=TextRec(T).CodePage;
  529. {$else}
  530. GetTextCodePage:=0;
  531. {$endif}
  532. end;
  533. procedure SetTextCodePage(var T: Text; CodePage: TSystemCodePage);
  534. begin
  535. {$ifdef FPC_HAS_CPSTRING}
  536. TextRec(T).CodePage:=TranslatePlaceholderCP(CodePage);
  537. {$endif}
  538. end;
  539. Function fpc_get_input:PText;compilerproc;
  540. begin
  541. fpc_get_input:=@Input;
  542. end;
  543. Function fpc_get_output:PText;compilerproc;
  544. begin
  545. fpc_get_output:=@Output;
  546. end;
  547. {*****************************************************************************
  548. Write(Ln)
  549. *****************************************************************************}
  550. Procedure fpc_WriteBuffer(var f:Text;const b;len:SizeInt);
  551. var
  552. p : pchar;
  553. left,
  554. idx : SizeInt;
  555. begin
  556. p:=pchar(@b);
  557. idx:=0;
  558. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  559. while len>left do
  560. begin
  561. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],left);
  562. dec(len,left);
  563. inc(idx,left);
  564. inc(TextRec(f).BufPos,left);
  565. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  566. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  567. end;
  568. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],len);
  569. inc(TextRec(f).BufPos,len);
  570. end;
  571. Procedure fpc_WriteBlanks(var f:Text;len:longint);
  572. var
  573. left : longint;
  574. begin
  575. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  576. while len>left do
  577. begin
  578. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],left,' ');
  579. dec(len,left);
  580. inc(TextRec(f).BufPos,left);
  581. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  582. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  583. end;
  584. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],len,' ');
  585. inc(TextRec(f).BufPos,len);
  586. end;
  587. Procedure fpc_Write_End(var f:Text); iocheck; compilerproc;
  588. begin
  589. if TextRec(f).FlushFunc<>nil then
  590. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  591. end;
  592. Procedure fpc_Writeln_End(var f:Text); iocheck; compilerproc;
  593. begin
  594. If InOutRes <> 0 then exit;
  595. case TextRec(f).mode of
  596. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  597. begin
  598. { Write EOL }
  599. fpc_WriteBuffer(f,TextRec(f).LineEnd[1],length(TextRec(f).LineEnd));
  600. { Flush }
  601. if TextRec(f).FlushFunc<>nil then
  602. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  603. end;
  604. fmInput: InOutRes:=105
  605. else InOutRes:=103;
  606. end;
  607. end;
  608. Procedure fpc_Write_Text_ShortStr(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR']; compilerproc;
  609. Begin
  610. If (InOutRes<>0) then
  611. exit;
  612. case TextRec(f).mode of
  613. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  614. begin
  615. If Len>Length(s) Then
  616. fpc_WriteBlanks(f,Len-Length(s));
  617. fpc_WriteBuffer(f,s[1],Length(s));
  618. end;
  619. fmInput: InOutRes:=105
  620. else InOutRes:=103;
  621. end;
  622. End;
  623. Procedure fpc_Write_Text_ShortStr_Iso(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR_ISO']; compilerproc;
  624. Begin
  625. If (InOutRes<>0) then
  626. exit;
  627. case TextRec(f).mode of
  628. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  629. begin
  630. { default value? }
  631. If Len=-1 then
  632. Len:=length(s);
  633. If Len>Length(s) Then
  634. begin
  635. fpc_WriteBlanks(f,Len-Length(s));
  636. fpc_WriteBuffer(f,s[1],Length(s));
  637. end
  638. else
  639. fpc_WriteBuffer(f,s[1],Len);
  640. end;
  641. fmInput: InOutRes:=105
  642. else InOutRes:=103;
  643. end;
  644. End;
  645. { provide local access to write_str }
  646. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  647. { provide local access to write_str_iso }
  648. procedure Write_Str_Iso(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR_ISO'];
  649. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  650. var
  651. ArrayLen : longint;
  652. p : pchar;
  653. Begin
  654. If (InOutRes<>0) then
  655. exit;
  656. case TextRec(f).mode of
  657. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  658. begin
  659. p:=pchar(@s);
  660. if zerobased then
  661. begin
  662. { can't use StrLen, since that one could try to read past the end }
  663. { of the heap (JM) }
  664. ArrayLen:=IndexByte(p^,high(s)+1,0);
  665. { IndexByte returns -1 if not found (JM) }
  666. if ArrayLen = -1 then
  667. ArrayLen := high(s)+1;
  668. end
  669. else
  670. ArrayLen := high(s)+1;
  671. If Len>ArrayLen Then
  672. fpc_WriteBlanks(f,Len-ArrayLen);
  673. fpc_WriteBuffer(f,p^,ArrayLen);
  674. end;
  675. fmInput: InOutRes:=105
  676. else InOutRes:=103;
  677. end;
  678. End;
  679. Procedure fpc_Write_Text_Pchar_as_Array_Iso(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  680. var
  681. ArrayLen : longint;
  682. p : pchar;
  683. Begin
  684. If (InOutRes<>0) then
  685. exit;
  686. case TextRec(f).mode of
  687. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  688. begin
  689. p:=pchar(@s);
  690. if zerobased then
  691. begin
  692. { can't use StrLen, since that one could try to read past the end }
  693. { of the heap (JM) }
  694. ArrayLen:=IndexByte(p^,high(s)+1,0);
  695. { IndexByte returns -1 if not found (JM) }
  696. if ArrayLen = -1 then
  697. ArrayLen := high(s)+1;
  698. end
  699. else
  700. ArrayLen := high(s)+1;
  701. { default value? }
  702. If Len=-1 then
  703. Len:=ArrayLen;
  704. If Len>ArrayLen Then
  705. begin
  706. fpc_WriteBlanks(f,Len-ArrayLen);
  707. fpc_WriteBuffer(f,p^,ArrayLen);
  708. end
  709. else
  710. fpc_WriteBuffer(f,p^,Len);
  711. end;
  712. fmInput: InOutRes:=105
  713. else InOutRes:=103;
  714. end;
  715. End;
  716. Procedure fpc_Write_Text_PChar_As_Pointer(Len : Longint;var f : Text;p : PChar); iocheck; compilerproc;
  717. var
  718. PCharLen : longint;
  719. Begin
  720. If (p=nil) or (InOutRes<>0) then
  721. exit;
  722. case TextRec(f).mode of
  723. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  724. begin
  725. PCharLen:=StrLen(p);
  726. If Len>PCharLen Then
  727. fpc_WriteBlanks(f,Len-PCharLen);
  728. fpc_WriteBuffer(f,p^,PCharLen);
  729. end;
  730. fmInput: InOutRes:=105
  731. else InOutRes:=103;
  732. end;
  733. End;
  734. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; const S : RawByteString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; compilerproc;
  735. {
  736. Writes a AnsiString to the Text file T
  737. }
  738. var
  739. SLen: longint;
  740. a: RawByteString;
  741. begin
  742. If (InOutRes<>0) then
  743. exit;
  744. case TextRec(f).mode of
  745. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  746. begin
  747. SLen:=Length(s);
  748. If Len>SLen Then
  749. fpc_WriteBlanks(f,Len-SLen);
  750. if SLen > 0 then
  751. begin
  752. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  753. if TextRec(f).CodePage<>TranslatePlaceholderCP(StringCodePage(S)) then
  754. begin
  755. a:=fpc_AnsiStr_To_AnsiStr(S,TextRec(f).CodePage);
  756. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  757. end
  758. else
  759. {$endif}
  760. fpc_WriteBuffer(f,PAnsiChar(s)^,SLen);
  761. end;
  762. end;
  763. fmInput: InOutRes:=105
  764. else InOutRes:=103;
  765. end;
  766. end;
  767. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  768. Procedure fpc_Write_Text_UnicodeStr (Len : Longint; Var f : Text; const S : UnicodeString); iocheck; compilerproc;
  769. {
  770. Writes a UnicodeString to the Text file T
  771. }
  772. var
  773. SLen: longint;
  774. a: RawByteString;
  775. begin
  776. If (pointer(S)=nil) or (InOutRes<>0) then
  777. exit;
  778. case TextRec(f).mode of
  779. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  780. begin
  781. SLen:=Length(s);
  782. If Len>SLen Then
  783. fpc_WriteBlanks(f,Len-SLen);
  784. {$ifdef FPC_HAS_CPSTRING}
  785. WideStringManager.Unicode2AnsiMoveProc(PUnicodeChar(S),a,TextRec(f).CodePage,SLen);
  786. {$else}
  787. a:=s;
  788. {$endif FPC_HAS_CPSTRING}
  789. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  790. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  791. end;
  792. fmInput: InOutRes:=105
  793. else InOutRes:=103;
  794. end;
  795. end;
  796. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  797. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  798. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  799. {
  800. Writes a WideString to the Text file T
  801. }
  802. var
  803. SLen: longint;
  804. a: RawByteString;
  805. begin
  806. If (pointer(S)=nil) or (InOutRes<>0) then
  807. exit;
  808. case TextRec(f).mode of
  809. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  810. begin
  811. SLen:=Length(s);
  812. If Len>SLen Then
  813. fpc_WriteBlanks(f,Len-SLen);
  814. {$ifdef FPC_HAS_CPSTRING}
  815. widestringmanager.Wide2AnsiMoveProc(PWideChar(s), a, TextRec(f).CodePage, SLen);
  816. {$else}
  817. a:=s;
  818. {$endif}
  819. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  820. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  821. end;
  822. fmInput: InOutRes:=105
  823. else InOutRes:=103;
  824. end;
  825. end;
  826. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  827. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  828. var
  829. s : String;
  830. Begin
  831. If (InOutRes<>0) then
  832. exit;
  833. Str(l,s);
  834. Write_Str(Len,t,s);
  835. End;
  836. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  837. var
  838. s : String;
  839. Begin
  840. If (InOutRes<>0) then
  841. exit;
  842. Str(L,s);
  843. Write_Str(Len,t,s);
  844. End;
  845. Procedure fpc_Write_Text_SInt_Iso(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  846. var
  847. s : String;
  848. Begin
  849. If (InOutRes<>0) then
  850. exit;
  851. Str(l,s);
  852. { default value? }
  853. if len=-1 then
  854. len:=11
  855. else if len<length(s) then
  856. len:=length(s);
  857. Write_Str_Iso(Len,t,s);
  858. End;
  859. Procedure fpc_Write_Text_UInt_Iso(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  860. var
  861. s : String;
  862. Begin
  863. If (InOutRes<>0) then
  864. exit;
  865. Str(L,s);
  866. { default value? }
  867. if len=-1 then
  868. len:=11
  869. else if len<length(s) then
  870. len:=length(s);
  871. Write_Str_Iso(Len,t,s);
  872. End;
  873. {$ifndef CPU64}
  874. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; compilerproc;
  875. var
  876. s : string;
  877. begin
  878. if (InOutRes<>0) then
  879. exit;
  880. str(q,s);
  881. write_str(len,t,s);
  882. end;
  883. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; compilerproc;
  884. var
  885. s : string;
  886. begin
  887. if (InOutRes<>0) then
  888. exit;
  889. str(i,s);
  890. write_str(len,t,s);
  891. end;
  892. procedure fpc_write_text_qword_iso(len : longint;var t : text;q : qword); iocheck; compilerproc;
  893. var
  894. s : string;
  895. begin
  896. if (InOutRes<>0) then
  897. exit;
  898. str(q,s);
  899. { default value? }
  900. if len=-1 then
  901. len:=20
  902. else if len<length(s) then
  903. len:=length(s);
  904. write_str_iso(len,t,s);
  905. end;
  906. procedure fpc_write_text_int64_iso(len : longint;var t : text;i : int64); iocheck; compilerproc;
  907. var
  908. s : string;
  909. begin
  910. if (InOutRes<>0) then
  911. exit;
  912. str(i,s);
  913. { default value? }
  914. if len=-1 then
  915. len:=20
  916. else if len<length(s) then
  917. len:=length(s);
  918. write_str_iso(len,t,s);
  919. end;
  920. {$endif CPU64}
  921. {$if defined(CPU16) or defined(CPU8)}
  922. procedure fpc_write_text_longword(len : longint;var t : text;q : longword); iocheck; compilerproc;
  923. var
  924. s : string;
  925. begin
  926. if (InOutRes<>0) then
  927. exit;
  928. str(q,s);
  929. write_str(len,t,s);
  930. end;
  931. procedure fpc_write_text_longint(len : longint;var t : text;i : longint); iocheck; compilerproc;
  932. var
  933. s : string;
  934. begin
  935. if (InOutRes<>0) then
  936. exit;
  937. str(i,s);
  938. write_str(len,t,s);
  939. end;
  940. procedure fpc_write_text_longword_iso(len : longint;var t : text;q : longword); iocheck; compilerproc;
  941. var
  942. s : string;
  943. begin
  944. if (InOutRes<>0) then
  945. exit;
  946. str(q,s);
  947. { default value? }
  948. if len=-1 then
  949. len:=11
  950. else if len<length(s) then
  951. len:=length(s);
  952. write_str_iso(len,t,s);
  953. end;
  954. procedure fpc_write_text_longint_iso(len : longint;var t : text;i : longint); iocheck; compilerproc;
  955. var
  956. s : string;
  957. begin
  958. if (InOutRes<>0) then
  959. exit;
  960. str(i,s);
  961. { default value? }
  962. if len=-1 then
  963. len:=11
  964. else if len<length(s) then
  965. len:=length(s);
  966. write_str_iso(len,t,s);
  967. end;
  968. {$endif CPU16 or CPU8}
  969. {$ifndef FPUNONE}
  970. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  971. var
  972. s : String;
  973. Begin
  974. If (InOutRes<>0) then
  975. exit;
  976. Str_real(Len,fixkomma,r,treal_type(rt),s);
  977. Write_Str(Len,t,s);
  978. End;
  979. Procedure fpc_Write_Text_Float_iso(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  980. var
  981. s : String;
  982. Begin
  983. If (InOutRes<>0) then
  984. exit;
  985. Str_real_iso(Len,fixkomma,r,treal_type(rt),s);
  986. Write_Str(Len,t,s);
  987. End;
  988. {$endif}
  989. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; compilerproc;
  990. var
  991. s:string;
  992. begin
  993. if textrec(t).mode<>fmoutput then
  994. begin
  995. if textrec(t).mode=fminput then
  996. inoutres:=105
  997. else
  998. inoutres:=103;
  999. exit;
  1000. end;
  1001. inoutres := fpc_shortstr_enum_intern(ordinal, len, typinfo, ord2strindex, s);
  1002. if (inoutres <> 0) then
  1003. exit;
  1004. fpc_writeBuffer(t,s[1],length(s));
  1005. end;
  1006. {$ifdef FPC_HAS_STR_CURRENCY}
  1007. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; compilerproc;
  1008. var
  1009. s : String;
  1010. Begin
  1011. If (InOutRes<>0) then
  1012. exit;
  1013. str(c:Len:fixkomma,s);
  1014. Write_Str(Len,t,s);
  1015. End;
  1016. {$endif FPC_HAS_STR_CURRENCY}
  1017. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  1018. Begin
  1019. If (InOutRes<>0) then
  1020. exit;
  1021. { Can't use array[boolean] because b can be >0 ! }
  1022. if b then
  1023. Write_Str(Len,t,'TRUE')
  1024. else
  1025. Write_Str(Len,t,'FALSE');
  1026. End;
  1027. Procedure fpc_Write_Text_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  1028. Begin
  1029. If (InOutRes<>0) then
  1030. exit;
  1031. { Can't use array[boolean] because b can be >0 ! }
  1032. { default value? }
  1033. If Len=-1 then
  1034. Len:=5;
  1035. if b then
  1036. Write_Str_Iso(Len,t,'true')
  1037. else
  1038. Write_Str_Iso(Len,t,'false');
  1039. End;
  1040. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  1041. Begin
  1042. If (InOutRes<>0) then
  1043. exit;
  1044. if (TextRec(t).mode<>fmOutput) Then
  1045. begin
  1046. if TextRec(t).mode=fmClosed then
  1047. InOutRes:=103
  1048. else
  1049. InOutRes:=105;
  1050. exit;
  1051. end;
  1052. If Len>1 Then
  1053. fpc_WriteBlanks(t,Len-1);
  1054. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  1055. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  1056. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  1057. Inc(TextRec(t).BufPos);
  1058. End;
  1059. Procedure fpc_Write_Text_Char_Iso(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  1060. Begin
  1061. If (InOutRes<>0) then
  1062. exit;
  1063. if (TextRec(t).mode<>fmOutput) Then
  1064. begin
  1065. if TextRec(t).mode=fmClosed then
  1066. InOutRes:=103
  1067. else
  1068. InOutRes:=105;
  1069. exit;
  1070. end;
  1071. { default value? }
  1072. If Len=-1 then
  1073. Len:=1;
  1074. If Len>1 Then
  1075. fpc_WriteBlanks(t,Len-1)
  1076. else If Len<1 Then
  1077. exit;
  1078. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  1079. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  1080. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  1081. Inc(TextRec(t).BufPos);
  1082. End;
  1083. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1084. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  1085. var
  1086. a: RawByteString;
  1087. Begin
  1088. If (InOutRes<>0) then
  1089. exit;
  1090. if (TextRec(t).mode<>fmOutput) Then
  1091. begin
  1092. if TextRec(t).mode=fmClosed then
  1093. InOutRes:=103
  1094. else
  1095. InOutRes:=105;
  1096. exit;
  1097. end;
  1098. If Len>1 Then
  1099. fpc_WriteBlanks(t,Len-1);
  1100. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  1101. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  1102. { a widechar can be translated into more than a single ansichar }
  1103. {$ifdef FPC_HAS_CPSTRING}
  1104. widestringmanager.Wide2AnsiMoveProc(@c,a,TextRec(t).CodePage,1);
  1105. {$else}
  1106. a:=c;
  1107. {$endif}
  1108. fpc_WriteBuffer(t,PAnsiChar(a)^,Length(a));
  1109. End;
  1110. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1111. {*****************************************************************************
  1112. Read(Ln)
  1113. *****************************************************************************}
  1114. Function NextChar(var f:Text;var s:string):Boolean;
  1115. begin
  1116. NextChar:=false;
  1117. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  1118. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  1119. begin
  1120. if length(s)<high(s) then
  1121. begin
  1122. inc(s[0]);
  1123. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  1124. end;
  1125. Inc(TextRec(f).BufPos);
  1126. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1127. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1128. NextChar:=true;
  1129. end;
  1130. end;
  1131. Function IgnoreSpaces(var f:Text):Boolean;
  1132. {
  1133. Removes all leading spaces,tab,eols from the input buffer, returns true if
  1134. the buffer is empty
  1135. }
  1136. var
  1137. s : string;
  1138. begin
  1139. s:='';
  1140. IgnoreSpaces:=false;
  1141. { Return false when already at EOF }
  1142. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1143. exit;
  1144. (* Check performed separately to avoid accessing memory outside buffer *)
  1145. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1146. exit;
  1147. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  1148. begin
  1149. if not NextChar(f,s) then
  1150. exit;
  1151. { EOF? }
  1152. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1153. break;
  1154. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1155. break;
  1156. end;
  1157. IgnoreSpaces:=true;
  1158. end;
  1159. procedure ReadNumeric(var f:Text;var s:string);
  1160. {
  1161. Read numeric input, if buffer is empty then return True
  1162. }
  1163. begin
  1164. repeat
  1165. if not NextChar(f,s) then
  1166. exit;
  1167. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  1168. end;
  1169. function CheckRead(var f:Text):Boolean;
  1170. begin
  1171. CheckRead:=False;
  1172. { Check error and if file is open and load buf if empty }
  1173. If (InOutRes<>0) then
  1174. exit;
  1175. if (TextRec(f).mode<>fmInput) Then
  1176. begin
  1177. case TextRec(f).mode of
  1178. fmOutPut,fmAppend:
  1179. InOutRes:=104;
  1180. else
  1181. InOutRes:=103;
  1182. end;
  1183. exit;
  1184. end;
  1185. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1186. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1187. CheckRead:=True;
  1188. end;
  1189. procedure ReadInteger(var f:Text;var s:string);
  1190. {
  1191. Ignore leading blanks (incl. EOF) and return the first characters matching
  1192. an integer in the format recognized by the Val procedure:
  1193. [+-]?[0-9]+
  1194. or [+-]?(0x|0X|x|X)[0-9A-Za-z]+
  1195. or [+-]?&[0-7]+
  1196. or [+-]?%[0-1]+
  1197. A partial match may be returned, e.g.: '' or '+' or '0x'.
  1198. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1199. standard function in ISO mode.
  1200. }
  1201. var
  1202. Base: Integer;
  1203. begin
  1204. s := '';
  1205. with TextRec(f) do begin
  1206. if not CheckRead(f) then Exit;
  1207. IgnoreSpaces(f);
  1208. if BufPos >= BufEnd then Exit;
  1209. if BufPtr^[BufPos] in ['+','-'] then
  1210. NextChar(f,s);
  1211. Base := 10;
  1212. if BufPos >= BufEnd then Exit;
  1213. if BufPtr^[BufPos] in ['$','x','X','%','&'] then
  1214. begin
  1215. case BufPtr^[BufPos] of
  1216. '$','x','X': Base := 16;
  1217. '%': Base := 2;
  1218. '&': Base := 8;
  1219. end;
  1220. NextChar(f,s);
  1221. end else if BufPtr^[BufPos] = '0' then
  1222. begin
  1223. NextChar(f,s);
  1224. if BufPos >= BufEnd then Exit;
  1225. if BufPtr^[BufPos] in ['x','X'] then
  1226. begin
  1227. Base := 16;
  1228. NextChar(f,s);
  1229. end;
  1230. end;
  1231. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1232. if (((Base = 2) and (BufPtr^[BufPos] in ['0'..'1']))
  1233. or ((Base = 8) and (BufPtr^[BufPos] in ['0'..'7']))
  1234. or ((Base = 10) and (BufPtr^[BufPos] in ['0'..'9']))
  1235. or ((Base = 16) and (BufPtr^[BufPos] in ['0'..'9','a'..'f','A'..'F']))) then
  1236. NextChar(f,s)
  1237. else Exit;
  1238. end;
  1239. end;
  1240. procedure ReadReal(var f:Text;var s:string);
  1241. {
  1242. Ignore leading blanks (incl. EOF) and return the first characters matching
  1243. a float number in the format recognized by the Val procedure:
  1244. [+-]?([0-9]+)?\.[0-9]+([eE][+-]?[0-9]+)?
  1245. or [+-]?[0-9]+\.([0-9]+)?([eE][+-]?[0-9]+)?
  1246. A partial match may be returned, e.g.: '' or '+' or '.' or '1e' or even '+.'.
  1247. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1248. standard function in ISO mode.
  1249. }
  1250. var digit: Boolean;
  1251. begin
  1252. s := '';
  1253. with TextRec(f) do begin
  1254. if not CheckRead(f) then Exit;
  1255. IgnoreSpaces(f);
  1256. if BufPos >= BufEnd then Exit;
  1257. if BufPtr^[BufPos] in ['+','-'] then
  1258. NextChar(f,s);
  1259. digit := false;
  1260. if BufPos >= BufEnd then Exit;
  1261. if BufPtr^[BufPos] in ['0'..'9'] then
  1262. begin
  1263. digit := true;
  1264. repeat
  1265. NextChar(f,s);
  1266. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1267. until not (BufPtr^[BufPos] in ['0'..'9']);
  1268. end;
  1269. if BufPtr^[BufPos] = '.' then
  1270. begin
  1271. NextChar(f,s);
  1272. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1273. if BufPtr^[BufPos] in ['0'..'9'] then
  1274. begin
  1275. digit := true;
  1276. repeat
  1277. NextChar(f,s);
  1278. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1279. until not (BufPtr^[BufPos] in ['0'..'9']);
  1280. end;
  1281. end;
  1282. {at least one digit is required on the left of the exponent}
  1283. if digit and (BufPtr^[BufPos] in ['e','E']) then
  1284. begin
  1285. NextChar(f,s);
  1286. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1287. if BufPtr^[BufPos] in ['+','-'] then
  1288. NextChar(f,s);
  1289. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1290. if BufPtr^[BufPos] in ['0'..'9'] then
  1291. NextChar(f,s)
  1292. else break;
  1293. end;
  1294. end;
  1295. end;
  1296. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  1297. begin
  1298. if TextRec(f).FlushFunc<>nil then
  1299. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1300. end;
  1301. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  1302. var prev: char;
  1303. Begin
  1304. If not CheckRead(f) then
  1305. exit;
  1306. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1307. { Flush if set }
  1308. begin
  1309. if (TextRec(f).FlushFunc<>nil) then
  1310. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1311. exit;
  1312. end;
  1313. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1314. Exit;
  1315. repeat
  1316. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1317. inc(TextRec(f).BufPos);
  1318. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1319. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1320. if prev = #10 then
  1321. exit;
  1322. {$ifdef MACOS}
  1323. if prev = #13 then
  1324. {StdInput on macos never have dos line ending, so this is safe.}
  1325. if TextRec(f).Handle = StdInputHandle then
  1326. exit;
  1327. {$endif MACOS}
  1328. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1329. begin
  1330. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1331. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1332. { Flush if set }
  1333. begin
  1334. if (TextRec(f).FlushFunc<>nil) then
  1335. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1336. exit;
  1337. end;
  1338. end;
  1339. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1340. Exit;
  1341. if (prev=#13) then
  1342. { is there also a #10 after it? }
  1343. begin
  1344. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1345. { yes, skip that one as well }
  1346. inc(TextRec(f).BufPos);
  1347. exit;
  1348. end;
  1349. until false;
  1350. End;
  1351. Procedure fpc_ReadLn_End_Iso(var f : Text);[Public,Alias:'FPC_READLN_END_ISO']; iocheck; compilerproc;
  1352. var prev: char;
  1353. Begin
  1354. If not CheckRead(f) then
  1355. exit;
  1356. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1357. { Flush if set }
  1358. begin
  1359. if (TextRec(f).FlushFunc<>nil) then
  1360. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1361. exit;
  1362. end;
  1363. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1364. begin
  1365. inc(TextRec(f).BufPos);
  1366. Exit;
  1367. end;
  1368. repeat
  1369. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1370. inc(TextRec(f).BufPos);
  1371. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1372. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1373. if prev = #10 then
  1374. exit;
  1375. {$ifdef MACOS}
  1376. if prev = #13 then
  1377. {StdInput on macos never have dos line ending, so this is safe.}
  1378. if TextRec(f).Handle = StdInputHandle then
  1379. exit;
  1380. {$endif MACOS}
  1381. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1382. begin
  1383. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1384. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1385. { Flush if set }
  1386. begin
  1387. if (TextRec(f).FlushFunc<>nil) then
  1388. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1389. exit;
  1390. end;
  1391. end;
  1392. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1393. begin
  1394. inc(TextRec(f).BufPos);
  1395. Exit;
  1396. end;
  1397. if (prev=#13) then
  1398. { is there also a #10 after it? }
  1399. begin
  1400. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1401. { yes, skip that one as well }
  1402. inc(TextRec(f).BufPos);
  1403. exit;
  1404. end;
  1405. until false;
  1406. End;
  1407. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  1408. var
  1409. sPos,len : Longint;
  1410. p,startp,maxp : pchar;
  1411. end_of_string:boolean;
  1412. Begin
  1413. ReadPCharLen:=0;
  1414. If not CheckRead(f) then
  1415. exit;
  1416. { Read maximal until Maxlen is reached }
  1417. sPos:=0;
  1418. end_of_string:=false;
  1419. repeat
  1420. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1421. begin
  1422. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1423. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1424. break;
  1425. end;
  1426. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1427. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  1428. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  1429. else
  1430. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1431. startp:=p;
  1432. { find stop character }
  1433. while p<maxp do
  1434. begin
  1435. { Optimization: Do a quick check for a control character first }
  1436. if (p^<' ') then
  1437. begin
  1438. if (p^ in [#10,#13]) or
  1439. (ctrlZmarkseof and (p^=#26)) then
  1440. begin
  1441. end_of_string:=true;
  1442. break;
  1443. end;
  1444. end;
  1445. inc(p);
  1446. end;
  1447. { calculate read bytes }
  1448. len:=p-startp;
  1449. inc(TextRec(f).BufPos,Len);
  1450. Move(startp^,s[sPos],Len);
  1451. inc(sPos,Len);
  1452. until (spos=MaxLen) or end_of_string;
  1453. ReadPCharLen:=spos;
  1454. End;
  1455. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  1456. Begin
  1457. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  1458. End;
  1459. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  1460. Begin
  1461. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  1462. End;
  1463. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  1464. var
  1465. len: longint;
  1466. Begin
  1467. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  1468. if zerobased and
  1469. (len > high(s)) then
  1470. len := high(s);
  1471. if (len <= high(s)) then
  1472. s[len] := #0;
  1473. End;
  1474. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1475. 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;
  1476. var
  1477. slen,len : SizeInt;
  1478. Begin
  1479. slen:=0;
  1480. Repeat
  1481. // SetLength will reallocate the length.
  1482. SetLength(s,slen+255);
  1483. len:=ReadPCharLen(f,pchar(Pointer(s)+slen),255);
  1484. inc(slen,len);
  1485. Until len<255;
  1486. // Set actual length
  1487. SetLength(s,Slen);
  1488. {$ifdef FPC_HAS_CPSTRING}
  1489. SetCodePage(s,TextRec(f).CodePage,false);
  1490. if cp<>TextRec(f).CodePage then
  1491. s:=fpc_AnsiStr_To_AnsiStr(s,cp);
  1492. {$endif FPC_HAS_CPSTRING}
  1493. End;
  1494. 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'];
  1495. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1496. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1497. Procedure fpc_Read_Text_UnicodeStr(var f : Text;out us : UnicodeString); iocheck; compilerproc;
  1498. var
  1499. s: RawByteString;
  1500. Begin
  1501. // all standard input is assumed to be ansi-encoded
  1502. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1503. // Convert to unicodestring
  1504. {$ifdef FPC_HAS_CPSTRING}
  1505. widestringmanager.Ansi2UnicodeMoveProc(PAnsiChar(s),StringCodePage(s),us,Length(s));
  1506. {$else}
  1507. us:=s;
  1508. {$endif}
  1509. End;
  1510. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1511. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1512. Procedure fpc_Read_Text_WideStr(var f : Text;out ws : WideString); iocheck; compilerproc;
  1513. var
  1514. s: RawByteString;
  1515. Begin
  1516. // all standard input is assumed to be ansi-encoded
  1517. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1518. // Convert to widestring
  1519. {$ifdef FPC_HAS_CPSTRING}
  1520. widestringmanager.Ansi2WideMoveProc(PAnsiChar(s),StringCodePage(s),ws,Length(s));
  1521. {$else}
  1522. ws:=s;
  1523. {$endif}
  1524. End;
  1525. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1526. procedure fpc_Read_Text_Char(var f : Text; out c: char); [public, alias: 'FPC_READ_TEXT_CHAR']; iocheck; compilerproc;
  1527. Begin
  1528. c:=#0;
  1529. If not CheckRead(f) then
  1530. exit;
  1531. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1532. begin
  1533. c := #26;
  1534. exit;
  1535. end;
  1536. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1537. inc(TextRec(f).BufPos);
  1538. end;
  1539. procedure fpc_Read_Text_Char_intern(var f : Text; out c: char); iocheck; [external name 'FPC_READ_TEXT_CHAR'];
  1540. function fpc_GetBuf(var f : Text) : pchar; iocheck; compilerproc;
  1541. Begin
  1542. Result:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1543. If not CheckRead(f) then
  1544. exit;
  1545. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1546. exit;
  1547. Result:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1548. end;
  1549. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1550. procedure fpc_Read_Text_WideChar(var f : Text; out wc: widechar); iocheck;compilerproc;
  1551. var
  1552. ws: widestring;
  1553. i: longint;
  1554. { maximum code point length is 6 characters (with UTF-8) }
  1555. str: array[0..5] of char;
  1556. Begin
  1557. fillchar(str[0],sizeof(str),0);
  1558. for i:=low(str) to high(str) do
  1559. begin
  1560. fpc_Read_Text_Char_intern(f,str[i]);
  1561. case widestringmanager.CodePointLengthProc(@str[0],i+1) of
  1562. -1: { possibly incomplete code point, try with an extra character }
  1563. ;
  1564. 0: { null character }
  1565. begin
  1566. wc:=#0;
  1567. exit;
  1568. end;
  1569. else
  1570. begin
  1571. { valid code point -> convert to widestring}
  1572. {$ifdef FPC_HAS_CPSTRING}
  1573. widestringmanager.Ansi2WideMoveProc(@str[0],TextRec(f).CodePage,ws,i+1);
  1574. {$else}
  1575. widestringmanager.Ansi2WideMoveProc(@str[0],DefaultSystemCodePage,ws,i+1);
  1576. {$endif}
  1577. { has to be exactly one widechar }
  1578. if length(ws)=1 then
  1579. begin
  1580. wc:=ws[1];
  1581. exit
  1582. end
  1583. else
  1584. break;
  1585. end;
  1586. end;
  1587. end;
  1588. { invalid widechar input }
  1589. inoutres:=106;
  1590. end;
  1591. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1592. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: char); iocheck;compilerproc;
  1593. Begin
  1594. c:=' ';
  1595. If not CheckRead(f) then
  1596. exit;
  1597. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1598. begin
  1599. c:=' ';
  1600. exit;
  1601. end;
  1602. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1603. inc(TextRec(f).BufPos);
  1604. if c=#13 then
  1605. begin
  1606. c:=' ';
  1607. If not CheckRead(f) or
  1608. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1609. exit;
  1610. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1611. inc(TextRec(f).BufPos);
  1612. { ignore #26 following a new line }
  1613. If not CheckRead(f) or
  1614. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1615. exit;
  1616. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1617. inc(TextRec(f).BufPos);
  1618. end
  1619. else if c=#10 then
  1620. begin
  1621. c:=' ';
  1622. { ignore #26 following a new line }
  1623. If not CheckRead(f) or
  1624. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1625. exit;
  1626. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1627. inc(TextRec(f).BufPos);
  1628. end
  1629. else if c=#26 then
  1630. c:=' ';
  1631. end;
  1632. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1633. var
  1634. hs : String;
  1635. code : ValSInt;
  1636. Begin
  1637. l:=0;
  1638. If not CheckRead(f) then
  1639. exit;
  1640. hs:='';
  1641. if IgnoreSpaces(f) then
  1642. begin
  1643. { When spaces were found and we are now at EOF,
  1644. then we return 0 }
  1645. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1646. exit;
  1647. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1648. exit;
  1649. ReadNumeric(f,hs);
  1650. end;
  1651. if (hs = '') then
  1652. L := 0
  1653. else
  1654. begin
  1655. Val(hs,l,code);
  1656. if Code <> 0 then
  1657. InOutRes:=106;
  1658. end;
  1659. End;
  1660. Procedure fpc_Read_Text_SInt_Iso(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1661. var
  1662. hs : String;
  1663. code : ValSInt;
  1664. Begin
  1665. ReadInteger(f,hs);
  1666. Val(hs,l,code);
  1667. if Code <> 0 then
  1668. InOutRes:=106;
  1669. End;
  1670. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1671. var
  1672. hs : String;
  1673. code : ValSInt;
  1674. Begin
  1675. u:=0;
  1676. If not CheckRead(f) then
  1677. exit;
  1678. hs:='';
  1679. if IgnoreSpaces(f) then
  1680. begin
  1681. { When spaces were found and we are now at EOF,
  1682. then we return 0 }
  1683. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1684. exit;
  1685. ReadNumeric(f,hs);
  1686. end;
  1687. if (hs = '') then
  1688. u := 0
  1689. else
  1690. begin
  1691. val(hs,u,code);
  1692. If code<>0 Then
  1693. InOutRes:=106;
  1694. end;
  1695. End;
  1696. Procedure fpc_Read_Text_UInt_Iso(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1697. var
  1698. hs : String;
  1699. code : ValSInt;
  1700. Begin
  1701. ReadInteger(f,hs);
  1702. Val(hs,u,code);
  1703. If code<>0 Then
  1704. InOutRes:=106;
  1705. End;
  1706. {$ifndef FPUNONE}
  1707. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1708. var
  1709. hs : string;
  1710. code : Word;
  1711. begin
  1712. v:=0.0;
  1713. If not CheckRead(f) then
  1714. exit;
  1715. hs:='';
  1716. if IgnoreSpaces(f) then
  1717. begin
  1718. { When spaces were found and we are now at EOF,
  1719. then we return 0 }
  1720. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1721. exit;
  1722. ReadNumeric(f,hs);
  1723. end;
  1724. val(hs,v,code);
  1725. If code<>0 Then
  1726. InOutRes:=106;
  1727. end;
  1728. procedure fpc_Read_Text_Float_Iso(var f : Text; out v : ValReal); iocheck; compilerproc;
  1729. var
  1730. hs : string;
  1731. code : Word;
  1732. begin
  1733. ReadReal(f,hs);
  1734. Val(hs,v,code);
  1735. If code<>0 Then
  1736. InOutRes:=106;
  1737. end;
  1738. {$endif}
  1739. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1740. var s:string;
  1741. code:valsint;
  1742. begin
  1743. if not checkread(t) then
  1744. exit;
  1745. s:='';
  1746. if ignorespaces(t) then
  1747. begin
  1748. { When spaces were found and we are now at EOF, then we return 0 }
  1749. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1750. exit;
  1751. ReadNumeric(t,s);
  1752. end;
  1753. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1754. if code<>0 then
  1755. InOutRes:=106;
  1756. end;
  1757. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1758. var
  1759. hs : string;
  1760. code : ValSInt;
  1761. begin
  1762. {$ifdef FPUNONE}
  1763. v:=0;
  1764. {$else}
  1765. v:=0.0;
  1766. {$endif}
  1767. If not CheckRead(f) then
  1768. exit;
  1769. hs:='';
  1770. if IgnoreSpaces(f) then
  1771. begin
  1772. { When spaces were found and we are now at EOF,
  1773. then we return 0 }
  1774. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1775. exit;
  1776. ReadNumeric(f,hs);
  1777. end;
  1778. val(hs,v,code);
  1779. If code<>0 Then
  1780. InOutRes:=106;
  1781. end;
  1782. procedure fpc_Read_Text_Currency_Iso(var f : Text; out v : Currency); iocheck; compilerproc;
  1783. var
  1784. hs : string;
  1785. code : ValSInt;
  1786. begin
  1787. ReadReal(f,hs);
  1788. Val(hs,v,code);
  1789. If code<>0 Then
  1790. InOutRes:=106;
  1791. end;
  1792. {$ifndef cpu64}
  1793. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1794. var
  1795. hs : String;
  1796. code : longint;
  1797. Begin
  1798. q:=0;
  1799. If not CheckRead(f) then
  1800. exit;
  1801. hs:='';
  1802. if IgnoreSpaces(f) then
  1803. begin
  1804. { When spaces were found and we are now at EOF,
  1805. then we return 0 }
  1806. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1807. exit;
  1808. ReadNumeric(f,hs);
  1809. end;
  1810. val(hs,q,code);
  1811. If code<>0 Then
  1812. InOutRes:=106;
  1813. End;
  1814. procedure fpc_Read_Text_QWord_Iso(var f : text; out q : qword); iocheck; compilerproc;
  1815. var
  1816. hs : String;
  1817. code : longint;
  1818. Begin
  1819. ReadInteger(f,hs);
  1820. Val(hs,q,code);
  1821. If code<>0 Then
  1822. InOutRes:=106;
  1823. End;
  1824. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1825. var
  1826. hs : String;
  1827. code : Longint;
  1828. Begin
  1829. i:=0;
  1830. If not CheckRead(f) then
  1831. exit;
  1832. hs:='';
  1833. if IgnoreSpaces(f) then
  1834. begin
  1835. { When spaces were found and we are now at EOF,
  1836. then we return 0 }
  1837. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1838. exit;
  1839. ReadNumeric(f,hs);
  1840. end;
  1841. Val(hs,i,code);
  1842. If code<>0 Then
  1843. InOutRes:=106;
  1844. End;
  1845. procedure fpc_Read_Text_Int64_Iso(var f : text; out i : int64); iocheck; compilerproc;
  1846. var
  1847. hs : String;
  1848. code : Longint;
  1849. Begin
  1850. ReadInteger(f,hs);
  1851. Val(hs,i,code);
  1852. If code<>0 Then
  1853. InOutRes:=106;
  1854. End;
  1855. {$endif CPU64}
  1856. {$if defined(CPU16) or defined(CPU8)}
  1857. procedure fpc_Read_Text_LongWord(var f : text; out q : longword); iocheck; compilerproc;
  1858. var
  1859. hs : String;
  1860. code : longint;
  1861. Begin
  1862. q:=0;
  1863. If not CheckRead(f) then
  1864. exit;
  1865. hs:='';
  1866. if IgnoreSpaces(f) then
  1867. begin
  1868. { When spaces were found and we are now at EOF,
  1869. then we return 0 }
  1870. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1871. exit;
  1872. ReadNumeric(f,hs);
  1873. end;
  1874. val(hs,q,code);
  1875. If code<>0 Then
  1876. InOutRes:=106;
  1877. End;
  1878. procedure fpc_Read_Text_LongInt(var f : text; out i : longint); iocheck; compilerproc;
  1879. var
  1880. hs : String;
  1881. code : Longint;
  1882. Begin
  1883. i:=0;
  1884. If not CheckRead(f) then
  1885. exit;
  1886. hs:='';
  1887. if IgnoreSpaces(f) then
  1888. begin
  1889. { When spaces were found and we are now at EOF,
  1890. then we return 0 }
  1891. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1892. exit;
  1893. ReadNumeric(f,hs);
  1894. end;
  1895. Val(hs,i,code);
  1896. If code<>0 Then
  1897. InOutRes:=106;
  1898. End;
  1899. {$endif CPU16 or CPU8}
  1900. {*****************************************************************************
  1901. WriteStr/ReadStr
  1902. *****************************************************************************}
  1903. const
  1904. { pointer to target string }
  1905. StrPtrIndex = 1;
  1906. { temporary destination for writerstr, because the original value of the
  1907. destination may be used in the writestr expression }
  1908. TempWriteStrDestIndex = 9;
  1909. ShortStrLenIndex = 17;
  1910. { how many bytes of the string have been processed already (used for readstr) }
  1911. BytesReadIndex = 17;
  1912. procedure WriteStrShort(var t: textrec);
  1913. var
  1914. str: pshortstring;
  1915. newbytes,
  1916. oldlen: longint;
  1917. begin
  1918. if (t.bufpos=0) then
  1919. exit;
  1920. str:=pshortstring(ppointer(@t.userdata[TempWriteStrDestIndex])^);
  1921. newbytes:=t.BufPos;
  1922. oldlen:=length(str^);
  1923. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  1924. begin
  1925. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  1926. {$ifdef writestr_iolencheck}
  1927. // GPC only gives an io error if {$no-truncate-strings} is active
  1928. // FPC does not have this setting (it never gives errors when a
  1929. // a string expression is truncated)
  1930. { "disk full" }
  1931. inoutres:=101;
  1932. {$endif}
  1933. end;
  1934. setlength(str^,length(str^)+newbytes);
  1935. move(t.bufptr^,str^[oldlen+1],newbytes);
  1936. t.bufpos:=0;
  1937. end;
  1938. procedure WriteStrShortFlush(var t: textrec);
  1939. begin
  1940. { move written data from internal buffer to temporary string (don't move
  1941. directly from buffer to final string, because the temporary string may
  1942. already contain data in case the textbuf was smaller than the string
  1943. length) }
  1944. WriteStrShort(t);
  1945. { move written data to original string }
  1946. move(PPointer(@t.userdata[TempWriteStrDestIndex])^^,
  1947. PPointer(@t.userdata[StrPtrIndex])^^,
  1948. t.userdata[ShortStrLenIndex]+1);
  1949. { free temporary buffer }
  1950. freemem(PPointer(@t.userdata[TempWriteStrDestIndex])^);
  1951. end;
  1952. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1953. procedure WriteStrAnsi(var t: textrec);
  1954. var
  1955. str: pansistring;
  1956. oldlen: longint;
  1957. begin
  1958. if (t.bufpos=0) then
  1959. exit;
  1960. str:=pansistring(@t.userdata[TempWriteStrDestIndex]);
  1961. oldlen:=length(str^);
  1962. setlength(str^,oldlen+t.bufpos);
  1963. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  1964. t.bufpos:=0;
  1965. end;
  1966. procedure WriteStrAnsiFlush(var t: textrec);
  1967. begin
  1968. { see comment in WriteStrShortFlush }
  1969. WriteStrAnsi(t);
  1970. pansistring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  1971. pansistring(@t.userdata[TempWriteStrDestIndex])^;
  1972. { free memory/finalize temp }
  1973. pansistring(@t.userdata[TempWriteStrDestIndex])^:='';
  1974. end;
  1975. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1976. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1977. function UTF8CodePointLength(firstbyte: byte): SizeInt;
  1978. var
  1979. firstzerobit: SizeInt;
  1980. begin
  1981. result:=1;
  1982. { bsr searches for the leftmost 1 bit. We are interested in the
  1983. leftmost 0 bit, so first invert the value
  1984. }
  1985. firstzerobit:=BsrByte(not(firstbyte));
  1986. { if there is no zero bit or the first zero bit is the rightmost bit
  1987. (bit 0), this is an invalid UTF-8 byte ($ff cannot appear in an
  1988. UTF-8-encoded string, and in the worst case bit 1 has to be zero)
  1989. }
  1990. if (firstzerobit=0) or (firstzerobit=255) then
  1991. exit;
  1992. { the number of bytes belonging to this code point is
  1993. 7-(pos first 0-bit).
  1994. }
  1995. result:=7-firstzerobit;
  1996. end;
  1997. function EndOfLastCompleteUTF8CodePoint(var t: textrec): SizeInt;
  1998. var
  1999. i, lenfound, codepointlen: SizeInt;
  2000. b: byte;
  2001. begin
  2002. lenfound:=0;
  2003. for i:=t.bufpos-1 downto 0 do
  2004. begin
  2005. b:=byte(t.bufptr^[i]);
  2006. if b<=127 then
  2007. begin
  2008. if lenfound = 0 then
  2009. { valid simple code point }
  2010. result:=i+1
  2011. else
  2012. { valid simple code point followed by a bunch of invalid data ->
  2013. handle everything since it can't become valid by adding more
  2014. bytes }
  2015. result:=t.bufpos;
  2016. exit;
  2017. end;
  2018. { start of a complex character }
  2019. if (b and %11000000)<>0 then
  2020. begin
  2021. codepointlen:=UTF8CodePointLength(b);
  2022. { we did not yet get all bytes of the last code point -> handle
  2023. everything until the start of this character }
  2024. if codepointlen>lenfound+1 then
  2025. if i<>0 then
  2026. result:=i
  2027. { the buffer is too small to contain the entire utf-8 code point
  2028. -> nothing else to do but handle the entire buffer (and end up
  2029. with an invalid character) -- since writestr uses the default
  2030. buffer size of 32 bytes, this can only happen for invalid utf-8
  2031. encodings }
  2032. else
  2033. result:=t.bufpos
  2034. { the last code point is invalid -> handle everything since it can't
  2035. become valid by adding more bytes; in case it's complete, we also
  2036. handle everything, of course}
  2037. else
  2038. result:=t.bufpos;
  2039. exit;
  2040. end;
  2041. inc(lenfound);
  2042. end;
  2043. { all invalid data, or the buffer is too small to be able to deal with the
  2044. complete utf8char -> nothing else to do but to handle the entire buffer }
  2045. result:=t.bufpos;
  2046. end;
  2047. procedure WriteStrUnicodeIntern(var t: textrec; flush: boolean);
  2048. var
  2049. temp: unicodestring;
  2050. str: punicodestring;
  2051. validend: SizeInt;
  2052. begin
  2053. if (t.bufpos=0) then
  2054. exit;
  2055. str:=punicodestring(@t.userdata[TempWriteStrDestIndex]);
  2056. if not flush then
  2057. validend:=EndOfLastCompleteUTF8CodePoint(t)
  2058. else
  2059. validend:=t.bufpos;
  2060. widestringmanager.Ansi2UnicodeMoveProc(@t.bufptr^[0],CP_UTF8,temp,validend);
  2061. str^:=str^+temp;
  2062. dec(t.bufpos,validend);
  2063. { move remainder to the start }
  2064. if t.bufpos<>0 then
  2065. move(t.bufptr^[validend],t.bufptr^[0],t.bufpos);
  2066. end;
  2067. procedure WriteStrUnicode(var t: textrec);
  2068. begin
  2069. WriteStrUnicodeIntern(t,false);
  2070. end;
  2071. procedure WriteStrUnicodeFlush(var t: textrec);
  2072. begin
  2073. { see comment in WriteStrShortFlush }
  2074. WriteStrUnicodeIntern(t,true);
  2075. punicodestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  2076. punicodestring(@t.userdata[TempWriteStrDestIndex])^;
  2077. { free memory/finalize temp }
  2078. punicodestring(@t.userdata[TempWriteStrDestIndex])^:='';
  2079. end;
  2080. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  2081. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2082. procedure WriteStrWideIntern(var t: textrec; flush: boolean);
  2083. var
  2084. temp: unicodestring;
  2085. str: pwidestring;
  2086. validend: SizeInt;
  2087. begin
  2088. if (t.bufpos=0) then
  2089. exit;
  2090. str:=pwidestring(@t.userdata[TempWriteStrDestIndex]);
  2091. if not flush then
  2092. validend:=EndOfLastCompleteUTF8CodePoint(t)
  2093. else
  2094. validend:=t.bufpos;
  2095. widestringmanager.Ansi2UnicodeMoveProc(@t.bufptr^[0],CP_UTF8,temp,validend);
  2096. str^:=str^+temp;
  2097. dec(t.bufpos,validend);
  2098. { move remainder to the start }
  2099. if t.bufpos<>0 then
  2100. move(t.bufptr^[validend],t.bufptr^[0],t.bufpos);
  2101. end;
  2102. procedure WriteStrWide(var t: textrec);
  2103. begin
  2104. WriteStrUnicodeIntern(t,false);
  2105. end;
  2106. procedure WriteStrWideFlush(var t: textrec);
  2107. begin
  2108. { see comment in WriteStrShortFlush }
  2109. WriteStrWideIntern(t,true);
  2110. pwidestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  2111. pwidestring(@t.userdata[TempWriteStrDestIndex])^;
  2112. { free memory/finalize temp }
  2113. finalize(pwidestring(@t.userdata[TempWriteStrDestIndex])^);
  2114. end;
  2115. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2116. procedure SetupWriteStrCommon(out t: textrec; cp: TSystemCodePage);
  2117. begin
  2118. // initialise
  2119. Assign(text(t),'');
  2120. t.mode:=fmOutput;
  2121. t.OpenFunc:=nil;
  2122. t.CloseFunc:=nil;
  2123. {$ifdef FPC_HAS_CPSTRING}
  2124. t.CodePage:=TranslatePlaceholderCP(cp);
  2125. {$endif}
  2126. end;
  2127. procedure fpc_SetupWriteStr_Shortstr(var ReadWriteStrText: text; var s: shortstring); compilerproc;
  2128. begin
  2129. SetupWriteStrCommon(TextRec(ReadWriteStrText),DefaultSystemCodePage);
  2130. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2131. { temporary destination (see comments for TempWriteStrDestIndex) }
  2132. getmem(PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^,high(s)+1);
  2133. setlength(pshortstring(ppointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^)^,0);
  2134. TextRec(ReadWriteStrText).userdata[ShortStrLenIndex]:=high(s);
  2135. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrShort;
  2136. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrShortFlush;
  2137. end;
  2138. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2139. procedure fpc_SetupWriteStr_Ansistr(var ReadWriteStrText: text; var s: ansistring; cp: TSystemCodePage); compilerproc;
  2140. begin
  2141. { destination rawbytestring -> use CP_ACP }
  2142. if cp=CP_NONE then
  2143. cp:=CP_ACP;
  2144. SetupWriteStrCommon(TextRec(ReadWriteStrText),cp);
  2145. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2146. { temp destination ansistring, nil = empty string }
  2147. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  2148. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrAnsi;
  2149. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrAnsiFlush;
  2150. end;
  2151. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2152. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  2153. procedure fpc_SetupWriteStr_Unicodestr(var ReadWriteStrText: text; var s: unicodestring); compilerproc;
  2154. begin
  2155. SetupWriteStrCommon(TextRec(ReadWriteStrText),CP_UTF8);
  2156. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2157. { temp destination unicodestring, nil = empty string }
  2158. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  2159. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrUnicode;
  2160. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrUnicodeFlush;
  2161. end;
  2162. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  2163. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2164. procedure fpc_SetupWriteStr_Widestr(var ReadWriteStrText: text; var s: widestring); compilerproc;
  2165. begin
  2166. SetupWriteStrCommon(TextRec(ReadWriteStrText),CP_UTF8);
  2167. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2168. { temp destination widestring }
  2169. PWideString(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:='';
  2170. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrWide;
  2171. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrWideFlush;
  2172. end;
  2173. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2174. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2175. procedure ReadAnsiStrFinal(var t: textrec);
  2176. begin
  2177. { finalise the temp ansistring }
  2178. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  2179. end;
  2180. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2181. procedure ReadStrCommon(var t: textrec; strdata: pchar; len: sizeint);
  2182. var
  2183. newbytes: sizeint;
  2184. begin
  2185. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  2186. if (t.BufSize <= newbytes) then
  2187. newbytes := t.BufSize;
  2188. if (newbytes > 0) then
  2189. begin
  2190. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  2191. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  2192. end;
  2193. t.BufEnd:=newbytes;
  2194. t.BufPos:=0;
  2195. end;
  2196. procedure ReadStrAnsi(var t: textrec);
  2197. var
  2198. str: pansistring;
  2199. begin
  2200. str:=pansistring(@t.userdata[StrPtrIndex]);
  2201. ReadStrCommon(t,@str^[1],length(str^));
  2202. end;
  2203. procedure SetupReadStrCommon(out t: textrec; cp: TSystemCodePage);
  2204. begin
  2205. // initialise
  2206. Assign(text(t),'');
  2207. t.mode:=fmInput;
  2208. t.OpenFunc:=nil;
  2209. t.CloseFunc:=nil;
  2210. {$ifdef FPC_HAS_CPSTRING}
  2211. t.CodePage:=TranslatePlaceholderCP(cp);
  2212. {$endif}
  2213. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  2214. end;
  2215. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2216. procedure fpc_SetupReadStr_Ansistr(var ReadWriteStrText: text; const s: ansistring); [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  2217. begin
  2218. SetupReadStrCommon(TextRec(ReadWriteStrText),StringCodePage(s));
  2219. { we need a reference, because 's' may be a temporary expression }
  2220. PAnsiString(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=s;
  2221. TextRec(ReadWriteStrText).InOutFunc:=@ReadStrAnsi;
  2222. { this is called at the end, by fpc_read_end }
  2223. TextRec(ReadWriteStrText).FlushFunc:=@ReadAnsiStrFinal;
  2224. end;
  2225. procedure fpc_SetupReadStr_Ansistr_Intern(var ReadWriteStrText: text; const s: rawbytestring); [external name 'FPC_SETUPREADSTR_ANSISTR'];
  2226. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2227. procedure fpc_SetupReadStr_Shortstr(var ReadWriteStrText: text; const s: shortstring); compilerproc;
  2228. begin
  2229. { the reason we convert the short string to ansistring, is because the semantics of
  2230. readstr are defined as:
  2231. *********************
  2232. Apart from the restrictions imposed by requirements given in this clause,
  2233. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  2234. string-expression and v 1 ,...,v n denote variable-accesses possessing the
  2235. char-type (or a subrange of char-type), the integer-type (or a subrange of
  2236. integer-type), the real-type, a fixed-string-type, or a
  2237. variable-string-type, shall be equivalent to
  2238. begin
  2239. rewrite(f);
  2240. writeln(f, e);
  2241. reset(f);
  2242. read(f, v 1 ,...,v n )
  2243. end
  2244. *********************
  2245. This means that any side effects caused by the evaluation of v 1 .. v n
  2246. must not affect the value of e (= our argument s) -> we need a copy of it.
  2247. An ansistring is the easiest way to get a threadsafe copy, and allows us
  2248. to use the other ansistring readstr helpers too.
  2249. }
  2250. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2251. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  2252. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  2253. runerror(217);
  2254. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2255. end;
  2256. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  2257. procedure fpc_SetupReadStr_Unicodestr(var ReadWriteStrText: text; const s: unicodestring); compilerproc;
  2258. begin
  2259. { we use an utf8string to avoid code duplication }
  2260. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,utf8string(s));
  2261. end;
  2262. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  2263. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2264. procedure fpc_SetupReadStr_Widestr(var ReadWriteStrText: text; const s: widestring); compilerproc;
  2265. begin
  2266. { we use an utf8string to avoid code duplication }
  2267. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,utf8string(s));
  2268. end;
  2269. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2270. {*****************************************************************************
  2271. Initializing
  2272. *****************************************************************************}
  2273. procedure OpenStdIO(var f:text;mode:longint;hdl:thandle);
  2274. begin
  2275. Assign(f,'');
  2276. TextRec(f).Handle:=hdl;
  2277. TextRec(f).Mode:=mode;
  2278. TextRec(f).Closefunc:=@FileCloseFunc;
  2279. case mode of
  2280. fmInput :
  2281. begin
  2282. TextRec(f).InOutFunc:=@FileReadFunc;
  2283. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  2284. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleInput);
  2285. {$endif}
  2286. end;
  2287. fmOutput :
  2288. begin
  2289. TextRec(f).InOutFunc:=@FileWriteFunc;
  2290. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  2291. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleOutput);
  2292. {$endif}
  2293. if Do_Isdevice(hdl) then
  2294. TextRec(f).FlushFunc:=@FileWriteFunc;
  2295. end;
  2296. else
  2297. HandleError(102);
  2298. end;
  2299. end;