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