iso7185.pp 7.4 KB

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