typefile.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. Procedure Assign(out f:TypedFile;const Name:string);
  14. {
  15. Assign Name to file f so it can be used with the file routines
  16. }
  17. Begin
  18. FillChar(f,SizeOF(FileRec),0);
  19. FileRec(f).Handle:=UnusedHandle;
  20. FileRec(f).mode:=fmClosed;
  21. Move(Name[1],FileRec(f).Name,Length(Name));
  22. End;
  23. Procedure Assign(out f:TypedFile;p:pchar);
  24. {
  25. Assign Name to file f so it can be used with the file routines
  26. }
  27. begin
  28. Assign(f,StrPas(p));
  29. end;
  30. Procedure Assign(out f:TypedFile;c:char);
  31. {
  32. Assign Name to file f so it can be used with the file routines
  33. }
  34. begin
  35. Assign(f,string(c));
  36. end;
  37. Procedure fpc_reset_typed(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED']; compilerproc;
  38. Begin
  39. If InOutRes <> 0 then
  40. exit;
  41. Reset(UnTypedFile(f),Size);
  42. End;
  43. Procedure fpc_rewrite_typed(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_TYPED']; compilerproc;
  44. Begin
  45. If InOutRes <> 0 then
  46. exit;
  47. Rewrite(UnTypedFile(f),Size);
  48. End;
  49. {$ifdef FPC_HAS_FEATURE_RANDOM}
  50. { this code is duplicated in the iso7185 unit }
  51. Procedure DoAssign(var t : TypedFile);
  52. Begin
  53. Assign(t,'fpc_'+HexStr(random(1000000000),8)+'.tmp');
  54. End;
  55. {$else FPC_HAS_FEATURE_RANDOM}
  56. { this code is duplicated in the iso7185 unit }
  57. Procedure DoAssign(var t : TypedFile);
  58. const
  59. start : dword = 0;
  60. Begin
  61. Assign(t,'fpc_'+HexStr(start,8)+'.tmp');
  62. inc(start);
  63. End;
  64. {$endif FPC_HAS_FEATURE_RANDOM}
  65. Procedure fpc_reset_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED_ISO']; compilerproc;
  66. Begin
  67. If InOutRes <> 0 then
  68. exit;
  69. { create file name? }
  70. if FileRec(f).mode=0 then
  71. DoAssign(f);
  72. Reset(UnTypedFile(f),Size);
  73. End;
  74. Procedure fpc_rewrite_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_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. Rewrite(UnTypedFile(f),Size);
  82. End;
  83. Procedure fpc_typed_write(TypeSize : Longint;var f : TypedFile;const Buf);[IOCheck, Public, Alias :'FPC_TYPED_WRITE']; compilerproc;
  84. Begin
  85. If InOutRes <> 0 then
  86. exit;
  87. case fileRec(f).mode of
  88. fmOutPut,fmInOut:
  89. Do_Write(FileRec(f).Handle,@Buf,TypeSize);
  90. fmInput: inOutRes := 105;
  91. else inOutRes := 103;
  92. end;
  93. End;
  94. Procedure fpc_typed_read(TypeSize : Longint;var f : TypedFile;out Buf);[IOCheck, Public, Alias :'FPC_TYPED_READ']; compilerproc;
  95. var
  96. Result : Longint;
  97. Begin
  98. If InOutRes <> 0 then
  99. exit;
  100. case FileRec(f).mode of
  101. fmInput,fmInOut:
  102. begin
  103. Result:=Do_Read(FileRec(f).Handle,@Buf,TypeSize);
  104. If Result<TypeSize Then
  105. InOutRes:=100
  106. end;
  107. fmOutPut: inOutRes := 104
  108. else inOutRes := 103;
  109. end;
  110. End;