VAPoR  3.0.0
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 
242  static bool EnableErrMsg(bool enable) {
243  bool prev = Enabled; Enabled = enable; return (prev);
244  };
245 
246  // N.B. the error codes/messages are stored in static class members!!!
247  static char *ErrMsg;
248  static int ErrCode;
249  static int ErrMsgSize;
250  static FILE *ErrMsgFilePtr;
251  static ErrMsgCB_T ErrMsgCB;
252 
253  static char *DiagMsg;
254  static int DiagMsgSize;
255  static FILE *DiagMsgFilePtr;
256  static DiagMsgCB_T DiagMsgCB;
257  static bool Enabled;
258 
259 
260 
261 protected:
262  void SetClassName(const string &name) { _className = name; };
263 
264 private:
265  static void _SetErrMsg(char **msg,int *sz,const char *format, va_list args);
266  string _className; // name of class
267 
268 };
269 
270  COMMON_API inline int IsOdd(int x) { return(x % 2); };
271 
273  //
277  COMMON_API int IsPowerOfTwo(unsigned int x);
278 
279  COMMON_API inline int Min(int a, int b) { return(a<b ? a : b); };
280  COMMON_API inline int Max(int a, int b) { return(a>b ? a : b); };
281 
282  COMMON_API inline size_t Min(size_t a, size_t b) { return(a<b ? a : b); };
283  COMMON_API inline size_t Max(size_t a, size_t b) { return(a>b ? a : b); };
284 
285  COMMON_API inline float Min(float a, float b) { return(a<b ? a : b); };
286  COMMON_API inline float Max(float a, float b) { return(a>b ? a : b); };
287 
288  COMMON_API inline double Min(double a, double b) { return(a<b ? a : b); };
289  COMMON_API inline double Max(double a, double b) { return(a>b ? a : b); };
290 
291 
292  COMMON_API inline double LogBaseN(double x, double n) { return(log(x) / log(n)); };
293 
294  //Find integer log, base 2:
295  COMMON_API int ILog2(int n);
296 
298  //
301  //
302  COMMON_API int StrCmpNoCase(const string &s, const string &t);
303 
304 
306  //
309  COMMON_API void StrRmWhiteSpace(string &s);
310 
312  //
319  //
320  COMMON_API void StrToWordVec(const string &s, vector <string> &v);
321 
322 
323  std::vector<std::string> &SplitString(
324  const std::string &s, char delim, std::vector<std::string> &elems
325  );
326  std::vector<size_t> &SplitString(
327  const std::string &s, char delim, std::vector<size_t> &elems
328  );
329  std::vector<int> &SplitString(
330  const std::string &s, char delim, std::vector<int> &elems
331  );
332  std::vector<float> &SplitString(
333  const std::string &s, char delim, std::vector<float> &elems
334  );
335  std::vector<double> &SplitString(
336  const std::string &s, char delim, std::vector<double> &elems
337  );
338 
339 
340 
349  //
350  COMMON_API unsigned long long GetBits64(
351  unsigned long long targ,
352  int pos,
353  int n
354  );
355 
364  //
365  COMMON_API unsigned long long SetBits64(
366  unsigned long long targ,
367  int pos,
368  int n,
369  unsigned long long src
370  );
371 
372 #ifdef WIN32
373  COMMON_API inline int rint(float X) {
374  return ((X)>0)?((int)(X+0.5)):((int)(X-0.5));
375  }
376 #endif
377  // ran1 function declaration (from numerical recipes in C)
378 COMMON_API double ran1(long *);
379 };
380 
381 
382 //
383 // Handle OS differences in 64-bit IO operators
384 //
385 
386 
387 // 64-bit fseek
388 //
389 
390 #ifdef FSEEK64
391 #undef FSEEK64
392 #endif
393 
394 #if defined(WIN32)
395 //Note: win32 won't seek beyond 32 bits
396 #define FSEEK64 fseek
397 #endif
398 
399 #if defined(Linux) || defined(AIX)
400 #define FSEEK64 fseeko64
401 #endif
402 
403 #if defined(Darwin)
404 #define FSEEK64 fseeko
405 #endif
406 
407 #ifndef FSEEK64
408 #define FSEEK64 fseek64
409 #endif
410 
411 // 64-bit fopen
412 //
413 
414 #ifdef FOPEN64
415 #undef FOPEN64
416 #endif
417 
418 #if defined(WIN32) || defined(Darwin)
419 #define FOPEN64 fopen
420 #endif
421 
422 #ifndef FOPEN64
423 #define FOPEN64 fopen64
424 #endif
425 
426 // 64-bit stat
427 //
428 
429 #ifdef STAT64
430 #undef STAT64
431 #endif
432 
433 #ifdef STAT64_T
434 #undef STAT64_T
435 #endif
436 
437 #if defined(WIN32)
438 #define STAT64_T _stat
439 #define STAT64 _stat
440 #endif
441 
442 #if defined(Darwin)
443 #define STAT64_T stat
444 #define STAT64 stat
445 #endif
446 
447 #ifndef STAT64
448 #define STAT64_T stat64
449 #define STAT64 stat64
450 #endif
451 
452 
453 #ifndef TIME64_T
454 #ifdef WIN32
455 #define TIME64_T __int64
456 #else
457 #define TIME64_T int64_t
458 #endif
459 #endif
460 
461 
462 #endif //MYBASE_H
463 
static int ErrCode
Definition: MyBase.h:248
static const char * GetDiagMsg()
Definition: MyBase.h:189
static FILE * ErrMsgFilePtr
Definition: MyBase.h:250
static bool EnableErrMsg(bool enable)
Definition: MyBase.h:242
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:292
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:256
COMMON_API double Max(double a, double b)
Definition: MyBase.h:289
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:251
COMMON_API double Min(double a, double b)
Definition: MyBase.h:288
COMMON_API int IsOdd(int x)
Definition: MyBase.h:270
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:262
COMMON_API int ILog2(int n)
static DiagMsgCB_T GetDiagMsgCB()
Definition: MyBase.h:212
static FILE * DiagMsgFilePtr
Definition: MyBase.h:255
static void SetErrMsgFilePtr(FILE *fp)
Definition: MyBase.h:159
VetsUtil base class.
Definition: MyBase.h:68
static int ErrMsgSize
Definition: MyBase.h:249
#define COMMON_API
static char * DiagMsg
Definition: MyBase.h:253
static int DiagMsgSize
Definition: MyBase.h:254
static bool Enabled
Definition: MyBase.h:257
static void SetErrCode(int err_code)
Record an error code.
Definition: MyBase.h:117
static void SetErrMsgCB(ErrMsgCB_T cb)
Definition: MyBase.h:137
std::vector< double > & SplitString(const std::string &s, char delim, std::vector< double > &elems)
static ErrMsgCB_T GetErrMsgCB()
Definition: MyBase.h:148