text.inc 67 KB

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