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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines