text.inc 68 KB

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