diff options
author | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2006-09-18 15:08:25 +0000 |
---|---|---|
committer | dshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573> | 2006-09-18 15:08:25 +0000 |
commit | a4d4f3ecccb29c3f5fc1ce55579119106c399911 (patch) | |
tree | 5677ea04b4dfdadd961c01ba3baf7502f8d6e0d0 /stone_soup/crawl-ref/source/MacString.cc | |
parent | 571501e1135989d3b9dc44e3d332562a7cf78b35 (diff) | |
download | crawl-ref-a4d4f3ecccb29c3f5fc1ce55579119106c399911.tar.gz crawl-ref-a4d4f3ecccb29c3f5fc1ce55579119106c399911.zip |
Updated stone_soup-0.1b1 tag to include fix for Poison Arrow of Doom.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/tags/stone_soup-0.1b1@49 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'stone_soup/crawl-ref/source/MacString.cc')
-rw-r--r-- | stone_soup/crawl-ref/source/MacString.cc | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/stone_soup/crawl-ref/source/MacString.cc b/stone_soup/crawl-ref/source/MacString.cc new file mode 100644 index 0000000000..2d5c14442e --- /dev/null +++ b/stone_soup/crawl-ref/source/MacString.cc @@ -0,0 +1,199 @@ +/* + * File: MacString.cc + * Summary: Wrapper around an immutable CFString. + * Written by: Jesse Jones (jesjones@mindspring.com) + * + * Change History (most recent first): + * + * <1> 6/04/02 JDJ Created + */ + +#include "AppHdr.h" +#include "MacString.h" + +#if macintosh + +#include <CoreFoundation/CFString.h> + +#include "debug.h" + + +// ======================================================================== +// Internal Functions +// ======================================================================== + +//--------------------------------------------------------------- +// +// ThrowIf +// +//--------------------------------------------------------------- +static void ThrowIf(bool predicate, const std::string& text) +{ + if (predicate) + throw std::runtime_error(text); +} + +#if __MWERKS__ +#pragma mark - +#endif + +// ============================================================================ +// class MacString +// ============================================================================ + +//--------------------------------------------------------------- +// +// MacString::~MacString +// +//--------------------------------------------------------------- +MacString::~MacString() +{ + CFRelease(mString); +} + + +//--------------------------------------------------------------- +// +// MacString::MacString () +// +//--------------------------------------------------------------- +MacString::MacString() +{ + mString = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, NULL, 0); + ThrowIf(mString == NULL, "Couldn't create the CFString"); +} + + +//--------------------------------------------------------------- +// +// MacString::MacString (unsigned char*) +// +//--------------------------------------------------------------- +MacString::MacString(const unsigned char* str) +{ + ASSERT(str != NULL); + + CFStringEncoding encoding = CFStringGetSystemEncoding(); + mString = CFStringCreateWithPascalString(kCFAllocatorSystemDefault, str, encoding); + ThrowIf(mString == NULL, "Couldn't create the CFString"); +} + + +//--------------------------------------------------------------- +// +// MacString::MacString (char*) +// +//--------------------------------------------------------------- +MacString::MacString(const char* str) +{ + ASSERT(str != NULL); + + CFStringEncoding encoding = CFStringGetSystemEncoding(); + mString = CFStringCreateWithCString(kCFAllocatorSystemDefault, str, encoding); + ThrowIf(mString == NULL, "Couldn't create the CFString"); +} + + +//--------------------------------------------------------------- +// +// MacString::MacString (CFStringRef) +// +//--------------------------------------------------------------- +MacString::MacString(CFStringRef str) +{ + ASSERT(str != NULL); + + mString = str; + CFRetain(mString); +} + + +//--------------------------------------------------------------- +// +// MacString::MacString (CFMutableStringRef) +// +//--------------------------------------------------------------- +MacString::MacString(CFMutableStringRef str) +{ + ASSERT(str != NULL); + + mString = CFStringCreateCopy(kCFAllocatorSystemDefault, str); + ThrowIf(mString == NULL, "Couldn't create the CFString"); +} + + +//--------------------------------------------------------------- +// +// MacString::MacString (int) +// +//--------------------------------------------------------------- +MacString::MacString(int value) +{ + char buffer[32]; + sprintf(buffer, "%d", value); + + CFStringEncoding encoding = CFStringGetSystemEncoding(); + mString = CFStringCreateWithCString(kCFAllocatorSystemDefault, buffer, encoding); + ThrowIf(mString == NULL, "Couldn't create the CFString"); +} + + +//--------------------------------------------------------------- +// +// MacString::MacString (MacString) +// +//--------------------------------------------------------------- +MacString::MacString(const MacString& str) +{ + mString = str.mString; // immutable so we can refcount + CFRetain(mString); +} + + +//--------------------------------------------------------------- +// +// MacString::operator= (MacString) +// +//--------------------------------------------------------------- +MacString& MacString::operator=(const MacString& rhs) +{ + if (this != &rhs) + { + CFRelease(mString); + + mString = rhs.mString; // immutable so we can refcount + CFRetain(mString); + } + + return *this; +} + + +//--------------------------------------------------------------- +// +// MacString::length +// +//--------------------------------------------------------------- +size_t MacString::length() const +{ + size_t len = (size_t) CFStringGetLength(mString); + + return len; +} + + +//--------------------------------------------------------------- +// +// MacString::CopyTo +// +//--------------------------------------------------------------- +void MacString::CopyTo(unsigned char* buffer, CFIndex bytes) +{ + ASSERT(buffer != NULL || bytes == 0); + + bool converted = CFStringGetPascalString(mString, buffer, bytes, CFStringGetSystemEncoding()); + ThrowIf(!converted, "Couldn't convert the CFString into a Pascal string"); +} + + +#endif // macintosh |