1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| // Some macros for casting and text conversion
typedef WCHAR _WCHAR;
typedef CHAR _CHAR;
#define _WTEXT(t) LPCWSTR(L ## t)
#define _W(t) LPCWSTR(L ## t)
#define _WC(c) WCHAR(L ## c)
#define _NTEXT(t) LPCSTR(t)
#define _N(t) LPCSTR(t)
#define _BTEXT(t) String(_W(t))
#define _B(t) String(_W(t))
#define WNULL LPCWSTR(NULL)
#define BNULL String(WNULL)
// Helper converts LPSTR to BSTR
BSTR SysAllocStringA(LPCSTR sz);
BSTR SysAllocStringLenA(LPCSTR sz, unsigned cch);
typedef Long FindFlags;
const FindFlags ffNone = 0x0;
const FindFlags ffReverse = 0x1;
const FindFlags ffIgnoreCase = 0x2;
//@B String1
class String
{
friend class Buffer;
public:
// Constructors
String();
String(const String& s);
// Danger! C++ can't tell the difference between BSTR and LPWSTR. If
// you pass LPWSTR to this constructor, you'll get very bad results,
// so don't. Instead cast to constant before assigning.
String(BSTR bs);
// Any non-const LPSTR or LPWSTR should be cast to LPCSTR or LPCWSTR
// so that it comes through here
String(LPCSTR sz);
String(LPCWSTR wsz);
// Filled with given character (default -1 means unitialized allocate)
String(int cch, WCHAR wch = WCHAR(-1));
// Destructor
~String();
//@E String1
// Assignment
const String& operator=(const String& s);
// BSTR only -- use const version for character strings
const String& operator=(BSTR bs);
// Cast LPSTR to LPCSTR
const String& operator=(LPCSTR sz);
// Cast LPWSTR to LPCWSTR
const String& operator=(LPCWSTR wsz);
const String& operator=(CHAR ch);
const String& operator=(WCHAR wch);
// Type casts
operator BSTR() const; // as a const OLE BSTR
operator BSTR(); // as an OLE BSTR
operator LPCSTR(); // as a const ANSI string
operator LPCWSTR(); // as a const Unicode string
// No LPSTR operator (use Buffer object)
// Attributes & Operations
int Length() const;
int LengthZ() const; // Length to first null
BOOL IsEmpty();
BOOL IsNull();
void Empty(); // Make an empty string
void Nullify(); // Make a null string
String& NullIfEmpty(); // Return null for empty (handy for APIs)
// Indexing
WCHAR& operator[](int i);
// String concatenation
const String & operator+=(const String& string);
const String & operator+=(WCHAR wch);
const String & operator+=(CHAR ch);
const String & operator+=(LPCWSTR wsz);
const String & operator+=(LPCSTR sz);
friend String operator+(const String& string1, const String& string2);
friend String operator+(const String& string, const WCHAR wch);
friend String operator+(const WCHAR wch, const String& string);
friend String operator+(const String& string, const CHAR ch);
friend String operator+(const CHAR ch, const String& string);
friend String operator+(const String& string, BSTR bs);
friend String operator+(BSTR bs, const String& string);
friend String operator+(const String& string, LPCWSTR wsz);
friend String operator+(const String& string, LPCSTR sz);
friend String operator+(LPCWSTR wsz, const String& string);
friend String operator+(LPCSTR sz, const String& string);
// Searching (return starting index, or 0 if not found)
// like "C" strstr
int Find( String& s, FindFlags ff = ffNone, Long iStart = -1) const;
int Find(LPCWSTR wsz, FindFlags ff = ffNone, Long iStart = -1) const;
int Find(LPCSTR sz, FindFlags ff = ffNone, Long iStart = -1) const;
// Like "C" strchr
int Find(WCHAR wch, FindFlags ff = ffNone, Long iStart = -1) const;
int Find(CHAR ch, FindFlags ff = ffNone, Long iStart = -1) const;
friend ostream& operator<<(ostream& os, String& s);
//@B String2
private:
BSTR m_bs; // The Unicode data
LPSTR m_pch; // ANSI representation of it
Boolean m_fDestroy; // Destruction flag
// Implementation helpers
void Concat(int c, LPCWSTR wsz);
void Destroy();
void DestroyA();
public:
// Bruno STEUX 2000 : added a usefuel little function
void MustBeDestroyed() {m_fDestroy=True;}
};
//@E String2
// Buffer object for non-const string parameters
class Buffer
{
public:
// Constructor
Buffer(String& s);
Buffer(String& s, Long c);
// Destructor
~Buffer();
// Conversion operator
operator LPSTR();
operator LPWSTR();
private:
String * m_ps;
// No assignment allowed (declared private, but not defined)
Buffer& operator=(const Buffer&);
Buffer(const Buffer&);
};
// iostream helpers
// manipulator for CrLf sequence
inline ostream& __cdecl endcl(ostream& os) { return os << "\r\n" << flush; }
ostream& operator<<(ostream& os, WCHAR wch);
// Implementation of inline functions
//@B StringCon
inline String::String()
: m_bs(SysAllocString(NULL)), m_pch(NULL), m_fDestroy(True)
{
}
inline String::String(const String& s)
: m_bs(SysAllocString(s.m_bs)), m_pch(NULL), m_fDestroy(True)
{
}
// Convert BSTR to String
inline String::String(BSTR bs)
: m_bs(bs), m_pch(NULL), m_fDestroy(False)
{
}
inline String::String(LPCWSTR wsz)
: m_bs(SysAllocString(wsz)), m_pch(NULL), m_fDestroy(True)
{
}
inline String::String(LPCSTR sz)
: m_bs(SysAllocStringA(sz)), m_pch(NULL), m_fDestroy(True)
{
}
//@E StringCon
//@B StringDes
inline String::~String()
{
Destroy();
}
// Invalidate ANSI buffer
inline void String::DestroyA()
{
if (m_pch) {
delete[] m_pch;
m_pch = NULL;
}
}
//@E StringDes
inline String::operator BSTR() const
{
return (BSTR)m_bs;
}
inline String::operator BSTR()
{
return SysAllocString(m_bs);
}
inline String::operator LPCWSTR()
{
return m_bs;
}
inline const String& String::operator+=(const String& s)
{
Concat(s.Length(), s.m_bs);
return *this;
}
inline const String& String::operator+=(WCHAR wch)
{
Concat(1, &wch);
return *this;
}
inline const String& String::operator+=(LPCWSTR wsz)
{
Concat(wcslen(wsz), wsz);
return *this;
}
inline int String::Find(String& s, FindFlags ff, Long iStart) const
{
return (Find(s.m_bs, ff, iStart));
}
inline int String::Find(LPCSTR sz, FindFlags ff, Long iStart) const
{
return (Find(String(sz), ff, iStart));
}
inline int String::Find(CHAR ch, FindFlags ff, Long iStart) const
{
return (Find(CharToWChar(ch), ff, iStart));
} |
Partager