www.pudn.com > Ftpwork > sfbase.cpp
/***************************************************************************
sfbase.cpp - description
-------------------
begin : Sat Sep 28 2002
copyright : (C) 2002 by
email :
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include
#include
#include
#include <./sfbase.h>
//#include
String::String(const char * string)
{
_string = new char[strlen(string)+1];
strcpy(_string, string);
}
int String::toInt()
{
return atoi(_string);
}
long String::toLong()
{
return atol(_string);
}
double String::toDouble()
{
return atof(_string);
}
const char String::operator[](const int index)
{
return _string[index];
}
const char* String::buffer()
{
return _string;
}
String& String::operator=(const String & str)
{
if(this == &str)
return *this;
int length = strlen(str._string);
if(_string != NULL)
delete _string;
_string =new char[ length+1];
strcpy(_string, str._string);
return *this;
}
String& String::operator=(const char* str)
{
int length =strlen(str);
if(_string == NULL)
_string =new char[ length+1];
strncpy(_string, str, length);
return *this;
}
String& String::operator+(const String & str)
{
int length =strlen(_string);
char *temp = new char[length+1];
temp = strcpy(temp, _string);
delete _string;
length +=strlen(str._string);
_string = new char[ length +1];
_string = strcpy(_string, temp);
_string = strcat(_string, str._string);
return *this;
}
String& String::operator+(const char* str)
{
int length =strlen(_string);
char* temp = new char[length+1];
temp = strcpy(temp, _string);
delete _string;
length +=strlen(str);
_string = new char[ length +1];
_string = strcpy(_string, temp);
_string = strcat(_string, str);
return *this;
}
String& String::operator+=(const String & str)
{
return *this+str;
}
String& String::operator+=(const char* str)
{
return *this+str;
}
BOOL String::operator==(const String& str)
{
if( this == &str)
return TRUE;
else
if( strcmp(this->_string, str._string) ==0)
return TRUE;
return FALSE;
}
const int String::indexof(const char ch)
{
for(unsigned int k=0;k_string, str);
if(pchar == NULL)
return -1;
else
return (pchar - _string);
}
const int String::indexof(const String& str)
{
return indexof(str._string);
}
const char String::getAt(const int index)
{
return _string[index];
}
const char String::putAt(const int index, const char ch)
{
return _string[index]=ch;
}
String::~String()
{
if(_string !=NULL)
delete _string;
}