text.inc 67 KB

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