]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/libbinio/src/binfile.cpp
wwww~
[16.git] / 16 / adplug / libbinio / src / binfile.cpp
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  * 
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * Lesser General Public License for more details.
11  * 
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  *
16  * binfile.h - Binary file I/O
17  * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
18  */
19
20 #include <stdio.h>
21 #include <errno.h>
22
23 #include "binfile.h"
24
25 /***** binfbase *****/
26
27 binfbase::binfbase()
28   : f(NULL)
29 {
30 }
31
32 binfbase::~binfbase()
33 {
34   if(f != NULL) close();
35 }
36
37 void binfbase::close()
38 {
39   if(f != NULL) {
40     if(fclose(f) == EOF) err |= Fatal; else f = NULL;
41   } else
42     err |= NotOpen;
43 }
44
45 void binfbase::seek(long pos, Offset offs)
46 {
47   int error;
48
49   if(f == NULL) { err |= NotOpen; return; }
50
51   switch(offs) {
52   case Set: error = fseek(f, pos, SEEK_SET); break;
53   case Add: error = fseek(f, pos, SEEK_CUR); break;
54   case End: error = fseek(f, pos, SEEK_END); break;
55   }
56
57   if(error == -1) err |= Fatal;
58 }
59
60 long binfbase::pos()
61 {
62   long pos;
63
64   if(f == NULL) { err |= NotOpen; return 0; }
65
66   pos = ftell(f);
67
68   if(pos == -1) {
69     err |= Fatal;
70     return 0;
71   } else
72     return pos;
73 }
74
75 /***** binifstream *****/
76
77 binifstream::binifstream()
78 {
79 }
80
81 binifstream::binifstream(const char *filename, const Mode mode)
82 {
83   open(filename, mode);
84 }
85
86 #if BINIO_ENABLE_STRING
87 binifstream::binifstream(const std::string &filename, const Mode mode)
88 {
89   open(filename, mode);
90 }
91 #endif
92
93 binifstream::~binifstream()
94 {
95 }
96
97 void binifstream::open(const char *filename, const Mode mode)
98 {
99   f = fopen(filename, "rb");
100
101   if(f == NULL)
102     switch(errno) {
103     case ENOENT: err |= NotFound; break;
104     case EACCES: err |= Denied; break;
105     default: err |= NotOpen; break;
106     }
107 }
108
109 #if BINIO_ENABLE_STRING
110 void binifstream::open(const std::string &filename, const Mode mode)
111 {
112   open(filename.c_str(), mode);
113 }
114 #endif
115
116 binifstream::Byte binifstream::getByte()
117 {
118   int read;
119
120   if(f != NULL) {
121     read = fgetc(f);
122     if(read == EOF) err |= Eof;
123     return (Byte)read;
124   } else {
125     err |= NotOpen;
126     return 0;
127   }
128 }
129
130 /***** binofstream *****/
131
132 binofstream::binofstream()
133 {
134 }
135
136 binofstream::binofstream(const char *filename, const Mode mode)
137 {
138   open(filename, mode);
139 }
140
141 #if BINIO_ENABLE_STRING
142 binofstream::binofstream(const std::string &filename, const Mode mode)
143 {
144   open(filename, mode);
145 }
146 #endif
147
148 binofstream::~binofstream()
149 {
150 }
151
152 void binofstream::open(const char *filename, const Mode mode)
153 {
154   char *modestr = "wb";
155
156   // Check if append mode is desired
157   if(mode & Append) modestr = "ab";
158
159   f = fopen(filename, modestr);
160
161   if(f == NULL)
162     switch(errno) {
163     case EEXIST:
164     case EACCES:
165     case EROFS:
166       err |= Denied;
167       break;
168     case ENOENT: err |= NotFound; break;
169     default: err |= NotOpen; break;
170     }
171 }
172
173 #if BINIO_ENABLE_STRING
174 void binofstream::open(const std::string &filename, const Mode mode)
175 {
176   open(filename.c_str(), mode);
177 }
178 #endif
179
180 void binofstream::putByte(Byte b)
181 {
182   if(f == NULL) { err |= NotOpen; return; }
183
184   if(fputc(b, f) == EOF)
185     err |= Fatal;
186 }
187
188 /***** binfstream *****/
189
190 binfstream::binfstream()
191 {
192 }
193
194 binfstream::binfstream(const char *filename, const Mode mode)
195 {
196   open(filename, mode);
197 }
198
199 #if BINIO_ENABLE_STRING
200 binfstream::binfstream(const std::string &filename, const Mode mode)
201 {
202   open(filename, mode);
203 }
204 #endif
205
206 binfstream::~binfstream()
207 {
208 }
209
210 void binfstream::open(const char *filename, const Mode mode)
211 {
212   char  *modestr = "w+b";       // Create & at beginning
213   int   ferror = 0;
214
215   // Apply desired mode
216   if(mode & NoCreate) {
217     if(!(mode & Append))
218       modestr[0] = 'r'; // NoCreate & at beginning
219   } else
220     if(mode & Append)   // Create & append
221       modestr[0] = 'a';
222
223   f = fopen(filename, modestr);
224
225   // NoCreate & append (emulated -- not possible with standard C fopen())
226   if(f != NULL && (mode & Append) && (mode & NoCreate))
227     ferror = fseek(f, 0, SEEK_END);
228
229   if(f == NULL || ferror == -1) {
230     switch(errno) {
231     case EEXIST:
232     case EACCES:
233     case EROFS:
234       err |= Denied;
235       break;
236     case ENOENT: err |= NotFound; break;
237     default: err |= NotOpen; break;
238     }
239   }
240 }
241
242 #if BINIO_ENABLE_STRING
243 void binfstream::open(const std::string &filename, const Mode mode)
244 {
245   open(filename.c_str(), mode);
246 }
247 #endif