text.inc 70 KB

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