text.inc 69 KB

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