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, this will be fixed later on anways because
  576. the current way of handling iso program parameters is apparently
  577. wrong }
  578. assign(t,chr((nr mod 16)+65));
  579. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  580. reset(t);
  581. end;
  582. Procedure fpc_textclose_iso(var t : Text);compilerproc;
  583. begin
  584. close(t);
  585. end;
  586. {*****************************************************************************
  587. Write(Ln)
  588. *****************************************************************************}
  589. Procedure fpc_WriteBuffer(var f:Text;const b;len:SizeInt);
  590. var
  591. p : pchar;
  592. left,
  593. idx : SizeInt;
  594. begin
  595. p:=pchar(@b);
  596. idx:=0;
  597. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  598. while len>left do
  599. begin
  600. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],left);
  601. dec(len,left);
  602. inc(idx,left);
  603. inc(TextRec(f).BufPos,left);
  604. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  605. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  606. end;
  607. move(p[idx],TextRec(f).Bufptr^[TextRec(f).BufPos],len);
  608. inc(TextRec(f).BufPos,len);
  609. end;
  610. Procedure fpc_WriteBlanks(var f:Text;len:longint);
  611. var
  612. left : longint;
  613. begin
  614. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  615. while len>left do
  616. begin
  617. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],left,' ');
  618. dec(len,left);
  619. inc(TextRec(f).BufPos,left);
  620. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  621. left:=TextRec(f).BufSize-TextRec(f).BufPos;
  622. end;
  623. FillChar(TextRec(f).Bufptr^[TextRec(f).BufPos],len,' ');
  624. inc(TextRec(f).BufPos,len);
  625. end;
  626. Procedure fpc_Write_End(var f:Text); iocheck; compilerproc;
  627. begin
  628. if TextRec(f).FlushFunc<>nil then
  629. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  630. end;
  631. Procedure fpc_Writeln_End(var f:Text); iocheck; compilerproc;
  632. begin
  633. If InOutRes <> 0 then exit;
  634. case TextRec(f).mode of
  635. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  636. begin
  637. { Write EOL }
  638. fpc_WriteBuffer(f,TextRec(f).LineEnd[1],length(TextRec(f).LineEnd));
  639. { Flush }
  640. if TextRec(f).FlushFunc<>nil then
  641. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  642. end;
  643. fmInput: InOutRes:=105
  644. else InOutRes:=103;
  645. end;
  646. end;
  647. Procedure fpc_Write_Text_ShortStr(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR']; compilerproc;
  648. Begin
  649. If (InOutRes<>0) then
  650. exit;
  651. case TextRec(f).mode of
  652. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  653. begin
  654. If Len>Length(s) Then
  655. fpc_WriteBlanks(f,Len-Length(s));
  656. fpc_WriteBuffer(f,s[1],Length(s));
  657. end;
  658. fmInput: InOutRes:=105
  659. else InOutRes:=103;
  660. end;
  661. End;
  662. Procedure fpc_Write_Text_ShortStr_Iso(Len : Longint;var f : Text;const s : String); iocheck; [Public,Alias:'FPC_WRITE_TEXT_SHORTSTR_ISO']; compilerproc;
  663. Begin
  664. If (InOutRes<>0) then
  665. exit;
  666. case TextRec(f).mode of
  667. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  668. begin
  669. { default value? }
  670. If Len=-1 then
  671. Len:=length(s);
  672. If Len>Length(s) Then
  673. begin
  674. fpc_WriteBlanks(f,Len-Length(s));
  675. fpc_WriteBuffer(f,s[1],Length(s));
  676. end
  677. else
  678. fpc_WriteBuffer(f,s[1],Len);
  679. end;
  680. fmInput: InOutRes:=105
  681. else InOutRes:=103;
  682. end;
  683. End;
  684. { provide local access to write_str }
  685. procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR'];
  686. { provide local access to write_str_iso }
  687. procedure Write_Str_Iso(Len : Longint;var f : Text;const s : String); iocheck; [external name 'FPC_WRITE_TEXT_SHORTSTR_ISO'];
  688. Procedure fpc_Write_Text_Pchar_as_Array(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  689. var
  690. ArrayLen : longint;
  691. p : pchar;
  692. Begin
  693. If (InOutRes<>0) then
  694. exit;
  695. case TextRec(f).mode of
  696. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  697. begin
  698. p:=pchar(@s);
  699. if zerobased then
  700. begin
  701. { can't use StrLen, since that one could try to read past the end }
  702. { of the heap (JM) }
  703. ArrayLen:=IndexByte(p^,high(s)+1,0);
  704. { IndexByte returns -1 if not found (JM) }
  705. if ArrayLen = -1 then
  706. ArrayLen := high(s)+1;
  707. end
  708. else
  709. ArrayLen := high(s)+1;
  710. If Len>ArrayLen Then
  711. fpc_WriteBlanks(f,Len-ArrayLen);
  712. fpc_WriteBuffer(f,p^,ArrayLen);
  713. end;
  714. fmInput: InOutRes:=105
  715. else InOutRes:=103;
  716. end;
  717. End;
  718. Procedure fpc_Write_Text_Pchar_as_Array_Iso(Len : Longint;var f : Text;const s : array of char; zerobased: boolean = true); iocheck; compilerproc;
  719. var
  720. ArrayLen : longint;
  721. p : pchar;
  722. Begin
  723. If (InOutRes<>0) then
  724. exit;
  725. case TextRec(f).mode of
  726. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  727. begin
  728. p:=pchar(@s);
  729. if zerobased then
  730. begin
  731. { can't use StrLen, since that one could try to read past the end }
  732. { of the heap (JM) }
  733. ArrayLen:=IndexByte(p^,high(s)+1,0);
  734. { IndexByte returns -1 if not found (JM) }
  735. if ArrayLen = -1 then
  736. ArrayLen := high(s)+1;
  737. end
  738. else
  739. ArrayLen := high(s)+1;
  740. { default value? }
  741. If Len=-1 then
  742. Len:=ArrayLen;
  743. If Len>ArrayLen Then
  744. begin
  745. fpc_WriteBlanks(f,Len-ArrayLen);
  746. fpc_WriteBuffer(f,p^,ArrayLen);
  747. end
  748. else
  749. fpc_WriteBuffer(f,p^,Len);
  750. end;
  751. fmInput: InOutRes:=105
  752. else InOutRes:=103;
  753. end;
  754. End;
  755. Procedure fpc_Write_Text_PChar_As_Pointer(Len : Longint;var f : Text;p : PChar); iocheck; compilerproc;
  756. var
  757. PCharLen : longint;
  758. Begin
  759. If (p=nil) or (InOutRes<>0) then
  760. exit;
  761. case TextRec(f).mode of
  762. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  763. begin
  764. PCharLen:=StrLen(p);
  765. If Len>PCharLen Then
  766. fpc_WriteBlanks(f,Len-PCharLen);
  767. fpc_WriteBuffer(f,p^,PCharLen);
  768. end;
  769. fmInput: InOutRes:=105
  770. else InOutRes:=103;
  771. end;
  772. End;
  773. Procedure fpc_Write_Text_AnsiStr (Len : Longint; Var f : Text; const S : RawByteString); iocheck; [Public,alias:'FPC_WRITE_TEXT_ANSISTR']; compilerproc;
  774. {
  775. Writes a AnsiString to the Text file T
  776. }
  777. var
  778. SLen: longint;
  779. a: RawByteString;
  780. begin
  781. If (InOutRes<>0) then
  782. exit;
  783. case TextRec(f).mode of
  784. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  785. begin
  786. SLen:=Length(s);
  787. If Len>SLen Then
  788. fpc_WriteBlanks(f,Len-SLen);
  789. if SLen > 0 then
  790. begin
  791. {$if defined(FPC_HAS_CPSTRING) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  792. if TextRec(f).CodePage<>TranslatePlaceholderCP(StringCodePage(S)) then
  793. begin
  794. a:=fpc_AnsiStr_To_AnsiStr(S,TextRec(f).CodePage);
  795. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  796. end
  797. else
  798. {$endif}
  799. fpc_WriteBuffer(f,PAnsiChar(s)^,SLen);
  800. end;
  801. end;
  802. fmInput: InOutRes:=105
  803. else InOutRes:=103;
  804. end;
  805. end;
  806. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  807. Procedure fpc_Write_Text_UnicodeStr (Len : Longint; Var f : Text; const S : UnicodeString); iocheck; compilerproc;
  808. {
  809. Writes a UnicodeString to the Text file T
  810. }
  811. var
  812. SLen: longint;
  813. a: RawByteString;
  814. begin
  815. If (pointer(S)=nil) or (InOutRes<>0) then
  816. exit;
  817. case TextRec(f).mode of
  818. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  819. begin
  820. SLen:=Length(s);
  821. If Len>SLen Then
  822. fpc_WriteBlanks(f,Len-SLen);
  823. {$ifdef FPC_HAS_CPSTRING}
  824. WideStringManager.Unicode2AnsiMoveProc(PUnicodeChar(S),a,TextRec(f).CodePage,SLen);
  825. {$else}
  826. a:=s;
  827. {$endif FPC_HAS_CPSTRING}
  828. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  829. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  830. end;
  831. fmInput: InOutRes:=105
  832. else InOutRes:=103;
  833. end;
  834. end;
  835. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  836. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  837. Procedure fpc_Write_Text_WideStr (Len : Longint; Var f : Text; const S : WideString); iocheck; compilerproc;
  838. {
  839. Writes a WideString to the Text file T
  840. }
  841. var
  842. SLen: longint;
  843. a: RawByteString;
  844. begin
  845. If (pointer(S)=nil) or (InOutRes<>0) then
  846. exit;
  847. case TextRec(f).mode of
  848. fmOutput { fmAppend gets changed to fmOutPut in do_open (JM) }:
  849. begin
  850. SLen:=Length(s);
  851. If Len>SLen Then
  852. fpc_WriteBlanks(f,Len-SLen);
  853. {$ifdef FPC_HAS_CPSTRING}
  854. widestringmanager.Wide2AnsiMoveProc(PWideChar(s), a, TextRec(f).CodePage, SLen);
  855. {$else}
  856. a:=s;
  857. {$endif}
  858. { length(a) can be > slen, e.g. after utf-16 -> utf-8 }
  859. fpc_WriteBuffer(f,PAnsiChar(a)^,Length(a));
  860. end;
  861. fmInput: InOutRes:=105
  862. else InOutRes:=103;
  863. end;
  864. end;
  865. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  866. Procedure fpc_Write_Text_SInt(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  867. var
  868. s : String;
  869. Begin
  870. If (InOutRes<>0) then
  871. exit;
  872. Str(l,s);
  873. Write_Str(Len,t,s);
  874. End;
  875. Procedure fpc_Write_Text_UInt(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  876. var
  877. s : String;
  878. Begin
  879. If (InOutRes<>0) then
  880. exit;
  881. Str(L,s);
  882. Write_Str(Len,t,s);
  883. End;
  884. Procedure fpc_Write_Text_SInt_Iso(Len : Longint;var t : Text;l : ValSInt); iocheck; compilerproc;
  885. var
  886. s : String;
  887. Begin
  888. If (InOutRes<>0) then
  889. exit;
  890. Str(l,s);
  891. { default value? }
  892. if len=-1 then
  893. len:=11
  894. else if len<length(s) then
  895. len:=length(s);
  896. Write_Str_Iso(Len,t,s);
  897. End;
  898. Procedure fpc_Write_Text_UInt_Iso(Len : Longint;var t : Text;l : ValUInt); iocheck; compilerproc;
  899. var
  900. s : String;
  901. Begin
  902. If (InOutRes<>0) then
  903. exit;
  904. Str(L,s);
  905. { default value? }
  906. if len=-1 then
  907. len:=11
  908. else if len<length(s) then
  909. len:=length(s);
  910. Write_Str_Iso(Len,t,s);
  911. End;
  912. {$ifndef CPU64}
  913. procedure fpc_write_text_qword(len : longint;var t : text;q : qword); iocheck; compilerproc;
  914. var
  915. s : string;
  916. begin
  917. if (InOutRes<>0) then
  918. exit;
  919. str(q,s);
  920. write_str(len,t,s);
  921. end;
  922. procedure fpc_write_text_int64(len : longint;var t : text;i : int64); iocheck; compilerproc;
  923. var
  924. s : string;
  925. begin
  926. if (InOutRes<>0) then
  927. exit;
  928. str(i,s);
  929. write_str(len,t,s);
  930. end;
  931. procedure fpc_write_text_qword_iso(len : longint;var t : text;q : qword); iocheck; compilerproc;
  932. var
  933. s : string;
  934. begin
  935. if (InOutRes<>0) then
  936. exit;
  937. str(q,s);
  938. { default value? }
  939. if len=-1 then
  940. len:=20
  941. else if len<length(s) then
  942. len:=length(s);
  943. write_str_iso(len,t,s);
  944. end;
  945. procedure fpc_write_text_int64_iso(len : longint;var t : text;i : int64); iocheck; compilerproc;
  946. var
  947. s : string;
  948. begin
  949. if (InOutRes<>0) then
  950. exit;
  951. str(i,s);
  952. { default value? }
  953. if len=-1 then
  954. len:=20
  955. else if len<length(s) then
  956. len:=length(s);
  957. write_str_iso(len,t,s);
  958. end;
  959. {$endif CPU64}
  960. {$if defined(CPU16) or defined(CPU8)}
  961. procedure fpc_write_text_longword(len : longint;var t : text;q : longword); iocheck; compilerproc;
  962. var
  963. s : string;
  964. begin
  965. if (InOutRes<>0) then
  966. exit;
  967. str(q,s);
  968. write_str(len,t,s);
  969. end;
  970. procedure fpc_write_text_longint(len : longint;var t : text;i : longint); iocheck; compilerproc;
  971. var
  972. s : string;
  973. begin
  974. if (InOutRes<>0) then
  975. exit;
  976. str(i,s);
  977. write_str(len,t,s);
  978. end;
  979. procedure fpc_write_text_longword_iso(len : longint;var t : text;q : longword); iocheck; compilerproc;
  980. var
  981. s : string;
  982. begin
  983. if (InOutRes<>0) then
  984. exit;
  985. str(q,s);
  986. { default value? }
  987. if len=-1 then
  988. len:=11
  989. else if len<length(s) then
  990. len:=length(s);
  991. write_str_iso(len,t,s);
  992. end;
  993. procedure fpc_write_text_longint_iso(len : longint;var t : text;i : longint); iocheck; compilerproc;
  994. var
  995. s : string;
  996. begin
  997. if (InOutRes<>0) then
  998. exit;
  999. str(i,s);
  1000. { default value? }
  1001. if len=-1 then
  1002. len:=11
  1003. else if len<length(s) then
  1004. len:=length(s);
  1005. write_str_iso(len,t,s);
  1006. end;
  1007. procedure fpc_write_text_word(len : longint;var t : text;q : word); iocheck; compilerproc;
  1008. var
  1009. s : string;
  1010. begin
  1011. if (InOutRes<>0) then
  1012. exit;
  1013. str(q,s);
  1014. write_str(len,t,s);
  1015. end;
  1016. procedure fpc_write_text_smallint(len : longint;var t : text;i : smallint); iocheck; compilerproc;
  1017. var
  1018. s : string;
  1019. begin
  1020. if (InOutRes<>0) then
  1021. exit;
  1022. str(i,s);
  1023. write_str(len,t,s);
  1024. end;
  1025. procedure fpc_write_text_word_iso(len : longint;var t : text;q : word); iocheck; compilerproc;
  1026. var
  1027. s : string;
  1028. begin
  1029. if (InOutRes<>0) then
  1030. exit;
  1031. str(q,s);
  1032. { default value? }
  1033. if len=-1 then
  1034. len:=11
  1035. else if len<length(s) then
  1036. len:=length(s);
  1037. write_str_iso(len,t,s);
  1038. end;
  1039. procedure fpc_write_text_smallint_iso(len : longint;var t : text;i : smallint); iocheck; compilerproc;
  1040. var
  1041. s : string;
  1042. begin
  1043. if (InOutRes<>0) then
  1044. exit;
  1045. str(i,s);
  1046. { default value? }
  1047. if len=-1 then
  1048. len:=11
  1049. else if len<length(s) then
  1050. len:=length(s);
  1051. write_str_iso(len,t,s);
  1052. end;
  1053. {$endif CPU16 or CPU8}
  1054. {$ifndef FPUNONE}
  1055. Procedure fpc_Write_Text_Float(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  1056. var
  1057. s : String;
  1058. Begin
  1059. If (InOutRes<>0) then
  1060. exit;
  1061. Str_real(Len,fixkomma,r,treal_type(rt),s);
  1062. Write_Str(Len,t,s);
  1063. End;
  1064. Procedure fpc_Write_Text_Float_iso(rt,fixkomma,Len : Longint;var t : Text;r : ValReal); iocheck; compilerproc;
  1065. var
  1066. s : String;
  1067. Begin
  1068. If (InOutRes<>0) then
  1069. exit;
  1070. Str_real_iso(Len,fixkomma,r,treal_type(rt),s);
  1071. Write_Str(Len,t,s);
  1072. End;
  1073. {$endif}
  1074. procedure fpc_write_text_enum(typinfo,ord2strindex:pointer;len:sizeint;var t:text;ordinal:longint); iocheck; compilerproc;
  1075. var
  1076. s:string;
  1077. begin
  1078. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1079. runerror(219);
  1080. {$else EXCLUDE_COMPLEX_PROCS}
  1081. if textrec(t).mode<>fmoutput then
  1082. begin
  1083. if textrec(t).mode=fminput then
  1084. inoutres:=105
  1085. else
  1086. inoutres:=103;
  1087. exit;
  1088. end;
  1089. inoutres := fpc_shortstr_enum_intern(ordinal, len, typinfo, ord2strindex, s);
  1090. if (inoutres <> 0) then
  1091. exit;
  1092. fpc_writeBuffer(t,s[1],length(s));
  1093. {$endif EXCLUDE_COMPLEX_PROCS}
  1094. end;
  1095. {$ifdef FPC_HAS_STR_CURRENCY}
  1096. Procedure fpc_Write_Text_Currency(fixkomma,Len : Longint;var t : Text;c : Currency); iocheck; compilerproc;
  1097. var
  1098. s : String;
  1099. Begin
  1100. If (InOutRes<>0) then
  1101. exit;
  1102. str(c:Len:fixkomma,s);
  1103. Write_Str(Len,t,s);
  1104. End;
  1105. {$endif FPC_HAS_STR_CURRENCY}
  1106. Procedure fpc_Write_Text_Boolean(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  1107. Begin
  1108. If (InOutRes<>0) then
  1109. exit;
  1110. { Can't use array[boolean] because b can be >0 ! }
  1111. if b then
  1112. Write_Str(Len,t,'TRUE')
  1113. else
  1114. Write_Str(Len,t,'FALSE');
  1115. End;
  1116. Procedure fpc_Write_Text_Boolean_Iso(Len : Longint;var t : Text;b : Boolean); iocheck; compilerproc;
  1117. Begin
  1118. If (InOutRes<>0) then
  1119. exit;
  1120. { Can't use array[boolean] because b can be >0 ! }
  1121. { default value? }
  1122. If Len=-1 then
  1123. Len:=5;
  1124. if b then
  1125. Write_Str_Iso(Len,t,'true')
  1126. else
  1127. Write_Str_Iso(Len,t,'false');
  1128. End;
  1129. Procedure fpc_Write_Text_Char(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  1130. Begin
  1131. If (InOutRes<>0) then
  1132. exit;
  1133. if (TextRec(t).mode<>fmOutput) Then
  1134. begin
  1135. if TextRec(t).mode=fmClosed then
  1136. InOutRes:=103
  1137. else
  1138. InOutRes:=105;
  1139. exit;
  1140. end;
  1141. If Len>1 Then
  1142. fpc_WriteBlanks(t,Len-1);
  1143. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  1144. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  1145. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  1146. Inc(TextRec(t).BufPos);
  1147. End;
  1148. Procedure fpc_Write_Text_Char_Iso(Len : Longint;var t : Text;c : Char); iocheck; compilerproc;
  1149. Begin
  1150. If (InOutRes<>0) then
  1151. exit;
  1152. if (TextRec(t).mode<>fmOutput) Then
  1153. begin
  1154. if TextRec(t).mode=fmClosed then
  1155. InOutRes:=103
  1156. else
  1157. InOutRes:=105;
  1158. exit;
  1159. end;
  1160. { default value? }
  1161. If Len=-1 then
  1162. Len:=1;
  1163. If Len>1 Then
  1164. fpc_WriteBlanks(t,Len-1)
  1165. else If Len<1 Then
  1166. exit;
  1167. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  1168. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  1169. TextRec(t).Bufptr^[TextRec(t).BufPos]:=c;
  1170. Inc(TextRec(t).BufPos);
  1171. End;
  1172. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1173. Procedure fpc_Write_Text_WideChar(Len : Longint;var t : Text;c : WideChar); iocheck; compilerproc;
  1174. var
  1175. a: RawByteString;
  1176. Begin
  1177. If (InOutRes<>0) then
  1178. exit;
  1179. if (TextRec(t).mode<>fmOutput) Then
  1180. begin
  1181. if TextRec(t).mode=fmClosed then
  1182. InOutRes:=103
  1183. else
  1184. InOutRes:=105;
  1185. exit;
  1186. end;
  1187. If Len>1 Then
  1188. fpc_WriteBlanks(t,Len-1);
  1189. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  1190. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  1191. { a widechar can be translated into more than a single ansichar }
  1192. {$ifdef FPC_HAS_CPSTRING}
  1193. widestringmanager.Wide2AnsiMoveProc(@c,a,TextRec(t).CodePage,1);
  1194. {$else}
  1195. a:=c;
  1196. {$endif}
  1197. fpc_WriteBuffer(t,PAnsiChar(a)^,Length(a));
  1198. End;
  1199. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1200. {*****************************************************************************
  1201. Read(Ln)
  1202. *****************************************************************************}
  1203. Function NextChar(var f:Text;var s:string):Boolean;
  1204. begin
  1205. NextChar:=false;
  1206. if (TextRec(f).BufPos<TextRec(f).BufEnd) then
  1207. if not (CtrlZMarksEOF) or (TextRec(f).Bufptr^[TextRec(f).BufPos]<>#26) then
  1208. begin
  1209. if length(s)<high(s) then
  1210. begin
  1211. inc(s[0]);
  1212. s[length(s)]:=TextRec(f).BufPtr^[TextRec(f).BufPos];
  1213. end;
  1214. Inc(TextRec(f).BufPos);
  1215. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1216. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1217. NextChar:=true;
  1218. end;
  1219. end;
  1220. Function IgnoreSpaces(var f:Text):Boolean;
  1221. {
  1222. Removes all leading spaces,tab,eols from the input buffer, returns true if
  1223. the buffer is empty
  1224. }
  1225. var
  1226. s : string;
  1227. begin
  1228. s:='';
  1229. IgnoreSpaces:=false;
  1230. { Return false when already at EOF }
  1231. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1232. exit;
  1233. (* Check performed separately to avoid accessing memory outside buffer *)
  1234. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1235. exit;
  1236. while (TextRec(f).Bufptr^[TextRec(f).BufPos] <= ' ') do
  1237. begin
  1238. if not NextChar(f,s) then
  1239. exit;
  1240. { EOF? }
  1241. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1242. break;
  1243. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1244. break;
  1245. end;
  1246. IgnoreSpaces:=true;
  1247. end;
  1248. procedure ReadNumeric(var f:Text;var s:string);
  1249. {
  1250. Read numeric input, if buffer is empty then return True
  1251. }
  1252. begin
  1253. repeat
  1254. if not NextChar(f,s) then
  1255. exit;
  1256. until (length(s)=high(s)) or (TextRec(f).BufPtr^[TextRec(f).BufPos] <= ' ');
  1257. end;
  1258. function CheckRead(var f:Text):Boolean;
  1259. begin
  1260. CheckRead:=False;
  1261. { Check error and if file is open and load buf if empty }
  1262. If (InOutRes<>0) then
  1263. exit;
  1264. if (TextRec(f).mode<>fmInput) Then
  1265. begin
  1266. case TextRec(f).mode of
  1267. fmOutPut,fmAppend:
  1268. InOutRes:=104;
  1269. else
  1270. InOutRes:=103;
  1271. end;
  1272. exit;
  1273. end;
  1274. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1275. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1276. CheckRead:=True;
  1277. end;
  1278. procedure ReadInteger(var f:Text;var s:string);
  1279. {
  1280. Ignore leading blanks (incl. EOF) and return the first characters matching
  1281. an integer in the format recognized by the Val procedure:
  1282. [+-]?[0-9]+
  1283. or [+-]?(0x|0X|x|X)[0-9A-Za-z]+
  1284. or [+-]?&[0-7]+
  1285. or [+-]?%[0-1]+
  1286. A partial match may be returned, e.g.: '' or '+' or '0x'.
  1287. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1288. standard function in ISO mode.
  1289. }
  1290. var
  1291. Base: Integer;
  1292. begin
  1293. s := '';
  1294. with TextRec(f) do begin
  1295. if not CheckRead(f) then Exit;
  1296. IgnoreSpaces(f);
  1297. if BufPos >= BufEnd then Exit;
  1298. if BufPtr^[BufPos] in ['+','-'] then
  1299. NextChar(f,s);
  1300. Base := 10;
  1301. if BufPos >= BufEnd then Exit;
  1302. if BufPtr^[BufPos] in ['$','x','X','%','&'] then
  1303. begin
  1304. case BufPtr^[BufPos] of
  1305. '$','x','X': Base := 16;
  1306. '%': Base := 2;
  1307. '&': Base := 8;
  1308. end;
  1309. NextChar(f,s);
  1310. end else if BufPtr^[BufPos] = '0' then
  1311. begin
  1312. NextChar(f,s);
  1313. if BufPos >= BufEnd then Exit;
  1314. if BufPtr^[BufPos] in ['x','X'] then
  1315. begin
  1316. Base := 16;
  1317. NextChar(f,s);
  1318. end;
  1319. end;
  1320. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1321. if (((Base = 2) and (BufPtr^[BufPos] in ['0'..'1']))
  1322. or ((Base = 8) and (BufPtr^[BufPos] in ['0'..'7']))
  1323. or ((Base = 10) and (BufPtr^[BufPos] in ['0'..'9']))
  1324. or ((Base = 16) and (BufPtr^[BufPos] in ['0'..'9','a'..'f','A'..'F']))) then
  1325. NextChar(f,s)
  1326. else Exit;
  1327. end;
  1328. end;
  1329. procedure ReadReal(var f:Text;var s:string);
  1330. {
  1331. Ignore leading blanks (incl. EOF) and return the first characters matching
  1332. a float number in the format recognized by the Val procedure:
  1333. [+-]?([0-9]+)?\.[0-9]+([eE][+-]?[0-9]+)?
  1334. or [+-]?[0-9]+\.([0-9]+)?([eE][+-]?[0-9]+)?
  1335. A partial match may be returned, e.g.: '' or '+' or '.' or '1e' or even '+.'.
  1336. Used by some fpc_Read_Text_*_Iso functions which implement the read()
  1337. standard function in ISO mode.
  1338. }
  1339. var digit: Boolean;
  1340. begin
  1341. s := '';
  1342. with TextRec(f) do begin
  1343. if not CheckRead(f) then Exit;
  1344. IgnoreSpaces(f);
  1345. if BufPos >= BufEnd then Exit;
  1346. if BufPtr^[BufPos] in ['+','-'] then
  1347. NextChar(f,s);
  1348. digit := false;
  1349. if BufPos >= BufEnd then Exit;
  1350. if BufPtr^[BufPos] in ['0'..'9'] then
  1351. begin
  1352. digit := true;
  1353. repeat
  1354. NextChar(f,s);
  1355. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1356. until not (BufPtr^[BufPos] in ['0'..'9']);
  1357. end;
  1358. if BufPtr^[BufPos] = '.' then
  1359. begin
  1360. NextChar(f,s);
  1361. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1362. if BufPtr^[BufPos] in ['0'..'9'] then
  1363. begin
  1364. digit := true;
  1365. repeat
  1366. NextChar(f,s);
  1367. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1368. until not (BufPtr^[BufPos] in ['0'..'9']);
  1369. end;
  1370. end;
  1371. {at least one digit is required on the left of the exponent}
  1372. if digit and (BufPtr^[BufPos] in ['e','E']) then
  1373. begin
  1374. NextChar(f,s);
  1375. if (BufPos >= BufEnd) or (Length(s) >= High(s)) then Exit;
  1376. if BufPtr^[BufPos] in ['+','-'] then
  1377. NextChar(f,s);
  1378. while (BufPos < BufEnd) and (Length(s) < High(s)) do
  1379. if BufPtr^[BufPos] in ['0'..'9'] then
  1380. NextChar(f,s)
  1381. else break;
  1382. end;
  1383. end;
  1384. end;
  1385. Procedure fpc_Read_End(var f:Text);[Public,Alias:'FPC_READ_END']; iocheck; compilerproc;
  1386. begin
  1387. if TextRec(f).FlushFunc<>nil then
  1388. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1389. end;
  1390. Procedure fpc_ReadLn_End(var f : Text);[Public,Alias:'FPC_READLN_END']; iocheck; compilerproc;
  1391. var prev: char;
  1392. Begin
  1393. If not CheckRead(f) then
  1394. exit;
  1395. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1396. { Flush if set }
  1397. begin
  1398. if (TextRec(f).FlushFunc<>nil) then
  1399. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1400. exit;
  1401. end;
  1402. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1403. Exit;
  1404. repeat
  1405. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1406. inc(TextRec(f).BufPos);
  1407. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1408. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1409. if prev = #10 then
  1410. exit;
  1411. {$ifdef MACOS}
  1412. if prev = #13 then
  1413. {StdInput on macos never have dos line ending, so this is safe.}
  1414. if TextRec(f).Handle = StdInputHandle then
  1415. exit;
  1416. {$endif MACOS}
  1417. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1418. begin
  1419. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1420. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1421. { Flush if set }
  1422. begin
  1423. if (TextRec(f).FlushFunc<>nil) then
  1424. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1425. exit;
  1426. end;
  1427. end;
  1428. if CtrlZMarksEOF and (TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26) then
  1429. Exit;
  1430. if (prev=#13) then
  1431. { is there also a #10 after it? }
  1432. begin
  1433. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1434. { yes, skip that one as well }
  1435. inc(TextRec(f).BufPos);
  1436. exit;
  1437. end;
  1438. until false;
  1439. End;
  1440. Procedure fpc_ReadLn_End_Iso(var f : Text);[Public,Alias:'FPC_READLN_END_ISO']; iocheck; compilerproc;
  1441. var prev: char;
  1442. Begin
  1443. If not CheckRead(f) then
  1444. exit;
  1445. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1446. { Flush if set }
  1447. begin
  1448. if (TextRec(f).FlushFunc<>nil) then
  1449. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1450. exit;
  1451. end;
  1452. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1453. begin
  1454. inc(TextRec(f).BufPos);
  1455. Exit;
  1456. end;
  1457. repeat
  1458. prev := TextRec(f).BufPtr^[TextRec(f).BufPos];
  1459. inc(TextRec(f).BufPos);
  1460. { no system uses #10#13 as line seperator (#10 = *nix, #13 = Mac, }
  1461. { #13#10 = Dos), so if we've got #10, we can safely exit }
  1462. if prev = #10 then
  1463. exit;
  1464. {$ifdef MACOS}
  1465. if prev = #13 then
  1466. {StdInput on macos never have dos line ending, so this is safe.}
  1467. if TextRec(f).Handle = StdInputHandle then
  1468. exit;
  1469. {$endif MACOS}
  1470. if TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1471. begin
  1472. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1473. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1474. { Flush if set }
  1475. begin
  1476. if (TextRec(f).FlushFunc<>nil) then
  1477. FileFunc(TextRec(f).FlushFunc)(TextRec(f));
  1478. exit;
  1479. end;
  1480. end;
  1481. if TextRec (F).BufPtr^ [TextRec (F).BufPos] = #26 then
  1482. begin
  1483. inc(TextRec(f).BufPos);
  1484. Exit;
  1485. end;
  1486. if (prev=#13) then
  1487. { is there also a #10 after it? }
  1488. begin
  1489. if (TextRec(f).BufPtr^[TextRec(f).BufPos]=#10) then
  1490. { yes, skip that one as well }
  1491. inc(TextRec(f).BufPos);
  1492. exit;
  1493. end;
  1494. until false;
  1495. End;
  1496. Function ReadPCharLen(var f:Text;s:pchar;maxlen:longint):longint;
  1497. var
  1498. sPos,len : Longint;
  1499. p,startp,maxp : pchar;
  1500. end_of_string:boolean;
  1501. Begin
  1502. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1503. runerror(219);
  1504. {$else EXCLUDE_COMPLEX_PROCS}
  1505. ReadPCharLen:=0;
  1506. If not CheckRead(f) then
  1507. exit;
  1508. { Read maximal until Maxlen is reached }
  1509. sPos:=0;
  1510. end_of_string:=false;
  1511. repeat
  1512. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1513. begin
  1514. FileFunc(TextRec(f).InOutFunc)(TextRec(f));
  1515. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1516. break;
  1517. end;
  1518. p:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1519. if SPos+TextRec(f).BufEnd-TextRec(f).BufPos>MaxLen then
  1520. maxp:=@TextRec(f).BufPtr^[TextRec(f).BufPos+MaxLen-SPos]
  1521. else
  1522. maxp:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1523. startp:=p;
  1524. { find stop character }
  1525. while p<maxp do
  1526. begin
  1527. { Optimization: Do a quick check for a control character first }
  1528. if (p^<' ') then
  1529. begin
  1530. if (p^ in [#10,#13]) or
  1531. (ctrlZmarkseof and (p^=#26)) then
  1532. begin
  1533. end_of_string:=true;
  1534. break;
  1535. end;
  1536. end;
  1537. inc(p);
  1538. end;
  1539. { calculate read bytes }
  1540. len:=p-startp;
  1541. inc(TextRec(f).BufPos,Len);
  1542. Move(startp^,s[sPos],Len);
  1543. inc(sPos,Len);
  1544. until (spos=MaxLen) or end_of_string;
  1545. ReadPCharLen:=spos;
  1546. {$endif EXCLUDE_COMPLEX_PROCS}
  1547. End;
  1548. Procedure fpc_Read_Text_ShortStr(var f : Text;out s : String); iocheck; compilerproc;
  1549. Begin
  1550. s[0]:=chr(ReadPCharLen(f,pchar(@s[1]),high(s)));
  1551. End;
  1552. Procedure fpc_Read_Text_PChar_As_Pointer(var f : Text; const s : PChar); iocheck; compilerproc;
  1553. Begin
  1554. pchar(s+ReadPCharLen(f,s,$7fffffff))^:=#0;
  1555. End;
  1556. Procedure fpc_Read_Text_PChar_As_Array(var f : Text;out s : array of char; zerobased: boolean = false); iocheck; compilerproc;
  1557. var
  1558. len: longint;
  1559. Begin
  1560. len := ReadPCharLen(f,pchar(@s),high(s)+1);
  1561. if zerobased and
  1562. (len > high(s)) then
  1563. len := high(s);
  1564. if (len <= high(s)) then
  1565. s[len] := #0;
  1566. End;
  1567. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1568. 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;
  1569. var
  1570. slen,len : SizeInt;
  1571. Begin
  1572. slen:=0;
  1573. Repeat
  1574. // SetLength will reallocate the length.
  1575. SetLength(s,slen+255);
  1576. len:=ReadPCharLen(f,pchar(Pointer(s)+slen),255);
  1577. inc(slen,len);
  1578. Until len<255;
  1579. // Set actual length
  1580. SetLength(s,Slen);
  1581. {$ifdef FPC_HAS_CPSTRING}
  1582. SetCodePage(s,TextRec(f).CodePage,false);
  1583. if cp<>TextRec(f).CodePage then
  1584. s:=fpc_AnsiStr_To_AnsiStr(s,cp);
  1585. {$endif FPC_HAS_CPSTRING}
  1586. End;
  1587. 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'];
  1588. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  1589. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1590. Procedure fpc_Read_Text_UnicodeStr(var f : Text;out us : UnicodeString); iocheck; compilerproc;
  1591. var
  1592. s: RawByteString;
  1593. Begin
  1594. // all standard input is assumed to be ansi-encoded
  1595. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1596. // Convert to unicodestring
  1597. {$ifdef FPC_HAS_CPSTRING}
  1598. widestringmanager.Ansi2UnicodeMoveProc(PAnsiChar(s),StringCodePage(s),us,Length(s));
  1599. {$else}
  1600. us:=s;
  1601. {$endif}
  1602. End;
  1603. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1604. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1605. Procedure fpc_Read_Text_WideStr(var f : Text;out ws : WideString); iocheck; compilerproc;
  1606. var
  1607. s: RawByteString;
  1608. Begin
  1609. // all standard input is assumed to be ansi-encoded
  1610. fpc_Read_Text_AnsiStr_Intern(f,s{$ifdef FPC_HAS_CPSTRING},DefaultSystemCodePage{$endif FPC_HAS_CPSTRING});
  1611. // Convert to widestring
  1612. {$ifdef FPC_HAS_CPSTRING}
  1613. widestringmanager.Ansi2WideMoveProc(PAnsiChar(s),StringCodePage(s),ws,Length(s));
  1614. {$else}
  1615. ws:=s;
  1616. {$endif}
  1617. End;
  1618. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  1619. procedure fpc_Read_Text_Char(var f : Text; out c: char); [public, alias: 'FPC_READ_TEXT_CHAR']; iocheck; compilerproc;
  1620. Begin
  1621. c:=#0;
  1622. If not CheckRead(f) then
  1623. exit;
  1624. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1625. begin
  1626. c := #26;
  1627. exit;
  1628. end;
  1629. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1630. inc(TextRec(f).BufPos);
  1631. end;
  1632. procedure fpc_Read_Text_Char_intern(var f : Text; out c: char); iocheck; [external name 'FPC_READ_TEXT_CHAR'];
  1633. function fpc_GetBuf_Text(var f : Text) : pchar; iocheck; compilerproc;
  1634. Begin
  1635. Result:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1636. If not CheckRead(f) then
  1637. exit;
  1638. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1639. exit;
  1640. Result:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1641. end;
  1642. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1643. procedure fpc_Read_Text_WideChar(var f : Text; out wc: widechar); iocheck;compilerproc;
  1644. var
  1645. ws: widestring;
  1646. i: longint;
  1647. { maximum code point length is 6 characters (with UTF-8) }
  1648. str: array[0..5] of char;
  1649. Begin
  1650. fillchar(str[0],sizeof(str),0);
  1651. for i:=low(str) to high(str) do
  1652. begin
  1653. fpc_Read_Text_Char_intern(f,str[i]);
  1654. case widestringmanager.CodePointLengthProc(@str[0],i+1) of
  1655. -1: { possibly incomplete code point, try with an extra character }
  1656. ;
  1657. 0: { null character }
  1658. begin
  1659. wc:=#0;
  1660. exit;
  1661. end;
  1662. else
  1663. begin
  1664. { valid code point -> convert to widestring}
  1665. {$ifdef FPC_HAS_CPSTRING}
  1666. widestringmanager.Ansi2WideMoveProc(@str[0],TextRec(f).CodePage,ws,i+1);
  1667. {$else}
  1668. widestringmanager.Ansi2WideMoveProc(@str[0],DefaultSystemCodePage,ws,i+1);
  1669. {$endif}
  1670. { has to be exactly one widechar }
  1671. if length(ws)=1 then
  1672. begin
  1673. wc:=ws[1];
  1674. exit
  1675. end
  1676. else
  1677. break;
  1678. end;
  1679. end;
  1680. end;
  1681. { invalid widechar input }
  1682. inoutres:=106;
  1683. end;
  1684. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1685. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: char); iocheck;compilerproc;
  1686. Begin
  1687. c:=' ';
  1688. If not CheckRead(f) then
  1689. exit;
  1690. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1691. begin
  1692. c:=' ';
  1693. exit;
  1694. end;
  1695. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1696. inc(TextRec(f).BufPos);
  1697. if c=#13 then
  1698. begin
  1699. c:=' ';
  1700. If not CheckRead(f) or
  1701. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1702. exit;
  1703. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1704. inc(TextRec(f).BufPos);
  1705. { ignore #26 following a new line }
  1706. If not CheckRead(f) or
  1707. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1708. exit;
  1709. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1710. inc(TextRec(f).BufPos);
  1711. end
  1712. else if c=#10 then
  1713. begin
  1714. c:=' ';
  1715. { ignore #26 following a new line }
  1716. If not CheckRead(f) or
  1717. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1718. exit;
  1719. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1720. inc(TextRec(f).BufPos);
  1721. end
  1722. else if c=#26 then
  1723. c:=' ';
  1724. end;
  1725. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1726. var
  1727. hs : String;
  1728. code : ValSInt;
  1729. Begin
  1730. l:=0;
  1731. If not CheckRead(f) then
  1732. exit;
  1733. hs:='';
  1734. if IgnoreSpaces(f) then
  1735. begin
  1736. { When spaces were found and we are now at EOF,
  1737. then we return 0 }
  1738. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1739. exit;
  1740. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1741. exit;
  1742. ReadNumeric(f,hs);
  1743. end;
  1744. if (hs = '') then
  1745. L := 0
  1746. else
  1747. begin
  1748. Val(hs,l,code);
  1749. if Code <> 0 then
  1750. InOutRes:=106;
  1751. end;
  1752. End;
  1753. Procedure fpc_Read_Text_SInt_Iso(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1754. var
  1755. hs : String;
  1756. code : ValSInt;
  1757. Begin
  1758. ReadInteger(f,hs);
  1759. Val(hs,l,code);
  1760. if Code <> 0 then
  1761. InOutRes:=106;
  1762. End;
  1763. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1764. var
  1765. hs : String;
  1766. code : ValSInt;
  1767. Begin
  1768. u:=0;
  1769. If not CheckRead(f) then
  1770. exit;
  1771. hs:='';
  1772. if IgnoreSpaces(f) then
  1773. begin
  1774. { When spaces were found and we are now at EOF,
  1775. then we return 0 }
  1776. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1777. exit;
  1778. ReadNumeric(f,hs);
  1779. end;
  1780. if (hs = '') then
  1781. u := 0
  1782. else
  1783. begin
  1784. val(hs,u,code);
  1785. If code<>0 Then
  1786. InOutRes:=106;
  1787. end;
  1788. End;
  1789. Procedure fpc_Read_Text_UInt_Iso(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1790. var
  1791. hs : String;
  1792. code : ValSInt;
  1793. Begin
  1794. ReadInteger(f,hs);
  1795. Val(hs,u,code);
  1796. If code<>0 Then
  1797. InOutRes:=106;
  1798. End;
  1799. {$ifndef FPUNONE}
  1800. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1801. var
  1802. hs : string;
  1803. code : Word;
  1804. begin
  1805. v:=0.0;
  1806. If not CheckRead(f) then
  1807. exit;
  1808. hs:='';
  1809. if IgnoreSpaces(f) then
  1810. begin
  1811. { When spaces were found and we are now at EOF,
  1812. then we return 0 }
  1813. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1814. exit;
  1815. ReadNumeric(f,hs);
  1816. end;
  1817. val(hs,v,code);
  1818. If code<>0 Then
  1819. InOutRes:=106;
  1820. end;
  1821. procedure fpc_Read_Text_Float_Iso(var f : Text; out v : ValReal); iocheck; compilerproc;
  1822. var
  1823. hs : string;
  1824. code : Word;
  1825. begin
  1826. ReadReal(f,hs);
  1827. Val(hs,v,code);
  1828. If code<>0 Then
  1829. InOutRes:=106;
  1830. end;
  1831. {$endif}
  1832. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1833. var s:string;
  1834. code:valsint;
  1835. begin
  1836. if not checkread(t) then
  1837. exit;
  1838. s:='';
  1839. if ignorespaces(t) then
  1840. begin
  1841. { When spaces were found and we are now at EOF, then we return 0 }
  1842. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1843. exit;
  1844. ReadNumeric(t,s);
  1845. end;
  1846. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1847. if code<>0 then
  1848. InOutRes:=106;
  1849. end;
  1850. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1851. var
  1852. hs : string;
  1853. code : ValSInt;
  1854. begin
  1855. {$ifdef FPUNONE}
  1856. v:=0;
  1857. {$else}
  1858. v:=0.0;
  1859. {$endif}
  1860. If not CheckRead(f) then
  1861. exit;
  1862. hs:='';
  1863. if IgnoreSpaces(f) then
  1864. begin
  1865. { When spaces were found and we are now at EOF,
  1866. then we return 0 }
  1867. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1868. exit;
  1869. ReadNumeric(f,hs);
  1870. end;
  1871. val(hs,v,code);
  1872. If code<>0 Then
  1873. InOutRes:=106;
  1874. end;
  1875. procedure fpc_Read_Text_Currency_Iso(var f : Text; out v : Currency); iocheck; compilerproc;
  1876. var
  1877. hs : string;
  1878. code : ValSInt;
  1879. begin
  1880. ReadReal(f,hs);
  1881. Val(hs,v,code);
  1882. If code<>0 Then
  1883. InOutRes:=106;
  1884. end;
  1885. {$ifndef cpu64}
  1886. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1887. var
  1888. hs : String;
  1889. code : longint;
  1890. Begin
  1891. q:=0;
  1892. If not CheckRead(f) then
  1893. exit;
  1894. hs:='';
  1895. if IgnoreSpaces(f) then
  1896. begin
  1897. { When spaces were found and we are now at EOF,
  1898. then we return 0 }
  1899. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1900. exit;
  1901. ReadNumeric(f,hs);
  1902. end;
  1903. val(hs,q,code);
  1904. If code<>0 Then
  1905. InOutRes:=106;
  1906. End;
  1907. procedure fpc_Read_Text_QWord_Iso(var f : text; out q : qword); iocheck; compilerproc;
  1908. var
  1909. hs : String;
  1910. code : longint;
  1911. Begin
  1912. ReadInteger(f,hs);
  1913. Val(hs,q,code);
  1914. If code<>0 Then
  1915. InOutRes:=106;
  1916. End;
  1917. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1918. var
  1919. hs : String;
  1920. code : Longint;
  1921. Begin
  1922. i:=0;
  1923. If not CheckRead(f) then
  1924. exit;
  1925. hs:='';
  1926. if IgnoreSpaces(f) then
  1927. begin
  1928. { When spaces were found and we are now at EOF,
  1929. then we return 0 }
  1930. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1931. exit;
  1932. ReadNumeric(f,hs);
  1933. end;
  1934. Val(hs,i,code);
  1935. If code<>0 Then
  1936. InOutRes:=106;
  1937. End;
  1938. procedure fpc_Read_Text_Int64_Iso(var f : text; out i : int64); iocheck; compilerproc;
  1939. var
  1940. hs : String;
  1941. code : Longint;
  1942. Begin
  1943. ReadInteger(f,hs);
  1944. Val(hs,i,code);
  1945. If code<>0 Then
  1946. InOutRes:=106;
  1947. End;
  1948. {$endif CPU64}
  1949. {$if defined(CPU16) or defined(CPU8)}
  1950. procedure fpc_Read_Text_LongWord(var f : text; out q : longword); iocheck; compilerproc;
  1951. var
  1952. hs : String;
  1953. code : longint;
  1954. Begin
  1955. q:=0;
  1956. If not CheckRead(f) then
  1957. exit;
  1958. hs:='';
  1959. if IgnoreSpaces(f) then
  1960. begin
  1961. { When spaces were found and we are now at EOF,
  1962. then we return 0 }
  1963. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1964. exit;
  1965. ReadNumeric(f,hs);
  1966. end;
  1967. val(hs,q,code);
  1968. If code<>0 Then
  1969. InOutRes:=106;
  1970. End;
  1971. procedure fpc_Read_Text_LongInt(var f : text; out i : longint); iocheck; compilerproc;
  1972. var
  1973. hs : String;
  1974. code : Longint;
  1975. Begin
  1976. i:=0;
  1977. If not CheckRead(f) then
  1978. exit;
  1979. hs:='';
  1980. if IgnoreSpaces(f) then
  1981. begin
  1982. { When spaces were found and we are now at EOF,
  1983. then we return 0 }
  1984. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1985. exit;
  1986. ReadNumeric(f,hs);
  1987. end;
  1988. Val(hs,i,code);
  1989. If code<>0 Then
  1990. InOutRes:=106;
  1991. End;
  1992. {$endif CPU16 or CPU8}
  1993. {*****************************************************************************
  1994. WriteStr/ReadStr
  1995. *****************************************************************************}
  1996. const
  1997. { pointer to target string }
  1998. StrPtrIndex = 1;
  1999. { temporary destination for writerstr, because the original value of the
  2000. destination may be used in the writestr expression }
  2001. TempWriteStrDestIndex = 9;
  2002. ShortStrLenIndex = 17;
  2003. { how many bytes of the string have been processed already (used for readstr) }
  2004. BytesReadIndex = 17;
  2005. procedure WriteStrShort(var t: textrec);
  2006. var
  2007. str: pshortstring;
  2008. newbytes,
  2009. oldlen: longint;
  2010. begin
  2011. if (t.bufpos=0) then
  2012. exit;
  2013. str:=pshortstring(ppointer(@t.userdata[TempWriteStrDestIndex])^);
  2014. newbytes:=t.BufPos;
  2015. oldlen:=length(str^);
  2016. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  2017. begin
  2018. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  2019. {$ifdef writestr_iolencheck}
  2020. // GPC only gives an io error if {$no-truncate-strings} is active
  2021. // FPC does not have this setting (it never gives errors when a
  2022. // a string expression is truncated)
  2023. { "disk full" }
  2024. inoutres:=101;
  2025. {$endif}
  2026. end;
  2027. setlength(str^,length(str^)+newbytes);
  2028. move(t.bufptr^,str^[oldlen+1],newbytes);
  2029. t.bufpos:=0;
  2030. end;
  2031. procedure WriteStrShortFlush(var t: textrec);
  2032. begin
  2033. { move written data from internal buffer to temporary string (don't move
  2034. directly from buffer to final string, because the temporary string may
  2035. already contain data in case the textbuf was smaller than the string
  2036. length) }
  2037. WriteStrShort(t);
  2038. { move written data to original string }
  2039. move(PPointer(@t.userdata[TempWriteStrDestIndex])^^,
  2040. PPointer(@t.userdata[StrPtrIndex])^^,
  2041. t.userdata[ShortStrLenIndex]+1);
  2042. { free temporary buffer }
  2043. freemem(PPointer(@t.userdata[TempWriteStrDestIndex])^);
  2044. end;
  2045. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2046. procedure WriteStrAnsi(var t: textrec);
  2047. var
  2048. str: pansistring;
  2049. oldlen: longint;
  2050. begin
  2051. if (t.bufpos=0) then
  2052. exit;
  2053. str:=pansistring(@t.userdata[TempWriteStrDestIndex]);
  2054. oldlen:=length(str^);
  2055. setlength(str^,oldlen+t.bufpos);
  2056. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  2057. t.bufpos:=0;
  2058. end;
  2059. procedure WriteStrAnsiFlush(var t: textrec);
  2060. begin
  2061. { see comment in WriteStrShortFlush }
  2062. WriteStrAnsi(t);
  2063. pansistring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  2064. pansistring(@t.userdata[TempWriteStrDestIndex])^;
  2065. { free memory/finalize temp }
  2066. pansistring(@t.userdata[TempWriteStrDestIndex])^:='';
  2067. end;
  2068. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2069. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  2070. function EndOfLastCompleteUTF8CodePoint(var t: textrec): SizeInt;
  2071. var
  2072. i, lenfound, codepointlen: sizeint;
  2073. b: byte;
  2074. begin
  2075. lenfound:=0;
  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;