iso7185.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 FilePos(var f:TypedFile):Int64;
  38. Function Eof(var f:TypedFile): Boolean;
  39. implementation
  40. {$i isotmp.inc}
  41. {$i-}
  42. procedure DoAssign(var t : Text);
  43. {$ifndef FPC_HAS_FEATURE_RANDOM}
  44. const
  45. NextIndex : Word = 1;
  46. {$endif FPC_HAS_FEATURE_RANDOM}
  47. begin
  48. {$ifdef FPC_HAS_FEATURE_RANDOM}
  49. Assign(t,getTempDir+'fpc_'+HexStr(random(1000000000),8)+'.tmp');
  50. {$else FPC_HAS_FEATURE_RANDOM}
  51. Assign(t,getTempDir+'fpc_'+HexStr(NextIndex,4)+'.tmp');
  52. Inc(NextIndex);
  53. {$endif FPC_HAS_FEATURE_RANDOM}
  54. end;
  55. Procedure Rewrite(var t : Text);[IOCheck];
  56. Begin
  57. { create file name? }
  58. if Textrec(t).mode=0 then
  59. DoAssign(t);
  60. System.Rewrite(t);
  61. End;
  62. Procedure Reset(var t : Text);[IOCheck];
  63. Begin
  64. case Textrec(t).mode of
  65. { create file name? }
  66. 0:
  67. DoAssign(t);
  68. fmOutput:
  69. Write(t,#26);
  70. end;
  71. System.Reset(t);
  72. End;
  73. Procedure Rewrite(var t : Text;const filename : string);[IOCheck];
  74. Begin
  75. { create file name? }
  76. if Textrec(t).mode=0 then
  77. Assign(t,filename);
  78. System.Rewrite(t);
  79. End;
  80. Procedure Reset(var t : Text;const filename : string);[IOCheck];
  81. Begin
  82. case Textrec(t).mode of
  83. { create file name? }
  84. 0:
  85. Assign(t,filename);
  86. fmOutput:
  87. Write(t,#26);
  88. end;
  89. System.Reset(t);
  90. End;
  91. Function Eof(Var t: Text): Boolean;[IOCheck];
  92. var
  93. OldCtrlZMarksEof : Boolean;
  94. Begin
  95. { not sure if this is correct, but we are always at eof when
  96. writing to a file }
  97. if TextRec(t).mode=fmOutput then
  98. Eof:=true
  99. else
  100. begin
  101. OldCtrlZMarksEof:=CtrlZMarksEOF;
  102. CtrlZMarksEof:=false;
  103. Eof:=System.Eof(t);
  104. CtrlZMarksEof:=OldCtrlZMarksEOF;
  105. end;
  106. end;
  107. Function Eof:Boolean;
  108. Begin
  109. Eof:=Eof(Input);
  110. End;
  111. Function Eoln(Var t: Text): Boolean;[IOCheck];
  112. var
  113. OldCtrlZMarksEof : Boolean;
  114. Begin
  115. OldCtrlZMarksEof:=CtrlZMarksEOF;
  116. CtrlZMarksEof:=true;
  117. Eoln:=System.Eoln(t);
  118. CtrlZMarksEof:=OldCtrlZMarksEOF;
  119. end;
  120. Function Eoln:Boolean;
  121. Begin
  122. Eoln:=Eoln(Input);
  123. End;
  124. Procedure Page;[IOCheck];
  125. begin
  126. Page(Output);
  127. end;
  128. Procedure Page(var t : Text);[IOCheck];
  129. Begin
  130. write(#12);
  131. End;
  132. procedure Get(var t : Text);[IOCheck];
  133. var
  134. c : char;
  135. Begin
  136. Read(t,c);
  137. End;
  138. Procedure Put(var t : Text);[IOCheck];
  139. type
  140. FileFunc = Procedure(var t : TextRec);
  141. begin
  142. inc(TextRec(t).BufPos);
  143. If TextRec(t).BufPos>=TextRec(t).BufSize Then
  144. FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  145. end;
  146. procedure Get(var f:TypedFile);[IOCheck];
  147. Begin
  148. if not(eof(f)) then
  149. BlockRead(f,(pbyte(@f)+sizeof(FileRec))^,1);
  150. End;
  151. Procedure Put(var f:TypedFile);[IOCheck];
  152. begin
  153. BlockWrite(f,(pbyte(@f)+sizeof(FileRec))^,1);
  154. end;
  155. Function Eof(var f:TypedFile): Boolean;[IOCheck];
  156. Begin
  157. Eof:=FileRec(f)._private[1]=1;
  158. End;
  159. Procedure Seek(var f:TypedFile;Pos:Int64);[IOCheck];
  160. Begin
  161. System.Seek(f,Pos);
  162. if (FileRec(f).mode=fmInOut) or
  163. (FileRec(f).mode=fmInput) then
  164. begin
  165. if FilePos(f)<FileSize(f) then
  166. begin
  167. FileRec(f)._private[1]:=0;
  168. Get(f);
  169. end
  170. else
  171. FileRec(f)._private[1]:=1;
  172. end;
  173. End;
  174. Function FilePos(var f:TypedFile):Int64;[IOCheck];
  175. Begin
  176. FilePos:=System.FilePos(f);
  177. { in case of reading a file, the buffer is always filled, so the result of Do_FilePos is off by one }
  178. if (FileRec(f).mode=fmInOut) or
  179. (FileRec(f).mode=fmInput) then
  180. dec(FilePos);
  181. End;
  182. begin
  183. { we shouldn't do this because it might confuse user programs, but for now it
  184. is good enough to get pretty unique tmp file names }
  185. {$ifdef FPC_HAS_FEATURE_RANDOM}
  186. Randomize;
  187. {$endif FPC_HAS_FEATURE_RANDOM}
  188. { reset opens with read-only }
  189. Filemode:=0;
  190. end.