Add weapon cycling bindings for mouse and joystick buttons. Add weapon cycling bindi...
[chocolate-doom.git] / src / w_file_win32.c
blob9e5d963fadbce2413313f3b2313d5369c40096d8
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2008 Simon Howard
6 //
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
20 // 02111-1307, USA.
22 // DESCRIPTION:
23 // WAD I/O functions.
25 //-----------------------------------------------------------------------------
27 #include "config.h"
29 #ifdef _WIN32
31 #include <stdio.h>
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
36 #include "i_system.h"
37 #include "w_file.h"
38 #include "z_zone.h"
40 // This constant doesn't exist in VC6:
42 #ifndef INVALID_SET_FILE_POINTER
43 #define INVALID_SET_FILE_POINTER 0xffffffff
44 #endif
46 typedef struct
48 wad_file_t wad;
49 HANDLE handle;
50 HANDLE handle_map;
51 } win32_wad_file_t;
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,
58 NULL,
59 PAGE_WRITECOPY,
62 NULL);
64 if (wad->handle_map == NULL)
66 fprintf(stderr, "W_Win32_OpenFile: Unable to CreateFileMapping() "
67 "for %s\n", filename);
68 return;
71 wad->wad.mapped = MapViewOfFile(wad->handle_map,
72 FILE_MAP_COPY,
73 0, 0, 0);
75 if (wad->wad.mapped == NULL)
77 fprintf(stderr, "W_Win32_OpenFile: Unable to MapViewOfFile() for %s\n",
78 filename);
82 unsigned int GetFileLength(HANDLE handle)
84 DWORD result;
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");
93 return result;
96 static wad_file_t *W_Win32_OpenFile(char *path)
98 win32_wad_file_t *result;
99 wchar_t wpath[MAX_PATH + 1];
100 HANDLE handle;
102 // Open the file:
104 MultiByteToWideChar(CP_OEMCP, 0,
105 path, strlen(path) + 1,
106 wpath, sizeof(wpath));
108 handle = CreateFileW(wpath,
109 GENERIC_READ,
110 FILE_SHARE_READ,
111 NULL,
112 OPEN_EXISTING,
113 FILE_ATTRIBUTE_NORMAL,
114 NULL);
116 if (handle == INVALID_HANDLE_VALUE)
118 return NULL;
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);
132 return &result->wad;
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);
153 // Close the file
155 if (win32_wad->handle != NULL)
157 CloseHandle(win32_wad->handle);
160 Z_Free(win32_wad);
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;
170 DWORD bytes_read;
171 DWORD result;
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",
182 offset);
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");
192 return bytes_read;
196 wad_file_class_t win32_wad_file =
198 W_Win32_OpenFile,
199 W_Win32_CloseFile,
200 W_Win32_Read,
204 #endif /* #ifdef _WIN32 */