File: MultiString.h

package info (click to toggle)
gcvs 1.0final-5sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 12,244 kB
  • ctags: 10,630
  • sloc: ansic: 71,709; cpp: 39,780; sh: 18,434; makefile: 1,912; yacc: 1,299; tcl: 1,283; perl: 910; lex: 249; csh: 185; lisp: 7
file content (186 lines) | stat: -rwxr-xr-x 4,811 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- August 1998
 */

/*
 * MultiString.h --- remember several unique persistents strings
 */

#ifndef MULTISTRING_H
#define MULTISTRING_H

#include <vector>
#include "Persistent.h"
#include "CPStr.h"

#ifndef NAMESPACE
#	if defined(_MSC_VER) || defined(__MWERKS__) || (__GNUC__ > 2)
#		define NAMESPACE(w) w::
#	else
#		define NAMESPACE(w) ::
#	endif
#endif

//	Persistent array
template<class T>
class TMPersistent : public CPersistent
{
public:
	typedef	NAMESPACE(std) vector<T>	list_t;
	
	TMPersistent(const char *uniqueName, kClassPersistent pclass = kNoClass)
	: CPersistent(uniqueName, pclass) {};

	virtual ~TMPersistent() {};
	
	virtual unsigned int SizeOf(void) const {return sizeof (int) + sizeof (T) * fAllItems.size ();};
	virtual const void *GetData(void) const
		{
			fBuf.AdjustSize(SizeOf());
			char *tmp = fBuf;
			*(int *) tmp = fAllItems.size();
			tmp += sizeof(int) / sizeof(char);
			
			for(typename list_t::const_iterator i = fAllItems.begin(); i != fAllItems.end(); ++i)
			{
				memcpy(tmp, (const char *)(&*i), sizeof (T));
				tmp += sizeof (T);
			}
			
			return (char *)fBuf;
		};

	virtual void SetData(const void *ptr, unsigned int size)
		{
			const char *tmp = (char *)ptr;
			fAllItems.erase(fAllItems.begin(), fAllItems.end());
			unsigned int thesize = *(int *)tmp;
			tmp += sizeof(int) / sizeof(char);
			for(unsigned int i = 0; i < thesize; ++i)
			{
				fAllItems.push_back(*((T*)tmp));
				tmp += sizeof (T);
			}
		};
		// persistent interface
	
	inline const list_t & GetList(void) const { return fAllItems; }
	inline list_t & GetList(void) { return fAllItems; }
	
protected:
	list_t 						fAllItems;
	mutable	CStaticAllocT<char> fBuf;
};

template<class T>
class TMString : public CPersistent
{
public:
	typedef	NAMESPACE(std) vector<T> list_t;
	
	TMString(unsigned int maxstr, const char *uniqueName,
		char * const *defaultStr = 0L, kClassPersistent pclass = kNoClass);
		// defaultStr is a null terminated set of strings

	virtual ~TMString();
	
	virtual unsigned int SizeOf(void) const;
	virtual const void *GetData(void) const;
	virtual void SetData(const void *ptr, unsigned int size);
		// persistent interface
	
	void Insert(const char *newstr);
		// add a new string
	
	bool Remove(const char *newstr);
		// remove a new string, true if it was found
	
	typename list_t::const_iterator Find(const char *instr) const;
		// find a string
	
	inline const list_t & GetList(void) const { return fAllStrs; }
	inline typename list_t::const_iterator begin() const { return GetList ().begin ();};
	inline typename list_t::const_iterator end() const { return GetList ().end ();};
	
protected:
	virtual int Compare(const char *s1, const char *s2) const
	{
		return strcmp(s1, s2);
	}

	list_t fAllStrs;
	mutable	CStaticAllocT<char> fBuf;
	unsigned int fMaxStr;
};

class CMString : public TMString<CStr>
{
	UDECLARE_DYNAMIC(CMString)
public:
	CMString(unsigned int maxstr, const char *uniqueName,
		char * const *defaultStr = 0L, kClassPersistent pclass = kNoClass) :
		TMString<CStr>(maxstr, uniqueName, defaultStr, pclass)
	{
	}

	virtual ~CMString()
	{
	}
};

class CMPString : public TMString<CPStr>
{
	UDECLARE_DYNAMIC(CMPString)
public:
	CMPString(unsigned int maxstr, const char *uniqueName,
		char * const *defaultStr = 0L, kClassPersistent pclass = kNoClass) :
		TMString<CPStr>(maxstr, uniqueName, defaultStr, pclass)
	{
	}

	virtual ~CMPString()
	{
	}
};

// this is a multi-string for storing map (i.e. dictionary)
class CKeyString : public CMString
{
public:
	CKeyString(unsigned int maxstr, const char *uniqueName,
		char * const *defaultStr = 0L, kClassPersistent pclass = kNoClass) :
		CMString(maxstr, uniqueName, defaultStr, pclass)
	{
	}

	virtual ~CKeyString()
	{
	}

	static void Concatenate(UStr & res, const char *key, const char *value);
	static void Split(const char *str, UStr & key, UStr & value);
protected:
	virtual int Compare(const char *s1, const char *s2) const;
};

#if qUnix
	void DoDataExchange(bool fill, int widid, int cmdid, CMString & mstr);
#endif // qUnix

#endif /* AUTHEN_H */