text.inc 67 KB

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