VAPoR  0.1
MyBase.h
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 //************************************************************************
5 // *
6 // Copyright (C) 2004 *
7 // University Corporation for Atmospheric Research *
8 // All Rights Reserved *
9 // *
10 //************************************************************************/
11 //
12 // File:
13 //
14 // Author: John Clyne
15 // National Center for Atmospheric Research
16 // PO 3000, Boulder, Colorado
17 //
18 // Date: Wed Sep 29 15:40:23 MDT 2004
19 //
20 // Description: A collection of general purpose utilities - things that
21 // probably should be in the STL but aren't.
22 //
23 
24 #ifndef _MyBase_h_
25 #define _MyBase_h_
26 
27 #include <cmath>
28 #include <cstdarg>
29 #include <string>
30 #include <cstring>
31 #include <vector>
32 #include <vapor/common.h>
33 
34 #ifdef NEW_DEBUG
35 #include <new>
36 void * operator new (size_t sz);
37 void * operator new[] (size_t sz);
38 #endif
39 
40 
41 #ifdef WIN32
42 //Silence an annoying and unnecessary compiler warning
43 #pragma warning(disable : 4251)
44 #endif
45 using namespace std;
46 
47 namespace VetsUtil {
48 
49 
58 
59 //default values used for variables outside valid grid
60 const float ABOVE_GRID = 0.f;
61 const float BELOW_GRID = 0.f;
62 
63 //
64 // The MyBase base class provides a simple error reporting mechanism
65 // that can be used by derrived classes. N.B. the error messages/codes
66 // are stored in static class members.
67 //
69 public:
70 
71  typedef void (*ErrMsgCB_T) (const char *msg, int err_code);
72  typedef void (*DiagMsgCB_T) (const char *msg);
73 
74  MyBase();
75  const string &getClassName() const {return (_className); };
76 
77 
79  //
86  //
87  static void SetErrMsg(const char *format, ...);
88 
90  //
98  //
99  static void SetErrMsg(int errcode, const char *format, ...);
100 
108  //
109  static const char *GetErrMsg() {return(ErrMsg);}
110 
112  //
116  //
117  static void SetErrCode(int err_code) { ErrCode = err_code; }
118 
120  //
125  //
126  static int GetErrCode() {return(ErrCode); }
127 
136  //
137  static void SetErrMsgCB(ErrMsgCB_T cb) { ErrMsgCB = cb; };
138 
147  //
148  static ErrMsgCB_T GetErrMsgCB() { return(ErrMsgCB); };
149 
158  //
159  static void SetErrMsgFilePtr(FILE *fp) { ErrMsgFilePtr = fp; };
160 
167  //
168  static const FILE *SetErrMsgFilePtr() { return(ErrMsgFilePtr); };
169 
171  //
179  //
180  static void SetDiagMsg(const char *format, ...);
181 
188  //
189  static const char *GetDiagMsg() {return(DiagMsg);}
190 
191 
200  //
201  static void SetDiagMsgCB(DiagMsgCB_T cb) { DiagMsgCB = cb; };
202 
211  //
212  static DiagMsgCB_T GetDiagMsgCB() { return(DiagMsgCB); };
213 
222  //
223  static void SetDiagMsgFilePtr(FILE *fp) { DiagMsgFilePtr = fp; };
224 
231  //
232 
244  static bool EnableErrMsg(bool enable) {
245  bool prev = Enabled; Enabled = enable; return (prev);
246  };
247 
248  // N.B. the error codes/messages are stored in static class members!!!
249  static char *ErrMsg;
250  static int ErrCode;
251  static int ErrMsgSize;
252  static FILE *ErrMsgFilePtr;
253  static ErrMsgCB_T ErrMsgCB;
254 
255  static char *DiagMsg;
256  static int DiagMsgSize;
257  static FILE *DiagMsgFilePtr;
258  static DiagMsgCB_T DiagMsgCB;
259  static bool Enabled;
260 
261 
262 
263 protected:
264  void SetClassName(const string &name) { _className = name; };
265 
266 private:
267  static void _SetErrMsg(char **msg,int *sz,const char *format, va_list args);
268  string _className; // name of class
269 
270 };
271 
272  COMMON_API inline int IsOdd(int x) { return(x % 2); };
273 
275  //
279  COMMON_API int IsPowerOfTwo(unsigned int x);
280 
281  COMMON_API inline int Min(int a, int b) { return(a<b ? a : b); };
282  COMMON_API inline int Max(int a, int b) { return(a>b ? a : b); };
283 
284  COMMON_API inline size_t Min(size_t a, size_t b) { return(a<b ? a : b); };
285  COMMON_API inline size_t Max(size_t a, size_t b) { return(a>b ? a : b); };
286 
287  COMMON_API inline float Min(float a, float b) { return(a<b ? a : b); };
288  COMMON_API inline float Max(float a, float b) { return(a>b ? a : b); };
289 
290  COMMON_API inline double Min(double a, double b) { return(a<b ? a : b); };
291  COMMON_API inline double Max(double a, double b) { return(a>b ? a : b); };
292 
293 
294  COMMON_API inline double LogBaseN(double x, double n) { return(log(x) / log(n)); };
295 
296  //Find integer log, base 2:
297  COMMON_API int ILog2(int n);
298 
300  //
303  //
304  COMMON_API int StrCmpNoCase(const string &s, const string &t);
305 
306 
308  //
311  COMMON_API void StrRmWhiteSpace(string &s);
312 
314  //
321  //
322  COMMON_API void StrToWordVec(const string &s, vector <string> &v);
323 
324 
325  std::vector<std::string> &SplitString(
326  const std::string &s, char delim, std::vector<std::string> &elems
327  );
328  std::vector<size_t> &SplitString(
329  const std::string &s, char delim, std::vector<size_t> &elems
330  );
331  std::vector<int> &SplitString(
332  const std::string &s, char delim, std::vector<int> &elems
333  );
334  std::vector<float> &SplitString(
335  const std::string &s, char delim, std::vector<float> &elems
336  );
337 
338 
339 
348  //
349  COMMON_API unsigned long long GetBits64(
350  unsigned long long targ,
351  int pos,
352  int n
353  );
354 
363  //
364  COMMON_API unsigned long long SetBits64(
365  unsigned long long targ,
366  int pos,
367  int n,
368  unsigned long long src
369  );
370 
371 #ifdef WIN32
372  COMMON_API inline int rint(float X) {
373  return ((X)>0)?((int)(X+0.5)):((int)(X-0.5));
374  }
375 #endif
376  // ran1 function declaration (from numerical recipes in C)
377 COMMON_API double ran1(long *);
378 };
379 
380 
381 //
382 // Handle OS differences in 64-bit IO operators
383 //
384 
385 
386 // 64-bit fseek
387 //
388 
389 #ifdef FSEEK64
390 #undef FSEEK64
391 #endif
392 
393 #if defined(WIN32)
394 //Note: win32 won't seek beyond 32 bits
395 #define FSEEK64 fseek
396 #endif
397 
398 #if defined(Linux) || defined(AIX)
399 #define FSEEK64 fseeko64
400 #endif
401 
402 #if defined(Darwin)
403 #define FSEEK64 fseeko
404 #endif
405 
406 #ifndef FSEEK64
407 #define FSEEK64 fseek64
408 #endif
409 
410 // 64-bit fopen
411 //
412 
413 #ifdef FOPEN64
414 #undef FOPEN64
415 #endif
416 
417 #if defined(WIN32) || defined(Darwin)
418 #define FOPEN64 fopen
419 #endif
420 
421 #ifndef FOPEN64
422 #define FOPEN64 fopen64
423 #endif
424 
425 // 64-bit stat
426 //
427 
428 #ifdef STAT64
429 #undef STAT64
430 #endif
431 
432 #ifdef STAT64_T
433 #undef STAT64_T
434 #endif
435 
436 #if defined(WIN32)
437 #define STAT64_T _stat
438 #define STAT64 _stat
439 #endif
440 
441 #if defined(Darwin)
442 #define STAT64_T stat
443 #define STAT64 stat
444 #endif
445 
446 #ifndef STAT64
447 #define STAT64_T stat64
448 #define STAT64 stat64
449 #endif
450 
451 
452 #ifndef TIME64_T
453 #ifdef WIN32
454 #define TIME64_T __int64
455 #else
456 #define TIME64_T int64_t
457 #endif
458 #endif
459 
460 
461 #endif //MYBASE_H
462 
static int ErrCode
Definition: MyBase.h:250
static const char * GetDiagMsg()
Definition: MyBase.h:189
static FILE * ErrMsgFilePtr
Definition: MyBase.h:252
static bool EnableErrMsg(bool enable)
Definition: MyBase.h:244
static void SetDiagMsgCB(DiagMsgCB_T cb)
Definition: MyBase.h:201
COMMON_API void StrRmWhiteSpace(string &s)
Remove white space from a string.
const string & getClassName() const
Definition: MyBase.h:75
COMMON_API int IsPowerOfTwo(unsigned int x)
Return true if power of two.
COMMON_API double ran1(long *)
COMMON_API double LogBaseN(double x, double n)
Definition: MyBase.h:294
COMMON_API unsigned long long SetBits64(unsigned long long targ, int pos, int n, unsigned long long src)
static DiagMsgCB_T DiagMsgCB
Definition: MyBase.h:258
COMMON_API double Max(double a, double b)
Definition: MyBase.h:291
static const char * GetErrMsg()
Definition: MyBase.h:109
const float ABOVE_GRID
Definition: MyBase.h:60
const float BELOW_GRID
Definition: MyBase.h:61
COMMON_API int StrCmpNoCase(const string &s, const string &t)
Case-insensitive string comparison.
COMMON_API unsigned long long GetBits64(unsigned long long targ, int pos, int n)
static const FILE * SetErrMsgFilePtr()
Definition: MyBase.h:168
COMMON_API void StrToWordVec(const string &s, vector< string > &v)
Parse a string, returning a vector of words.
static ErrMsgCB_T ErrMsgCB
Definition: MyBase.h:253
COMMON_API double Min(double a, double b)
Definition: MyBase.h:290
std::vector< float > & SplitString(const std::string &s, char delim, std::vector< float > &elems)
COMMON_API int IsOdd(int x)
Definition: MyBase.h:272
static void SetDiagMsgFilePtr(FILE *fp)
Definition: MyBase.h:223
static int GetErrCode()
Retrieve the current error code.
Definition: MyBase.h:126
void SetClassName(const string &name)
Definition: MyBase.h:264
COMMON_API int ILog2(int n)
static DiagMsgCB_T GetDiagMsgCB()
Definition: MyBase.h:212
static FILE * DiagMsgFilePtr
Definition: MyBase.h:257
#define COMMON_API
Definition: common.h:60
static void SetErrMsgFilePtr(FILE *fp)
Definition: MyBase.h:159
VetsUtil base class.
Definition: MyBase.h:68
static int ErrMsgSize
Definition: MyBase.h:251
static char * DiagMsg
Definition: MyBase.h:255
static int DiagMsgSize
Definition: MyBase.h:256
static bool Enabled
Definition: MyBase.h:259
static void SetErrCode(int err_code)
Record an error code.
Definition: MyBase.h:117
static void SetErrMsgCB(ErrMsgCB_T cb)
Definition: MyBase.h:137
static ErrMsgCB_T GetErrMsgCB()
Definition: MyBase.h:148