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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines