File: util.h

package info (click to toggle)
swish%2B%2B 6.1.5-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,256 kB
  • ctags: 1,759
  • sloc: ansic: 11,931; lisp: 804; sh: 629; perl: 366; makefile: 80
file content (347 lines) | stat: -rw-r--r-- 10,708 bytes parent folder | download | duplicates (6)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
**	    SWISH++
**	    util.h
**
**	    Copyright (C) 1998-2005  Paul J. Lucas
**
**	    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 2 of the License, 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.
*/

#ifndef	util_H
#define	util_H

// standard
#include <cctype>
#include <cerrno>
#include <climits>
#include <cstring>
#include <iostream>
#include <string>
#include <time.h>			            /* needed by sys/resource.h */
#include <sys/time.h>			        /* needed by FreeBSD systems */
#include <sys/types.h>			        /* needed by FreeBSD systems */
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>			            /* for _exit(2), geteuid(2) */

//
// POSIX.1 is, IMHO, brain-damaged in the way it makes you determine the
// maximum path-name length, so we'll simply pick a sufficiently large constant
// such as 1024.  In practice, this is the actual value used on many SVR4 as
// well as 4.3+BSD systems.
//
// See also: W. Richard Stevens.  "Advanced Programming in the Unix
// Environment," Addison-Wesley, Reading, MA, 1993.  pp. 34-42.
//
#ifdef	PATH_MAX
#undef	PATH_MAX
#endif
int const		    PATH_MAX = 1024;

// local
#include "exit_codes.h"
#include "omanip.h"
#include "platform.h"			        /* for PJL_NO_SYMBOLIC_LINKS */

extern char const*	me;

//*****************************************************************************
//
// SYNOPSIS
//
        template< int Buf_Size, int N > class char_buffer_pool
//
// DESCRIPTION
//
//	    A char_buffer_pool maintains a small set ("pool") of size N of
//	    available character buffers, each of size Buf_Size, and issues them in
//	    a round-robin manner.
//
//	    This is used by functions to return a character string without having
//	    to allocate memory dynamically nor have previously returned strings
//	    overwritten.
//
//*****************************************************************************
{
public:
	char_buffer_pool() : next_buf_index_( 0 ), cur_buf_( buf_[ 0 ] ) { }

	char*	current() const { return cur_buf_; }
	char*	next() {
                cur_buf_ = buf_[ next_buf_index_ ];
                next_buf_index_ = (next_buf_index_ + 1) % N;
                return cur_buf_;
            }
private:
	char	buf_[ N ][ Buf_Size ];
	int	    next_buf_index_;
	char	*cur_buf_;
};

//*****************************************************************************
//
// SYNOPSIS
//
        inline char const* find_newline( char const *c, char const *end )
//
// DESCRIPTION
//
//	    Finds the next newline (CR or LF) in a character sequence.
//
// PARAMETERS
//
//	    c	    The iterator to use.
//
//	    end	    The iterator marking the end of the range to check.
//
// RETURN VALUE
//
//	    Returns an iterator positioned either at the first character of a CR-LF
//	    if found, or at "end" if not.
//
//*****************************************************************************
{
	while ( c != end && *c != '\n' && *c != '\r' )
		++c;
	return c;
}

//*****************************************************************************
//
// SYNOPSIS
//
        template< typename T > void max_out_limit( T resource )
//
// DESCRIPTION
//
//	    Set the limit for the given resource to its maximum value.  If we're
//	    running as root, set it to infinity.
//
// PARAMETERS
//
//	    resource	The ID for the resource as given in sys/resources.h.
//
// NOTE
//
//	    This can't be an ordinary function since the type "resource" isn't int
//	    on some systems (e.g., Linux).
//
// SEE ALSO
//
//	    W. Richard Stevens.  "Advanced Programming in the Unix Environment,"
//	    Addison-Wesley, Reading, MA, 1993. pp. 180-184.
//
//*****************************************************************************
{
	struct rlimit r;
#ifdef	RLIM_INFINITY	/* hey, you never know: it might be undefined */
	if ( ::geteuid() == 0 )
		r.rlim_max = RLIM_INFINITY;
	else
#endif
		::getrlimit( resource, &r );
	r.rlim_cur = r.rlim_max;
	::setrlimit( resource, &r );
}

//*****************************************************************************
//
// SYNOPSIS
//
        inline char const* pjl_basename( char const *file_name )
//
// DESCRIPTION
//
//	    Determine the base name of a given file name.
//
// PARAMETERS
//
//	    file_name	The file_name.
//
// RETURN VALUE
//
//	    Returns a pointer to the base name of the file name.  Note that the
//	    pointer points within file_name, i.e., the two will share storage.
//
//*****************************************************************************
{
        char const *const slash = ::strrchr( file_name, '/' );
        return slash ? slash + 1 : file_name;
}

//*****************************************************************************
//
// SYNOPSIS
//
	    inline char const* skip_newline( char const *c, char const *end )
//
// DESCRIPTION
//
//	    Skips the next newline (CR and/or LF) in a character sequence.
//
// PARAMETERS
//
//	    c	    The iterator positioned at either a CR or LF.
//
//	    end	    The iterator marking the end of the range.
//
// RETURN VALUE
//
//	    Returns an iterator positioned either at a character after a newline if
//	    found, or untouched if not.
//
//*****************************************************************************
{
	if ( c != end && *c == '\r' )
		++c;
	if ( c != end && *c == '\n' )
		++c;
	return c;
}

//*****************************************************************************
//
//	    File test functions.  Those that do not take an argument operate on the
//	    last file stat'ed.
//
//*****************************************************************************

extern struct stat	stat_buf;		    // someplace to do a stat(2) in

inline bool	    file_exists( char const *path ) {
                    return ::stat( path, &stat_buf ) != -1;
                }
inline bool	    file_exists( std::string const &path ) {
                    return file_exists( path.c_str() );
                }

inline off_t	file_size() { return stat_buf.st_size; }

inline bool	    is_directory() {
                    return S_ISDIR( stat_buf.st_mode );
                }
inline bool	    is_directory( char const *path ) {
                    return file_exists( path ) && is_directory();
                }
inline bool	    is_directory( std::string const &path ) {
                    return is_directory( path.c_str() );
                }

inline bool	    is_plain_file() {
                    return S_ISREG( stat_buf.st_mode );
                }
inline bool	    is_plain_file( char const *path ) {
                    return file_exists( path ) && is_plain_file();
                }
inline bool	    is_plain_file( std::string const &path ) {
                    return is_plain_file( path.c_str() );
                }

#ifndef	PJL_NO_SYMBOLIC_LINKS
inline bool	    is_symbolic_link() {
                    return S_ISLNK( stat_buf.st_mode & S_IFLNK );
                }
inline bool	    is_symbolic_link( char const *path ) {
                    return	::lstat( path, &stat_buf ) != -1
                        && is_symbolic_link();
                }
inline bool	    is_symbolic_link( std::string const &path ) {
                    return is_symbolic_link( path.c_str() );
                }
#endif	/* PJL_NO_SYMBOLIC_LINKS */

//*****************************************************************************
//
//	    Miscelleneous.
//
//*****************************************************************************

inline int pjl_abs( int n ) {
	return n >= 0 ? n : -n;
}

inline std::ostream& error( std::ostream &o = std::cerr ) {
	return o << me << ": error: ";
}

inline std::ostream& error_string( std::ostream &o, int err_code ) {
	return o << ": " << std::strerror( err_code ) << std::endl;
}

inline std::ostream& error_string( std::ostream &o = std::cerr ) {
	return error_string( o, errno );
}

inline PJL::omanip<int> error_string( int err_code ) {
	return PJL::omanip<int>( error_string, err_code );
}

#define	internal_error \
	std::cerr << (me ? me : "SWISH++") << ", \"" \
	<< __FILE__ << "\", line " << __LINE__ << ": internal error: "

#define	NUM_ELEMENTS(a)	(sizeof (a) / sizeof( (a)[0] ))

inline std::ostream& report_error( std::ostream &o = std::cerr ) {
	o << "; please report this error" << std::endl;
	::_exit( Exit_Internal_Error );
	return o;	                        // just to make compiler happy
}

inline char*    new_strdup( char const *s ) {
                    return std::strcpy( new char[ std::strlen( s ) + 1 ], s );
                }

                // ensure function semantics: 'c' is expanded once
inline bool	    is_alnum( char c ) {
                    return isalnum( static_cast<unsigned char>( c ) );
                }
inline bool	    is_alpha( char c ) {
                    return isalpha( static_cast<unsigned char>( c ) );
                }
inline bool	    is_digit( char c ) {
                    return isdigit( static_cast<unsigned char>( c ) );
                }
inline bool	    is_punct( char c ) {
                    return ispunct( static_cast<unsigned char>( c ) );
                }
inline bool	    is_space( char c ) {
                    return isspace( static_cast<unsigned char>( c ) );
                }
inline bool	    is_upper( char c ) {
                    return isupper( static_cast<unsigned char>( c ) );
                }
inline bool	    is_xdigit( char c ) {
                    return isxdigit( static_cast<unsigned char>( c ) );
                }

inline char	    to_lower( char c ) { return tolower( c ); }
extern char*	to_lower( char const* );
extern char*	to_lower( char const *begin, char const *end );
extern char*	to_lower( char *buf, char const* );
extern char*	to_lower_r( char const* );
extern char*	to_lower_r( char const *begin, char const *end );

#define	FOR_EACH(T,C,I) \
	for ( T::const_iterator I = (C).begin(); I != (C).end(); ++I )

#define	FOR_EACH_IN_PAIR(T,P,I) \
	for ( T::const_iterator I = (P).first; I != (P).second; ++I )

#define	TRANSFORM_EACH(T,C,I) \
	for ( T::iterator I = (C).begin(); I != (C).end(); ++I )

#endif	/* util_H */
/* vim:set et sw=4 ts=4: */