iso7185.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 2010 by Florian Klaempfl
  4. This unit contain procedures specific for iso pascal mode.
  5. It should be platform independant.
  6. See the file COPYING.FPC, included in this distribution,
  7. For details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit iso7185;
  13. interface
  14. const
  15. MaxInt = MaxLongint;
  16. type
  17. Integer = Longint;
  18. Procedure Rewrite(var t : Text);
  19. Procedure Reset(var t : Text);
  20. Procedure Reset(var f : TypedFile); [INTERNPROC: fpc_in_Reset_TypedFile];
  21. Procedure Rewrite(var f : TypedFile); [INTERNPROC: fpc_in_Rewrite_TypedFile];
  22. Procedure Rewrite(var t : Text;const filename : string);
  23. Procedure Reset(var t : Text;const filename : string);
  24. Procedure Reset(var f : TypedFile;const filename : string); [INTERNPROC: fpc_in_Reset_TypedFile_Name];
  25. Procedure Rewrite(var f : TypedFile;const filename : string); [INTERNPROC: fpc_in_Rewrite_TypedFile_Name];
  26. Function Eof(Var t: Text): Boolean;
  27. Function Eof:Boolean;
  28. Function Eoln(Var t: Text): Boolean;
  29. Function Eoln:Boolean;
  30. Procedure Page;
  31. Procedure Page(Var t: Text);
  32. Procedure Get(Var t: Text);
  33. Procedure Put(Var t: Text);
  34. Procedure Get(Var f: TypedFile);
  35. Procedure Put(Var f: TypedFile);
  36. Procedure Seek(var f:TypedFile;Pos:Int64);
  37. Function Eof(var f:TypedFile): Boolean;
  38. implementation
  39. {$i isotmp.inc}
  40. {$i-}
  41. procedure DoAssign(var t : Text);
  42. {$ifndef FPC_HAS_FEATURE_RANDOM}
  43. const
  44. NextIndex : Word = 1;
  45. {$endif FPC_HAS_FEATURE_RANDOM}
  46. begin
  47. {$ifdef FPC_HAS_FEATURE_RANDOM}
  48. Assign(t,getTempDir+'fpc_'+HexStr(random(1000000000),8)+'.tmp');
  49. {$else FPC_HAS_FEATURE_RANDOM}
  50. Assign(t,getTempDir+'fpc_'+HexStr(NextIndex,4)+'.tmp');
  51. Inc(NextIndex);
  52. {$endif FPC_HAS_FEATURE_RANDOM}
  53. end;
  54. Procedure Rewrite(var t : Text);[IOCheck];
  55. Begin
  56. { create file name? }
  57. if Textrec(t).mode=0 then
  58. DoAssign(t);
  59. System.Rewrite(t);
  60. End;
  61. Procedure Reset(var t : Text);[IOCheck];
  62. Begin
  63. case Textrec(t).mode of
  64. { create file name? }
  65. 0:
  66. DoAssign(t);
  67. fmOutput:
  68. Write(t,#26);
  69. end;
  70. System.Reset(t);
  71. End;
  72. Procedure Rewrite(var t : Text;const filename : string);[IOCheck];
  73. Begin
  74. { create file name? }
  75. if Textrec(t).mode=0 then
  76. Assign(t,filename);
  77. System.Rewrite(t);
  78. End;
  79. Procedure Reset(var t : Text;const filename : string);[IOCheck];
  80. Begin
  81. case Textrec(t).mode of
  82. { create file name? }
  83. 0:
  84. Assign(t,filename);
  85. fmOutput:
  86. Write(t,#26);
  87. end;
  88. System.Reset(t);
  89. End;
  90. Function Eof(Var t: Text): Boolean;[IOCheck];
  91. var
  92. OldCtrlZMarksEof : Boolean;
  93. Begin
  94. { not sure if this is correct, but we are always at eof when
  95. writing to a file }
  96. if TextRec(t).mode=fmOutput then
  97. Eof:=true
  98. else
  99. begin
  100. OldCtrlZMarksEof:=CtrlZMarksEOF;
  101. CtrlZMarksEof:=false;
  102. Eof:=System.Eof(t);
  103. CtrlZMarksEof:=OldCtrlZMarksEOF;
  104. end;
  105. end;
  106. Function Eof:Boolean;
  107. Begin
  108. Eof:=Eof(Input);
  109. End;
  110. Function Eoln(Var t: Text): Boolean;[IOCheck];
  111. var
  112. OldCtrlZMarksEof : Boolean;
  113. Begin
  114. OldCtrlZMarksEof:=CtrlZMarksEOF;
  115. CtrlZMarksEof:=true;
  116. Eoln:=System.Eoln(t);
  117. CtrlZMarksEof:=OldCtrlZMarksEOF;
  118. end;
  119. Function Eoln:Boolean;
  120. Begin
  121. Eoln:=Eoln(Input);
  122. End;
  123. Procedure Page;[IOCheck];
  124. begin
  125. Page(Output);
  126. end;
  127. Procedure Page(var t : Text);[IOCheck];
  128. Begin
  129. write(#12);
  130. End;
  131. procedure Get(var t : Text);[IOCheck];
  132. var
  133. c : char;
  134. Begin
  135. Read(t,c);
  136. End;
  137. Procedure Put(var t : Text);[IOCheck];
  138. type
  139. FileFunc = Procedure(var t : TextRec);
  140. begin
  141. inc(TextRec(t).BufPos);
  142. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  143. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  144. end;
  145. procedure Get(var f:TypedFile);[IOCheck];
  146. Begin
  147. if not(eof(f)) then
  148. BlockRead(f,(pbyte(@f)+sizeof(FileRec))^,1);
  149. End;
  150. Procedure Put(var f:TypedFile);[IOCheck];
  151. begin
  152. BlockWrite(f,(pbyte(@f)+sizeof(FileRec))^,1);
  153. end;
  154. Function Eof(var f:TypedFile): Boolean;[IOCheck];
  155. Begin
  156. Eof:=FileRec(f)._private[1]=1;
  157. End;
  158. Procedure Seek(var f:TypedFile;Pos:Int64);
  159. Begin
  160. System.Seek(f,Pos);
  161. if (FileRec(f).mode=fmInOut) or
  162. (FileRec(f).mode=fmInput) then
  163. begin
  164. if FilePos(f)<FileSize(f) then
  165. begin
  166. FileRec(f)._private[1]:=0;
  167. Get(f);
  168. end
  169. else
  170. FileRec(f)._private[1]:=1;
  171. end;
  172. End;
  173. begin
  174. { we shouldn't do this because it might confuse user programs, but for now it
  175. is good enough to get pretty unique tmp file names }
  176. {$ifdef FPC_HAS_FEATURE_RANDOM}
  177. Randomize;
  178. {$endif FPC_HAS_FEATURE_RANDOM}
  179. { reset opens with read-only }
  180. Filemode:=0;
  181. end.