iso7185.pp 6.2 KB

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