text.inc 69 KB

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