text.inc 68 KB

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