My Project
Loading...
Searching...
No Matches
file.h
1/*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#ifndef FILE_H
20#define FILE_H
21
22#include "SDL_endian.h"
23
24#include <stdbool.h>
25#include <stdio.h>
26
27extern const char *custom_data_dir;
28
29const char *data_dir(void);
30
31FILE *dir_fopen(const char *dir, const char *file, const char *mode);
32FILE *dir_fopen_warn(const char *dir, const char *file, const char *mode);
33FILE *dir_fopen_die(const char *dir, const char *file, const char *mode);
34
35bool dir_file_exists(const char *dir, const char *file);
36
37long ftell_eof(FILE *f);
38
39void fread_die(void *buffer, size_t size, size_t count, FILE *stream);
40
41// 8-bit fread that dies if read fails
42static inline void fread_bool_die(bool *buffer, FILE *stream)
43{
44 Uint8 temp;
45 fread_die(&temp, sizeof(Uint8), 1, stream);
46 *buffer = temp != 0;
47}
48
49// 8-bit fread
50static inline size_t fread_u8(Uint8 *buffer, size_t count, FILE *stream)
51{
52 return fread(buffer, sizeof(Uint8), count, stream);
53}
54
55// 8-bit fread that dies if read fails
56static inline void fread_u8_die(Uint8 *buffer, size_t count, FILE *stream)
57{
58 fread_die(buffer, sizeof(Uint8), count, stream);
59}
60
61// 8-bit fread that dies if read fails
62static inline void fread_s8_die(Sint8 *buffer, size_t count, FILE *stream)
63{
64 fread_die(buffer, sizeof(Sint8), count, stream);
65}
66
67// 16-bit endian-swapping fread that dies if read fails
68static inline void fread_u16_die(Uint16 *buffer, size_t count, FILE *stream)
69{
70 fread_die(buffer, sizeof(Uint16), count, stream);
71
72#if SDL_BYTEORDER == SDL_BIG_ENDIAN
73 for (size_t i = 0; i < count; ++i)
74 buffer[i] = SDL_Swap16(buffer[i]);
75#endif
76}
77
78// 16-bit endian-swapping fread that dies if read fails
79static inline void fread_s16_die(Sint16 *buffer, size_t count, FILE *stream)
80{
81 fread_die(buffer, sizeof(Sint16), count, stream);
82
83#if SDL_BYTEORDER == SDL_BIG_ENDIAN
84 for (size_t i = 0; i < count; ++i)
85 buffer[i] = SDL_Swap16(buffer[i]);
86#endif
87}
88
89// 32-bit endian-swapping fread that dies if read fails
90static inline void fread_u32_die(Uint32 *buffer, size_t count, FILE *stream)
91{
92 fread_die(buffer, sizeof(Uint32), count, stream);
93
94#if SDL_BYTEORDER == SDL_BIG_ENDIAN
95 for (size_t i = 0; i < count; ++i)
96 buffer[i] = SDL_Swap32(buffer[i]);
97#endif
98}
99
100// 32-bit endian-swapping fread that dies if read fails
101static inline void fread_s32_die(Sint32 *buffer, size_t count, FILE *stream)
102{
103 fread_die(buffer, sizeof(Sint32), count, stream);
104
105#if SDL_BYTEORDER == SDL_BIG_ENDIAN
106 for (size_t i = 0; i < count; ++i)
107 buffer[i] = SDL_Swap32(buffer[i]);
108#endif
109}
110
111void fwrite_die(const void *buffer, size_t size, size_t count, FILE *stream);
112
113// 8-bit fwrite that dies if write fails
114static inline void fwrite_bool_die(const bool *buffer, FILE *stream)
115{
116 Uint8 temp = *buffer ? 1 : 0;
117 fwrite_die(&temp, sizeof(Uint8), 1, stream);
118}
119
120// 8-bit fwrite
121static inline size_t fwrite_u8(const Uint8 *buffer, size_t count, FILE *stream)
122{
123 return fwrite(buffer, sizeof(Uint8), count, stream);
124}
125
126// 8-bit fwrite that dies if write fails
127static inline void fwrite_u8_die(const Uint8 *buffer, size_t count, FILE *stream)
128{
129 fwrite_die(buffer, sizeof(Uint8), count, stream);
130}
131
132// 8-bit fwrite that dies if write fails
133static inline void fwrite_s8_die(const Sint8 *buffer, size_t count, FILE *stream)
134{
135 fwrite_die(buffer, sizeof(Sint8), count, stream);
136}
137
138// 16-bit endian-swapping fwrite that dies if write fails
139static inline void fwrite_u16_die(const Uint16 *buffer, FILE *stream)
140{
141#if SDL_BYTEORDER == SDL_BIG_ENDIAN
142 Uint16 temp = SDL_Swap16(*buffer);
143 buffer = &temp;
144#endif
145
146 fwrite_die(buffer, sizeof(Uint16), 1, stream);
147}
148
149// 16-bit endian-swapping fwrite that dies if write fails
150static inline void fwrite_s16_die(const Sint16 *buffer, FILE *stream)
151{
152#if SDL_BYTEORDER == SDL_BIG_ENDIAN
153 Sint16 temp = SDL_Swap16(*buffer);
154 buffer = &temp;
155#endif
156
157 fwrite_die(buffer, sizeof(Sint16), 1, stream);
158}
159
160// 32-bit endian-swapping fwrite that dies if write fails
161static inline void fwrite_u32_die(const Uint32 *buffer, FILE *stream)
162{
163#if SDL_BYTEORDER == SDL_BIG_ENDIAN
164 Uint32 temp = SDL_Swap32(*buffer);
165 buffer = &temp;
166#endif
167
168 fwrite_die(buffer, sizeof(Uint32), 1, stream);
169}
170
171// 32-bit endian-swapping fwrite that dies if write fails
172static inline void fwrite_s32_die(const Sint32 *buffer, FILE *stream)
173{
174#if SDL_BYTEORDER == SDL_BIG_ENDIAN
175 Sint32 temp = SDL_Swap32(*buffer);
176 buffer = &temp;
177#endif
178
179 fwrite_die(buffer, sizeof(Sint32), 1, stream);
180}
181
182#endif // FILE_H