objinc.inc 6.3 KB

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