183812f67SIngo Weinhold // String.cpp
283812f67SIngo Weinhold
3*eff61480SMichael Lotz #include <new>
483812f67SIngo Weinhold #include <string.h>
583812f67SIngo Weinhold
683812f67SIngo Weinhold #include "String.h"
783812f67SIngo Weinhold
883812f67SIngo Weinhold /*!
983812f67SIngo Weinhold \class String
1083812f67SIngo Weinhold \brief A very simple string class.
1183812f67SIngo Weinhold */
1283812f67SIngo Weinhold
1383812f67SIngo Weinhold // constructor
String()1483812f67SIngo Weinhold String::String()
1583812f67SIngo Weinhold : fLength(0),
1683812f67SIngo Weinhold fString(NULL)
1783812f67SIngo Weinhold {
1883812f67SIngo Weinhold }
1983812f67SIngo Weinhold
2083812f67SIngo Weinhold // copy constructor
String(const String & string)2183812f67SIngo Weinhold String::String(const String &string)
2283812f67SIngo Weinhold : fLength(0),
2383812f67SIngo Weinhold fString(NULL)
2483812f67SIngo Weinhold {
2583812f67SIngo Weinhold *this = string;
2683812f67SIngo Weinhold }
2783812f67SIngo Weinhold
2883812f67SIngo Weinhold // constructor
String(const char * string,int32 length)2983812f67SIngo Weinhold String::String(const char *string, int32 length)
3083812f67SIngo Weinhold : fLength(0),
3183812f67SIngo Weinhold fString(NULL)
3283812f67SIngo Weinhold {
3383812f67SIngo Weinhold SetTo(string, length);
3483812f67SIngo Weinhold }
3583812f67SIngo Weinhold
3683812f67SIngo Weinhold // destructor
~String()3783812f67SIngo Weinhold String::~String()
3883812f67SIngo Weinhold {
3983812f67SIngo Weinhold Unset();
4083812f67SIngo Weinhold }
4183812f67SIngo Weinhold
4283812f67SIngo Weinhold // SetTo
4383812f67SIngo Weinhold bool
SetTo(const char * string,int32 maxLength)4483812f67SIngo Weinhold String::SetTo(const char *string, int32 maxLength)
4583812f67SIngo Weinhold {
4683812f67SIngo Weinhold if (string) {
4783812f67SIngo Weinhold if (maxLength > 0)
4883812f67SIngo Weinhold maxLength = strnlen(string, maxLength);
4983812f67SIngo Weinhold else if (maxLength < 0)
5083812f67SIngo Weinhold maxLength = strlen(string);
5183812f67SIngo Weinhold }
5283812f67SIngo Weinhold return _SetTo(string, maxLength);
5383812f67SIngo Weinhold }
5483812f67SIngo Weinhold
5583812f67SIngo Weinhold // Unset
5683812f67SIngo Weinhold void
Unset()5783812f67SIngo Weinhold String::Unset()
5883812f67SIngo Weinhold {
5983812f67SIngo Weinhold if (fString) {
6083812f67SIngo Weinhold delete[] fString;
6183812f67SIngo Weinhold fString = NULL;
6283812f67SIngo Weinhold }
6383812f67SIngo Weinhold fLength = 0;
6483812f67SIngo Weinhold }
6583812f67SIngo Weinhold
6683812f67SIngo Weinhold // Truncate
6783812f67SIngo Weinhold void
Truncate(int32 newLength)6883812f67SIngo Weinhold String::Truncate(int32 newLength)
6983812f67SIngo Weinhold {
7083812f67SIngo Weinhold if (newLength < 0)
7183812f67SIngo Weinhold newLength = 0;
7283812f67SIngo Weinhold if (newLength < fLength) {
7383812f67SIngo Weinhold char *string = fString;
7483812f67SIngo Weinhold fString = NULL;
7583812f67SIngo Weinhold if (!_SetTo(string, newLength)) {
7683812f67SIngo Weinhold fString = string;
7783812f67SIngo Weinhold fLength = newLength;
7883812f67SIngo Weinhold fString[fLength] = '\0';
7983812f67SIngo Weinhold } else
8083812f67SIngo Weinhold delete[] string;
8183812f67SIngo Weinhold }
8283812f67SIngo Weinhold }
8383812f67SIngo Weinhold
8483812f67SIngo Weinhold // GetString
8583812f67SIngo Weinhold const char *
GetString() const8683812f67SIngo Weinhold String::GetString() const
8783812f67SIngo Weinhold {
8883812f67SIngo Weinhold if (fString)
8983812f67SIngo Weinhold return fString;
9083812f67SIngo Weinhold return "";
9183812f67SIngo Weinhold }
9283812f67SIngo Weinhold
9383812f67SIngo Weinhold // =
9483812f67SIngo Weinhold String &
operator =(const String & string)9583812f67SIngo Weinhold String::operator=(const String &string)
9683812f67SIngo Weinhold {
9783812f67SIngo Weinhold if (&string != this)
9883812f67SIngo Weinhold _SetTo(string.fString, string.fLength);
9983812f67SIngo Weinhold return *this;
10083812f67SIngo Weinhold }
10183812f67SIngo Weinhold
10283812f67SIngo Weinhold // ==
10383812f67SIngo Weinhold bool
operator ==(const String & string) const10483812f67SIngo Weinhold String::operator==(const String &string) const
10583812f67SIngo Weinhold {
10683812f67SIngo Weinhold return (fLength == string.fLength
10783812f67SIngo Weinhold && (fLength == 0 || !strcmp(fString, string.fString)));
10883812f67SIngo Weinhold }
10983812f67SIngo Weinhold
11083812f67SIngo Weinhold // _SetTo
11183812f67SIngo Weinhold bool
_SetTo(const char * string,int32 length)11283812f67SIngo Weinhold String::_SetTo(const char *string, int32 length)
11383812f67SIngo Weinhold {
11483812f67SIngo Weinhold bool result = true;
11583812f67SIngo Weinhold Unset();
11683812f67SIngo Weinhold if (string && length > 0) {
117*eff61480SMichael Lotz fString = new(std::nothrow) char[length + 1];
11883812f67SIngo Weinhold if (fString) {
11983812f67SIngo Weinhold memcpy(fString, string, length);
12083812f67SIngo Weinhold fString[length] = '\0';
12183812f67SIngo Weinhold fLength = length;
12283812f67SIngo Weinhold } else
12383812f67SIngo Weinhold result = false;
12483812f67SIngo Weinhold }
12583812f67SIngo Weinhold return result;
12683812f67SIngo Weinhold }
12783812f67SIngo Weinhold
128