text.inc 69 KB

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