1 //----------------------------------------------------------------------------
2 //  EDGE Platform Interface Utility Header
3 //----------------------------------------------------------------------------
4 //
5 //  Copyright (c) 2004-2008  The EDGE Team.
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //----------------------------------------------------------------------------
18 #ifndef __EPI_UTIL__
19 #define __EPI_UTIL__
20 
21 #include "arrays.h"
22 
23 namespace epi
24 {
25 	// Forward declaration
26 	class strlist_c;
27 
28     // Primitive array for base types
29     class s32array_c : public array_c
30     {
31     public:
s32array_c()32         s32array_c() : array_c(sizeof(s32_t)) {};
~s32array_c()33         ~s32array_c() { Clear(); }
34 
35     private:
CleanupObject(void * obj)36     	void CleanupObject(void *obj) { /* Do Nothing */ }
37 
38     public:
GetSize()39     	int GetSize() const { return array_entries; }
Insert(s32_t num)40 	    int Insert(s32_t num) { return InsertObject((void*)&num); }
41 	    s32_t& operator[](int idx) const { return *(s32_t*)FetchObject(idx); }
42     };
43 
44     class u32array_c : public array_c
45     {
46     public:
u32array_c()47         u32array_c() : array_c(sizeof(u32_t)) {};
~u32array_c()48         ~u32array_c() { Clear(); }
49 
50     private:
CleanupObject(void * obj)51     	void CleanupObject(void *obj) { /* Do Nothing */ }
52 
53     public:
GetSize()54     	int GetSize() const { return array_entries; }
Insert(u32_t num)55 	    int Insert(u32_t num) { return InsertObject((void*)&num); }
56 	    u32_t& operator[](int idx) const { return *(u32_t*)FetchObject(idx); }
57     };
58 
59     // String Box
60     class strbox_c
61     {
62     public:
63     	strbox_c();
64     	strbox_c(strbox_c &rhs);
65     	~strbox_c();
66 
67     private:
68     	char **strs;
69     	int numstrs;
70 
71     	char *data;
72     	int datasize;
73 
74     	void Copy(strbox_c &src);
75 
76     public:
77     	void Clear();
GetSize()78     	int GetSize() const { return numstrs; }
79     	void Set(strlist_c &src);
80 
81     	strbox_c& operator=(strbox_c &rhs);
82     	char* operator[](int idx) const { return strs[idx]; }
83     };
84 
85     // String entry
86     class strent_c
87     {
88 	public:
strent_c()89 		strent_c() : data(NULL) { }
strent_c(const char * s)90 		strent_c(const char *s) : data(NULL) { Set(s); }
strent_c(const strent_c & rhs)91 		strent_c(const strent_c &rhs) : data(NULL) { Set(rhs.data); }
~strent_c()92 		~strent_c() { clear(); };
93 
94 	private:
95 		char *data;
96 
97 	public:
clear()98 		void clear()
99 		{
100 			if (data)
101 				delete [] data;
102 
103 			data = NULL;
104 		}
105 
c_str(void)106 		const char *c_str(void) const { return data; }
107 
empty()108 		bool empty() const { return (data && data[0]) ? false : true; }
109 
110 		void Set(const char *s);
111 		void Set(const char *s, int max);
112 
113 		strent_c& operator= (const strent_c &rhs)
114 		{
115 			if(&rhs != this)
116 				Set(rhs.data);
117 
118 			return *this;
119 		}
120 
121 		char operator[](int idx) const { return data[idx]; }
122 
123 		// FIXME: -AJA- not sure about this auto-conversion
124 		operator const char* () const { return data; }
125     };
126 
127     // String list
128     class strlist_c : public array_c
129     {
130     public:
131         strlist_c();
132         strlist_c(strlist_c &rhs);
133         ~strlist_c();
134 
135     private:
136         void CleanupObject(void *obj);
137 		void Copy(strlist_c &src);
138 
139     public:
Delete(int idx)140         void Delete(int idx) { RemoveObject(idx); }
141 	    int Find(const char* refname);
GetSize()142 	    int GetSize() const { return array_entries; }
143 	    int Insert(const char *s);
144     	void Set(strbox_c &src);
145 
146 		strlist_c& operator=(strlist_c &rhs);
147 	    char* operator[](int idx) const { return *(char**)FetchObject(idx); }
148     };
149 
150 };
151 
152 
153 
154 
155 //
156 // Z_Clear
157 //
158 // Clears memory to zero.
159 //
160 #define Z_Clear(ptr, type, num)  \
161 	memset((void *)(ptr), ((ptr) - ((type *)(ptr))), (num) * sizeof(type))
162 
163 //
164 // Z_MoveData
165 //
166 // moves data from src to dest.
167 //
168 #define Z_MoveData(dest, src, type, num)  \
169 	memmove((void *)(dest), (void *)(src), (num) * sizeof(type) + ((src) - (type *)(src)) + ((dest) - (type *)(dest)))
170 
171 //
172 // Z_StrNCpy
173 //
174 // Copies up to max characters of src into dest, and then applies a
175 // terminating zero (so dest must hold at least max+1 characters).
176 // The terminating zero is always applied (there is no reason not to)
177 //
178 #define Z_StrNCpy(dest, src, max) \
179 	(void)(strncpy((dest), (src), (max)), (dest)[(max)] = 0)
180 
181 
182 #endif /* __EPI_UTIL__ */
183 
184 
185 //--- editor settings ---
186 // vi:ts=4:sw=4:noexpandtab
187