typefile.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. {$ifdef FPC_HAS_FEATURE_RANDOM}
  55. { this code is duplicated in the iso7185 unit }
  56. Procedure DoAssign(var t : TypedFile);
  57. Begin
  58. Assign(t,'fpc_'+HexStr(random(1000000000),8)+'.tmp');
  59. End;
  60. {$else FPC_HAS_FEATURE_RANDOM}
  61. { this code is duplicated in the iso7185 unit }
  62. Procedure DoAssign(var t : TypedFile);
  63. const
  64. start : dword = 0;
  65. Begin
  66. {$ifdef EXCLUDE_COMPLEX_PROCS}
  67. runerror(219);
  68. {$else EXCLUDE_COMPLEX_PROCS}
  69. Assign(t,'fpc_'+HexStr(start,8)+'.tmp');
  70. inc(start);
  71. {$endif EXCLUDE_COMPLEX_PROCS}
  72. End;
  73. {$endif FPC_HAS_FEATURE_RANDOM}
  74. Procedure fpc_reset_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED_ISO']; compilerproc;
  75. Begin
  76. If InOutRes <> 0 then
  77. exit;
  78. { create file name? }
  79. if FileRec(f).mode=0 then
  80. DoAssign(f);
  81. Reset(UnTypedFile(f),Size);
  82. BlockRead(UntypedFile(f),(pbyte(@f)+sizeof(FileRec))^,1);
  83. End;
  84. Procedure fpc_rewrite_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_TYPED_ISO']; compilerproc;
  85. Begin
  86. If InOutRes <> 0 then
  87. exit;
  88. { create file name? }
  89. if FileRec(f).mode=0 then
  90. DoAssign(f);
  91. Rewrite(UnTypedFile(f),Size);
  92. End;
  93. Procedure fpc_typed_write(TypeSize : Longint;var f : TypedFile;const Buf);[IOCheck, Public, Alias :'FPC_TYPED_WRITE']; compilerproc;
  94. Begin
  95. If InOutRes <> 0 then
  96. exit;
  97. case fileRec(f).mode of
  98. fmOutPut,fmInOut:
  99. Do_Write(FileRec(f).Handle,@Buf,TypeSize);
  100. fmInput: inOutRes := 105;
  101. else inOutRes := 103;
  102. end;
  103. End;
  104. Procedure fpc_typed_read(TypeSize : Longint;var f : TypedFile;out Buf);[IOCheck, Public, Alias :'FPC_TYPED_READ']; compilerproc;
  105. var
  106. Result : Longint;
  107. Begin
  108. If InOutRes <> 0 then
  109. exit;
  110. case FileRec(f).mode of
  111. fmInput,fmInOut:
  112. begin
  113. Result:=Do_Read(FileRec(f).Handle,@Buf,TypeSize);
  114. If Result<TypeSize Then
  115. InOutRes:=100
  116. end;
  117. fmOutPut: inOutRes := 104
  118. else inOutRes := 103;
  119. end;
  120. End;
  121. Procedure fpc_typed_read_iso(TypeSize : Longint;var f : TypedFile;out Buf);[IOCheck, Public, Alias :'FPC_TYPED_READ_ISO']; compilerproc;
  122. var
  123. Result : Longint;
  124. Begin
  125. move((pbyte(@f)+sizeof(TypedFile))^,Buf,TypeSize);
  126. if not(eof(f)) then
  127. BlockRead(f,(pbyte(@f)+sizeof(FileRec))^,1)
  128. End;
  129. function fpc_GetBuf_TypedFile(var f : TypedFile) : pointer; [IOCheck]; compilerproc;
  130. Begin
  131. Result:=pbyte(@f)+sizeof(TypedFile);
  132. end;