objinc.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993-98 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=$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 - Never checked }
  72. { Returns 0 on failure }
  73. {---------------------------------------------------------------------------}
  74. FUNCTION FileOpen (Var FileName: AsciiZ; Mode: Word): THandle;
  75. var
  76. oflags,cd: longint;
  77. AHandle : THandle;
  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 = 0 then
  98. DosStreamError:=word(GetLastError);
  99. FileOpen := AHandle;
  100. end;
  101. {***************************************************************************}
  102. { DosSetFilePtr -> Platforms WIN32 - Not Checked }
  103. {***************************************************************************}
  104. FUNCTION SetFilePos (Handle: THandle; Pos: LongInt; MoveType: Word;
  105. Var Actual: LongInt): Word;
  106. BEGIN
  107. { WARNING WIN32 CURRECTLY HAS THE SAME SEEK MODE AS MSDOS }
  108. { if this changes don't forget to change and check the flags }
  109. { accordingly. }
  110. if SetFilePointer(handle,pos,nil,MoveType)=-1 then
  111. DosStreamError:=word(GetLastError);
  112. Actual := pos;
  113. SetFilePos := DosStreamError; { Return any error }
  114. END;
  115. {---------------------------------------------------------------------------}
  116. { FileRead -> Platforms WIN32 - Not checked }
  117. {---------------------------------------------------------------------------}
  118. FUNCTION FileRead (Handle: THandle; Var Buf; Count: Sw_Word;
  119. Var Actual: Sw_Word): Word;
  120. BEGIN
  121. if readfile(handle,pointer(@buf),count,Actual,nil)=0 then
  122. Begin
  123. DosStreamError:=word(GetLastError);
  124. end;
  125. FileRead:=DosStreamError;
  126. end;
  127. {---------------------------------------------------------------------------}
  128. { FileWrite -> Platforms WIN32 - Not Checked }
  129. {---------------------------------------------------------------------------}
  130. FUNCTION FileWrite (Handle: THandle; Var Buf; Count: Sw_Word; Var Actual: Sw_Word): Word;
  131. BEGIN
  132. if writefile(handle,pointer(@buf),count,Actual,nil)=0 then
  133. Begin
  134. DosStreamError:=word(GetLasterror);
  135. end;
  136. FileWrite:=DosStreamError;
  137. end;
  138. {---------------------------------------------------------------------------}
  139. { SetFileSize -> Platforms DOS - Not Checked }
  140. {---------------------------------------------------------------------------}
  141. FUNCTION SetFileSize (Handle: THandle; FileSize: LongInt): Word;
  142. VAR Actual, Buf: LongInt;
  143. BEGIN
  144. SetFilePos(Handle,FileSize,0,Actual);
  145. If (Actual = FileSize) Then
  146. Begin
  147. Actual := FileWrite(Handle, Pointer(@Buf), 0,Actual); { Truncate the file }
  148. If (Actual <> -1) Then
  149. SetFileSize := 0
  150. Else
  151. SetFileSize := 103; { File truncate error }
  152. End
  153. Else
  154. SetFileSize := 103; { File truncate error }
  155. END;
  156. {
  157. $Log$
  158. Revision 1.1 1998-07-07 12:38:46 carl
  159. + First version
  160. Revision 1.4 1998/07/06 12:26:19 carl
  161. * Glurbl.... now work perfectly! Do not change :)
  162. Revision 1.3 1998/07/02 12:25:27 carl
  163. * NOTHING would work, FileOpen is now correct!!
  164. Revision 1.2 1998/05/31 14:18:18 peter
  165. * force att or direct assembling
  166. * cleanup of some files
  167. }