ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
(Generate patch)

Comparing rxvt-unicode/src/rxvtutil.h (file contents):
Revision 1.4 by root, Thu Sep 2 07:44:40 2004 UTC vs.
Revision 1.46 by sf-exg, Thu Jun 2 13:07:28 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines