#include <iostream>
#include <string.h>
using namespace std;
#define KSTRING_BUFF_MAX 100
#define KSTRING_DEFAULT_CH '\0'
class kstring
{
private:
char data[KSTRING_BUFF_MAX];
int data_p;
public:
kstring(void)
{
memset(this->data, KSTRING_DEFAULT_CH, sizeof(this->data));
this->data_p = 0;
}
kstring(kstring &str)
{
*this = str;
}
kstring(const char *str)
{
*this = str;
}
kstring(int size = 0, char ch = KSTRING_DEFAULT_CH)
{
memset(this->data, KSTRING_DEFAULT_CH, sizeof(this->data));
memset(this->data, ch, size);
this->data_p = size;
}
~kstring(void)
{
cout << "~kstring" << endl;
memset(this->data, KSTRING_DEFAULT_CH, sizeof(this->data));
this->data_p = 0;
}
bool empty(void)
{
if(this->data_p > 0)
return true;
else
return false;
}
int size(void)
{
return this->data_p;
}
void testout(void)
{
cout << this->data_p << ":" << this->data << endl;
}
char& operator [](int i)
{
return this->data[i];
}
kstring& operator =(kstring &str)
{
memset(this->data, KSTRING_DEFAULT_CH, sizeof(this->data));
this->data_p = str.size();
strcpy(this->data, &str[0]);
return *this;
}
kstring& operator =(const char *str)
{
memset(this->data, KSTRING_DEFAULT_CH, sizeof(this->data));
this->data_p = strlen(str);
strcpy(this->data, str);
return *this;
}
kstring operator +(kstring &str)
{
kstring temp(this->size() + str.size());
strcpy(&temp[0], this->data);
strcpy(&temp[this->data_p], &str[0]);
return temp;
}
kstring operator +(const char *str)
{
kstring temp(this->size() + strlen(str));
strcpy(&temp[0], this->data);
strcpy(&temp[this->data_p], str);
return temp;
}
kstring& operator +=(kstring &str)
{
strcpy(&this->data[this->data_p], &str[0]);
this->data_p += str.size();
return *this;
}
kstring& operator +=(const char *str)
{
strcpy(&this->data[this->data_p], str);
this->data_p += strlen(str);
return *this;
}
bool operator ==(kstring &str)
{
if(this->size() != str.size())
return false;
if(strcmp(this->data, &str[0]) == 0)
return true;
return false;
}
friend ostream& operator <<(ostream &output, const kstring &str)
{
output << str.data;
return output;
}
friend istream& operator >>(istream &input, kstring &str)
{
input >> str.data;
str.data_p = strlen(str.data);
return input;
}
};
|