ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.9
Committed: Sun Jun 26 19:59:29 2005 UTC (18 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-5_7, rel-5_8
Changes since 1.8: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #ifndef RXVT_UTIL_H
2 #define RXVT_UTIL_H
3
4 #include <cstring>
5
6 extern class byteorder {
7 static unsigned int e; // at least 32 bits
8 public:
9 byteorder ();
10
11 static bool big_endian () { return e == 0x11223344; };
12 static bool network () { return e == 0x11223344; };
13 static bool little_endian () { return e == 0x44332211; };
14 static bool vax () { return e == 0x44332211; };
15 } byteorder;
16
17 template<typename T, typename U>
18 static inline T min (T a, U b) { return a < b ? a : (T)b; }
19 template<typename T, typename U>
20 static inline T max (T a, U b) { return a > b ? a : (T)b; }
21 template<typename T>
22 static inline void swap (T& a, T& b) { T t=a; a=b; b=t; }
23
24 #define IN_RANGE(val,beg,end) \
25 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
26
27 struct zero_initialized {
28 void *operator new (size_t s);
29 void operator delete (void *p, size_t s);
30 };
31
32 /* simplevec taken (and heavily modified), from:
33 *
34 * MICO --- a free CORBA implementation
35 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
36 */
37 template<class T>
38 struct simplevec {
39 typedef T* iterator;
40 typedef const T* const_iterator;
41 typedef unsigned long size_type;
42
43 private:
44 size_type _last, _size;
45 T *_buf;
46
47 public:
48 const_iterator begin () const
49 {
50 return &_buf[0];
51 }
52 iterator begin ()
53 {
54 return &_buf[0];
55 }
56 const_iterator end () const
57 {
58 return &_buf[_last];
59 }
60 iterator end ()
61 {
62 return &_buf[_last];
63 }
64 size_type capacity () const
65 {
66 return _size;
67 }
68 size_type size () const
69 {
70 return _last;
71 }
72
73 private:
74 static T *alloc (size_type n)
75 {
76 return (T *)::operator new ((size_t) (n * sizeof (T)));
77 }
78 static void dealloc (T *buf)
79 {
80 if (buf)
81 ::operator delete (buf);
82 }
83
84 void reserve (iterator where, size_type n)
85 {
86 if (_last + n <= _size) {
87 memmove (where+n, where, (end ()-where)*sizeof (T));
88 } else {
89 size_type sz = _last+n;
90 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
91 T *nbuf = alloc (sz);
92 if (_buf) {
93 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
94 memcpy (nbuf + (where-begin ()) + n, where,
95 (end ()-where)*sizeof (T));
96 dealloc (_buf);
97 }
98 _buf = nbuf;
99 _size = sz;
100 }
101 }
102
103 public:
104 void reserve (size_type sz)
105 {
106 if (_size < sz) {
107 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
108 T *nbuf = alloc (sz);
109 if (_buf) {
110 memcpy (nbuf, begin (), size ()*sizeof (T));
111 dealloc (_buf);
112 }
113 _buf = nbuf;
114 _size = sz;
115 }
116 }
117 simplevec ()
118 : _last(0), _size(0), _buf(0)
119 {
120 }
121 simplevec (size_type n, const T& t = T ())
122 : _last(0), _size(0), _buf(0)
123 {
124 insert (begin (), n, t);
125 }
126 simplevec (const_iterator first, const_iterator last)
127 : _last(0), _size(0), _buf(0)
128 {
129 insert (begin (), first, last);
130 }
131 simplevec (const simplevec<T> &v)
132 : _last(0), _size(0), _buf(0)
133 {
134 reserve (v._last);
135 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
136 _last = v._last;
137 }
138 simplevec<T> &operator= (const simplevec<T> &v)
139 {
140 if (this != &v) {
141 _last = 0;
142 reserve (v._last);
143 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
144 _last = v._last;
145 }
146 return *this;
147 }
148 ~simplevec ()
149 {
150 dealloc (_buf);
151 }
152 const T &front () const
153 {
154 //ministl_assert (size () > 0);
155 return _buf[0];
156 }
157 T &front ()
158 {
159 //ministl_assert (size () > 0);
160 return _buf[0];
161 }
162 const T &back () const
163 {
164 //ministl_assert (size () > 0);
165 return _buf[_last-1];
166 }
167 T &back ()
168 {
169 //ministl_assert (size () > 0);
170 return _buf[_last-1];
171 }
172 bool empty () const
173 {
174 return _last == 0;
175 }
176 void clear ()
177 {
178 _last = 0;
179 }
180 void push_back (const T &t)
181 {
182 reserve (_last+1);
183 *end () = t;
184 ++_last;
185 }
186 void push_back (T &t)
187 {
188 reserve (_last+1);
189 *end () = t;
190 ++_last;
191 }
192 void pop_back ()
193 {
194 //ministl_assert (size () > 0);
195 --_last;
196 }
197 const T &operator[] (size_type idx) const
198 {
199 //ministl_assert (idx < size ());
200 return _buf[idx];
201 }
202 T &operator[] (size_type idx)
203 {
204 //ministl_assert (idx < size ());
205 return _buf[idx];
206 }
207 iterator insert (iterator pos, const T &t)
208 {
209 //ministl_assert (pos <= end ());
210 long at = pos - begin ();
211 reserve (pos, 1);
212 pos = begin ()+at;
213 *pos = t;
214 ++_last;
215 return pos;
216 }
217 iterator insert (iterator pos, const_iterator first, const_iterator last)
218 {
219 //ministl_assert (pos <= end ());
220 long n = last - first;
221 long at = pos - begin ();
222 if (n > 0) {
223 reserve (pos, n);
224 pos = begin ()+at;
225 memcpy (pos, first, (last-first)*sizeof (T));
226 _last += n;
227 }
228 return pos;
229 }
230 iterator insert (iterator pos, size_type n, const T &t)
231 {
232 //ministl_assert (pos <= end ());
233 long at = pos - begin ();
234 if (n > 0) {
235 reserve (pos, n);
236 pos = begin ()+at;
237 for (int i = 0; i < n; ++i)
238 pos[i] = t;
239 _last += n;
240 }
241 return pos;
242 }
243 void erase (iterator first, iterator last)
244 {
245 if (last != first) {
246 memmove (first, last, (end ()-last)*sizeof (T));
247 _last -= last - first;
248 }
249 }
250 void erase (iterator pos)
251 {
252 if (pos != end ()) {
253 memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T));
254 --_last;
255 }
256 }
257 void swap (simplevec<T> &t)
258 {
259 ::swap(_last, t._last);
260 ::swap(_size, t._size);
261 ::swap(_buf, t._buf);
262 }
263 };
264
265 template<class T>
266 bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
267 {
268 if (v1.size () != v2.size ())
269 return false;
270 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
271 }
272
273 template<class T>
274 bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
275 {
276 unsigned long minlast = min (v1.size (), v2.size ());
277 for (unsigned long i = 0; i < minlast; ++i) {
278 if (v1[i] < v2[i])
279 return true;
280 if (v2[i] < v1[i])
281 return false;
282 }
283 return v1.size () < v2.size ();
284 }
285
286
287 template<typename T>
288 struct vector : simplevec<T>
289 { };
290
291 #if 0
292 template<typename T>
293 struct rxvt_vec : simplevec<void *> {
294 typedef T *iterator;
295
296 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
297 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
298 void erase (int i) { erase (begin () + i); }
299 void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
300 iterator begin () const { return (iterator)simplevec<void *>::begin (); }
301 iterator end () const { return (iterator)simplevec<void *>::end (); }
302 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
303 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
304 };
305 #endif
306
307 template <typename I, typename T>
308 I find (I first, I last, const T& value)
309 {
310 while (first != last && *first != value)
311 ++first;
312
313 return first;
314 }
315
316 template<typename T>
317 struct auto_ptr {
318 T *p;
319
320 auto_ptr () : p (0) { }
321 auto_ptr (T *a) : p (a) { }
322
323 auto_ptr (auto_ptr<T> &a)
324 {
325 p = a.p;
326 a.p = 0;
327 }
328
329 template<typename A>
330 auto_ptr (auto_ptr<A> &a)
331 {
332 p = a.p;
333 a.p = 0;
334 }
335
336 ~auto_ptr ()
337 {
338 delete p;
339 }
340
341 // void because it makes sense in our context
342 void operator = (T *a)
343 {
344 delete p;
345 p = a;
346 }
347
348 void operator = (auto_ptr &a)
349 {
350 *this = a.p;
351 a.p = 0;
352 }
353
354 template<typename A>
355 void operator = (auto_ptr<A> &a)
356 {
357 *this = a.p;
358 a.p = 0;
359 }
360
361 operator T * () const { return p; }
362
363 T *operator -> () const { return p; }
364 T &operator * () const { return *p; }
365
366 T *get ()
367 {
368 T *r = p;
369 p = 0;
370 return r;
371 }
372 };
373
374 typedef auto_ptr<char> auto_str;
375
376 struct stringvec : simplevec<char *>
377 {
378 ~stringvec ()
379 {
380 for (char **c = begin (); c != end (); c++)
381 delete [] *c;
382 }
383 };
384
385 #endif
386