iso7185.pp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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:=true;
  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(system.eof(f)) then
  160. BlockRead(f,(pbyte(@f)+sizeof(FileRec))^,1)
  161. else
  162. FileRec(f)._private[1]:=1;
  163. End;
  164. Procedure Put(var f:TypedFile);[IOCheck];
  165. begin
  166. BlockWrite(f,(pbyte(@f)+sizeof(FileRec))^,1);
  167. end;
  168. Function Eof(var f:TypedFile): Boolean;[IOCheck];
  169. Begin
  170. Eof:=FileRec(f)._private[1]=1;
  171. End;
  172. Procedure Seek(var f:TypedFile;Pos:Int64);[IOCheck];
  173. Begin
  174. System.Seek(f,Pos);
  175. if (FileRec(f).mode=fmInOut) or
  176. (FileRec(f).mode=fmInput) then
  177. begin
  178. if FilePos(f)<FileSize(f) then
  179. begin
  180. FileRec(f)._private[1]:=0;
  181. Get(f);
  182. end
  183. else
  184. FileRec(f)._private[1]:=1;
  185. end;
  186. End;
  187. Function FilePos(var f:TypedFile):Int64;[IOCheck];
  188. Begin
  189. FilePos:=System.FilePos(f);
  190. { in case of reading a file, the buffer is always filled, so the result of Do_FilePos is off by one }
  191. if (FileRec(f).mode=fmInOut) or
  192. (FileRec(f).mode=fmInput) then
  193. dec(FilePos);
  194. End;
  195. {$ifndef FPUNONE}
  196. {$ifdef FPC_CURRENCY_IS_INT64}
  197. function round(c : currency) : int64;
  198. begin
  199. if c>=0.0 then
  200. Round:=Trunc(c+0.5)
  201. else
  202. Round:=Trunc(c-0.5);
  203. end;
  204. {$ifndef cpujvm}
  205. function round(c : comp) : int64;
  206. begin
  207. if c>=0.0 then
  208. round:=Trunc(c+0.5)
  209. else
  210. round:=Trunc(c-0.5);
  211. end;
  212. {$else not cpujvm}
  213. function round_comp(c : comp) : int64;
  214. begin
  215. if c>=0.0 then
  216. round_comp:=Trunc(c+0.5)
  217. else
  218. round_comp:=Trunc(c-0.5);
  219. end;
  220. {$endif cpujvm}
  221. {$endif FPC_CURRENCY_IS_INT64}
  222. function Round(d : ValReal) : int64;
  223. begin
  224. if d>=0.0 then
  225. Round:=Trunc(d+0.5)
  226. else
  227. Round:=Trunc(d-0.5);
  228. end;
  229. {$endif FPUNONE}
  230. begin
  231. { we shouldn't do this because it might confuse user programs, but for now it
  232. is good enough to get pretty unique tmp file names }
  233. {$ifdef FPC_HAS_FEATURE_RANDOM}
  234. Randomize;
  235. {$endif FPC_HAS_FEATURE_RANDOM}
  236. { reset opens with read-only }
  237. Filemode:=0;
  238. end.