1 /*
2 This file is part of "Avanor, the Land of Mystery" roguelike game
3 Home page: http://www.avanor.com/
4 Copyright (C) 2000-2003 Vadim Gaidukevich
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #ifndef __XFILE_H
22 #define __XFILE_H
23 
24 #include <stdio.h>
25 #include <assert.h>
26 
27 class XFile
28 {
29 public:
XFile()30 	XFile() : file(NULL) {}
~XFile()31 	~XFile()
32 	{
33 		if (file)
34 			fclose(file);
35 	}
36 
Open(char * name,char * param)37 	int Open(char * name, char * param)
38 	{
39 		file = fopen(name, param);
40 		if (file)
41 			return 1;
42 		else
43 			return 0;
44 	}
45 
Close()46 	void Close()
47 	{
48 		fclose(file);
49 		file = NULL;
50 	}
51 
52 	int Write(const void * data, size_t block_size, size_t block_count = 1)
53 	{
54 		return fwrite(data, block_size, block_count, file);
55 	}
56 
Write(const int * data)57 	int Write(const int * data)
58 	{
59 		return fwrite(data, sizeof(int), 1, file);
60 	}
61 
Write(const unsigned int * data)62 	int Write(const unsigned int * data)
63 	{
64 		return fwrite(data, sizeof(unsigned int), 1, file);
65 	}
66 
67 
68 	int Read(void * data, size_t block_size, size_t block_count = 1)
69 	{
70 		unsigned int res = fread(data, block_size, block_count, file);
71 		assert(res == block_count);
72 		return res;
73 	}
74 
Read(int * data)75 	int Read(int * data)
76 	{
77 		return fread(data, sizeof(int), 1, file);
78 	}
79 
Read(unsigned int * data)80 	int Read(unsigned int * data)
81 	{
82 		return fread(data, sizeof(unsigned int), 1, file);
83 	}
84 
85 
86 protected:
87 	FILE * file;
88 };
89 
90 #endif
91