Subversion Repositories spk

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
/*
2
  things used both in bob_dom_cut and bob_dom_bob are defined here
3
*/
4
 
5
#ifndef BOB_DOM_BASE_INCLUDED
6
#define BOB_DOM_BASE_INCLUDED
7
 
8
#include "../common/ext_list.h"
9
#include "bob_names.h"
10
 
11
#include "bob_dom_stream.h"
12
#include "bob_dom_filestream.h"
13
 
14
#include <string.h>
15
#include <stdarg.h>
16
 
17
class bob_dom_section;
18
class bob_dom_string;
19
class bob_dom_info;
20
 
21
enum bob_error_codes
22
{
23
	e_noError,
24
	e_badHeader,
25
	e_badEndHeader,
26
	e_notEnoughData,
27
	e_moreData,
28
	e_unkPointHeader,
29
	e_error,
30
	e_format_noStatFormat,
31
	e_format_notEnoughData,
32
	e_format_UserWarning,
33
	e_pointNotFound,
34
	e_badVersion,
35
	e_unsupportedFrameFlags,
36
	e_unkMaterialValueType
37
};
38
 
39
enum bob_error_severity
40
{
41
	s_warning,
42
	s_error
43
};
44
 
45
const char* bob_traslate_error(bob_error_codes code);
46
int peek(bob_dom_ibinaryfilestream& is);
47
 
48
class bob_with_errors
49
{
50
	protected:
51
		void error(bob_error_codes code);
52
		void error(bob_error_severity severity, bob_error_codes code);
53
		void error(bob_error_codes code, const char *format, ...);
54
		void error(bob_error_severity severity, bob_error_codes code, const char *format, ...);
55
		void verror(bob_error_severity severity, bob_error_codes code, const char *format, va_list ap);
56
 
57
	public:
58
		struct bob_error
59
		{
60
			bob_error_codes code;
61
			bob_error_severity severity;
62
			char text[255];
63
		};
64
 
65
		typedef ext::list<bob_error*> ErrorList;
66
		typedef ext::list<bob_error*>::iterator ErrorIterator;
67
 
68
		ErrorList errors;
69
 
70
		virtual ~bob_with_errors()
71
		{
72
			for(ErrorIterator &it=errors.begin(); it!=errors.end(); it++){
73
				delete *it;
74
			}
75
		}
76
};
77
 
78
class bob_dom_section : public bob_with_errors
79
{
80
	public:
81
		static const int null_value=-1;
82
 
83
		int peek(bob_dom_ibinaryfilestream& is)
84
		{
85
			int i;
86
			is >> i;
87
			is.advance(-(int)sizeof(int));
88
			return i;
89
		}
90
};
91
 
92
template <class Ty>
93
class bob_dom_cantainer
94
{
95
	private:
96
		typedef ext::list<Ty*> list_type;
97
 
98
	public:
99
		typedef Ty value_type;
100
		typedef Ty* value_type_ptr;
101
		typedef value_type child_type;
102
		typedef typename list_type::size_type size_type;
103
		typedef typename list_type::iterator iterator;
104
		typedef typename list_type::const_iterator const_iterator;
105
 
106
	protected:
107
		list_type children;
108
 
109
	public:
110
		const_iterator begin() const { return children.begin(); }
111
		iterator begin() { return children.begin(); }
112
		const_iterator end() const { return children.end(); }
113
		iterator end() { return children.end(); }
114
 
115
		virtual ~bob_dom_cantainer()
116
		{
117
			removeChildren();
118
		}
119
 
120
		void removeChildren()
121
		{
122
			for(iterator &it=children.begin(); it!=children.end(); ++it){
123
				delete *it;
124
			}
125
			children.clear();
126
		}
127
 
128
		size_type size() const { return children.size(); }
129
 
130
		value_type_ptr createChild() { return *(children.push_back(new value_type())); }
131
 
132
		value_type_ptr front() { return children.front(); }
133
		value_type_ptr back() { return children.back(); }
134
 
135
		void removeChild(iterator &where) 
136
		{
137
			delete *where;
138
			children.erase(where);
139
		}
140
 
141
		void removeChild(value_type_ptr child) 
142
		{
143
			iterator it, old;
144
			while((it=children.find(child))!=children.end()){
145
				old=it - 1;
146
				delete *it;
147
				children.erase(it);
148
				it=old;
149
			}
150
		}
151
};
152
 
153
class bob_dom_string : public bob_dom_section
154
{
155
	public:
156
		char *m_text;
157
 
158
		const char *text() const { return m_text; }
159
		void text(const char *str) { text(str, strlen(str));	}
160
		void text(const char *str, size_t size) 
161
		{
162
			delete[] m_text;
163
			m_text=new char[size + 1];
164
			memcpy(m_text, str, size);
165
			m_text[size]=0;
166
		}
167
 
168
		bob_dom_string() { m_text=0; }
169
		virtual ~bob_dom_string() { delete[] m_text; }
170
 
171
		bool load(unsigned char *data, size_t size, int startHeader, int endHeader);
172
		bool load(bob_dom_ibinaryfilestream& is, int startHeader, int endHeader);
173
		bool toFile(bob_dom_obinaryfilestream& os, int begin, int end);
174
};
175
 
176
class bob_dom_info : public bob_dom_string
177
{
178
	public:
179
		static const int hdr_begin=BOB_SECTION_NAME_INFO_BEGIN;
180
		static const int hdr_end=BOB_SECTION_NAME_INFO_END;
181
 
182
		bool load(unsigned char *data, size_t size) { return bob_dom_string::load(data, size, hdr_begin, hdr_end); }
183
		bool load(bob_dom_ibinaryfilestream& is) { return bob_dom_string::load(is, hdr_begin, hdr_end); }
184
		bool toFile(bob_dom_obinaryfilestream& os) { return bob_dom_string::toFile(os, hdr_begin, hdr_end); }
185
		bool toFile(bob_dom_otextfilestream& os);
186
};
187
 
188
#endif // !defined(BOB_DOM_BASE_INCLUDED)