2003-09-21 23:24:53 +02:00
|
|
|
/*
|
|
|
|
CStr.h
|
|
|
|
by Caecus
|
|
|
|
Caecus@0ad.wildfiregames.com
|
|
|
|
|
|
|
|
Overview:
|
|
|
|
|
|
|
|
Contains CStr class which is a versatile class for making string use easy.
|
|
|
|
Basic functionality implemented via STL string, so performance is limited
|
|
|
|
based on the STL implementation we use.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
CStr MyString = _T("Hello, World.");
|
2004-06-01 18:51:37 +02:00
|
|
|
int MyNum = 126;
|
2003-09-21 23:24:53 +02:00
|
|
|
MyString += _T(" I'm the number ") += CStr(MyNumber);
|
|
|
|
|
|
|
|
// Prints "Hello, World. I'm the number 126"
|
2003-11-10 03:02:28 +01:00
|
|
|
|
|
|
|
_tcout << (LPCTSTR)MyString << endl;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
MyString = _("2341");
|
|
|
|
MyNum = MyString.ToInt();
|
|
|
|
|
|
|
|
// Prints "2341"
|
|
|
|
_tcout << MyNum << endl;
|
|
|
|
|
|
|
|
More Info:
|
|
|
|
http://wildfiregames.com/0ad/codepit/TDD/CStr.html
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2004-05-22 01:46:16 +02:00
|
|
|
// Last modified: 19 May 04, Mark Thompson (mot20@cam.ac.uk / mark@wildfiregames.com)
|
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
#ifndef CSTR_H
|
|
|
|
#define CSTR_H
|
|
|
|
|
|
|
|
#include "Prometheus.h"
|
|
|
|
#include <string> // Used for basic string functionality
|
|
|
|
#include <iostream>
|
2003-11-10 03:02:28 +01:00
|
|
|
|
2003-11-13 20:50:26 +01:00
|
|
|
#include "posix.h"
|
2004-03-04 21:27:37 +01:00
|
|
|
#include "lib.h"
|
2003-11-30 17:39:22 +01:00
|
|
|
#include "Network/Serialization.h"
|
2003-11-10 03:02:28 +01:00
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// DEFINES/ENUMS
|
|
|
|
#define CONVERSION_BUFFER_SIZE 32
|
|
|
|
#define FLOAT_CONVERSION _T("%.6f")
|
|
|
|
|
|
|
|
#ifdef _UNICODE
|
|
|
|
typedef wstring tstring;
|
|
|
|
#define _tcout wcout
|
|
|
|
#define _tstod wcstod
|
2003-11-07 01:59:13 +01:00
|
|
|
typedef wchar_t TCHAR;
|
2003-11-07 02:31:32 +01:00
|
|
|
#define _ttoi _wtoi
|
|
|
|
#define _ttol _wtol
|
|
|
|
#define _itot _itow
|
|
|
|
#define _ultot _itow
|
2003-11-07 01:59:13 +01:00
|
|
|
#define _T(t) L ## t
|
2003-11-07 02:31:32 +01:00
|
|
|
|
|
|
|
#define _totlower towlower
|
|
|
|
#define _istspace iswspace
|
|
|
|
#define _tsprintf wsprintf
|
|
|
|
#define _ltot _ltow
|
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
#else
|
2003-11-07 02:31:32 +01:00
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
typedef string tstring;
|
|
|
|
#define _tcout cout
|
|
|
|
#define _tstod strtod
|
2003-11-07 02:31:32 +01:00
|
|
|
#define _ttoi atoi
|
|
|
|
#define _ttol atol
|
|
|
|
#define _itot _itoa
|
2003-11-07 01:59:13 +01:00
|
|
|
typedef char TCHAR;
|
|
|
|
#define _T(t) t
|
2003-11-07 02:31:32 +01:00
|
|
|
#define _istspace isspace
|
|
|
|
#define _tsprintf sprintf
|
|
|
|
#define _totlower tolower
|
|
|
|
#define _ultot _ultoa
|
|
|
|
#define _ltot _ltoa
|
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
enum PS_TRIM_MODE {PS_TRIM_LEFT, PS_TRIM_RIGHT, PS_TRIM_BOTH};
|
|
|
|
|
2004-05-15 19:48:39 +02:00
|
|
|
// yuck - MAX already defines a CStr class...
|
|
|
|
#define CStr PS_CStr
|
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// CStr class, the mother of all strings
|
2003-11-30 17:39:22 +01:00
|
|
|
class CStr: public ISerializable
|
2003-09-21 23:24:53 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
// CONSTRUCTORS
|
|
|
|
CStr(); // Default constructor
|
|
|
|
CStr(const CStr &Str); // Copy Constructor
|
|
|
|
|
|
|
|
CStr(tstring String); // Creates CStr from C++ string
|
2003-11-07 01:59:13 +01:00
|
|
|
CStr(const TCHAR* String); // Creates CStr from C-Style TCHAR string
|
2003-09-21 23:24:53 +02:00
|
|
|
CStr(TCHAR Char); // Creates CStr from a TCHAR
|
2004-06-01 18:51:37 +02:00
|
|
|
CStr(int Number); // Creates CStr from a int
|
|
|
|
CStr(unsigned int Number); // Creates CStr from a uint
|
|
|
|
CStr(long Number); // Creates CStr from a long
|
|
|
|
CStr(unsigned long Number); // Creates CStr from a ulong
|
|
|
|
CStr(float Number); // Creates CStr from a float
|
|
|
|
CStr(double Number); // Creates CStr from a double
|
2003-09-21 23:24:53 +02:00
|
|
|
~CStr(); // Destructor
|
|
|
|
|
|
|
|
// Conversions
|
2004-06-01 18:51:37 +02:00
|
|
|
int ToInt() const;
|
|
|
|
unsigned int ToUInt() const;
|
|
|
|
long ToLong() const;
|
|
|
|
unsigned long ToULong() const;
|
|
|
|
float ToFloat() const;
|
|
|
|
double ToDouble() const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
2003-11-30 17:39:22 +01:00
|
|
|
size_t Length() const {return m_String.length();}
|
2003-09-21 23:24:53 +02:00
|
|
|
// Retrieves the substring within the string
|
2004-05-29 05:53:38 +02:00
|
|
|
CStr GetSubstring(size_t start, size_t len) const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
//Search the string for another string
|
2004-06-01 18:51:37 +02:00
|
|
|
long Find(const CStr &Str) const;
|
2004-05-29 05:53:38 +02:00
|
|
|
|
|
|
|
//Search the string for another string
|
2004-06-01 18:51:37 +02:00
|
|
|
long Find(const TCHAR &tchar) const;
|
2004-05-29 05:53:38 +02:00
|
|
|
|
|
|
|
//Search the string for another string
|
2004-06-01 18:51:37 +02:00
|
|
|
long Find(const int &start, const TCHAR &tchar) const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
//You can also do a "ReverseFind"- i.e. search starting from the end
|
2004-06-01 18:51:37 +02:00
|
|
|
long ReverseFind(const CStr &Str) const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// Lowercase and uppercase
|
2004-05-29 05:53:38 +02:00
|
|
|
CStr LowerCase() const;
|
|
|
|
CStr UpperCase() const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// Lazy funcs
|
2004-05-29 05:53:38 +02:00
|
|
|
CStr LCase() const;
|
|
|
|
CStr UCase() const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// Retreive the substring of the first n characters
|
2004-06-01 18:51:37 +02:00
|
|
|
CStr Left(long len) const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
// Retreive the substring of the last n characters
|
2004-06-01 18:51:37 +02:00
|
|
|
CStr Right(long len) const;
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
//Remove all occurences of some character or substring
|
|
|
|
void Remove(const CStr &Str);
|
|
|
|
|
|
|
|
//Replace all occurences of some substring by another
|
|
|
|
void Replace(const CStr &StrToReplace, const CStr &ReplaceWith);
|
|
|
|
|
|
|
|
// Returns a trimed string, removes whitespace from the left/right/both
|
|
|
|
CStr Trim(PS_TRIM_MODE Mode);
|
|
|
|
|
|
|
|
// Overload operations
|
|
|
|
CStr &operator=(const CStr &Str);
|
2003-11-07 01:59:13 +01:00
|
|
|
CStr &operator=(const TCHAR* String);
|
2003-09-21 23:24:53 +02:00
|
|
|
CStr &operator=(TCHAR Char);
|
2004-06-01 18:51:37 +02:00
|
|
|
CStr &operator=(int Number);
|
|
|
|
CStr &operator=(long Number);
|
|
|
|
CStr &operator=(unsigned int Number);
|
|
|
|
CStr &operator=(unsigned long Number);
|
|
|
|
CStr &operator=(float Number);
|
|
|
|
CStr &operator=(double Number);
|
|
|
|
|
|
|
|
bool operator==(const CStr &Str) const;
|
|
|
|
bool operator!=(const CStr &Str) const;
|
|
|
|
bool operator<(const CStr &Str) const;
|
|
|
|
bool operator<=(const CStr &Str) const;
|
|
|
|
bool operator>(const CStr &Str) const;
|
|
|
|
bool operator>=(const CStr &Str) const;
|
2003-11-25 03:17:52 +01:00
|
|
|
CStr &operator+=(const CStr &Str);
|
|
|
|
CStr operator+(const CStr &Str);
|
2003-11-07 01:59:13 +01:00
|
|
|
operator const TCHAR*();
|
2003-11-24 03:18:41 +01:00
|
|
|
operator const TCHAR*() const; // Gee, I've added this, Maybe the one above should be removed?
|
2004-06-01 18:51:37 +02:00
|
|
|
TCHAR &operator[](int n);
|
|
|
|
TCHAR &operator[](unsigned int n);
|
|
|
|
TCHAR &operator[](long n);
|
|
|
|
TCHAR &operator[](unsigned long n);
|
2003-09-21 23:24:53 +02:00
|
|
|
|
2004-03-04 21:27:37 +01:00
|
|
|
inline const char *c_str()
|
|
|
|
{ return m_String.c_str(); }
|
2004-05-22 01:46:16 +02:00
|
|
|
|
|
|
|
size_t GetHashCode() const;
|
2004-03-04 21:27:37 +01:00
|
|
|
|
2003-11-30 17:39:22 +01:00
|
|
|
// Serialization functions
|
2004-03-04 21:27:37 +01:00
|
|
|
virtual uint GetSerializedLength() const;
|
2003-11-30 17:39:22 +01:00
|
|
|
virtual u8 *Serialize(u8 *buffer) const;
|
|
|
|
virtual const u8 *Deserialize(const u8 *buffer, const u8 *bufferend);
|
2003-09-21 23:24:53 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
tstring m_String;
|
|
|
|
TCHAR m_ConversionBuffer[CONVERSION_BUFFER_SIZE];
|
|
|
|
};
|
|
|
|
|
2004-05-22 01:46:16 +02:00
|
|
|
class CStr_hash_compare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static const size_t bucket_size = 1;
|
|
|
|
static const size_t min_buckets = 16;
|
|
|
|
size_t operator()( const CStr& Key ) const
|
|
|
|
{
|
|
|
|
return( Key.GetHashCode() );
|
|
|
|
}
|
|
|
|
bool operator()( const CStr& _Key1, const CStr& _Key2 ) const
|
|
|
|
{
|
|
|
|
return( _Key1 < _Key2 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-09-21 23:24:53 +02:00
|
|
|
// overloaded operator for ostreams
|
|
|
|
ostream &operator<<(ostream &os, CStr &Str);
|
|
|
|
|
|
|
|
#endif
|