text.inc 68 KB

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