iso7185.pp 7.0 KB

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