Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

dsutil.h

Go to the documentation of this file.
00001 /*
00002  * $Id: dsutil.h,v 1.15 2004/10/05 13:42:31 dsamersoff Exp $
00003  */
00004 
00005 
00006 #ifndef dsutil_h
00007 #define dsutil_h 
00008 
00009 #ifdef HAVE_CONFIG_H
00010 # include <config.h>
00011 #endif
00012 
00013 
00014 #include <string.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 
00018 #ifdef HAVE_STL_STRING
00019 #include <typeinfo>
00020 #endif
00021 
00022 #include <dsSmartException.h>
00023 
00024 
00031 DECLARE_EXCEPTION(AutoStr);
00032 
00033 /* some usefull typedefs usually defined by OS */
00034 
00035 typedef unsigned short ushort;
00036 typedef unsigned long  ulong;
00037 typedef unsigned int   uint;
00038 
00039 
00047 unsigned long dsAtoul(char *src);
00048 unsigned long dsAtoul(std::istream& src);
00049 
00057 unsigned long dsAtoul(std::istream& src, int n);
00058 
00059 
00060 
00069 inline int equ(char *a, char *b)
00070 {
00071     if (a == 0 && b == 0 ) return 1;  
00072     if (a == 0 || b == 0 ) return (a-b);  
00073     return (strcmp(a,b) == 0);
00074 }
00078 inline int equ(char *a, char *b, int n)
00079 {
00080     if (a == 0 && b == 0 ) return 1;
00081     if (a == 0 || b == 0 ) return (a-b);
00082     return (strncmp(a,b,n) == 0);
00083 }
00084 
00092 char *ds_strndup(const char *s, int len);
00093 
00094 inline char *dsStrndup(const char *s, int len)
00095 {
00096     return ds_strndup(s, len);
00097 }
00098 
00103 char *ds_strdup(const char *s);
00104 
00105 inline char *dsStrdup(const char *s)
00106 {
00107     return ds_strdup(s);
00108 }
00109 
00110 #ifndef HAVE_STRNDUP
00111 inline char *strndup(const char *s, int len){ return ds_strndup(s,len); }
00112 #endif
00113 
00118 char *ds_strnchr(char *s, char c, int len);
00123 void size_strcpy(char *dest, const char *src, int size);
00124 void size_strcpy(char *dest, const char *src, int size, char *file, int line );
00125 
00126 #define __size_strcpy(dest,src, size) size_strcpy(dest,src,size,__FILE__,__LINE__);
00127 
00131 char *ds_zalloc(size_t size);
00132 
00136 char *trim(char *src);
00137 
00142 int split(char delem, char *str, ... );
00143 
00147 int split(char delem, char *str, int nf, ... );
00148 
00149 /* return conunction of passed arguments
00150  * All of them should be <b>char*</b> last is 0
00151  */
00152 char *unsplit(char delem, ... );
00153 
00164 class autostr
00165 {
00166   char *s;  
00167   int sz;
00168 
00169 public:
00173   autostr(){ s = 0; }
00174 
00179   autostr(const char *src);
00180 
00186   autostr(char *src, int size);
00187 
00192   autostr(int size);
00193 
00194  ~autostr(){ if(s) delete s; } 
00195 
00199   char * sprintf(char *format, ... );
00200 
00206   char& operator[](int i);
00207 
00212   autostr& operator +(char *ns); 
00213   autostr& operator +(autostr& as); 
00214 
00218   autostr& operator =(char * src);
00219 
00223   autostr& dup(char *src){ s = src; sz = strlen(src); return *this; }
00224  
00228  int size(){ return sz; }
00229 
00233  void ncpy(char *src, int n);
00234 
00238  char *c_str(){ return s; } 
00239  operator char *(){ return c_str(); } 
00240 
00244  operator int (){ return strlen(s); } 
00245 
00249  int operator == (char *ns){ return (strcmp(s,ns) == 0); }
00250  int operator != (char *ns){ return (strcmp(s,ns) != 0); }
00251 
00252 };
00253 
00254 template <class _T>
00255 class dsSafePtr
00256 {
00257     _T _ptr;
00258 
00259 public:
00260     dsSafePtr()
00261     {
00262         _ptr = 0; 
00263     }
00264     
00265     ~dsSafePtr()
00266     { 
00267         if (_ptr)
00268             delete[] _ptr;
00269     }
00270     
00271     void operator=(dsSafePtr<_T> x)
00272     {
00273         if (_ptr)
00274             delete _ptr;
00275 
00276         _ptr =  x.get();
00277     }
00278 
00279     void operator=(_T x)
00280     {
00281         if (_ptr)
00282             delete _ptr;
00283 
00284         _ptr =  x;
00285     }
00286 
00287 
00288     _T get()
00289     {
00290         return _ptr;
00291     }
00292 
00293     _T operator->()
00294     {
00295         return _ptr;
00296     }
00297 
00298     operator _T()
00299     {
00300         return _ptr;
00301     }
00302 };
00303 
00304 #endif

Generated on Mon May 16 18:26:58 2005 for libdms4 by doxygen1.3-rc2