| 1 |
/*************************************************************************** |
| 2 |
str.cpp - description |
| 3 |
------------------- |
| 4 |
begin : Tue Aug 1 2000 |
| 5 |
copyright : (C) 2000 by W. Baraldi & D. Scarpa |
| 6 |
email : [email protected] |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
#include "str.h" |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
const char Str::nul = '\0'; |
| 13 |
|
| 14 |
Str::Str(const char * string=NULL){ |
| 15 |
s=NULL; |
| 16 |
set(string); |
| 17 |
} |
| 18 |
|
| 19 |
Str::Str(Str * str){ |
| 20 |
s=NULL; |
| 21 |
if (str) set(str->s); |
| 22 |
} |
| 23 |
|
| 24 |
Str::~Str(){ |
| 25 |
if (s) delete[] s; |
| 26 |
} |
| 27 |
|
| 28 |
void Str::set(const char * string=NULL){ |
| 29 |
if (s) delete[] s; |
| 30 |
if (string){ |
| 31 |
s=new char[strlen(string)+1]; |
| 32 |
strcpy(s, string); |
| 33 |
}else s=NULL; |
| 34 |
} |
| 35 |
|
| 36 |
void Str::set(const Str * str){ |
| 37 |
set(((Str*)(str))->get()); |
| 38 |
} |
| 39 |
|
| 40 |
const char * Str::get(){ |
| 41 |
if (s) |
| 42 |
return (s); |
| 43 |
else |
| 44 |
return (&nul); |
| 45 |
} |
| 46 |
|
| 47 |
bool Str::contains(const char * string){ |
| 48 |
return (strstr(s, string)); |
| 49 |
} |
| 50 |
|
| 51 |
bool Str::contains(Str * str){ |
| 52 |
return (strstr(s, str->s)); |
| 53 |
} |
| 54 |
|
| 55 |
void Str::concat(const char * string){ |
| 56 |
char * aux=NULL; |
| 57 |
if (string){ |
| 58 |
aux=new char[strlen(s)+strlen(string)+1]; |
| 59 |
strcpy(aux, s), |
| 60 |
strcat(aux, string); |
| 61 |
free(s); |
| 62 |
} |
| 63 |
s=aux; |
| 64 |
} |
| 65 |
|
| 66 |
void Str::concat(Str * str){ |
| 67 |
concat(str->s); |
| 68 |
} |
| 69 |
|
| 70 |
int Str::lenght(){ |
| 71 |
return (strlen(s)); |
| 72 |
} |
| 73 |
|
| 74 |
bool Str::isEqual(Str * str, bool case_sensitive = true){ |
| 75 |
if (case_sensitive){ |
| 76 |
return (!strcmp(s, str->s)); |
| 77 |
} |
| 78 |
return (!strcasecmp(s, str->s)); |
| 79 |
} |
| 80 |
|
| 81 |
void Str::crop(unsigned int start, unsigned int end){ |
| 82 |
if (end>strlen(s)-1) end=strlen(s)-1; |
| 83 |
if (start<end){ |
| 84 |
char * aux=new char[strlen(s)+1]; |
| 85 |
char * aux2=s+start; |
| 86 |
strncpy(aux, aux2, end-start); |
| 87 |
aux[end-start]='\0'; |
| 88 |
delete[] s; |
| 89 |
s=aux; |
| 90 |
}else |
| 91 |
set(); |
| 92 |
} |
| 93 |
|
| 94 |
int Str::toInt(){ |
| 95 |
return atoi(s); |
| 96 |
} |
| 97 |
|
| 98 |
bool Str::toBool(){ |
| 99 |
if (!strcasecmp("f", s)) return (false); |
| 100 |
if (!strcasecmp("false", s)) return (false); |
| 101 |
if (!strcasecmp("t", s)) return (true); |
| 102 |
if (!strcasecmp("true", s)) return (true); |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
float Str::toFloat(){ |
| 107 |
return (atof(s)); |
| 108 |
} |
| 109 |
|