#if !defined(__Exception_h)
#define __Exception_h
//[]------------------------------------------------------------------------[]
//| |
//| Object Structural WorkBench Class Library |
//| Version 2.0 |
//| |
//| Copyright® 1999-2004, Paulo Aristarco Pagliosa |
//| All Rights Reserved. |
//| |
//[]------------------------------------------------------------------------[]
//
// OVERVIEW: Exception.h
// ========
// Class definition for generic exception.
#if !defined(__lstring_h)
#include "lstring.h"
#endif
//////////////////////////////////////////////////////////
//
// Exception: generic exception class
// =========
class Exception
{
public:
// Constructors
Exception();
Exception(const Exception&);
Exception(const String&);
String GetMessage() const;
operator const CHAR_T*() const;
protected:
String Message;
private:
Exception& operator =(const Exception&);
}; // Exception
//////////////////////////////////////////////////////////
//
// OutOfRangeException: out of range exception class
// ===================
class OutOfRangeException: public Exception
{
public:
OutOfRangeException();
OutOfRangeException(const String&);
}; // OutOfRangeException
//////////////////////////////////////////////////////////
//
// IndexOutOfBoundsException: index out of bounds exception class
// =========================
class IndexOutOfBoundsException: public Exception
{
public:
IndexOutOfBoundsException();
IndexOutOfBoundsException(const String&);
}; // IndexOutOfBoundsException
//////////////////////////////////////////////////////////
//
// ClassNotFoundException: class not found exception
// ======================
class ClassNotFoundException: public Exception
{
public:
// Constructor
ClassNotFoundException(const String&);
}; // ClassNotFoundException
//////////////////////////////////////////////////////////
//
// IOException: IO exception
// ===========
class IOException: public Exception
{
public:
// Constructors
IOException();
IOException(const String&);
}; // IOException
//////////////////////////////////////////////////////////
//
// Exception inline implementation
// =========
inline
Exception::Exception()
{}
inline
Exception::Exception(const Exception& e):
Message(e.Message)
{}
inline
Exception::Exception(const String& message):
Message(message)
{}
inline String
Exception::GetMessage() const
{
return Message;
}
inline
Exception::operator const CHAR_T*() const
{
return (const CHAR_T*)Message;
}
//////////////////////////////////////////////////////////
//
// OutOfRangeException inline implementation
// ===================
inline
OutOfRangeException::OutOfRangeException():
Exception(STRING_C("Out of range exception"))
{}
inline
OutOfRangeException::OutOfRangeException(const String& message):
Exception(message)
{}
//////////////////////////////////////////////////////////
//
// IndexOutOfBoundsException inline implementation
// =========================
inline
IndexOutOfBoundsException::IndexOutOfBoundsException():
Exception(STRING_C("Index out of bounds exception"))
{}
inline
IndexOutOfBoundsException::IndexOutOfBoundsException(const String& message):
Exception(message)
{}
//////////////////////////////////////////////////////////
//
// ClassNotFoundException inline implementation
// ======================
inline
ClassNotFoundException::ClassNotFoundException(const String& className)
{
Message = String(STRING_C("Class not found exception: ")) + className;
}
//////////////////////////////////////////////////////////
//
// IOException inline implementation
// ===========
inline
IOException::IOException()
{
Message = STRING_C("IO exception");
}
inline
IOException::IOException(const String& message):
Exception(message)
{}
#endif
//[]------------------------------------------------------------------------[]
//| |
//| Object Structural WorkBench Class Library |
//| Version 2.0 |
//| |
//| Copyright® 1999-2004, Paulo Aristarco Pagliosa |
//| All Rights Reserved. |
//| |
//[]------------------------------------------------------------------------[]
//
// OVERVIEW: lstring.cpp
// ========
// Class definition for string.
#if !defined(__Exception_h)
#include "Exception.h"
#endif
#define MIN_STRBUF_SIZE 4
//
// Roundup string body size
//
inline int
RoundupSize(size_t size)
{
return (size + MIN_STRBUF_SIZE - 1) & -MIN_STRBUF_SIZE;
}
//////////////////////////////////////////////////////////
//
// StringBody implementation
// ==========
StringBody* StringBody::EmptyString = StringBody::New(STRING_C(""));
void*
StringBody::operator new(size_t size, size_t length)
{
return ::operator new(RoundupSize(size + (length + 1) * sizeof(CHAR_T)));
}
StringBody*
StringBody::New(const CHAR_T* s)
{
return s == 0 ? New() : New(s, STRLEN(s));
}
StringBody*
StringBody::New(const CHAR_T* buffer, int length)
{
StringBody* body = New(length);
STRNCPY(body->StringBuffer(), buffer, length);
body->StringBuffer()[length] = 0;
return body;
}
StringBody*
StringBody::Append(StringBody* s)
{
if (s->IsEmpty())
return this->MakeUse();
if (this->IsEmpty())
return s->MakeUse();
StringBody* body = New(this->StringBuffer(), this->Length + s->Length);
STRCPY(body->StringBuffer() + this->Length, s->StringBuffer());
return body;
}
int
StringBody::Compare(StringBody* s)
{
return this == s ? 0 : STRCMP(StringBuffer(), s->StringBuffer());
}
const CHAR_T&
StringBody::operator [](int i)
{
if (i <>= Length)
throw OutOfRangeException();
return StringBuffer()[i];
}
int
StringBody::IndexOf(CHAR_T c) const
{
const CHAR_T* stringBuffer = (const CHAR_T*)(*this);
const CHAR_T* indexPointer = STRCHR(stringBuffer, c);
return indexPointer ? indexPointer - stringBuffer : - 1;
}
int
StringBody::LastIndexOf(CHAR_T c) const
{
const CHAR_T* stringBuffer = (const CHAR_T*)(*this);
const CHAR_T* indexPointer = STRRCHR(stringBuffer, c);
return indexPointer ? indexPointer - stringBuffer : - 1;
}
StringBody*
StringBody::Reverse()
{
if (this->IsEmpty())
return New();
CHAR_T* thisBuffer = StringBuffer();
StringBody* body = New(Length);
CHAR_T* bodyBuffer = body->StringBuffer();
for (int n = Length; n;)
*bodyBuffer++ = thisBuffer[--n];
*bodyBuffer = 0;
return body;
}
StringBody*
StringBody::ToLower()
{
if (this->IsEmpty())
return New();
CHAR_T* thisBuffer = StringBuffer();
StringBody* body = New(Length);
CHAR_T* bodyBuffer = body->StringBuffer();
for (int i = 0; i < Length; ++i)
bodyBuffer[i] = (CHAR_T)TOLOWER(thisBuffer[i]);
return body;
}
StringBody*
StringBody::ToUpper()
{
if (this->IsEmpty())
return New();
CHAR_T* thisBuffer = StringBuffer();
StringBody* body = New(Length);
CHAR_T* bodyBuffer = body->StringBuffer();
for (int i = 0; i <>Length; ++i)
bodyBuffer[i] = (CHAR_T)TOUPPER(thisBuffer[i]);
return body;
}
//////////////////////////////////////////////////////////
//
// String implementation
// ======
String&
String::Copy(const String& s, int copy)
{
Body->Delete();
Body = copy == DeepCopy ? StringBody::New(s.Body) : s.Body->MakeUse();
return *this;
}
CHAR_T
String::operator [](int i) const
{
return (*Body)[i];
}
CHAR_T&
String::operator [](int i)
{
return const_cast
}
String
String::ToString(CHAR_T c)
{
CHAR_T s[2];
s[0] = c;
s[1] = 0;
return String(s);
}
String
String::ToString(int i)
{
CHAR_T s[16];
#if defined __LINUX
SPRINTF(s, sizeof(s) / sizeof(CHAR_T) - 1, STRING_C("%d"), i);
#else
SPRINTF(s, STRING_C("%d"), i);
#endif
return String(s);
}
String
String::ToString(double d)
{
CHAR_T s[64];
#if defined __LINUX
SPRINTF(s, sizeof(s) / sizeof(CHAR_T) - 1, STRING_C("%lf"), d);
#else
SPRINTF(s, STRING_C("%lf"), d);
#endif
return String(s);
}
String
String::ToString(bool b)
{
return String(b ? STRING_C("true") : STRING_C("false"));
}
#if !defined(__lstring_h)
#define __lstring_h
//[]------------------------------------------------------------------------[]
//| |
//| Object Structural WorkBench Class Library |
//| Version 2.0 |
//| |
//| Copyright® 1999-2004, Paulo Aristarco Pagliosa |
//| All Rights Reserved. |
//| |
//[]------------------------------------------------------------------------[]
//
// OVERVIEW: lstring.h
// ========
// Class definition string.
#include
#include
#include
#include
#if defined __UNICODE
#define CHAR_T wchar_t
#define STRING_C(s) L##s
#if defined __LINUX
#include
#include
#endif
#define STRLEN wcslen
#define STRCPY wcscpy
#define STRNCPY wcsncpy
#define STRCMP wcscmp
#define STRNCMP wcsncmp
#define STRCHR wcschr
#define STRRCHR wcsrchr
#define TOUPPER towupper
#define TOLOWER towlower
#define PRINTF wprintf
#define SPRINTF swprintf
#define VSPRINTF vswprintf
#else
#define CHAR_T char
#define STRING_C(s) s
#if defined __LINUX
#define SPRINTF snprintf
#define VSPRINTF vsnprintf
#else
#define SPRINTF sprintf
#define VSPRINTF vsprintf
#endif
#define STRLEN strlen
#define STRCPY strcpy
#define STRNCPY strncpy
#define STRCMP strcmp
#define STRNCMP strncmp
#define STRCHR strchr
#define STRRCHR strrchr
#define PRINTF printf
#define TOUPPER toupper
#define TOLOWER tolower
#endif
//
// Constants
//
enum
{
ShallowCopy,
DeepCopy
};
//
// Forward definition
//
class StringBody;
//////////////////////////////////////////////////////////
//
// String: string class
// ======
class String
{
public:
// Constructors
String();
String(const String&);
String(const String&, int);
String(const CHAR_T*);
String(const CHAR_T*, int);
// Destructor
~String();
// Assignment
String& Copy(const String&, int = ShallowCopy);
String& operator =(const String&);
String& operator =(CHAR_T);
String& operator =(int);
String& operator =(double);
String& operator =(bool);
// Concatenation
String operator +(const String&) const;
String operator +(CHAR_T) const;
String operator +(int) const;
String operator +(double) const;
String operator +(bool) const;
String& Append(const String&);
String& operator +=(const String&);
String& operator +=(CHAR_T);
String& operator +=(int);
String& operator +=(double);
String& operator +=(bool);
// Comparison
int Compare(const String&) const;
bool operator ==(const String&) const;
bool operator !=(const String&) const;
// Subscripting
CHAR_T operator [](int) const;
CHAR_T& operator [](int);
// Character set searching
int IndexOf(CHAR_T) const;
int LastIndexOf(CHAR_T) const;
// Miscellaneous
operator const CHAR_T*() const;
int Length() const;
String& Reverse();
String& ToLower();
String& ToUpper();
// Static casting
static String ToString(CHAR_T);
static String ToString(int);
static String ToString(double);
static String ToString(bool);
private:
StringBody* Body; // this is the body
// Private constructor
String(StringBody*);
}; // String
//////////////////////////////////////////////////////////
//
// StringBody: string body class
// ==========
class StringBody
{
public:
// Free string body memory
void operator delete(void*, size_t);
private:
int RefCount;
int Length; // string buffer length
static StringBody* EmptyString;
// Constructor
StringBody(int = 0);
// Allocate memory to store a string body
void* operator new(size_t, size_t);
// Is this string body the empty string?
bool IsEmpty() const;
// Static "constructors"
static StringBody* New();
static StringBody* New(StringBody*);
static StringBody* New(int);
static StringBody* New(const CHAR_T*, int);
static StringBody* New(const CHAR_T*);
// Make a shallow-copy of this string body
StringBody* MakeUse();
// Delete this string body
void Delete();
// Get the string buffer of this string body
operator const CHAR_T*() const;
CHAR_T* StringBuffer();
// Get the character of this string body at the specified index
const CHAR_T& operator [](int);
// Get the index within this string body of the specified character
int IndexOf(CHAR_T) const;
int LastIndexOf(CHAR_T) const;
// Append another string body to this string body
StringBody* Append(StringBody*);
// Compare this string body to another
int Compare(StringBody*);
// Reverse the string buffer of this this string buffer
StringBody* Reverse();
// Convert this string body to lowercase
StringBody* ToLower();
// Convert this string body to uppercase
StringBody* ToUpper();
friend class String;
}; // StringBody
//////////////////////////////////////////////////////////
//
// StringBody inline implementation
// ==========
inline void
StringBody::operator delete(void* ptr, size_t)
{
::operator delete(ptr);
}
inline
StringBody::StringBody(int length):
RefCount(1),
Length(length)
{}
inline bool
StringBody::IsEmpty() const
{
return this == EmptyString;
}
inline StringBody*
StringBody::New()
{
return EmptyString->MakeUse();
}
inline StringBody*
StringBody::New(StringBody* s)
{
return s->IsEmpty() ? New() : New(s->StringBuffer(), s->Length);
}
inline StringBody*
StringBody::New(int length)
{
return new(length) StringBody(length);
}
inline StringBody*
StringBody::MakeUse()
{
++RefCount;
return this;
}
inline void
StringBody::Delete()
{
if (!--RefCount)
delete this;
}
inline
StringBody::operator const CHAR_T*() const
{
return reinterpret_cast
}
inline CHAR_T*
StringBody::StringBuffer()
{
return const_cast
}
//////////////////////////////////////////////////////////
//
// String inline implementation
// ======
inline
String::String()
{
Body = StringBody::New();
}
inline
String::String(const String& s)
{
Body = s.Body->MakeUse();
}
inline
String::String(const String& s, int copy)
{
Body = copy == DeepCopy ? StringBody::New(s.Body) : s.Body->MakeUse();
}
inline
String::String(const CHAR_T* s)
{
Body = StringBody::New(s);
}
inline
String::String(const CHAR_T* s, int len)
{
Body = StringBody::New(s, len);
}
inline
String::String(StringBody* body)
{
Body = body;
}
inline
String::~String()
{
Body->Delete();
}
inline String&
String::operator =(const String& s)
{
return Copy(s);
}
inline String&
String::operator =(CHAR_T c)
{
return operator =(ToString(c));
}
inline String&
String::operator =(int i)
{
return operator =(ToString(i));
}
inline String&
String::operator =(double d)
{
return operator =(ToString(d));
}
inline String&
String::operator =(bool b)
{
return operator =(ToString(b));
}
inline String
String::operator +(const String& s) const
{
return String(Body->Append(s.Body));
}
inline String&
String::Append(const String& s)
{
return operator =(operator +(s));
}
inline String&
String::operator +=(const String& s)
{
return Append(s);
}
inline String&
String::operator +=(CHAR_T c)
{
return Append(ToString(c));
}
inline String&
String::operator +=(int i)
{
return Append(ToString(i));
}
inline String&
String::operator +=(double d)
{
return Append(ToString(d));
}
inline String&
String::operator +=(bool b)
{
return Append(ToString(b));
}
inline int
String::Compare(const String& s) const
{
return Body->Compare(s.Body);
}
inline bool
String::operator ==(const String& s) const
{
return Compare(s) == 0;
}
inline bool
String::operator !=(const String& s) const
{
return !operator ==(s);
}
inline int
String::IndexOf(CHAR_T c) const
{
return Body->IndexOf(c);
}
inline int
String::LastIndexOf(CHAR_T c) const
{
return Body->LastIndexOf(c);
}
inline int
String::Length() const
{
return Body->Length;
}
inline
String::operator const CHAR_T*() const
{
return (const CHAR_T*)*Body;
}
inline String&
String::Reverse()
{
return operator =(String(Body->Reverse()));
}
inline String&
String::ToLower()
{
return operator =(String(Body->ToLower()));
}
inline String&
String::ToUpper()
{
return operator =(String(Body->ToUpper()));
}
#endif


6:43 AM
Unknown
0 comentários:
Postar um comentário