/** * $Id: mstring.cpp,v 1.27 2002/10/31 22:34:46 plasmahh Exp $ * $Revision: 1.27 $ * $Author: plasmahh $ * $Date: 2002/10/31 22:34:46 $ */ #include "mstring.h" #include "exception.h" unsigned long mstring::checksum(checksum_type type) const { switch (type) { case SUM : return checksum_sum(); case CRC8 : case CRC16 : case CRC32 : case CHECK8 : case CHECK16 : case CHECK32 : case MD5 : case SHA1 : throw new x_implemented ( "algo SHA1",__FLF__); break; default : throw new x_parameter ( "??? ", __FLF__); } // of switch } unsigned long mstring::checksum_sum( void ) const { unsigned long sum = 0; for(register unsigned int i=0;i < leng;i++) { sum+=(int) sp[i]; } return sum; } mstring operator + ( const mstring& a, const mstring& b) { char * ptr; ptr = new char[a.leng+b.leng+1]; #ifdef _SAFE_ if (!ptr) { throw new x_mem("operator+(char,char)",__FLF__); } #endif ptr[a.leng+b.leng]='\0'; strncpy(ptr, a.sp, a.leng); strncpy(ptr+a.leng,b.sp,b.leng); mstring g(ptr); delete [] ptr; return g; } mstring::mstring( ) { sp = new char[1]; sp[0] = '\0'; leng=0; } mstring::mstring(const uchar8 * cstr) { sp = NULL; leng = strlen((const char *)cstr); sp = new char[leng+1]; #ifdef _SAFE_ // Does this work here? if we can't get memory, what will really hapen to us ? if (!sp) throw new x_mem("mstring::mstring(char)",__FLF__); #endif strncpy(sp, (const char*)cstr, leng); sp[leng] = '\0'; } mstring::mstring(const char * cstr) { sp = NULL; leng = strlen(cstr); sp = new char[leng+1]; #ifdef _SAFE_ // Does this work here? if we can't get memory, what will really hapen to us ? if (!sp) throw new x_mem("mstring::mstring(char)",__FLF__); #endif strncpy(sp, cstr, leng); sp[leng] = '\0'; } mstring::mstring( const mstring & v) { sp = NULL; leng = v.leng; sp = new char[leng+1]; #ifdef _SAFE_ if (!sp) throw new x_mem("mstring:mstring(mstring)",__FLF__); #endif strncpy(sp, v.sp, leng); sp[leng] = '\0'; } mstring& mstring::operator += ( const mstring& v) { char * ptr; ptr = new char[leng + v.leng + 1]; #ifdef _SAFE_ if (!ptr) throw new x_mem("mstring::operator+=(mstring)",__FLF__); #endif if (sp) { strncpy ( ptr, sp, leng); delete [] sp; } strncpy ( ptr + leng, v.sp, v.leng); leng += v.leng; sp = ptr; sp[leng] = '\0'; return *this; } mstring& mstring::operator += ( const char * v) { char * ptr; unsigned long i = strlen(v); ptr = new char[leng + i + 1]; #ifdef _SAFE_ if (!ptr) throw new x_mem("mstring::operator+=(char)",__FLF__); #endif if (sp) { strncpy( ptr, sp, leng); delete [] sp; } strncpy( ptr + leng, v, i); leng += i; sp = ptr; sp[leng]='\0'; return *this; } mstring& mstring::operator = ( const char * v) { if (sp != v) { if(sp) { delete[] sp; } leng = strlen(v); //cout << "operator= : lenge : " << leng << " : " << v << endl; sp = new char[leng + 1]; #ifdef _SAFE_ if (!sp) throw new x_mem("mstring::operator=(char)",__FLF__); #endif strncpy(sp, v, leng); } sp[leng]='\0'; //cout << "AFTER : " << sp << endl; return *this; } mstring& mstring::operator = ( const mstring & v) { if ( sp != v.sp ) { if(sp) { delete[] sp; } sp = new char[v.leng+1]; #ifdef _SAFE_ if (!sp) throw new x_mem("mstring::operator=(mstring)",__FLF__); #endif strncpy(sp, v.sp, v.leng); leng = v.leng; } sp[leng]='\0'; return *this; } char * mstring::operator[] (unsigned long pos) { #ifndef _FAST_ if ( sp == NULL) return NULL; #endif #ifdef _SAFE_ // should be safer than somewhere in memory if >= length. if ( pos >= leng ) return NULL; #endif return & sp[pos]; } char mstring::operator() (unsigned long pos) const { #ifndef _FAST_ if ( sp == NULL) return '\0'; #endif #ifdef _SAFE_ if (pos >= leng) return '\0'; #endif return sp[pos]; } bool mstring::matches( const char * v , unsigned long pos) const { unsigned long len; len = strlen(v); #ifdef _SAFE_ if ( pos >= leng ) return false; #endif return ( strncmp (sp + pos, v,len ) == 0); } bool mstring::matches( const mstring& v , unsigned long pos) const { #ifdef _SAFE_ if ( pos >= leng ) return false; #endif return ( strncmp (sp + pos, v.sp, v.leng ) == 0); } unsigned long mstring::lsearch( const char* v, unsigned long spos) const { unsigned long t = (unsigned long) strstr(sp + spos, v); if ( t == 0) { return 0; } return t - (unsigned long) sp + 1; } unsigned long mstring::lsearch( const mstring& v, unsigned long spos) const { unsigned long t = (unsigned long) strstr(sp + spos, v.sp); if ( t == 0 ) { return 0; } return t - (unsigned long) sp + 1; } mstring mstring::strip_all_whitespaces( void ) { mstring stripped; char * strip; strip = new char[leng+1]; int ap = 0; for (unsigned int p=0;p < leng; p++) { if ( sp[p] == ' ' || sp[p] == '\t' || sp[p] == '\r' || sp[p] == '\n' ) { continue; } else { strip[ap++] = sp[p]; } } strip[ap] = '\0'; stripped = strip; delete[] strip; return stripped; } mstring mstring::strip_whitespaces( void ) { char * strip; bool begin = false; strip = new char[leng+1]; int ap = 0; // first strip all the leading whitespaces... for (unsigned int p=0;p <= leng; p++) { if (!begin && ( sp[p] == ' ' || sp[p] == '\t' || sp[p] == '\r' || sp[p] == '\n' )) { continue; } else { begin = true; strip[ap++] = sp[p]; } } begin = false; ap--; ap--; for (;ap >= 0;ap--) { if( (strip[ap] == ' ' || strip[ap] == '\t' || strip[ap] == '\r' || strip[ap] == '\n' )) { strip[ap] = '\0'; continue; } else { break; } } mstring stripped(strip); delete[] strip; return stripped; } mstring mstring::substring( unsigned long anf, unsigned long end) const { mstring sub; if ( end == 0 || end > leng) { end = leng; } if ( anf > leng || anf > end ) { return sub; } char * nw; nw = new char[end - anf + 2]; nw[ end - anf + 1] = '\0'; strncpy ( nw, sp + (anf - 1) , end - anf + 1 ); sub = nw; delete[] nw; return sub; } bool mstring::operator != ( const mstring& v) { return (strcmp(sp,v.sp) != 0); } bool mstring::operator != ( const char * v) { return (strcmp(sp,v) != 0); } bool mstring::operator == ( const mstring& v) { return (strcmp(sp, v.sp) == 0); } bool mstring::operator == ( const char * v) { return (strcmp(sp,v) == 0); } ostream& operator << ( ostream & o, const mstring& v) { o << v.sp; return o; } mstring & mstring::format( const char* fmtstr, ...) { // Our problem here is, that we do not know how big the resulting string will be. We could try // and "guess" how big it will be, but for now we will stop at ~20480 bytes, which will be enough // for normal use. // The trailing \0 should be set automatgically by vsprint, so we don't have to care bout it char tmp[20480 + 10]; // + 10 extra bytes, in case I made something wrong ;) va_list parms; va_start( parms, fmtstr ); vsnprintf( tmp,20480, fmtstr, parms ); va_end(parms); *this = tmp; return *this; } mstring operator * ( uint32 v, const mstring& g) { mstring ret; for (register uint32 i=0; i < v; i++) { ret = ret + g; } return ret; } bool mstring::operator < ( const mstring& v) const { return ( strcmp (sp, v.sp) < 0 ); } bool mstring::operator < ( const char * v) const { return ( strcmp (sp, v) < 0 ); } bool mstring::operator > ( const mstring& v) const { return ( strcmp (sp, v.sp) > 0 ); } bool mstring::operator > ( const char * v) const { return ( strcmp (sp, v) > 0 ); } /** *$Log: mstring.cpp,v $ *Revision 1.27 2002/10/31 22:34:46 plasmahh *mstring fix * *Revision 1.26 2002/10/22 14:01:07 plasmahh *implementing binary tree * *Revision 1.25 2002/10/16 23:07:21 plasmahh *fixed memory bugs in mstring *TODO : refine of exception structure * *Revision 1.24 2002/10/01 21:06:24 plasmahh *new math things * *Revision 1.23 2002/08/15 20:40:31 plasmahh *new string manipulation functions * *Revision 1.22 2002/08/12 13:15:25 plasmahh *removed some memory leaks * *Revision 1.21 2002/08/08 20:45:53 plasmahh *some slight changes * *Revision 1.20 2002/06/17 14:05:07 plasmahh *resolv for IPs added * *Revision 1.19 2002/06/11 14:17:05 plasmahh *Added operators. *Fixed sorting bug which destroyed origin list * *Revision 1.18 2002/06/10 22:10:54 plasmahh * added operators and fixed some sorting things * *Revision 1.17 2002/06/09 21:23:21 plasmahh *changed a few things that might be bugs * *Revision 1.16 2002/05/28 20:36:33 plasmahh *Added include for NetBSD compatibility * *Revision 1.15 2002/05/28 18:47:40 plasmahh *added more or less safe format function to the mstring class *introduced a parser class *some new cool exceptions * *Revision 1.14 2002/05/23 19:48:08 plasmahh *removed some compiler warnings. *fixed one or two bugs. *refined include structure, for faster compiling *hm, what else ? don't know, I hope this works all ;) * *Revision 1.13 2002/05/16 16:50:42 plasmahh *fixed bug in mstrig::match *tested some code for fingerprinting * *Revision 1.12 2002/05/15 19:41:16 plasmahh *fixed segfault bug in substring function * *Revision 1.11 2002/05/15 07:07:10 plasmahh *Added search functions to string class *played a bit ;) * *Revision 1.10 2002/05/14 20:52:22 plasmahh *Added fast lsearch function for string class. Additional functions will be implemented by request * *Revision 1.9 2002/05/14 16:31:13 plasmahh *Removed some compiler warnings *Added more code, hopefully fast ;) * *Revision 1.8 2002/05/13 21:32:57 plasmahh *implemented file class & readline *implemented additional in mstring * *Revision 1.7 2002/05/05 20:24:36 plasmahh *added doygen configuration file *made dllist work *added dlliterator to dllist.h *made some test progams cooler ;) *added some exceptions... *anything else to do ?? *G* * *Revision 1.6 2002/04/30 21:35:17 plasmahh *first working exceptions *first working strings, not everything implemented, should also be tested ! * *Revision 1.5 2002/04/30 19:34:06 plasmahh *fixed segfault with new gcc/g++ compilers * *Revision 1.4 2002/04/30 14:13:12 plasmahh *Added some #ifdefs for _SAFE_ ty checks in string memory allocations * *Revision 1.3 2002/04/29 22:59:08 plasmahh *implemented some parts of string class * *Revision 1.2 2002/04/29 18:57:09 plasmahh *humm... * *Revision 1.1 2002/04/29 18:51:29 plasmahh *Renamed string to mstring in order to avoid conflicts with standard libraries * *Revision 1.2 2002/04/18 23:01:15 plasmahh *- added [] operator and first sample use of _FAST_ and _SAFE_ defines * *Revision 1.1 2002/04/16 20:46:02 plasmahh *Added mstring class and test program * */