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_static.c
18 /// \brief Static functions for internal libwad operations.
19
20 #include "wad_static.h"
21
22 #define WAD_MAXERROR 1024
23 static char wad_error[WAD_MAXERROR];
24
WAD_ErrorSet(const char * text)25 void WAD_ErrorSet(const char *text)
26 {
27 strncpy(wad_error, text, WAD_MAXERROR);
28 wad_error[WAD_MAXERROR] = '\0';
29 }
30
WAD_FreeMemory(FILE * file,wadheader_t * header,void * lumpdir,lump_t * lumps,wad_t * wad)31 inline void WAD_FreeMemory(FILE *file, wadheader_t *header, void *lumpdir, lump_t *lumps, wad_t *wad)
32 {
33 lump_t *l, *ln = NULL;
34
35 if (file)
36 fclose(file);
37 if (header)
38 free(header);
39 if (lumpdir)
40 free(lumpdir);
41
42 if (lumps)
43 {
44 for (l = lumps; l; l = ln)
45 {
46 ln = l->next;
47 free(l->data);
48 free(l);
49 }
50 }
51
52 if (wad)
53 {
54 if (wad->filename)
55 free(wad->filename);
56
57 free(wad);
58 }
59 }
60
WAD_Error(void)61 char *WAD_Error(void)
62 {
63 return wad_error;
64 }
65
WAD_BaseName(char * path)66 char *WAD_BaseName(char *path)
67 {
68 wad_int32_t i;
69
70 //
71 // Check if there is no text to check.
72 //
73 if (!path || path[0] == '\0')
74 return NULL;
75
76 //
77 // Find the buffer entry where the actual filename begins.
78 //
79 for (i = strlen(path) - 1; i >= 0 && path[i] != '/'; i--);
80
81 return (i >= 0) ? &path[i+1] : path;
82 }
83
WAD_VA(const char * format,...)84 char *WAD_VA(const char *format, ...)
85 {
86 va_list argptr;
87 static char string[1024];
88
89 va_start(argptr, format);
90 vsprintf(string, format, argptr);
91 va_end(argptr);
92
93 return string;
94 }
95
WAD_INT16_Endianness(wad_int16_t x)96 inline wad_int16_t WAD_INT16_Endianness(wad_int16_t x)
97 {
98 #ifdef _BIG_ENDIAN
99 return ((wad_int16_t)((((wad_uint16_t)(x) & (wad_uint16_t)0x00ffU) << 8) | (((wad_uint16_t)(x) & (wad_uint16_t)0xff00U) >> 8)));
100 #else
101 return (wad_int16_t)x;
102 #endif
103 }
104
WAD_INT32_Endianness(wad_int32_t x)105 inline wad_int32_t WAD_INT32_Endianness(wad_int32_t x)
106 {
107 #ifdef _BIG_ENDIAN
108 return ((wad_int32_t)(\
109 (((wad_uint32_t)(x) & (wad_uint32_t)0x000000ffUL) << 24) |\
110 (((wad_uint32_t)(x) & (wad_uint32_t)0x0000ff00UL) << 8) |\
111 (((wad_uint32_t)(x) & (wad_uint32_t)0x00ff0000UL) >> 8) |\
112 (((wad_uint32_t)(x) & (wad_uint32_t)0xff000000UL) >> 24)));
113 #else
114 return (wad_int32_t)x;
115 #endif
116 }
117