1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // libwad: Doom WAD format interface library.
5 // Copyright (C) 2011 by Callum Dickinson.
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.
11 //
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.
16 //-----------------------------------------------------------------------------
17 /// \file wad.h
18 /// \brief WAD file manipulation library.
19 
20 #ifndef __WAD_H__
21 #define __WAD_H__
22 
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif
27 
28 //
29 // Exact-width integer types.
30 //
31 #ifdef _MSC_VER
32 typedef signed __int8 wad_int8_t;
33 typedef unsigned __int8 wad_uint8_t;
34 
35 typedef __int16 wad_int16_t;
36 typedef unsigned __int16 wad_uint16_t;
37 
38 typedef __int32 wad_int32_t;
39 typedef unsigned __int32 wad_uint32_t;
40 
41 typedef __int64 wad_int64_t;
42 typedef unsigned __int64 wad_uint64_t;
43 
44 // Older Visual C++ headers don't have the Win64-compatible typedefs...
45 #if ((_MSC_VER <= 1200) && (!defined(DWORD_PTR)))
46 #define DWORD_PTR DWORD
47 #endif
48 
49 #if ((_MSC_VER <= 1200) && (!defined(PDWORD_PTR)))
50 #define PDWORD_PTR PDWORD
51 #endif
52 
53 #elif defined (_arch_dreamcast)
54 #include <arch/types.h>
55 
56 typedef signed char wad_int8_t;
57 typedef unsigned char wad_uint8_t;
58 
59 typedef int16 wad_int16_t;
60 typedef uint16 wad_uint16_t;
61 
62 typedef int wad_int32_t;
63 typedef unsigned int wad_uint32_t;
64 
65 typedef int64 wad_int64_t;
66 typedef uint64 wad_uint64_t;
67 #elif defined (__DJGPP__)
68 typedef signed char wad_int8_t;
69 typedef unsigned char wad_uint8_t;
70 
71 typedef signed short int wad_int16_t;
72 typedef unsigned short int wad_uint16_t;
73 
74 typedef signed long wad_int32_t;
75 typedef unsigned long wad_uint32_t;
76 
77 typedef signed long long wad_int64_t;
78 typedef unsigned long long wad_uint64_t;
79 #else
80 #define __STDC_LIMIT_MACROS
81 #include <stdint.h>
82 
83 typedef int8_t wad_int8_t;
84 typedef uint8_t wad_uint8_t;
85 
86 typedef int16_t wad_int16_t;
87 typedef uint16_t wad_uint16_t;
88 
89 typedef int32_t wad_int32_t;
90 typedef uint32_t wad_uint32_t;
91 
92 typedef int64_t wad_int64_t;
93 typedef uint64_t wad_uint64_t;
94 #endif
95 
96 typedef enum
97 {
98 	false,
99 	true
100 } wad_boolean_t;
101 
102 typedef struct
103 {
104 	char id[4];		// The WAD header's 4-character ID.
105 	wad_uint32_t numlumps;	// The number of lumps the WAD has.
106 	wad_uint32_t lumpdir;	// The pointer to the lump directory.
107 } wadheader_t;
108 
109 typedef struct wad_s
110 {
111 	char *filename;		// The filename of the open WAD.
112 
113 	wadheader_t *header;	// The WAD's header.
114 	wad_boolean_t compressed;
115 
116 	struct lump_s *lumps;	// The WAD's lumps buffer.
117 } wad_t;
118 
119 typedef struct lump_s
120 {
121 	wad_t *wad;		// The WAD the lump belongs to.
122 
123 	char name[9];		// The name of the lump.
124 
125 	wad_uint64_t disksize;	// The size of the lump on disk.
126 	size_t size;		// The real (uncompressed) size of the lump.
127 
128 	wad_boolean_t compressed;
129 
130 	void *data;		// The actual lump data.
131 
132 	wad_uint32_t num;	// The lump number in the WAD.
133 	struct lump_s *prev; 	// The previous node in the tree.
134 	struct lump_s *next;	// The next node in the tree.
135 } lump_t;
136 
137 // a raw entry of the wad directory
138 typedef struct
139 {
140 	wad_uint32_t position;		// The position of the lump in the WAD file.
141 	wad_uint32_t size;		// Size of the lump.
142 	char name[8];			// The lump's name.
143 } lumpdir_t;
144 
145 //
146 // wad_static.c
147 // Static file for internal libwad operations.
148 //
149 extern char *WAD_Error(void);
150 extern char *WAD_BaseName(char *path);
151 extern char *WAD_VA(const char *format, ...);
152 
153 extern inline wad_int16_t WAD_INT16_Endianness(wad_int16_t x);
154 extern inline wad_int32_t WAD_INT32_Endianness(wad_int32_t x);
155 
156 //
157 // wad.c
158 // WAD interface functionality.
159 //
160 extern wad_uint32_t wad_numwads;
161 
162 extern wad_uint32_t WAD_WADLoopAdvance(wad_uint32_t i);
163 
164 extern wad_t *WAD_WADByNum(wad_uint16_t num);
165 extern wad_t *WAD_WADByFileName(const char *filename);
166 
167 extern wad_t *WAD_OpenWAD(const char *filename);
168 
169 extern wad_int32_t WAD_SaveWAD(wad_t *wad);
170 extern inline wad_int32_t WAD_SaveWADByNum(wad_uint16_t num);
171 extern inline wad_int32_t WAD_SaveWADByFileName(const char *filename);
172 
173 extern inline wad_int32_t WAD_SaveCloseWAD(wad_t *wad);
174 extern inline wad_int32_t WAD_SaveCloseWADByNum(wad_uint16_t num);
175 extern inline wad_int32_t WAD_SaveCloseWADByFileName(const char *filename);
176 
177 extern void WAD_CloseWAD(wad_t *wad);
178 extern inline void WAD_CloseWADByNum(wad_uint16_t num);
179 extern inline void WAD_CloseWADByFileName(const char *filename);
180 
181 //
182 // lump.c
183 // Lump interface functionality.
184 //
185 extern char *WAD_LumpNameFromFileName(char *path);
186 extern lump_t *WAD_LumpInWADByNum(wad_t *wad, wad_uint32_t num);
187 extern lump_t *WAD_LumpInWADByName(wad_t *wad, const char *name);
188 extern lump_t *WAD_LumpByName(const char *name);
189 
190 extern inline wad_uint32_t WAD_LumpNum(lump_t *lump);
191 extern inline wad_uint32_t WAD_LumpNumInWADByName(wad_t *wad, const char *name);
192 extern inline wad_uint32_t WAD_LumpNumByName(const char *name);
193 
194 extern inline size_t WAD_LumpSize(lump_t *lump);
195 extern inline size_t WAD_LumpSizeInWADByName(wad_t *wad, const char *name);
196 extern inline size_t WAD_LumpSizeByName(const char *name);
197 
198 extern lump_t *WAD_AddLump(wad_t *wad, lump_t *destlump, const char *name, const char *filename);
199 extern inline lump_t *WAD_AddLumpInWADByNum(wad_t *wad, wad_uint32_t num, const char *name, const char *filename);
200 extern inline lump_t *WAD_AddLumpInWADByName(wad_t *wad, const char *destname, const char *name, const char *filename);
201 extern inline lump_t *WAD_AddLumpByName(const char *destname, const char *name, const char *filename);
202 
203 extern lump_t *WAD_CacheLump(lump_t *lump);
204 extern inline lump_t *WAD_CacheLumpInWADByNum(wad_t *wad, wad_uint32_t num);
205 extern inline lump_t *WAD_CacheLumpInWADByName(wad_t *wad, const char *name);
206 extern inline lump_t *WAD_CacheLumpByName(const char *name);
207 
208 extern lump_t *WAD_MoveLump(lump_t *lump, lump_t *destlump);
209 extern inline lump_t *WAD_MoveLumpInWADByNum(wad_t *wad, wad_uint32_t num, wad_t *destwad, wad_uint32_t destnum);
210 extern inline lump_t *WAD_MoveLumpInWADByName(wad_t *wad, const char *name, wad_t *destwad, const char *destname);
211 extern inline lump_t *WAD_MoveLumpByName(const char *name, const char *destname);
212 
213 extern void WAD_UncacheLump(lump_t *lump);
214 extern inline void WAD_UncacheLumpInWADByNum(wad_t *wad, wad_uint32_t num);
215 extern inline void WAD_UncacheLumpInWADByName(wad_t *wad, const char *name);
216 extern inline void WAD_UncacheLumpByName(const char *name);
217 
218 extern void WAD_RemoveLump(lump_t *lump);
219 extern inline void WAD_RemoveLumpInWADByNum(wad_t *wad, wad_uint32_t num);
220 extern inline void WAD_RemoveLumpInWADByName(wad_t *wad, const char *name);
221 extern inline void WAD_RemoveLumpByName(const char *name);
222 
223 #ifdef __cplusplus
224 } // extern "C"
225 #endif
226 
227 #endif // __WAD_H__
228