text.inc 68 KB

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