text.inc 68 KB

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