objinc.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Includefile for objects.pp implementing OS-dependent file routines
  6. for WIN32
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program 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.
  12. **********************************************************************
  13. }
  14. CONST
  15. { REQUIRED TO PUT MANUALLY here, because of name conflicts in win32.inc }
  16. { flags for CreateFile }
  17. GENERIC_READ=longint($80000000);
  18. GENERIC_WRITE=$40000000;
  19. CREATE_NEW = 1;
  20. CREATE_ALWAYS = 2;
  21. OPEN_EXISTING = 3;
  22. OPEN_ALWAYS = 4;
  23. TRUNCATE_EXISTING = 5;
  24. FILE_ATTRIBUTE_ARCHIVE = 32;
  25. FILE_ATTRIBUTE_COMPRESSED = 2048;
  26. FILE_ATTRIBUTE_NORMAL = 128;
  27. FILE_ATTRIBUTE_DIRECTORY = 16;
  28. FILE_ATTRIBUTE_HIDDEN = 2;
  29. FILE_ATTRIBUTE_READONLY = 1;
  30. FILE_ATTRIBUTE_SYSTEM = 4;
  31. FILE_ATTRIBUTE_TEMPORARY = 256;
  32. { flags for SetFilePos }
  33. FILE_BEGIN = 0;
  34. FILE_CURRENT = 1;
  35. FILE_END = 2;
  36. { misc. functions }
  37. function GetLastError : DWORD;
  38. external 'kernel32' name 'GetLastError';
  39. function WriteFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  40. overlap:pointer):longint;
  41. external 'kernel32' name 'WriteFile';
  42. function ReadFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  43. overlap:pointer):longint;
  44. external 'kernel32' name 'ReadFile';
  45. function CloseHandle(h : longint) : longint;
  46. external 'kernel32' name 'CloseHandle';
  47. function DeleteFile(p : pchar) : longint;
  48. external 'kernel32' name 'DeleteFileA';
  49. function MoveFile(old,_new : pchar) : longint;
  50. external 'kernel32' name 'MoveFileA';
  51. function SetFilePointer(l1,l2 : longint;l3 : pointer;l4 : longint) : longint;
  52. external 'kernel32' name 'SetFilePointer';
  53. function GetFileSize(h:longint;p:pointer) : longint;
  54. external 'kernel32' name 'GetFileSize';
  55. function CreateFile(name : pointer;access,sharing : longint;
  56. security : pointer;how,attr,template : longint) : longint;
  57. external 'kernel32' name 'CreateFileA';
  58. function SetEndOfFile(h : longint) : boolean;
  59. external 'kernel32' name 'SetEndOfFile';
  60. function GetFileType(Handle:DWORD):DWord;
  61. external 'kernel32' name 'GetFileType';
  62. {---------------------------------------------------------------------------}
  63. { FileClose -> Platforms WIN32 - Not checked }
  64. {---------------------------------------------------------------------------}
  65. FUNCTION FileClose(Handle: THandle): word;
  66. begin
  67. closehandle(handle);
  68. FileClose := 0;
  69. end;
  70. {---------------------------------------------------------------------------}
  71. { FileOpen -> Platforms WIN32 - Tested MVC }
  72. { Returns 0 on failure }
  73. {---------------------------------------------------------------------------}
  74. FUNCTION FileOpen (Var FileName: AsciiZ; Mode: Word): THandle;
  75. var
  76. oflags,cd: longint;
  77. AHandle : longint;
  78. begin
  79. { On opening reset error code }
  80. DosStreamError := 0;
  81. if Mode=stCreate then
  82. Begin
  83. cd:=CREATE_ALWAYS;
  84. oflags:=GENERIC_WRITE or GENERIC_READ;
  85. End
  86. else
  87. Begin
  88. cd:=OPEN_EXISTING;
  89. { convert filemode to filerec modes }
  90. case (Mode and 3) of
  91. 0 : oflags:=GENERIC_READ;
  92. 1 : oflags:=GENERIC_WRITE;
  93. 2 : oflags:=GENERIC_WRITE or GENERIC_READ;
  94. end;
  95. end;
  96. AHandle:=CreateFile(pointer(@FileName),oflags,0,nil,cd,FILE_ATTRIBUTE_NORMAL,0);
  97. if AHandle = -1 then
  98. begin
  99. FileOpen:=0;
  100. DosStreamError:=word(GetLastError);
  101. end
  102. else
  103. FileOpen := AHandle;
  104. end;
  105. {***************************************************************************}
  106. { DosSetFilePtr -> Platforms WIN32 - Tested MVC }
  107. {***************************************************************************}
  108. FUNCTION SetFilePos (Handle: THandle; Pos: LongInt; MoveType: Word;
  109. Var Actual: LongInt): Word;
  110. BEGIN
  111. { WARNING WIN32 CURRECTLY HAS THE SAME SEEK MODE AS MSDOS }
  112. { if this changes don't forget to change and check the flags }
  113. { accordingly. }
  114. Actual:=SetFilePointer(handle,pos,nil,MoveType);
  115. If Actual=-1 then
  116. DosStreamError:=word(GetLastError);
  117. SetFilePos := DosStreamError; { Return any error }
  118. END;
  119. {---------------------------------------------------------------------------}
  120. { FileRead -> Platforms WIN32 - Tested MVC }
  121. {---------------------------------------------------------------------------}
  122. FUNCTION FileRead (Handle: THandle; Var Buf; Count: Sw_Word;
  123. Var Actual: Sw_Word): Word;
  124. Var res : longint;
  125. BEGIN
  126. res:=0;
  127. if readfile(handle,pointer(@buf),count,res,nil)=0 then
  128. DosStreamError:=word(GetLastError);
  129. Actual:=res;
  130. FileRead:=DosStreamError;
  131. end;
  132. {---------------------------------------------------------------------------}
  133. { FileWrite -> Platforms WIN32 - Not Checked }
  134. {---------------------------------------------------------------------------}
  135. FUNCTION FileWrite (Handle: THandle; Var Buf; Count: Sw_Word; Var Actual: Sw_Word): Word;
  136. BEGIN
  137. if writefile(handle,pointer(@buf),count,Actual,nil)=0 then
  138. Begin
  139. DosStreamError:=word(GetLasterror);
  140. end;
  141. FileWrite:=DosStreamError;
  142. end;
  143. {---------------------------------------------------------------------------}
  144. { SetFileSize -> Platforms DOS - Not Checked }
  145. {---------------------------------------------------------------------------}
  146. FUNCTION SetFileSize (Handle: THandle; FileSize: LongInt): Word;
  147. VAR Actual, Buf: LongInt;
  148. BEGIN
  149. SetFilePos(Handle,FileSize,0,Actual);
  150. If (Actual = FileSize) Then
  151. Begin
  152. Actual := FileWrite(Handle, Buf, 0,Actual); { Truncate the file }
  153. If (Actual <> -1) Then
  154. SetFileSize := 0
  155. Else
  156. SetFileSize := 103; { File truncate error }
  157. End
  158. Else
  159. SetFileSize := 103; { File truncate error }
  160. END;
  161. {
  162. $Log$
  163. Revision 1.3 2000-12-18 17:28:58 jonas
  164. * fixed range check errors
  165. Revision 1.2 2000/07/13 11:33:57 michael
  166. + removed logs
  167. }