text.inc 68 KB

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