filectrl.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. {
  2. $Id$
  3. System independent low-level file interface for Win32
  4. Copyright (c) 1999 by Florian Klaempfl
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. }
  17. uses
  18. Windows;
  19. function OpenFileStr(FName: PChar; Flags: Longint): TFileHandle;
  20. begin
  21. SetLastError(0);
  22. OpenFileStr :=
  23. Windows.CreateFile(FName,
  24. GENERIC_READ OR GENERIC_WRITE,
  25. FILE_SHARE_READ OR FILE_SHARE_WRITE,
  26. nil,
  27. OPEN_EXISTING,
  28. FILE_ATTRIBUTE_NORMAL,
  29. 0);
  30. ErrorCode := GetLastError;
  31. end; { func. OpenFileStr }
  32. function CreateFileStr(FName: PChar): TFileHandle;
  33. begin
  34. SetLastError(0);
  35. CreateFileStr :=
  36. Windows.CreateFile(FName,
  37. GENERIC_READ OR GENERIC_WRITE,
  38. FILE_SHARE_READ OR FILE_SHARE_WRITE,
  39. nil,
  40. CREATE_ALWAYS,
  41. FILE_ATTRIBUTE_NORMAL,
  42. 0);
  43. ErrorCode := GetLastError;
  44. end;
  45. procedure DeleteFileStr(FName: PChar);
  46. begin
  47. ErrorCode:=0;
  48. if NOT Windows.DeleteFile(FName) then
  49. ErrorCode:=GetLastError;
  50. end;
  51. procedure CloseFile(Handle: TFileHandle);
  52. begin
  53. ErrorCode:=0;
  54. if NOT CloseHandle(Handle) then
  55. ErrorCode:=GetLastError;
  56. end;
  57. function SeekFile(Handle: TFileHandle; Pos: TFileInt; SeekType: Word): TFileInt;
  58. var tmp: longint;
  59. begin
  60. ErrorCode:=0;
  61. Tmp := SetFilePointer(Handle, Pos, nil, SeekType);
  62. if tmp =$ffffffff then
  63. begin
  64. ErrorCode:=GetLastError;
  65. SeekFile := 0
  66. end
  67. else SeekFile := tmp;
  68. end;
  69. function ReadFile(Handle: TFileHandle; var Buff; Count: CPUWord): CPUWord;
  70. var
  71. Result : CPUWord;
  72. begin
  73. ErrorCode:=0;
  74. if Windows.ReadFile(Handle, Buff, Count, Result, nil) then
  75. ErrorCode:=GetLastError;
  76. ReadFile:=result;
  77. end;
  78. function WriteFile(Handle: TFileHandle; var Buff; Count: CPUWord): CPUWord;
  79. var
  80. Written : CPUWord;
  81. Size: Longint;
  82. begin
  83. ErrorCode:=0;
  84. if Windows.WriteFile(Handle, Buff, Count, Size, nil) then
  85. ErrorCode:=GetLastError;
  86. WriteFile:=Written;
  87. end;
  88. procedure FlushFile(Handle: TFileHandle);
  89. begin
  90. ErrorCode:=0;
  91. if FlushFileBuffers(Handle) then
  92. ErrorCode:=GetLastError;
  93. end;
  94. procedure TruncateFile(Handle: TFileHandle);
  95. begin
  96. ErrorCode:=0;
  97. SeekFile(Handle, 0, skEnd);
  98. if not(SetEndOfFile(Handle)) then
  99. ErrorCode:=GetLastError;
  100. end;
  101. function EndOfFile(Handle: TFileHandle): Boolean;
  102. begin
  103. ErrorCode:=0;
  104. EndOfFile := FilePos(Handle) >= FileSize(Handle);
  105. end;
  106. function FilePos(Handle: TFileHandle): TFileInt;
  107. var
  108. l : TFileInt;
  109. begin
  110. ErrorCode:=0;
  111. l:=SetFilePointer(Handle, 0, nil, FILE_CURRENT);
  112. if l=-1 then
  113. begin
  114. l:=0;
  115. ErrorCode:=GetLastError;
  116. end;
  117. FilePos:=l;
  118. end;
  119. function FileSize(Handle: TFileHandle): TFileInt;
  120. var
  121. aktfilepos : TFileInt;
  122. begin
  123. SetLastError(0);
  124. AktFilePos := FilePos(Handle);
  125. FileSize := SeekFile(Handle, 0, skEnd);
  126. SeekFile(Handle, aktfilepos, skBeg);
  127. end;
  128. {
  129. $Log$
  130. Revision 1.2 2000-07-13 11:32:27 michael
  131. + removed logs
  132. }