text.inc 69 KB

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