1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2008 Simon Howard
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 //-----------------------------------------------------------------------------
33 #define WIN32_LEAN_AND_MEAN
40 // This constant doesn't exist in VC6:
42 #ifndef INVALID_SET_FILE_POINTER
43 #define INVALID_SET_FILE_POINTER 0xffffffff
53 extern wad_file_class_t win32_wad_file
;
55 static void MapFile(win32_wad_file_t
*wad
, char *filename
)
57 wad
->handle_map
= CreateFileMapping(wad
->handle
,
64 if (wad
->handle_map
== NULL
)
66 fprintf(stderr
, "W_Win32_OpenFile: Unable to CreateFileMapping() "
67 "for %s\n", filename
);
71 wad
->wad
.mapped
= MapViewOfFile(wad
->handle_map
,
75 if (wad
->wad
.mapped
== NULL
)
77 fprintf(stderr
, "W_Win32_OpenFile: Unable to MapViewOfFile() for %s\n",
82 unsigned int GetFileLength(HANDLE handle
)
86 result
= SetFilePointer(handle
, 0, NULL
, FILE_END
);
88 if (result
== INVALID_SET_FILE_POINTER
)
90 I_Error("W_Win32_OpenFile: Failed to read file length");
96 static wad_file_t
*W_Win32_OpenFile(char *path
)
98 win32_wad_file_t
*result
;
99 wchar_t wpath
[MAX_PATH
+ 1];
104 MultiByteToWideChar(CP_OEMCP
, 0,
105 path
, strlen(path
) + 1,
106 wpath
, sizeof(wpath
));
108 handle
= CreateFileW(wpath
,
113 FILE_ATTRIBUTE_NORMAL
,
116 if (handle
== INVALID_HANDLE_VALUE
)
121 // Create a new win32_wad_file_t to hold the file handle.
123 result
= Z_Malloc(sizeof(win32_wad_file_t
), PU_STATIC
, 0);
124 result
->wad
.file_class
= &win32_wad_file
;
125 result
->wad
.length
= GetFileLength(handle
);
126 result
->handle
= handle
;
128 // Try to map the file into memory with mmap:
130 MapFile(result
, path
);
135 static void W_Win32_CloseFile(wad_file_t
*wad
)
137 win32_wad_file_t
*win32_wad
;
139 win32_wad
= (win32_wad_file_t
*) wad
;
141 // If mapped, unmap it.
143 if (win32_wad
->wad
.mapped
!= NULL
)
145 UnmapViewOfFile(win32_wad
->wad
.mapped
);
148 if (win32_wad
->handle_map
!= NULL
)
150 CloseHandle(win32_wad
->handle_map
);
155 if (win32_wad
->handle
!= NULL
)
157 CloseHandle(win32_wad
->handle
);
163 // Read data from the specified position in the file into the
164 // provided buffer. Returns the number of bytes read.
166 size_t W_Win32_Read(wad_file_t
*wad
, unsigned int offset
,
167 void *buffer
, size_t buffer_len
)
169 win32_wad_file_t
*win32_wad
;
173 win32_wad
= (win32_wad_file_t
*) wad
;
175 // Jump to the specified position in the file.
177 result
= SetFilePointer(win32_wad
->handle
, offset
, NULL
, FILE_BEGIN
);
179 if (result
== INVALID_SET_FILE_POINTER
)
181 I_Error("W_Win32_Read: Failed to set file pointer to %i",
185 // Read into the buffer.
187 if (!ReadFile(win32_wad
->handle
, buffer
, buffer_len
, &bytes_read
, NULL
))
189 I_Error("W_Win32_Read: Error reading from file");
196 wad_file_class_t win32_wad_file
=
204 #endif /* #ifdef _WIN32 */