text.inc 69 KB

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