• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

string2.h

00001 #ifndef string2_h
00002 #define string2_h
00003 
00004 /*@+
00005 ________________________________________________________________________
00006 
00007  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
00008  Author:        A.H.Bril
00009  Date:          11-4-1994
00010  Contents:      Extra string functions
00011  RCS:           $Id: string2.h,v 1.36 2010-10-15 09:45:12 cvsbert Exp $
00012 ________________________________________________________________________
00013 -*/
00014 
00015 
00016 
00017 #include "gendefs.h"
00018 #include "plftypes.h"
00019 #include <string.h>
00020 #include <ctype.h>
00021 
00022 #ifdef __cpp__
00023 extern "C" {
00024 #endif
00025 
00026 
00028 mGlobal void removeTrailingBlanks(char*);
00030 #define mSkipBlanks(ptr) \
00031     { if ( ptr ) { while ( *(ptr) && isspace(*(ptr)) ) (ptr)++; } }
00032 
00033 #define mSkipNonBlanks(ptr) \
00034     { if ( ptr ) { while ( *(ptr) && !isspace(*(ptr)) ) (ptr)++; } }
00035 
00036 #define mTrimBlanks(ptr) \
00037     { mSkipBlanks(ptr); removeTrailingBlanks(ptr); }
00038 
00039 
00041 mGlobal int caseInsensitiveEqual(const char*,const char*,
00042                                      int nr_chars_to_match_0_is_all);
00044 mGlobal int matchString(const char* startstring,const char* maybebigger);
00046 mGlobal int matchStringCI(const char*,const char*);
00047 mGlobal int stringEndsWith(const char* endstring,const char* maybebigger);
00048 mGlobal int stringEndsWithCI(const char*,const char*);
00049 
00052 mGlobal const char* getNextWord(const char*,char*);
00053 
00055 mGlobal int countCharacter(const char*,char);
00057 mGlobal void replaceCharacter(char*,char from,char to);
00059 mGlobal void replaceString(char*,const char* from,const char* to);
00061 mGlobal void removeCharacter(char*,char);
00064 mGlobal void cleanupString(char*,int,int,int);
00066 mGlobal int isNumberString(const char*,int int_only);
00067 
00069 mGlobal const char* getStringFromInt(od_int32);
00070 mGlobal const char* getStringFromUInt(od_uint32);
00071 mGlobal const char* getStringFromInt64(od_int64);
00072 mGlobal const char* getStringFromUInt64(od_uint64);
00073 
00076 mGlobal const char* getStringFromDouble(const char* fmt,double);
00078 mGlobal const char* getStringFromFloat(const char* fmt,float);
00080 mGlobal void prettyNumber(char*,int is_float);
00081 
00083 mGlobal const char* getYesNoString(int);
00085 mGlobal int yesNoFromString(const char*);
00087 mGlobal const char* getRankPostFix(int);
00089 mGlobal const char* getBytesString(od_uint64);
00091 mGlobal const char* getLimitedDisplayString(const char*,int nrchars,
00092                                             int trimright);
00093 
00094 mGlobal int getIndexInStringArrCI(const char*,const char* const* arr,
00095                                   int startnr,int nr_chars_to_match,
00096                                   int notfoundidx);
00097 
00098 
00099 #ifdef __cpp__
00100 }
00101 #include <stdlib.h>
00102 #include "undefval.h"
00103 
00104 inline const char* toString(od_int32 i) { return getStringFromInt( i ); }
00105 inline const char* toString(od_uint32 i){ return getStringFromUInt( i ); }
00106 inline const char* toString(od_int64 i) { return getStringFromInt64( i ); }
00107 inline const char* toString(od_uint64 i){ return getStringFromUInt64( i ); }
00108 inline const char* toString( float f )  { return getStringFromFloat( 0, f ); }
00109 inline const char* toString( double d ) { return getStringFromDouble( 0, d ); }
00110 inline const char* toString( bool b )   { return getYesNoString( b ); }
00111 inline const char* toString( short i)   { return getStringFromInt( (int)i ); }
00112 inline const char* toString( unsigned short i )
00113                                 { return getStringFromUInt( (unsigned int)i ); }
00114 
00115 #define mImplGetFromStrFunc( type, func, udfv ) \
00116 inline bool getFromString( type& i, const char* s, type undef=udfv ) \
00117 { \
00118     if ( s && *s ) \
00119     { \
00120         char* e; \
00121         i = (type)func; \
00122         if ( e==s ) \
00123         { \
00124             i = undef; \
00125             return false;\
00126         }\
00127         return true; \
00128     } \
00129  \
00130     i = undef; \
00131     return false; \
00132 }
00133 
00134 
00135 
00136 // inline bool getFromString( double& d, const char* s, double udefval );
00137 mImplGetFromStrFunc(double, strtod(s,&e), mUdf(double) )
00138 // inline bool getFromString( float& d, const char* s, float udefval );
00139 mImplGetFromStrFunc(float, strtod(s,&e), mUdf(float) )
00140 // inline bool getFromString( int& d, const char* s, int udefval );
00141 mImplGetFromStrFunc(int, strtol(s,&e,10), mUdf(int) )
00142 #undef mImplGetFromStrFunc
00143 
00144 inline bool getFromString( bool& b, const char* s )
00145 {
00146     if ( s )
00147     {
00148         b = ( yesNoFromString( s ) ? true : false );
00149         return true;
00150     }
00151 
00152     b = false;
00153     return false;
00154 }
00155 
00156 inline bool toBool( const char* s, bool defval=true )
00157 {
00158     return s && *s ? yesNoFromString(s) : defval;
00159 }
00160 
00161 inline float toFloat( const char* s, float defval=0 )
00162 {
00163     float ret = defval; getFromString( ret, s, ret ); return ret;
00164 }
00165 
00166 inline double toDouble( const char* s, double defval=0 )
00167 {
00168     double ret = defval; getFromString( ret, s, ret ); return ret;
00169 }
00170 
00171 inline int toInt( const char* s, int defval=0 )
00172 {
00173     int ret = defval; getFromString( ret, s, ret ); return ret;
00174 }
00175 
00176 
00177 #endif
00178 
00179 
00180 #endif

Generated on Tue Nov 30 2010 for Basic by  doxygen 1.7.1