text.inc 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 1993,97 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. Possible Defines:
  13. EXTENDED_EOF Use extended EOF checking for textfile, necessary for
  14. Pipes and Sockets under Linux
  15. EOF_CTRLZ Is Ctrl-Z (#26) a EOF mark for textfiles
  16. SHORT_LINEBREAK Use short Linebreaks #10 instead of #10#13
  17. Both EXTENDED_EOF and SHORT_LINEBREAK are defined in the Linux system
  18. unit (syslinux.pp)
  19. }
  20. {****************************************************************************
  21. subroutines For TextFile handling
  22. ****************************************************************************}
  23. Procedure FileCloseFunc(Var t:TextRec);
  24. Begin
  25. Do_Close(t.Handle);
  26. t.Handle:=UnusedHandle;
  27. End;
  28. Procedure FileReadFunc(var t:TextRec);
  29. Begin
  30. t.BufEnd:=Do_Read(t.Handle,Longint(t.Bufptr),t.BufSize);
  31. t.BufPos:=0;
  32. End;
  33. Procedure FileWriteFunc(var t:TextRec);
  34. Begin
  35. Do_Write(t.Handle,Longint(t.Bufptr),t.BufPos);
  36. t.BufPos:=0;
  37. End;
  38. Procedure FileOpenFunc(var t:TextRec);
  39. var
  40. Flags : Longint;
  41. Begin
  42. Case t.mode Of
  43. fmInput : Flags:=$1000;
  44. fmOutput : Flags:=$1101;
  45. fmAppend : Flags:=$1011;
  46. else
  47. RunError(102);
  48. End;
  49. Do_Open(t,PChar(@t.Name),Flags);
  50. t.CloseFunc:=@FileCloseFunc;
  51. t.FlushFunc:=nil;
  52. if t.Mode=fmInput then
  53. t.InOutFunc:=@FileReadFunc
  54. else
  55. begin
  56. t.InOutFunc:=@FileWriteFunc;
  57. { Only install flushing if its a NOT a file }
  58. if Do_Isdevice(t.Handle) then
  59. t.FlushFunc:=@FileWriteFunc;
  60. end;
  61. End;
  62. Procedure assign(var t:Text;const s:String);
  63. Begin
  64. FillChar(t,SizEof(TextRec),0);
  65. { only set things that are not zero }
  66. TextRec(t).Handle:=UnusedHandle;
  67. TextRec(t).mode:=fmClosed;
  68. TextRec(t).BufSize:=128;
  69. TextRec(t).Bufptr:=@TextRec(t).Buffer;
  70. TextRec(t).OpenFunc:=@FileOpenFunc;
  71. Move(s[1],TextRec(t).Name,Length(s));
  72. End;
  73. Procedure assign(var t:Text;p:pchar);
  74. begin
  75. Assign(t,StrPas(p));
  76. end;
  77. Procedure assign(var t:Text;c:char);
  78. begin
  79. Assign(t,string(c));
  80. end;
  81. Procedure Close(var t : Text);[Public,Alias: 'CLOSE_TEXT',IOCheck];
  82. Begin
  83. if InOutRes <> 0 then Exit;
  84. If (TextRec(t).mode<>fmClosed) Then
  85. Begin
  86. { Write pending buffer }
  87. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  88. TextRec(t).mode:=fmClosed;
  89. { Only close functions not connected to stdout.}
  90. If ((TextRec(t).Handle<>StdInputHandle) or
  91. (TextRec(t).Handle<>StdOutputHandle) or
  92. (TextRec(t).Handle<>StdErrorHandle)) Then
  93. FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  94. End;
  95. End;
  96. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  97. Begin
  98. Case TextRec(t).mode Of {This gives the fastest code}
  99. fmInput,fmOutput,fmInOut : Close(t);
  100. fmClosed : ;
  101. else
  102. Begin
  103. InOutRes:=102;
  104. exit;
  105. End;
  106. End;
  107. TextRec(t).mode:=word(mode);
  108. FileFunc(TextRec(t).OpenFunc)(TextRec(t))
  109. End;
  110. Procedure Rewrite(var t : Text);[IOCheck];
  111. Begin
  112. If InOutRes <> 0 then exit;
  113. OpenText(t,fmOutput,1);
  114. End;
  115. Procedure Reset(var t : Text);[IOCheck];
  116. Begin
  117. If InOutRes <> 0 then exit;
  118. OpenText(t,fmInput,0);
  119. End;
  120. Procedure Append(var t : Text);[IOCheck];
  121. Begin
  122. If InOutRes <> 0 then exit;
  123. OpenText(t,fmAppend,1);
  124. End;
  125. Procedure Flush(var t : Text);[IOCheck];
  126. Begin
  127. If InOutRes <> 0 then exit;
  128. If TextRec(t).mode<>fmOutput Then
  129. exit;
  130. { Not the flushfunc but the inoutfunc should be used, becuase that
  131. writes the data, flushfunc doesn't need to be assigned }
  132. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  133. End;
  134. Procedure Erase(var t:Text);[IOCheck];
  135. Begin
  136. If InOutRes <> 0 then exit;
  137. If TextRec(t).mode=fmClosed Then
  138. Do_Erase(PChar(@TextRec(t).Name));
  139. End;
  140. Procedure Rename(var t : text;p:pchar);[IOCheck];
  141. Begin
  142. If InOutRes <> 0 then exit;
  143. If TextRec(t).mode=fmClosed Then
  144. Begin
  145. Do_Rename(PChar(@TextRec(t).Name),p);
  146. Move(p,TextRec(t).Name,StrLen(p)+1);
  147. End;
  148. End;
  149. Procedure Rename(var t : Text;const s : string);[IOCheck];
  150. var
  151. p : array[0..255] Of Char;
  152. Begin
  153. If InOutRes <> 0 then exit;
  154. Move(s[1],p,Length(s));
  155. p[Length(s)]:=#0;
  156. Rename(t,Pchar(@p));
  157. End;
  158. Procedure Rename(var t : Text;c : char);[IOCheck];
  159. var
  160. p : array[0..1] Of Char;
  161. Begin
  162. If InOutRes <> 0 then exit;
  163. p[0]:=c;
  164. p[1]:=#0;
  165. Rename(t,Pchar(@p));
  166. End;
  167. Function Eof(Var t: Text): Boolean;[IOCheck];
  168. Begin
  169. If InOutRes <> 0 then exit;
  170. {$IFNDEF EXTENDED_EOF}
  171. {$IFDEF EOF_CTRLZ}
  172. Eof:=TextRec(t).Buffer[TextRec(t).BufPos]=#26;
  173. If Eof Then
  174. Exit;
  175. {$ENDIF EOL_CTRLZ}
  176. Eof:=(Do_FileSize(TextRec(t).Handle)<=Do_FilePos(TextRec(t).Handle));
  177. If Eof Then
  178. Eof:=TextRec(t).BufEnd <= TextRec(t).BufPos;
  179. {$ELSE EXTENDED_EOF}
  180. { The previous method will NOT work on stdin and pipes or sockets.
  181. So how to do it ?
  182. 1) Check if characters in buffer - Yes ? Eof=false;
  183. 2) Read buffer full. If 0 Chars Read : Eof !
  184. Michael.}
  185. If TextRec(T).mode=fmClosed Then { Sanity Check }
  186. Begin
  187. Eof:=True;
  188. Exit;
  189. End;
  190. If (TextRec(T).BufPos < TextRec(T).BufEnd) Then
  191. Begin
  192. Eof:=False;
  193. Exit
  194. End;
  195. TextRec(T).BufPos:=0;
  196. TextRec(T).BufEnd:=Do_Read(TextRec(T).Handle,Longint(TextRec(T).BufPtr),TextRec(T).BufSize);
  197. If TextRec(T).BufEnd<0 Then
  198. TextRec(T).BufEnd:=0;
  199. Eof:=(TextRec(T).BufEnd=0);
  200. {$ENDIF EXTENDED_EOF}
  201. End;
  202. Function Eof:Boolean;
  203. Begin
  204. Eof:=Eof(Input);
  205. End;
  206. Function SeekEof (Var F : Text) : Boolean;
  207. Var
  208. TR : ^TextRec;
  209. Temp : Longint;
  210. Begin
  211. TR:=@TextRec(f);
  212. If TR^.mode<>fmInput Then exit (true);
  213. SeekEof:=True;
  214. {No data in buffer ? Fill it }
  215. If TR^.BufPos>=TR^.BufEnd Then
  216. FileFunc(TR^.InOutFunc)(TR^);
  217. Temp:=TR^.BufPos;
  218. while (TR^.BufPos<TR^.BufEnd) Do
  219. Begin
  220. If (TR^.Bufptr^[Temp] In [#9,#10,#13,' ']) Then
  221. Inc(Temp)
  222. else
  223. Begin
  224. SeekEof:=False;
  225. TR^.BufPos:=Temp;
  226. exit;
  227. End;
  228. If Temp>=TR^.BufEnd Then
  229. Begin
  230. FileFunc(TR^.InOutFunc)(TR^);
  231. Temp:=TR^.BufPos+1;
  232. End;
  233. End;
  234. End;
  235. Function SeekEof : Boolean;
  236. Begin
  237. SeekEof:=SeekEof(Input);
  238. End;
  239. Function Eoln(var t:Text) : Boolean;
  240. Begin
  241. { maybe we need new data }
  242. If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  243. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  244. Eoln:=Eof(t) or (TextRec(t).Bufptr^[TextRec(t).BufPos] In [#10,#13]);
  245. End;
  246. Function Eoln : Boolean;
  247. Begin
  248. Eoln:=Eoln(Input);
  249. End;
  250. Function SeekEoln (Var F : Text) : Boolean;
  251. Var
  252. TR : ^TextRec;
  253. Temp : Longint;
  254. Begin
  255. TR:=@TextRec(f);
  256. If TR^.mode<>fmInput Then
  257. exit (true);
  258. SeekEoln:=True;
  259. {No data in buffer ? Fill it }
  260. If TR^.BufPos>=TR^.BufEnd Then
  261. FileFunc(TR^.InOutFunc)(TR^);
  262. Temp:=TR^.BufPos;
  263. while (TR^.BufPos<TR^.BufEnd) Do
  264. Begin
  265. Case (TR^.Bufptr^[Temp]) Of
  266. #10 : Exit;
  267. #9,' ' : Inc(Temp)
  268. else
  269. Begin
  270. SeekEoln:=False;
  271. TR^.BufPos:=Temp;
  272. exit;
  273. End;
  274. End;
  275. If Temp>=TR^.BufEnd Then
  276. Begin
  277. FileFunc(TR^.InOutFunc)(TR^);
  278. Temp:=TR^.BufPos+1;
  279. End;
  280. End;
  281. End;
  282. Function SeekEoln : Boolean;
  283. Begin
  284. SeekEoln:=SeekEoln(Input);
  285. End;
  286. Procedure SetTextBuf(Var F : Text; Var Buf);[INTERNPROC: In_settextbuf_file_x];
  287. Procedure SetTextBuf(Var F : Text; Var Buf; Size : Word);
  288. Begin
  289. TextRec(f).BufPtr:=@Buf;
  290. TextRec(f).BufSize:=Size;
  291. TextRec(f).BufPos:=0;
  292. TextRec(f).BufEnd:=0;
  293. End;
  294. {*****************************************************************************
  295. Write(Ln)
  296. *****************************************************************************}
  297. Procedure WriteBuffer(var f:TextRec;var b;len:longint);
  298. var
  299. p : pchar;
  300. left,
  301. idx : longint;
  302. begin
  303. p:=pchar(@b);
  304. idx:=0;
  305. left:=f.BufSize-f.BufPos;
  306. while len>left do
  307. begin
  308. move(p[idx],f.Bufptr^[f.BufPos],left);
  309. dec(len,left);
  310. inc(idx,left);
  311. inc(f.BufPos,left);
  312. FileFunc(f.InOutFunc)(f);
  313. left:=f.BufSize-f.BufPos;
  314. end;
  315. move(p[idx],f.Bufptr^[f.BufPos],len);
  316. inc(f.BufPos,len);
  317. end;
  318. Procedure WriteBlanks(var f:TextRec;len:longint);
  319. var
  320. left : longint;
  321. begin
  322. left:=f.BufSize-f.BufPos;
  323. while len>left do
  324. begin
  325. FillChar(f.Bufptr^[f.BufPos],left,' ');
  326. dec(len,left);
  327. inc(f.BufPos,left);
  328. FileFunc(f.InOutFunc)(f);
  329. left:=f.BufSize-f.BufPos;
  330. end;
  331. FillChar(f.Bufptr^[f.BufPos],len,' ');
  332. inc(f.BufPos,len);
  333. end;
  334. Procedure Write_End(var f:TextRec);[Public,Alias:'WRITE_END'];
  335. begin
  336. if f.FlushFunc<>nil then
  337. FileFunc(f.FlushFunc)(f);
  338. end;
  339. Procedure Writeln_End(var f:TextRec);[Public,Alias:'WRITELN_END'];
  340. const
  341. {$IFDEF SHORT_LINEBREAK}
  342. eollen=1;
  343. eol : array[0..0] of char=(#10);
  344. {$ELSE SHORT_LINEBREAK}
  345. eollen=2;
  346. eol : array[0..1] of char=(#13,#10);
  347. {$ENDIF SHORT_LINEBREAK}
  348. begin
  349. If InOutRes <> 0 then exit;
  350. { Write EOL }
  351. WriteBuffer(f,eol,eollen);
  352. { Flush }
  353. if f.FlushFunc<>nil then
  354. FileFunc(f.FlushFunc)(f);
  355. end;
  356. Procedure Write_Str(Len : Longint;var f : TextRec;const s : String);[Public,Alias: 'WRITE_TEXT_STRING'];
  357. Begin
  358. If InOutRes <> 0 then exit;
  359. If f.mode<>fmOutput Then
  360. exit;
  361. If Len>Length(s) Then
  362. WriteBlanks(f,Len-Length(s));
  363. WriteBuffer(f,s[1],Length(s));
  364. End;
  365. Type
  366. array00 = array[0..0] Of Char;
  367. Procedure Write_Array(Len : Longint;var f : TextRec;const p : array00);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_ARRAY'];
  368. var
  369. ArrayLen : longint;
  370. Begin
  371. If InOutRes <> 0 then exit;
  372. If f.mode<>fmOutput Then
  373. exit;
  374. ArrayLen:=StrLen(p);
  375. If Len>ArrayLen Then
  376. WriteBlanks(f,Len-ArrayLen);
  377. WriteBuffer(f,p,ArrayLen);
  378. End;
  379. Procedure Write_PChar(Len : Longint;var f : TextRec;p : PChar);[Public,Alias: 'WRITE_TEXT_PCHAR_AS_POINTER'];
  380. var
  381. PCharLen : longint;
  382. Begin
  383. If InOutRes <> 0 then exit;
  384. If f.mode<>fmOutput Then
  385. exit;
  386. PCharLen:=StrLen(p);
  387. If Len>PCharLen Then
  388. WriteBlanks(f,Len-PCharLen);
  389. WriteBuffer(f,p^,PCharLen);
  390. End;
  391. Procedure Write_LongInt(Len : Longint;var t : TextRec;l : Longint);[Public,Alias: 'WRITE_TEXT_LONGINT'];
  392. var
  393. s : String;
  394. Begin
  395. If InOutRes <> 0 then exit;
  396. Str(l,s);
  397. Write_Str(Len,t,s);
  398. End;
  399. Procedure Write_Real(fixkomma,Len : Longint;var t : TextRec;r : real);[Public,Alias: 'WRITE_TEXT_REAL'];
  400. var
  401. s : String;
  402. Begin
  403. If InOutRes <> 0 then exit;
  404. {$ifdef i386}
  405. Str_real(Len,fixkomma,r,rt_s64real,s);
  406. {$else}
  407. Str_real(Len,fixkomma,r,rt_s32real,s);
  408. {$endif}
  409. Write_Str(Len,t,s);
  410. End;
  411. Procedure Write_Cardinal(Len : Longint;var t : TextRec;l : cardinal);[Public,Alias: 'WRITE_TEXT_CARDINAL'];
  412. var
  413. s : String;
  414. Begin
  415. If InOutRes <> 0 then exit;
  416. Str(L,s);
  417. Write_Str(Len,t,s);
  418. End;
  419. {$ifdef SUPPORT_SINGLE}
  420. Procedure Write_Single(fixkomma,Len : Longint;var t : TextRec;r : single);[Public,Alias: 'WRITE_TEXT_SINGLE'];
  421. var
  422. s : String;
  423. Begin
  424. If InOutRes <> 0 then exit;
  425. Str_real(Len,fixkomma,r,rt_s32real,s);
  426. Write_Str(Len,t,s);
  427. End;
  428. {$endif SUPPORT_SINGLE}
  429. {$ifdef SUPPORT_EXTENDED}
  430. Procedure Write_Extended(fixkomma,Len : Longint;var t : TextRec;r : extended);[Public,Alias: 'WRITE_TEXT_EXTENDED'];
  431. var
  432. s : String;
  433. Begin
  434. If InOutRes <> 0 then exit;
  435. Str_real(Len,fixkomma,r,rt_s80real,s);
  436. Write_Str(Len,t,s);
  437. End;
  438. {$endif SUPPORT_EXTENDED}
  439. {$ifdef SUPPORT_COMP}
  440. Procedure Write_Comp(fixkomma,Len : Longint;var t : TextRec;r : comp);[Public,Alias: 'WRITE_TEXT_COMP'];
  441. var
  442. s : String;
  443. Begin
  444. If InOutRes <> 0 then exit;
  445. Str_real(Len,fixkomma,r,rt_s64bit,s);
  446. Write_Str(Len,t,s);
  447. End;
  448. {$endif SUPPORT_COMP}
  449. Procedure Write_Fixed(fixkomma,Len : Longint;var t : TextRec;r : fixed);[Public,Alias: 'WRITE_TEXT_FIXED'];
  450. var
  451. s : String;
  452. Begin
  453. If InOutRes <> 0 then exit;
  454. Str_real(Len,fixkomma,r,rt_f32bit,s);
  455. Write_Str(Len,t,s);
  456. End;
  457. Procedure Write_Boolean(Len : Longint;var t : TextRec;b : Boolean);[Public,Alias: 'WRITE_TEXT_BOOLEAN'];
  458. Begin
  459. If InOutRes <> 0 then exit;
  460. { Can't use array[boolean] because b can be >0 ! }
  461. if b then
  462. Write_Str(Len,t,'TRUE')
  463. else
  464. Write_Str(Len,t,'FALSE');
  465. End;
  466. Procedure Write_Char(Len : Longint;var t : TextRec;c : Char);[Public,Alias: 'WRITE_TEXT_CHAR'];
  467. Begin
  468. If InOutRes <> 0 then exit;
  469. If t.mode<>fmOutput Then
  470. exit;
  471. If Len>1 Then
  472. WriteBlanks(t,Len-1);
  473. If t.BufPos+1>=t.BufSize Then
  474. FileFunc(t.InOutFunc)(t);
  475. t.Bufptr^[t.BufPos]:=c;
  476. Inc(t.BufPos);
  477. End;
  478. {$IFNDEF NEW_READWRITE}
  479. Procedure w(var t : TextRec);[Public,Alias: 'WRITELN_TEXT'];
  480. var
  481. hs : String;
  482. Begin
  483. If InOutRes <> 0 then exit;
  484. {$IFDEF SHORT_LINEBREAK}
  485. hs:=#10;
  486. {$ELSE}
  487. hs:=#13#10;
  488. {$ENDIF}
  489. Write_Str(0,t,hs);
  490. End;
  491. {$ENDIF NEW_READWRITE}
  492. {*****************************************************************************
  493. Read(Ln)
  494. *****************************************************************************}
  495. Function OpenInput(var f:TextRec):boolean;
  496. begin
  497. If f.mode=fmInput Then
  498. begin
  499. { No characters in the buffer? Load them ! }
  500. If f.BufPos>=f.BufEnd Then
  501. FileFunc(f.InOutFunc)(f);
  502. OpenInput:=true;
  503. end
  504. else
  505. OpenInput:=false;
  506. end;
  507. Function NextChar(var f:TextRec;var s:string):Boolean;
  508. begin
  509. if f.BufPos<f.BufEnd then
  510. begin
  511. s:=s+f.BufPtr^[f.BufPos];
  512. Inc(f.BufPos);
  513. If f.BufPos>=f.BufEnd Then
  514. FileFunc(f.InOutFunc)(f);
  515. NextChar:=true;
  516. end
  517. else
  518. NextChar:=false;
  519. end;
  520. Function IgnoreSpaces(var f:TextRec):Boolean;
  521. {
  522. Removes all leading spaces,tab,eols from the input buffer, returns true if
  523. the buffer is empty
  524. }
  525. var
  526. s : string;
  527. begin
  528. s:='';
  529. IgnoreSpaces:=false;
  530. while f.Bufptr^[f.BufPos] in [#9,#10,#13,' '] do
  531. if not NextChar(f,s) then
  532. exit;
  533. IgnoreSpaces:=true;
  534. end;
  535. Function ReadSign(var f:TextRec;var s:string):Boolean;
  536. {
  537. Read + and - sign, return true if buffer is empty
  538. }
  539. begin
  540. ReadSign:=(not (f.Bufptr^[f.BufPos] in ['-','+'])) or NextChar(f,s);
  541. end;
  542. Function ReadBase(var f:TextRec;var s:string;var Base:longint):boolean;
  543. {
  544. Read the base $ For 16 and % For 2, if buffer is empty return true
  545. }
  546. begin
  547. case f.BufPtr^[f.BufPos] of
  548. '$' : Base:=16;
  549. '%' : Base:=2;
  550. else
  551. Base:=10;
  552. end;
  553. ReadBase:=(Base=10) or NextChar(f,s);
  554. end;
  555. Function ReadNumeric(var f:TextRec;var s:string;base:longint):Boolean;
  556. {
  557. Read numeric input, if buffer is empty then return True
  558. }
  559. var
  560. c : char;
  561. begin
  562. ReadNumeric:=false;
  563. c:=f.BufPtr^[f.BufPos];
  564. while ((base>=10) and (c in ['0'..'9'])) or
  565. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  566. ((base=2) and (c in ['0'..'1'])) do
  567. begin
  568. if not NextChar(f,s) then
  569. exit;
  570. c:=f.BufPtr^[f.BufPos];
  571. end;
  572. ReadNumeric:=true;
  573. end;
  574. Procedure Read_End(var f:TextRec);[Public,Alias:'READ_END'];
  575. begin
  576. if f.FlushFunc<>nil then
  577. FileFunc(f.FlushFunc)(f);
  578. end;
  579. Procedure ReadLn_End(var f : TextRec);[Public,Alias: 'READLN_END'];
  580. Begin
  581. If InOutRes <> 0 then exit;
  582. if not OpenInput(f) then
  583. exit;
  584. { Read until a linebreak }
  585. while (f.BufPos<f.BufEnd) do
  586. begin
  587. inc(f.BufPos);
  588. if (f.BufPtr^[f.BufPos-1]=#10) then
  589. exit;
  590. If f.BufPos>=f.BufEnd Then
  591. FileFunc(f.InOutFunc)(f);
  592. end;
  593. { Flush if set }
  594. if f.FlushFunc<>nil then
  595. FileFunc(f.FlushFunc)(f);
  596. End;
  597. Procedure Read_String(var f : TextRec;var s : String);[Public,Alias: 'READ_TEXT_STRING'];
  598. var
  599. Temp,sPos : Word;
  600. Begin
  601. { Delete the string }
  602. s:='';
  603. If InOutRes <> 0 then exit;
  604. if not OpenInput(f) then
  605. exit;
  606. Temp:=f.BufPos;
  607. sPos:=1;
  608. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  609. Begin
  610. { search linefeed }
  611. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  612. Inc(Temp);
  613. { copy String. Take 255 char limit in account.}
  614. If sPos+Temp-f.BufPos<=255 Then
  615. Begin
  616. Move (f.Bufptr^[f.BufPos],s[sPos],Temp-f.BufPos);
  617. sPos:=sPos+Temp-f.BufPos;
  618. { Remove #13 from a #13#10 break }
  619. If s[sPos-1]=#13 Then
  620. dec(sPos);
  621. End
  622. else
  623. Begin
  624. If (sPos<=255) Then
  625. Move(f.Bufptr^[f.BufPos],s[sPos],256-sPos);
  626. sPos:=256
  627. End;
  628. { update f.BufPos }
  629. f.BufPos:=Temp;
  630. If Temp>=f.BufEnd Then
  631. Begin
  632. FileFunc(f.InOutFunc)(f);
  633. Temp:=f.BufPos;
  634. End
  635. End;
  636. s[0]:=chr(sPos-1);
  637. End;
  638. Procedure Read_Char(var f : TextRec;var c : Char);[Public,Alias: 'READ_TEXT_CHAR'];
  639. Begin
  640. c:=#0;
  641. If InOutRes <> 0 then exit;
  642. if not OpenInput(f) then
  643. exit;
  644. If f.BufPos>=f.BufEnd Then
  645. c:=#26
  646. else
  647. c:=f.Bufptr^[f.BufPos];
  648. Inc(f.BufPos);
  649. End;
  650. Procedure Read_PChar(var f : TextRec;var s : PChar);[Public,Alias:'READ_TEXT_PCHAR_AS_POINTER'];
  651. var
  652. p : PChar;
  653. Temp : byte;
  654. Begin
  655. { Delete the string }
  656. s^:=#0;
  657. If InOutRes <> 0 then exit;
  658. p:=s;
  659. if not OpenInput(f) then
  660. exit;
  661. Temp:=f.BufPos;
  662. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  663. Begin
  664. { search linefeed }
  665. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  666. inc(Temp);
  667. { copy string. }
  668. Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
  669. Inc(Longint(p),Temp-f.BufPos);
  670. If pchar(p-1)^=#13 Then
  671. dec(p);
  672. { update f.BufPos }
  673. f.BufPos:=Temp;
  674. If Temp>=f.BufEnd Then
  675. Begin
  676. FileFunc(f.InOutFunc)(f);
  677. Temp:=f.BufPos;
  678. End
  679. End;
  680. p^:=#0;
  681. End;
  682. Procedure Read_Array(var f : TextRec;var s : array00);[Public,Alias:'READ_TEXT_PCHAR_AS_ARRAY'];
  683. var
  684. p : PChar;
  685. Temp : byte;
  686. Begin
  687. { Delete the string }
  688. s[0]:=#0;
  689. If InOutRes <> 0 then exit;
  690. p:=pchar(@s);
  691. if not OpenInput(f) then
  692. exit;
  693. Temp:=f.BufPos;
  694. while (f.BufPos<f.BufEnd) and (f.Bufptr^[Temp]<>#10) Do
  695. Begin
  696. { search linefeed }
  697. while (f.Bufptr^[Temp]<>#10) and (Temp<f.BufEnd) Do
  698. inc(Temp);
  699. { copy string. }
  700. Move (f.Bufptr^[f.BufPos],p^,Temp-f.BufPos);
  701. Inc(Longint(p),Temp-f.BufPos);
  702. If pchar(p-1)^=#13 Then
  703. dec(p);
  704. { update f.BufPos }
  705. f.BufPos:=Temp;
  706. If Temp>=f.BufEnd Then
  707. Begin
  708. FileFunc(f.InOutFunc)(f);
  709. Temp:=f.BufPos;
  710. End
  711. End;
  712. p^:=#0;
  713. End;
  714. Procedure Read_Longint(var f : TextRec;var l : Longint);[Public,Alias: 'READ_TEXT_LONGINT'];
  715. var
  716. hs : String;
  717. code : Word;
  718. base : longint;
  719. Begin
  720. l:=0;
  721. If InOutRes <> 0 then exit;
  722. hs:='';
  723. if not OpenInput(f) then
  724. exit;
  725. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  726. ReadNumeric(f,hs,Base);
  727. Val(hs,l,code);
  728. If code<>0 Then
  729. RunError(106);
  730. End;
  731. Procedure Read_Integer(var f : TextRec;var l : Integer);[Public,Alias: 'READ_TEXT_INTEGER'];
  732. var
  733. ll : Longint;
  734. Begin
  735. l:=0;
  736. If InOutRes <> 0 then exit;
  737. Read_Longint(f,ll);
  738. If (ll<-32768) or (ll>32767) Then
  739. RunError(106);
  740. l:=ll;
  741. End;
  742. Procedure Read_Word(var f : TextRec;var l : Word);[Public,Alias: 'READ_TEXT_WORD'];
  743. var
  744. ll : Longint;
  745. Begin
  746. l:=0;
  747. If InOutRes <> 0 then exit;
  748. Read_Longint(f,ll);
  749. If (ll<0) or (ll>$ffff) Then
  750. RunError(106);
  751. l:=ll;
  752. End;
  753. Procedure Read_Byte(var f : TextRec;var l : byte);[Public,Alias: 'READ_TEXT_BYTE'];
  754. var
  755. ll : Longint;
  756. Begin
  757. l:=0;
  758. If InOutRes <> 0 then exit;
  759. Read_Longint(f,ll);
  760. If (ll<0) or (ll>255) Then
  761. RunError(106);
  762. l:=ll;
  763. End;
  764. Procedure Read_Shortint(var f : TextRec;var l : shortint);[Public,Alias: 'READ_TEXT_SHORTINT'];
  765. var
  766. ll : Longint;
  767. Begin
  768. l:=0;
  769. If InOutRes <> 0 then exit;
  770. Read_Longint(f,ll);
  771. If (ll<-128) or (ll>127) Then
  772. RunError(106);
  773. l:=ll;
  774. End;
  775. Procedure Read_Cardinal(var f : TextRec;var l : cardinal);[Public,Alias: 'READ_TEXT_CARDINAL'];
  776. var
  777. hs : String;
  778. code : Word;
  779. base : longint;
  780. Begin
  781. l:=0;
  782. If InOutRes <> 0 then exit;
  783. hs:='';
  784. if not OpenInput(f) then
  785. exit;
  786. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadBase(f,hs,Base) then
  787. ReadNumeric(f,hs,Base);
  788. val(hs,l,code);
  789. If code<>0 Then
  790. RunError(106);
  791. End;
  792. Procedure Read_Real(var f : TextRec;var d : Real);[Public,Alias: 'READ_TEXT_REAL'];
  793. var
  794. hs : String;
  795. code : Word;
  796. Begin
  797. d:=0.0;
  798. If InOutRes <> 0 then exit;
  799. hs:='';
  800. if not OpenInput(f) then
  801. exit;
  802. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  803. begin
  804. { First check for a . }
  805. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  806. begin
  807. hs:=hs+'.';
  808. Inc(f.BufPos);
  809. If f.BufPos>=f.BufEnd Then
  810. FileFunc(f.InOutFunc)(f);
  811. ReadNumeric(f,hs,10);
  812. end;
  813. { Also when a point is found check for a E }
  814. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  815. begin
  816. hs:=hs+'E';
  817. Inc(f.BufPos);
  818. If f.BufPos>=f.BufEnd Then
  819. FileFunc(f.InOutFunc)(f);
  820. if ReadSign(f,hs) then
  821. ReadNumeric(f,hs,10);
  822. end;
  823. end;
  824. val(hs,d,code);
  825. If code<>0 Then
  826. RunError(106);
  827. End;
  828. {$ifdef SUPPORT_EXTENDED}
  829. Procedure Read_Extended(var f : TextRec;var d : extended);[Public,Alias: 'READ_TEXT_EXTENDED'];
  830. var
  831. hs : String;
  832. code : Word;
  833. Begin
  834. d:=0.0;
  835. If InOutRes <> 0 then exit;
  836. hs:='';
  837. if not OpenInput(f) then
  838. exit;
  839. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  840. begin
  841. { First check for a . }
  842. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  843. begin
  844. hs:=hs+'.';
  845. Inc(f.BufPos);
  846. If f.BufPos>=f.BufEnd Then
  847. FileFunc(f.InOutFunc)(f);
  848. ReadNumeric(f,hs,10);
  849. end;
  850. { Also when a point is found check for a E }
  851. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  852. begin
  853. hs:=hs+'E';
  854. Inc(f.BufPos);
  855. If f.BufPos>=f.BufEnd Then
  856. FileFunc(f.InOutFunc)(f);
  857. if ReadSign(f,hs) then
  858. ReadNumeric(f,hs,10);
  859. end;
  860. end;
  861. val(hs,d,code);
  862. If code<>0 Then
  863. RunError(106);
  864. End;
  865. {$endif SUPPORT_EXTENDED}
  866. {$ifdef SUPPORT_COMP}
  867. Procedure Read_Comp(var f : TextRec;var d : comp);[Public,Alias: 'READ_TEXT_COMP'];
  868. var
  869. hs : String;
  870. code : Word;
  871. Begin
  872. d:=comp(0.0);
  873. If InOutRes <> 0 then exit;
  874. hs:='';
  875. if not OpenInput(f) then
  876. exit;
  877. if IgnoreSpaces(f) and ReadSign(f,hs) and ReadNumeric(f,hs,10) then
  878. begin
  879. { First check for a . }
  880. if (f.Bufptr^[f.BufPos]='.') and (f.BufPos<f.BufEnd) Then
  881. begin
  882. hs:=hs+'.';
  883. Inc(f.BufPos);
  884. If f.BufPos>=f.BufEnd Then
  885. FileFunc(f.InOutFunc)(f);
  886. ReadNumeric(f,hs,10);
  887. end;
  888. { Also when a point is found check for a E }
  889. if (f.Bufptr^[f.BufPos] in ['e','E']) and (f.BufPos<f.BufEnd) Then
  890. begin
  891. hs:=hs+'E';
  892. Inc(f.BufPos);
  893. If f.BufPos>=f.BufEnd Then
  894. FileFunc(f.InOutFunc)(f);
  895. if ReadSign(f,hs) then
  896. ReadNumeric(f,hs,10);
  897. end;
  898. end;
  899. val(hs,d,code);
  900. If code<>0 Then
  901. RunError(106);
  902. End;
  903. {$endif SUPPORT_COMP}
  904. {$IFNDEF NEW_READWRITE}
  905. Procedure r(var f : TextRec);[Public,Alias: 'READLN_TEXT'];
  906. Begin
  907. If InOutRes <> 0 then exit;
  908. if not OpenInput(f) then
  909. exit;
  910. while (f.BufPos<f.BufEnd) do
  911. begin
  912. inc(f.BufPos);
  913. if (f.BufPtr^[f.BufPos-1]=#10) then
  914. exit;
  915. If f.BufPos>=f.BufEnd Then
  916. FileFunc(f.InOutFunc)(f);
  917. end;
  918. End;
  919. {$ENDIF NEW_READWRITE}
  920. {*****************************************************************************
  921. Initializing
  922. *****************************************************************************}
  923. procedure OpenStdIO(var f:text;mode:word;hdl:longint);
  924. begin
  925. Assign(f,'');
  926. TextRec(f).Handle:=hdl;
  927. TextRec(f).Mode:=mode;
  928. TextRec(f).Closefunc:=@FileCloseFunc;
  929. case mode of
  930. fmInput : TextRec(f).InOutFunc:=@FileReadFunc;
  931. fmOutput : begin
  932. TextRec(f).InOutFunc:=@FileWriteFunc;
  933. TextRec(f).FlushFunc:=@FileWriteFunc;
  934. end;
  935. else
  936. RunError(102);
  937. end;
  938. end;
  939. {
  940. $Log$
  941. Revision 1.14 1998-07-02 12:14:56 carl
  942. + Each IOCheck routine now check InOutRes before, just like TP
  943. Revision 1.13 1998/07/01 15:30:00 peter
  944. * better readln/writeln
  945. Revision 1.12 1998/07/01 14:48:10 carl
  946. * bugfix of WRITE_TEXT_BOOLEAN , was not TP compatible
  947. + added explicit typecast in OpenText
  948. Revision 1.11 1998/06/25 09:44:22 daniel
  949. + RTLLITE directive to compile minimal RTL.
  950. Revision 1.10 1998/06/04 23:46:03 peter
  951. * comp,extended are only i386 added support_comp,support_extended
  952. Revision 1.9 1998/06/02 16:47:56 pierre
  953. * bug for boolean values greater than one fixed
  954. Revision 1.8 1998/05/31 14:14:54 peter
  955. * removed warnings using comp()
  956. Revision 1.7 1998/05/27 00:19:21 peter
  957. * fixed crt input
  958. Revision 1.6 1998/05/21 19:31:01 peter
  959. * objects compiles for linux
  960. + assign(pchar), assign(char), rename(pchar), rename(char)
  961. * fixed read_text_as_array
  962. + read_text_as_pchar which was not yet in the rtl
  963. Revision 1.5 1998/05/12 10:42:45 peter
  964. * moved getopts to inc/, all supported OS's need argc,argv exported
  965. + strpas, strlen are now exported in the systemunit
  966. * removed logs
  967. * removed $ifdef ver_above
  968. Revision 1.4 1998/04/07 22:40:46 florian
  969. * final fix of comp writing
  970. }