text.inc 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  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 unnecessary 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 concatenating 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 concatenating 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. if TextRec(f).mode=fmOutput then
  1654. begin
  1655. Result:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1656. exit;
  1657. end;
  1658. Result:=@TextRec(f).Bufptr^[TextRec(f).BufEnd];
  1659. If not CheckRead(f) then
  1660. exit;
  1661. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1662. exit;
  1663. Result:=@TextRec(f).Bufptr^[TextRec(f).BufPos];
  1664. end;
  1665. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  1666. procedure fpc_Read_Text_WideChar(var f : Text; out wc: widechar); iocheck;compilerproc;
  1667. var
  1668. ws: widestring;
  1669. i: longint;
  1670. { maximum code point length is 6 characters (with UTF-8) }
  1671. str: array[0..5] of AnsiChar;
  1672. Begin
  1673. fillchar(str[0],sizeof(str),0);
  1674. for i:=low(str) to high(str) do
  1675. begin
  1676. fpc_Read_Text_Char_intern(f,str[i]);
  1677. case widestringmanager.CodePointLengthProc(@str[0],i+1) of
  1678. -1: { possibly incomplete code point, try with an extra character }
  1679. ;
  1680. 0: { null character }
  1681. begin
  1682. wc:=#0;
  1683. exit;
  1684. end;
  1685. else
  1686. begin
  1687. { valid code point -> convert to widestring}
  1688. widestringmanager.Ansi2WideMoveProc(@str[0],TextRec(f).CodePage,ws,i+1);
  1689. { has to be exactly one widechar }
  1690. if length(ws)=1 then
  1691. begin
  1692. wc:=ws[1];
  1693. exit
  1694. end
  1695. else
  1696. break;
  1697. end;
  1698. end;
  1699. end;
  1700. { invalid widechar input }
  1701. inoutres:=106;
  1702. end;
  1703. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  1704. procedure fpc_Read_Text_Char_Iso(var f : Text; out c: AnsiChar); iocheck;compilerproc;
  1705. Begin
  1706. c:=' ';
  1707. If not CheckRead(f) then
  1708. exit;
  1709. If TextRec(f).BufPos>=TextRec(f).BufEnd Then
  1710. begin
  1711. c:=' ';
  1712. exit;
  1713. end;
  1714. c:=TextRec(f).Bufptr^[TextRec(f).BufPos];
  1715. inc(TextRec(f).BufPos);
  1716. if c=#13 then
  1717. begin
  1718. c:=' ';
  1719. If not CheckRead(f) or
  1720. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1721. exit;
  1722. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#10 then
  1723. inc(TextRec(f).BufPos);
  1724. { ignore #26 following a new line }
  1725. If not CheckRead(f) or
  1726. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1727. exit;
  1728. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1729. inc(TextRec(f).BufPos);
  1730. end
  1731. else if c=#10 then
  1732. begin
  1733. c:=' ';
  1734. { ignore #26 following a new line }
  1735. If not CheckRead(f) or
  1736. (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1737. exit;
  1738. If TextRec(f).Bufptr^[TextRec(f).BufPos]=#26 then
  1739. inc(TextRec(f).BufPos);
  1740. end
  1741. else if c=#26 then
  1742. c:=' ';
  1743. end;
  1744. Procedure fpc_Read_Text_SInt(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1745. var
  1746. hs : ShortString;
  1747. code : ValSInt;
  1748. Begin
  1749. l:=0;
  1750. If not CheckRead(f) then
  1751. exit;
  1752. hs:='';
  1753. if IgnoreSpaces(f) then
  1754. begin
  1755. { When spaces were found and we are now at EOF,
  1756. then we return 0 }
  1757. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1758. exit;
  1759. if CtrlZMarksEOF and (TextRec(f).Bufptr^[TextRec(f).BufPos]=#26) then
  1760. exit;
  1761. ReadNumeric(f,hs);
  1762. end;
  1763. if (Length(hs)=0) then
  1764. L := 0
  1765. else
  1766. begin
  1767. Val(hs,l,code);
  1768. if Code <> 0 then
  1769. InOutRes:=106;
  1770. end;
  1771. End;
  1772. Procedure fpc_Read_Text_SInt_Iso(var f : Text; out l : ValSInt); iocheck; compilerproc;
  1773. var
  1774. hs : ShortString;
  1775. code : ValSInt;
  1776. Begin
  1777. l:=0;
  1778. if not CheckRead(f) then
  1779. Exit;
  1780. ReadInteger(f,hs);
  1781. Val(hs,l,code);
  1782. if Code <> 0 then
  1783. InOutRes:=106;
  1784. End;
  1785. Procedure fpc_Read_Text_UInt(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1786. var
  1787. hs : ShortString;
  1788. code : ValSInt;
  1789. Begin
  1790. u:=0;
  1791. If not CheckRead(f) then
  1792. exit;
  1793. hs:='';
  1794. if IgnoreSpaces(f) then
  1795. begin
  1796. { When spaces were found and we are now at EOF,
  1797. then we return 0 }
  1798. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1799. exit;
  1800. ReadNumeric(f,hs);
  1801. end;
  1802. if (Length(hs)=0) then
  1803. u := 0
  1804. else
  1805. begin
  1806. val(hs,u,code);
  1807. If code<>0 Then
  1808. InOutRes:=106;
  1809. end;
  1810. End;
  1811. Procedure fpc_Read_Text_UInt_Iso(var f : Text; out u : ValUInt); iocheck; compilerproc;
  1812. var
  1813. hs : ShortString;
  1814. code : ValSInt;
  1815. Begin
  1816. u:=0;
  1817. if not CheckRead(f) then
  1818. Exit;
  1819. ReadInteger(f,hs);
  1820. Val(hs,u,code);
  1821. If code<>0 Then
  1822. InOutRes:=106;
  1823. End;
  1824. {$ifndef FPUNONE}
  1825. procedure fpc_Read_Text_Float(var f : Text; out v : ValReal); iocheck; compilerproc;
  1826. var
  1827. hs : shortstring;
  1828. code : Word;
  1829. begin
  1830. v:=0.0;
  1831. If not CheckRead(f) then
  1832. exit;
  1833. hs:='';
  1834. if IgnoreSpaces(f) then
  1835. begin
  1836. { When spaces were found and we are now at EOF,
  1837. then we return 0 }
  1838. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1839. exit;
  1840. ReadNumeric(f,hs);
  1841. end;
  1842. val(hs,v,code);
  1843. If code<>0 Then
  1844. InOutRes:=106;
  1845. end;
  1846. procedure fpc_Read_Text_Float_Iso(var f : Text; out v : ValReal); iocheck; compilerproc;
  1847. var
  1848. hs : shortstring;
  1849. code : Word;
  1850. begin
  1851. v:=0.0;
  1852. if not CheckRead(f) then
  1853. Exit;
  1854. ReadReal(f,hs);
  1855. Val(hs,v,code);
  1856. If code<>0 Then
  1857. InOutRes:=106;
  1858. end;
  1859. {$endif}
  1860. {$ifdef VER3_2}
  1861. procedure fpc_read_text_enum(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1862. {$else VER3_2}
  1863. procedure fpc_read_text_enum_longint(str2ordindex:pointer;var t:text;out ordinal:longint); iocheck;compilerproc;
  1864. {$endif VER3_2}
  1865. var s:shortstring;
  1866. code:valsint;
  1867. begin
  1868. if not checkread(t) then
  1869. exit;
  1870. s:='';
  1871. if ignorespaces(t) then
  1872. begin
  1873. { When spaces were found and we are now at EOF, then we return 0 }
  1874. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1875. exit;
  1876. ReadNumeric(t,s);
  1877. end;
  1878. ordinal:=fpc_val_enum_shortstr(str2ordindex,s,code);
  1879. if code<>0 then
  1880. InOutRes:=106;
  1881. end;
  1882. {$ifndef VER3_2}
  1883. procedure fpc_read_text_enum_smallint(str2ordindex:pointer;var t:text;out ordinal:smallint); iocheck;compilerproc;
  1884. var s:shortstring;
  1885. code:valsint;
  1886. begin
  1887. if not checkread(t) then
  1888. exit;
  1889. s:='';
  1890. if ignorespaces(t) then
  1891. begin
  1892. { When spaces were found and we are now at EOF, then we return 0 }
  1893. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1894. exit;
  1895. ReadNumeric(t,s);
  1896. end;
  1897. ordinal:=smallint(fpc_val_enum_shortstr(str2ordindex,s,code));
  1898. if code<>0 then
  1899. InOutRes:=106;
  1900. end;
  1901. procedure fpc_read_text_enum_shortint(str2ordindex:pointer;var t:text;out ordinal:shortint); iocheck;compilerproc;
  1902. var s:shortstring;
  1903. code:valsint;
  1904. begin
  1905. if not checkread(t) then
  1906. exit;
  1907. s:='';
  1908. if ignorespaces(t) then
  1909. begin
  1910. { When spaces were found and we are now at EOF, then we return 0 }
  1911. if (TextRec(t).BufPos>=TextRec(t).BufEnd) then
  1912. exit;
  1913. ReadNumeric(t,s);
  1914. end;
  1915. ordinal:=shortint(fpc_val_enum_shortstr(str2ordindex,s,code));
  1916. if code<>0 then
  1917. InOutRes:=106;
  1918. end;
  1919. {$endif VER3_2}
  1920. procedure fpc_Read_Text_Currency(var f : Text; out v : Currency); iocheck; compilerproc;
  1921. var
  1922. hs : shortstring;
  1923. code : ValSInt;
  1924. begin
  1925. {$ifdef FPUNONE}
  1926. v:=0;
  1927. {$else}
  1928. v:=0.0;
  1929. {$endif}
  1930. If not CheckRead(f) then
  1931. exit;
  1932. hs:='';
  1933. if IgnoreSpaces(f) then
  1934. begin
  1935. { When spaces were found and we are now at EOF,
  1936. then we return 0 }
  1937. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1938. exit;
  1939. ReadNumeric(f,hs);
  1940. end;
  1941. val(hs,v,code);
  1942. If code<>0 Then
  1943. InOutRes:=106;
  1944. end;
  1945. procedure fpc_Read_Text_Currency_Iso(var f : Text; out v : Currency); iocheck; compilerproc;
  1946. var
  1947. hs : shortstring;
  1948. code : ValSInt;
  1949. begin
  1950. v:=0;
  1951. if not CheckRead(f) then
  1952. Exit;
  1953. ReadReal(f,hs);
  1954. Val(hs,v,code);
  1955. If code<>0 Then
  1956. InOutRes:=106;
  1957. end;
  1958. {$ifndef cpu64}
  1959. procedure fpc_Read_Text_QWord(var f : text; out q : qword); iocheck; compilerproc;
  1960. var
  1961. hs : ShortString;
  1962. code : longint;
  1963. Begin
  1964. q:=0;
  1965. If not CheckRead(f) then
  1966. exit;
  1967. hs:='';
  1968. if IgnoreSpaces(f) then
  1969. begin
  1970. { When spaces were found and we are now at EOF,
  1971. then we return 0 }
  1972. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  1973. exit;
  1974. ReadNumeric(f,hs);
  1975. end;
  1976. val(hs,q,code);
  1977. If code<>0 Then
  1978. InOutRes:=106;
  1979. End;
  1980. procedure fpc_Read_Text_QWord_Iso(var f : text; out q : qword); iocheck; compilerproc;
  1981. var
  1982. hs : ShortString;
  1983. code : longint;
  1984. Begin
  1985. q:=0;
  1986. if not CheckRead(f) then
  1987. Exit;
  1988. ReadInteger(f,hs);
  1989. Val(hs,q,code);
  1990. If code<>0 Then
  1991. InOutRes:=106;
  1992. End;
  1993. procedure fpc_Read_Text_Int64(var f : text; out i : int64); iocheck; compilerproc;
  1994. var
  1995. hs : ShortString;
  1996. code : Longint;
  1997. Begin
  1998. i:=0;
  1999. If not CheckRead(f) then
  2000. exit;
  2001. hs:='';
  2002. if IgnoreSpaces(f) then
  2003. begin
  2004. { When spaces were found and we are now at EOF,
  2005. then we return 0 }
  2006. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  2007. exit;
  2008. ReadNumeric(f,hs);
  2009. end;
  2010. Val(hs,i,code);
  2011. If code<>0 Then
  2012. InOutRes:=106;
  2013. End;
  2014. procedure fpc_Read_Text_Int64_Iso(var f : text; out i : int64); iocheck; compilerproc;
  2015. var
  2016. hs : ShortString;
  2017. code : Longint;
  2018. Begin
  2019. i:=0;
  2020. if not CheckRead(f) then
  2021. Exit;
  2022. ReadInteger(f,hs);
  2023. Val(hs,i,code);
  2024. If code<>0 Then
  2025. InOutRes:=106;
  2026. End;
  2027. {$endif CPU64}
  2028. {$if defined(CPU16) or defined(CPU8)}
  2029. procedure fpc_Read_Text_LongWord(var f : text; out q : longword); iocheck; compilerproc;
  2030. var
  2031. hs : ShortString;
  2032. code : longint;
  2033. Begin
  2034. q:=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,q,code);
  2047. If code<>0 Then
  2048. InOutRes:=106;
  2049. End;
  2050. procedure fpc_Read_Text_LongInt(var f : text; out i : longint); iocheck; compilerproc;
  2051. var
  2052. hs : ShortString;
  2053. code : Longint;
  2054. Begin
  2055. i:=0;
  2056. If not CheckRead(f) then
  2057. exit;
  2058. hs:='';
  2059. if IgnoreSpaces(f) then
  2060. begin
  2061. { When spaces were found and we are now at EOF,
  2062. then we return 0 }
  2063. if (TextRec(f).BufPos>=TextRec(f).BufEnd) then
  2064. exit;
  2065. ReadNumeric(f,hs);
  2066. end;
  2067. Val(hs,i,code);
  2068. If code<>0 Then
  2069. InOutRes:=106;
  2070. End;
  2071. {$endif CPU16 or CPU8}
  2072. {*****************************************************************************
  2073. WriteStr/ReadStr
  2074. *****************************************************************************}
  2075. const
  2076. { pointer to target shortstring }
  2077. StrPtrIndex = 1;
  2078. { temporary destination for writerstr, because the original value of the
  2079. destination may be used in the writestr expression }
  2080. TempWriteStrDestIndex = 9;
  2081. ShortStrLenIndex = 17;
  2082. { how many bytes of the shortstring have been processed already (used for readstr) }
  2083. BytesReadIndex = 17;
  2084. procedure WriteStrShort(var t: textrec);
  2085. var
  2086. str: pshortstring;
  2087. newbytes,
  2088. oldlen: longint;
  2089. begin
  2090. if (t.bufpos=0) then
  2091. exit;
  2092. str:=pshortstring(ppointer(@t.userdata[TempWriteStrDestIndex])^);
  2093. newbytes:=t.BufPos;
  2094. oldlen:=length(str^);
  2095. if (oldlen+t.bufpos > t.userdata[ShortStrLenIndex]) then
  2096. begin
  2097. newbytes:=t.userdata[ShortStrLenIndex]-oldlen;
  2098. {$ifdef writestr_iolencheck}
  2099. // GPC only gives an io error if {$no-truncate-strings} is active
  2100. // FPC does not have this setting (it never gives errors when a
  2101. // a shortstring expression is truncated)
  2102. { "disk full" }
  2103. inoutres:=101;
  2104. {$endif}
  2105. end;
  2106. setlength(str^,length(str^)+newbytes);
  2107. move(t.bufptr^,str^[oldlen+1],newbytes);
  2108. t.bufpos:=0;
  2109. end;
  2110. procedure WriteStrShortFlush(var t: textrec);
  2111. begin
  2112. { move written data from internal buffer to temporary shortstring (don't move
  2113. directly from buffer to final shortstring, because the temporary shortstring may
  2114. already contain data in case the textbuf was smaller than the shortstring
  2115. length) }
  2116. WriteStrShort(t);
  2117. { move written data to original shortstring }
  2118. move(PPointer(@t.userdata[TempWriteStrDestIndex])^^,
  2119. PPointer(@t.userdata[StrPtrIndex])^^,
  2120. t.userdata[ShortStrLenIndex]+1);
  2121. { free temporary buffer }
  2122. freemem(PPointer(@t.userdata[TempWriteStrDestIndex])^);
  2123. end;
  2124. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2125. procedure WriteStrAnsi(var t: textrec);
  2126. var
  2127. str: pansistring;
  2128. oldlen: longint;
  2129. begin
  2130. if (t.bufpos=0) then
  2131. exit;
  2132. str:=pansistring(@t.userdata[TempWriteStrDestIndex]);
  2133. oldlen:=length(str^);
  2134. setlength(str^,oldlen+t.bufpos);
  2135. move(t.bufptr^,str^[oldlen+1],t.bufpos);
  2136. t.bufpos:=0;
  2137. end;
  2138. procedure WriteStrAnsiFlush(var t: textrec);
  2139. begin
  2140. { see comment in WriteStrShortFlush }
  2141. WriteStrAnsi(t);
  2142. pansistring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  2143. pansistring(@t.userdata[TempWriteStrDestIndex])^;
  2144. { free memory/finalize temp }
  2145. pansistring(@t.userdata[TempWriteStrDestIndex])^:='';
  2146. end;
  2147. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2148. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  2149. function EndOfLastCompleteUTF8CodePoint(var t: textrec): SizeInt;
  2150. var
  2151. i, codepointlen: sizeint;
  2152. begin
  2153. for i:=t.bufpos-1 downto 0 do
  2154. begin
  2155. { we don't care about combining diacritical marks here: we just want a
  2156. valid UTF-8 codepoint that we can translate to UTF-16. The combining
  2157. diacritical marks can be translated separately }
  2158. codepointlen:=Utf8CodePointLen(pansichar(@t.bufptr^[i]),(t.bufpos-1-i)+1,false);
  2159. { complete codepoint -> flush till here }
  2160. if codepointlen>0 then
  2161. begin
  2162. result:=i+codepointlen;
  2163. exit;
  2164. end
  2165. end;
  2166. { all invalid data, or the buffer is too small to be able to deal with the
  2167. complete utf8char -> nothing else to do but to handle the entire buffer
  2168. (and end up with a partial/invalid character) }
  2169. result:=t.bufpos;
  2170. end;
  2171. procedure WriteStrUnicodeIntern(var t: textrec; flush: boolean);
  2172. var
  2173. temp: unicodestring;
  2174. str: punicodestring;
  2175. validend: SizeInt;
  2176. begin
  2177. if (t.bufpos=0) then
  2178. exit;
  2179. str:=punicodestring(@t.userdata[TempWriteStrDestIndex]);
  2180. if not flush then
  2181. validend:=EndOfLastCompleteUTF8CodePoint(t)
  2182. else
  2183. validend:=t.bufpos;
  2184. widestringmanager.Ansi2UnicodeMoveProc(@t.bufptr^[0],CP_UTF8,temp,validend);
  2185. str^:=str^+temp;
  2186. dec(t.bufpos,validend);
  2187. { move remainder to the start }
  2188. if t.bufpos<>0 then
  2189. move(t.bufptr^[validend],t.bufptr^[0],t.bufpos);
  2190. end;
  2191. procedure WriteStrUnicode(var t: textrec);
  2192. begin
  2193. WriteStrUnicodeIntern(t,false);
  2194. end;
  2195. procedure WriteStrUnicodeFlush(var t: textrec);
  2196. begin
  2197. { see comment in WriteStrShortFlush }
  2198. WriteStrUnicodeIntern(t,true);
  2199. punicodestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  2200. punicodestring(@t.userdata[TempWriteStrDestIndex])^;
  2201. { free memory/finalize temp }
  2202. punicodestring(@t.userdata[TempWriteStrDestIndex])^:='';
  2203. end;
  2204. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  2205. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2206. procedure WriteStrWideIntern(var t: textrec; flush: boolean);
  2207. var
  2208. temp: unicodestring;
  2209. str: pwidestring;
  2210. validend: SizeInt;
  2211. begin
  2212. if (t.bufpos=0) then
  2213. exit;
  2214. str:=pwidestring(@t.userdata[TempWriteStrDestIndex]);
  2215. if not flush then
  2216. validend:=EndOfLastCompleteUTF8CodePoint(t)
  2217. else
  2218. validend:=t.bufpos;
  2219. widestringmanager.Ansi2UnicodeMoveProc(@t.bufptr^[0],CP_UTF8,temp,validend);
  2220. str^:=str^+temp;
  2221. dec(t.bufpos,validend);
  2222. { move remainder to the start }
  2223. if t.bufpos<>0 then
  2224. move(t.bufptr^[validend],t.bufptr^[0],t.bufpos);
  2225. end;
  2226. procedure WriteStrWide(var t: textrec);
  2227. begin
  2228. WriteStrUnicodeIntern(t,false);
  2229. end;
  2230. procedure WriteStrWideFlush(var t: textrec);
  2231. begin
  2232. { see comment in WriteStrShortFlush }
  2233. WriteStrWideIntern(t,true);
  2234. pwidestring(ppointer(@t.userdata[StrPtrIndex])^)^:=
  2235. pwidestring(@t.userdata[TempWriteStrDestIndex])^;
  2236. { free memory/finalize temp }
  2237. finalize(pwidestring(@t.userdata[TempWriteStrDestIndex])^);
  2238. end;
  2239. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2240. procedure SetupWriteStrCommon(out t: textrec; cp: TSystemCodePage);
  2241. begin
  2242. // initialise
  2243. Assign(text(t),'');
  2244. t.mode:=fmOutput;
  2245. t.OpenFunc:=nil;
  2246. t.CloseFunc:=nil;
  2247. {$if defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  2248. t.CodePage:=TranslatePlaceholderCP(cp);
  2249. {$endif}
  2250. end;
  2251. procedure fpc_SetupWriteStr_Shortstr(var ReadWriteStrText: text; var s: shortstring); compilerproc;
  2252. begin
  2253. SetupWriteStrCommon(TextRec(ReadWriteStrText),DefaultSystemCodePage);
  2254. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2255. { temporary destination (see comments for TempWriteStrDestIndex) }
  2256. getmem(PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^,high(s)+1);
  2257. setlength(pshortstring(ppointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^)^,0);
  2258. TextRec(ReadWriteStrText).userdata[ShortStrLenIndex]:=high(s);
  2259. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrShort;
  2260. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrShortFlush;
  2261. end;
  2262. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2263. procedure fpc_SetupWriteStr_Ansistr(var ReadWriteStrText: text; var s: ansistring; cp: TSystemCodePage); compilerproc;
  2264. begin
  2265. { destination rawbytestring -> use CP_ACP }
  2266. if cp=CP_NONE then
  2267. cp:=CP_ACP;
  2268. SetupWriteStrCommon(TextRec(ReadWriteStrText),cp);
  2269. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2270. { temp destination ansistring, nil = empty shortstring }
  2271. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  2272. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrAnsi;
  2273. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrAnsiFlush;
  2274. end;
  2275. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2276. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  2277. procedure fpc_SetupWriteStr_Unicodestr(var ReadWriteStrText: text; var s: unicodestring); compilerproc;
  2278. begin
  2279. SetupWriteStrCommon(TextRec(ReadWriteStrText),CP_UTF8);
  2280. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2281. { temp destination unicodestring, nil = empty shortstring }
  2282. PPointer(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:=nil;
  2283. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrUnicode;
  2284. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrUnicodeFlush;
  2285. end;
  2286. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  2287. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2288. procedure fpc_SetupWriteStr_Widestr(var ReadWriteStrText: text; var s: widestring); compilerproc;
  2289. begin
  2290. SetupWriteStrCommon(TextRec(ReadWriteStrText),CP_UTF8);
  2291. PPointer(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=@s;
  2292. { temp destination widestring }
  2293. PWideString(@TextRec(ReadWriteStrText).userdata[TempWriteStrDestIndex])^:='';
  2294. TextRec(ReadWriteStrText).InOutFunc:=@WriteStrWide;
  2295. TextRec(ReadWriteStrText).FlushFunc:=@WriteStrWideFlush;
  2296. end;
  2297. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2298. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2299. procedure ReadAnsiStrFinal(var t: textrec);
  2300. begin
  2301. { finalise the temp ansistring }
  2302. PAnsiString(@t.userdata[StrPtrIndex])^ := '';
  2303. end;
  2304. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2305. procedure ReadStrCommon(var t: textrec; strdata: pansichar; len: sizeint);
  2306. var
  2307. newbytes: sizeint;
  2308. begin
  2309. newbytes := len - PSizeInt(@t.userdata[BytesReadIndex])^;
  2310. if (t.BufSize <= newbytes) then
  2311. newbytes := t.BufSize;
  2312. if (newbytes > 0) then
  2313. begin
  2314. move(strdata[PSizeInt(@t.userdata[BytesReadIndex])^],t.BufPtr^,newbytes);
  2315. inc(PSizeInt(@t.userdata[BytesReadIndex])^,newbytes);
  2316. end;
  2317. t.BufEnd:=newbytes;
  2318. t.BufPos:=0;
  2319. end;
  2320. procedure ReadStrAnsi(var t: textrec);
  2321. var
  2322. str: pansistring;
  2323. begin
  2324. str:=pansistring(@t.userdata[StrPtrIndex]);
  2325. ReadStrCommon(t,@str^[1],length(str^));
  2326. end;
  2327. procedure SetupReadStrCommon(out t: textrec; cp: TSystemCodePage);
  2328. begin
  2329. // initialise
  2330. Assign(text(t),'');
  2331. t.mode:=fmInput;
  2332. t.OpenFunc:=nil;
  2333. t.CloseFunc:=nil;
  2334. {$if defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  2335. t.CodePage:=TranslatePlaceholderCP(cp);
  2336. {$endif}
  2337. PSizeInt(@t.userdata[BytesReadIndex])^:=0;
  2338. end;
  2339. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2340. procedure fpc_SetupReadStr_Ansistr(var ReadWriteStrText: text; const s: ansistring); [public, alias: 'FPC_SETUPREADSTR_ANSISTR']; compilerproc;
  2341. begin
  2342. SetupReadStrCommon(TextRec(ReadWriteStrText),StringCodePage(s));
  2343. { we need a reference, because 's' may be a temporary expression }
  2344. PAnsiString(@TextRec(ReadWriteStrText).userdata[StrPtrIndex])^:=s;
  2345. TextRec(ReadWriteStrText).InOutFunc:=@ReadStrAnsi;
  2346. { this is called at the end, by fpc_read_end }
  2347. TextRec(ReadWriteStrText).FlushFunc:=@ReadAnsiStrFinal;
  2348. end;
  2349. procedure fpc_SetupReadStr_Ansistr_Intern(var ReadWriteStrText: text; const s: rawbytestring); [external name 'FPC_SETUPREADSTR_ANSISTR'];
  2350. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2351. procedure fpc_SetupReadStr_Shortstr(var ReadWriteStrText: text; const s: shortstring); compilerproc;
  2352. begin
  2353. { the reason we convert the shortstring to ansistring, is because the semantics of
  2354. readstr are defined as:
  2355. *********************
  2356. Apart from the restrictions imposed by requirements given in this clause,
  2357. the execution of readstr(e,v 1 ,...,v n ) where e denotes a
  2358. shortstring-expression and v 1 ,...,v n denote variable-accesses possessing the
  2359. AnsiChar-type (or a subrange of AnsiChar-type), the integer-type (or a subrange of
  2360. integer-type), the real-type, a fixed-string-type, or a
  2361. variable-string-type, shall be equivalent to
  2362. begin
  2363. rewrite(f);
  2364. writeln(f, e);
  2365. reset(f);
  2366. read(f, v 1 ,...,v n )
  2367. end
  2368. *********************
  2369. This means that any side effects caused by the evaluation of v 1 .. v n
  2370. must not affect the value of e (= our argument s) -> we need a copy of it.
  2371. An ansistring is the easiest way to get a threadsafe copy, and allows us
  2372. to use the other ansistring readstr helpers too.
  2373. }
  2374. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  2375. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,s);
  2376. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  2377. runerror(217);
  2378. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  2379. end;
  2380. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  2381. procedure fpc_SetupReadStr_Unicodestr(var ReadWriteStrText: text; const s: unicodestring); compilerproc;
  2382. begin
  2383. { we use an utf8string to avoid code duplication }
  2384. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,utf8string(s));
  2385. end;
  2386. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  2387. {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2388. procedure fpc_SetupReadStr_Widestr(var ReadWriteStrText: text; const s: widestring); compilerproc;
  2389. begin
  2390. { we use an utf8string to avoid code duplication }
  2391. fpc_SetupReadStr_Ansistr_Intern(ReadWriteStrText,utf8string(s));
  2392. end;
  2393. {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
  2394. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  2395. Function GetFullName(var t:Text) : UnicodeString;
  2396. begin
  2397. {$ifdef USE_FILEREC_FULLNAME}
  2398. if Assigned(TextRec(t).FullName) then
  2399. Result:=UnicodeString(TextRec(t).FullName)
  2400. else
  2401. {$endif USE_FILEREC_FULLNAME}
  2402. Result:=PFileTextRecChar(@TextRec(t).Name);
  2403. end;
  2404. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  2405. {*****************************************************************************
  2406. Initializing
  2407. *****************************************************************************}
  2408. procedure OpenStdIO(var f:text;mode:longint;hdl:thandle);
  2409. begin
  2410. Assign(f,'');
  2411. TextRec(f).Handle:=hdl;
  2412. TextRec(f).Mode:=mode;
  2413. TextRec(f).Closefunc:=@FileCloseFunc;
  2414. case mode of
  2415. fmInput :
  2416. begin
  2417. TextRec(f).InOutFunc:=@FileReadFunc;
  2418. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  2419. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleInput);
  2420. {$endif}
  2421. end;
  2422. fmOutput :
  2423. begin
  2424. TextRec(f).InOutFunc:=@FileWriteFunc;
  2425. {$if defined(FPC_HAS_FEATURE_WIDESTRINGS)}
  2426. TextRec(f).CodePage:=WideStringManager.GetStandardCodePageProc(scpConsoleOutput);
  2427. {$endif}
  2428. if Do_Isdevice(hdl) then
  2429. TextRec(f).FlushFunc:=@FileWriteFunc;
  2430. end;
  2431. else
  2432. HandleError(102);
  2433. end;
  2434. end;