1 /* -*- Mode: C++; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*-
2  *
3  * Quadra, an action puzzle game
4  * Copyright (C) 1998-2000  Ludus Design
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifndef _HEADER_RES
22 #define _HEADER_RES
23 
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "config.h"
28 #include "resfile.h"
29 #include "resmanager.h"
30 #include "config.h"
31 
32 #ifdef UGS_LINUX
33 	#include <unistd.h>
34 #endif
35 
36 #include "error.h"
37 #include "types.h"
38 
39 class Res {
40 public:
~Res()41 	virtual ~Res() { }
42 	virtual int read(void *b, int nb)=0;
43 	virtual Dword size()=0;
44 	virtual void position(Dword po)=0;
45 	virtual const void *buf()=0;
46 	virtual bool eof()=0;
47 	virtual Dword get_position()=0;
48 };
49 
50 class Res_mem: public Res {
51 protected:
52 	Byte *_buf;
53 	Dword pos;
54 public:
55 	Res_mem();
read(void * b,int nb)56 	virtual int read(void *b, int nb) {
57 		if(pos+nb>size()) {
58 			memset(b, 0, nb);
59 			nb=size()-pos;
60 		}
61 		memcpy(b, _buf + pos, nb);
62 		pos += nb;
63 		return nb;
64 	}
position(Dword po)65 	virtual void position(Dword po) {
66 		pos = po;
67 	}
buf()68 	virtual const void *buf() {
69 		return(_buf + pos);
70 	}
eof()71 	virtual bool eof() {
72 		return (pos >= size());
73 	}
get_position()74 	virtual Dword get_position() {
75 		return pos;
76 	}
77 };
78 
79 class Res_doze: public Res_mem {
80 	unsigned int ressize;
81 public:
Res_doze(const char * resname)82 	Res_doze(const char *resname) {
83 		ressize = resmanager->get(resname, &_buf);
84 		if(!_buf)
85 			fatal_msgbox("Unable to find resource: %s", resname);
86 	}
~Res_doze()87 	virtual ~Res_doze() {
88 	}
size()89 	virtual Dword size() {
90 		return ressize;
91 	}
92 };
93 
94 enum Res_mode {
95 	RES_READ,
96 	RES_WRITE,
97 	RES_CREATE,
98 	RES_TRY
99 };
100 
101 class Res_dos: public Res {
102 	int handle;
103 	void *_buf;
104 public:
105 	bool exist;
106 	Res_dos(const char *fil, Res_mode mode=RES_READ);
107 	virtual ~Res_dos();
108 	virtual void position(Dword po);
109 	virtual int read(void *b, int nb);
110 	virtual void write(const void *b, int nb);
111 	virtual Dword size();
112 	virtual const void* buf();
113 	virtual bool eof();
114 	virtual Dword get_position();
115 };
116 
117 #endif
118