typefile.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the File COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. subroutines for typed file handling
  12. ****************************************************************************}
  13. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  14. Procedure Assign(out f:TypedFile;const Name: UnicodeString);
  15. {
  16. Assign Name to file f so it can be used with the file routines
  17. }
  18. Begin
  19. Assign(UnTypedFile(f),Name);
  20. End;
  21. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  22. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  23. Procedure Assign(out f:TypedFile;const Name: RawByteString);
  24. {
  25. Assign Name to file f so it can be used with the file routines
  26. }
  27. Begin
  28. Assign(UnTypedFile(f),Name);
  29. End;
  30. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  31. Procedure Assign(out f:TypedFile;const Name: ShortString);
  32. {
  33. Assign Name to file f so it can be used with the file routines
  34. }
  35. Begin
  36. Assign(UnTypedFile(f),Name);
  37. End;
  38. Procedure Assign(out f:TypedFile;const p:PAnsiChar);
  39. Begin
  40. Assign(UnTypedFile(f),p);
  41. end;
  42. Procedure Assign(out f:TypedFile;const c:AnsiChar);
  43. Begin
  44. Assign(UnTypedFile(f),c);
  45. end;
  46. Procedure fpc_reset_typed(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED']; compilerproc;
  47. Begin
  48. Reset(UnTypedFile(f),Size);
  49. End;
  50. Procedure fpc_rewrite_typed(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_TYPED']; compilerproc;
  51. Begin
  52. Rewrite(UnTypedFile(f),Size);
  53. End;
  54. {$i isotmp.inc}
  55. {$ifdef FPC_HAS_FEATURE_RANDOM}
  56. { this code is duplicated in the iso7185 unit }
  57. Procedure DoAssign(var t : TypedFile);
  58. Begin
  59. Assign(t,getTempDir+'fpc_'+HexStr(random(1000000000),8)+'.tmp');
  60. End;
  61. {$else FPC_HAS_FEATURE_RANDOM}
  62. { this code is duplicated in the iso7185 unit }
  63. Procedure DoAssign(var t : TypedFile);
  64. const
  65. start : dword = 0;
  66. Begin
  67. {$ifdef EXCLUDE_COMPLEX_PROCS}
  68. runerror(219);
  69. {$else EXCLUDE_COMPLEX_PROCS}
  70. Assign(t,getTempDir+'fpc_'+HexStr(start,8)+'.tmp');
  71. inc(start);
  72. {$endif EXCLUDE_COMPLEX_PROCS}
  73. End;
  74. {$endif FPC_HAS_FEATURE_RANDOM}
  75. Procedure fpc_reset_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED_ISO']; compilerproc;
  76. Begin
  77. If InOutRes <> 0 then
  78. exit;
  79. { create file name? }
  80. if FileRec(f).mode=0 then
  81. DoAssign(f);
  82. { use _private[1] to track eof }
  83. FileRec(f)._private[1]:=0;
  84. Reset(UnTypedFile(f),Size);
  85. BlockRead(UntypedFile(f),(pbyte(@f)+sizeof(FileRec))^,1);
  86. End;
  87. Procedure fpc_rewrite_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_TYPED_ISO']; compilerproc;
  88. Begin
  89. If InOutRes <> 0 then
  90. exit;
  91. { create file name? }
  92. if FileRec(f).mode=0 then
  93. DoAssign(f);
  94. Rewrite(UnTypedFile(f),Size);
  95. End;
  96. Procedure fpc_reset_typed_name_iso(var f : TypedFile;const FileName : String;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED_NAME_ISO']; compilerproc;
  97. Begin
  98. If InOutRes <> 0 then
  99. exit;
  100. { create file name? }
  101. if FileRec(f).mode=0 then
  102. Assign(f,FileName);
  103. { use _private[1] to track eof }
  104. FileRec(f)._private[1]:=0;
  105. Reset(UnTypedFile(f),Size);
  106. BlockRead(UntypedFile(f),(pbyte(@f)+sizeof(FileRec))^,1);
  107. End;
  108. Procedure fpc_rewrite_typed_name_iso(var f : TypedFile;const FileName : String;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_TYPED_NAME_ISO']; compilerproc;
  109. Begin
  110. If InOutRes <> 0 then
  111. exit;
  112. { create file name? }
  113. if FileRec(f).mode=0 then
  114. Assign(f,FileName);
  115. Rewrite(UnTypedFile(f),Size);
  116. End;
  117. Procedure fpc_typed_write(TypeSize : Longint;var f : TypedFile;const Buf);[IOCheck, Public, Alias :'FPC_TYPED_WRITE']; compilerproc;
  118. Begin
  119. If InOutRes <> 0 then
  120. exit;
  121. case fileRec(f).mode of
  122. fmOutPut,fmInOut:
  123. Do_Write(FileRec(f).Handle,@Buf,TypeSize);
  124. fmInput: inOutRes := 105;
  125. else inOutRes := 103;
  126. end;
  127. End;
  128. Procedure fpc_typed_read(TypeSize : Longint;var f : TypedFile;out Buf);[IOCheck, Public, Alias :'FPC_TYPED_READ']; compilerproc;
  129. var
  130. Result : Longint;
  131. Begin
  132. If InOutRes <> 0 then
  133. exit;
  134. case FileRec(f).mode of
  135. fmInput,fmInOut:
  136. begin
  137. Result:=Do_Read(FileRec(f).Handle,@Buf,TypeSize);
  138. If Result<TypeSize Then
  139. InOutRes:=100
  140. end;
  141. fmOutPut: inOutRes := 104
  142. else inOutRes := 103;
  143. end;
  144. End;
  145. Procedure fpc_typed_read_iso(TypeSize : Longint;var f : TypedFile;out Buf);[IOCheck, Public, Alias :'FPC_TYPED_READ_ISO']; compilerproc;
  146. Begin
  147. move((pbyte(@f)+sizeof(TypedFile))^,Buf,TypeSize);
  148. if not(eof(f)) then
  149. BlockRead(f,(pbyte(@f)+sizeof(FileRec))^,1)
  150. else
  151. FileRec(f)._private[1]:=1;
  152. End;
  153. function fpc_GetBuf_TypedFile(var f : TypedFile) : pointer; [IOCheck]; compilerproc;
  154. Begin
  155. Result:=pbyte(@f)+sizeof(TypedFile);
  156. end;