Pol  Revision:3cfda13
clib.h
Go to the documentation of this file.
1 
7 #ifndef CLIB_CLIB_H
8 #define CLIB_CLIB_H
9 
10 #include "pol_global_config.h"
11 
12 #ifndef __STDDEF_H
13 #include "stddef.h"
14 #endif
15 #include "rawtypes.h"
16 #include <ctime>
17 
24 #define EXPLICIT_CAST( totype, fromtype ) ( totype )
25 
26 #define ms_to_clocks( ms ) ( ms * CLOCKS_PER_SEC / 1000 )
27 namespace Pol
28 {
33 namespace Clib
34 {
36 int favail( void );
37 
39 char* strpcpy( char* dest, const char* src, size_t maxlen );
40 
42 char* stracpy( char* dest, const char* src, size_t maxlen );
43 
44 
46 int match( char* a, char* b );
47 
48 /* strip leading, trailing spaces, trim multile whitespace to single space */
49 char* strip( char* s );
50 
51 #ifdef __STDIO_H /* well there you go, not portable... why not standard the idempotent mechanism? \
52  */
53 
54 FILE* uniqfile( char* directory, char* pathname );
55 #endif
56 
57 int forspec( const char* spec, void ( *func )( const char* pathname ) );
58 
64 char** decompose1( char* cmdline, int* pargc );
65 
67 void legalize( char* s, const char* allowed );
68 
70 int nwords( const char* s );
71 
73 char* binary( unsigned int val, int nbits );
74 
76 void incStr( char* str );
77 
78 
79 /* Compiler specific implementations for case insensitive compare... */
80 #ifdef __GNUC__
81 #define stricmp strcasecmp
82 #define strnicmp strncasecmp
83 #endif
84 
85 #ifdef _MSC_VER
86 #define stricmp _stricmp
87 #define strnicmp _strnicmp
88 #endif
89 
90 #ifdef __cplusplus
91 #define PARAMS ...
92 #else
93 #define PARAMS
94 #endif
95 
97 size_t getCurrentMemoryUsage();
98 
99 
100 template <typename T>
101 void sanitize_upperlimit( T* value, const T max )
102 {
103  if ( *value > max )
104  *value = max;
105 }
106 template <typename T>
107 void sanitize_lowerlimit( T* value, const T min )
108 {
109  if ( *value < min )
110  *value = min;
111 }
112 template <typename T>
113 void sanitize_bounds( T* value, const T min, const T max )
114 {
115  if ( *value > max )
116  *value = max;
117  else if ( *value < min )
118  *value = min;
119 }
120 
125 {
126 public:
128  void update( double value );
129  double variance() const;
130  double mean() const;
131  u64 count() const;
132  double max() const;
133 
134 private:
136  double _max;
137  double _mean;
138  double _m2;
139 };
140 
141 
143 inline std::tm localtime( const std::time_t& t )
144 {
145  std::tm result;
146 #ifdef _MSC_VER
147  localtime_s( &result, &t );
148 #else
149  localtime_r( &t, &result );
150 #endif
151  return result;
152 }
153 }
154 #define strzcpy Clib::stracpy
155 
156 #define STRCPY( a, b ) strncpy( a, b, sizeof a )
157 #define STRNCPY( a, b ) strncpy( a, b, sizeof a )
158 #define STRPCPY( a, b ) \
159  Clib: \
160  strpcpy( a, b, sizeof a )
161 #define STRACPY( a, b ) \
162  Clib: \
163  stracpy( a, b, sizeof a )
164 #define MEMCLR( a ) memset( &a, 0, sizeof a )
165 #define MEMCPY( a, b ) memcpy( &a, &b, sizeof a )
166 }
167 
168 #endif // CLIB_CLIB_H
size_t getCurrentMemoryUsage()
returns the current process size in bytes
Definition: xmain.cpp:26
void update(double value)
Definition: clib_utils.cpp:16
void sanitize_bounds(T *value, const T min, const T max)
Definition: clib.h:113
char * binary(unsigned int val, int nbits)
std::tm localtime(const std::time_t &t)
threadsafe version of localtime
Definition: clib.h:143
int match(char *a, char *b)
char * strpcpy(char *dest, const char *src, size_t maxlen)
void incStr(char *str)
char ** decompose1(char *cmdline, int *pargc)
char * stracpy(char *dest, const char *src, size_t maxlen)
Definition: stracpy.cpp:17
int nwords(const char *s)
unsigned long long u64
Definition: rawtypes.h:38
void sanitize_upperlimit(T *value, const T max)
Definition: clib.h:101
int forspec(const char *spec, void(*func)(const char *pathname))
Definition: forspcnt.cpp:15
void legalize(char *s, const char *allowed)
double variance() const
Definition: clib_utils.cpp:25
Definition: berror.cpp:12
void sanitize_lowerlimit(T *value, const T min)
Definition: clib.h:107
int favail(void)
char * strip(char *s)