utilits.h 3.98 KB
Newer Older
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
// last modified 11.07.2012
#ifndef UTIL_H
#define UTIL_H

#include <math.h>

#define MaxFileRecordLength 16384
#define RealMax 1.e+30
#define RealMin 1.e-30

#define My_PI  3.14159265358979
#define g2r(x)  (((double)(x))*My_PI/180)
#define r2g(x)  (((double)(x))/My_PI*180)
#define cosdeg(x) cos(g2r(x))
#define sindeg(x) sin(g2r(x))
#define tandeg(x) tan(g2r(x))

#define My_max(a,b)  (((a) > (b)) ? (a) : (b))
#define My_min(a,b)  (((a) < (b)) ? (a) : (b))

// Class Message
#define MSG_OUTSCRN 1
#define MSG_OUTFILE 2
class cMsg
{
protected:
  int enabled;
  int channel;
  char fname[64];

public:
  cMsg();
  ~cMsg();
  void enable();
  void disable();
  void setchannel( int newchannel );
  void setfilename( const char* newfilename );
  int print( const char* fmt, ... );
  };
extern cMsg Msg;


// Class Error message
class cErrMsg : public cMsg
{

public:
  cErrMsg();
  ~cErrMsg();
  int post( const char* fmt, ... );
  char* msgAllocateMem();
  char* msgOpenFile( const char *fname );
  char* msgReadFile( const char *fname, int line, const char *expected );
  };
extern cErrMsg Err;


// Class Log
class cLog : public cMsg
{
private:
  int timestamp_enabled;

public:
  cLog();
  ~cLog();
  void start( const char* filename );
  void timestamp_enable();
  void timestamp_disable();
  int print( const char* fmt, ... );
  };
extern cLog Log;


/***  Error messages and logging  ***/
char *utlCurrentTime();

// Following error functions are outdated. Use global Err object instead.
int utlPostError( const char* message );
char *utlErrMsgMemory();
char *utlErrMsgOpenFile( const char *fname );
char *utlErrMsgEndOfFile( const char *fname );
char *utlErrMsgReadFile( const char *fname, int line, const char *expected );

/*** Command-line processing ***/
int utlCheckCommandLineOption( int argc, char **argv, const char *option, int letters_to_compare );

/***  String handling  ***/
int utlStringReadOption( char *record, char *option_name, char *contents );
int utlReadSequenceOfInt( char *line, int *value );
int utlReadSequenceOfDouble( char *line, double *value );
int utlPickUpWordFromString( char *string, int n, char *word );
char *utlPickUpWordFromString( char *string, int n );
char *utlPickUpWordFromString( char *string, char *pos, char *word );
int utlCountWordsInString( char *record );
char *utlWords2String( int nwords, char **word );
int utlSubString( char *logstr, int p1, int p2, char *substr );
void utlPrintToString( char *string, int position, char *insert );

/***  File handling  ***/
int utlFindNextInputField( FILE *fp );
int utlReadNextRecord( FILE *fp, char *record, int *line );
int utlReadNextDataRecord( FILE *fp, char *record, int *line );
char *utlFileFindRecord( char *fname, char *pattern, char *record );
int utlFileParceToString( FILE *fp, char *pattern );
int utlGetNumberOfRecords( const char *fname );
int utlGetNumberOfDataRecords( const char *fname );
int utlFileReadOption( char *fname, char *option_name, char *contents );
char *utlFileChangeExtension( const char *fname, const char *newext );
char *utlFileRemoveExtension( const char *fname );
int utlFileRemoveExtension( char *newname, const char *fname );
char *utlFileAddExtension( const char *fname, const char *addext );
int utlReadXYdata( char *fname, double **x, double **y );

/***   MATH & GEOMETRY   ***/
double utlRandom( int& seed );
double utlRandom( double avg, double err );
double utlNormal( int& seed, double avg, double stdev );
int utlPointInPolygon( int nvrtx, double *xvrtx, double *yvrtx, double x, double y );

/***  O T H E R S    ***/
void utlTimeSplit( double ctime, int& nHour, int& nMin, int& nSec, int& nMsec );
char *utlTimeSplitString( double ctime );
char *utlTimeSplitString( int ctime );
int utlTimeHour( double timesec );
int utlTimeHour( int timesec );
int utlTimeMin( double timesec );
int utlTimeMin( int timesec );
int utlTimeSec( double timesec );
int utlTimeSec( int timesec );

#endif  // UTIL_H