sl@0: /* sl@0: ********************************************************************** sl@0: * Copyright (c) 2001-2004, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: ********************************************************************** sl@0: * Date Name Description sl@0: * 11/19/2001 aliu Creation. sl@0: ********************************************************************** sl@0: */ sl@0: sl@0: #ifndef CHARSTRING_H sl@0: #define CHARSTRING_H sl@0: sl@0: #include "unicode/utypes.h" sl@0: #include "unicode/uobject.h" sl@0: #include "unicode/unistr.h" sl@0: #include "cmemory.h" sl@0: sl@0: //-------------------------------------------------------------------- sl@0: // class CharString sl@0: // sl@0: // This is a tiny wrapper class that is used internally to make a sl@0: // UnicodeString look like a const char*. It can be allocated on the sl@0: // stack. It only creates a heap buffer if it needs to. sl@0: //-------------------------------------------------------------------- sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: class U_COMMON_API CharString : public UMemory { sl@0: public: sl@0: sl@0: #if !UCONFIG_NO_CONVERSION sl@0: // Constructor sl@0: // @param str The unicode string to be converted to char * sl@0: // @param codepage The char * code page. "" for invariant conversion. sl@0: // NULL for default code page. sl@0: // inline CharString(const UnicodeString& str, const char *codepage); sl@0: #endif sl@0: sl@0: inline CharString(const UnicodeString& str); sl@0: inline ~CharString(); sl@0: inline operator const char*() const { return ptr; } sl@0: sl@0: private: sl@0: char buf[128]; sl@0: char* ptr; sl@0: sl@0: CharString(const CharString &other); // forbid copying of this class sl@0: CharString &operator=(const CharString &other); // forbid copying of this class sl@0: }; sl@0: sl@0: #if !UCONFIG_NO_CONVERSION sl@0: sl@0: // PLEASE DON'T USE THIS FUNCTION. sl@0: // We don't want the static dependency on conversion or the performance hit that comes from a codepage conversion. sl@0: /* sl@0: inline CharString::CharString(const UnicodeString& str, const char *codepage) { sl@0: int32_t len; sl@0: ptr = buf; sl@0: len = str.extract(0, 0x7FFFFFFF, buf ,sizeof(buf)-1, codepage); sl@0: if (len >= (int32_t)(sizeof(buf)-1)) { sl@0: ptr = (char *)uprv_malloc(len+1); sl@0: str.extract(0, 0x7FFFFFFF, ptr, len+1, codepage); sl@0: } sl@0: }*/ sl@0: sl@0: #endif sl@0: sl@0: inline CharString::CharString(const UnicodeString& str) { sl@0: int32_t len; sl@0: ptr = buf; sl@0: len = str.extract(0, 0x7FFFFFFF, buf, (int32_t)(sizeof(buf)-1), US_INV); sl@0: if (len >= (int32_t)(sizeof(buf)-1)) { sl@0: ptr = (char *)uprv_malloc(len+1); sl@0: str.extract(0, 0x7FFFFFFF, ptr, len+1, US_INV); sl@0: } sl@0: } sl@0: sl@0: inline CharString::~CharString() { sl@0: if (ptr != buf) { sl@0: uprv_free(ptr); sl@0: } sl@0: } sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: #endif sl@0: //eof