2001-11-19 21:24:29 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2003-12-18 23:16:48 +00:00
|
|
|
* Copyright (c) 2001-2003, International Business Machines
|
2001-11-19 21:24:29 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 11/19/2001 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-06-27 01:19:20 +00:00
|
|
|
#include "unicode/uobject.h"
|
2001-11-19 21:24:29 +00:00
|
|
|
#include "unicode/unistr.h"
|
2002-07-16 01:55:55 +00:00
|
|
|
#include "cmemory.h"
|
2001-11-19 21:24:29 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// class CharString
|
|
|
|
//
|
|
|
|
// This is a tiny wrapper class that is used internally to make a
|
|
|
|
// UnicodeString look like a const char*. It can be allocated on the
|
|
|
|
// stack. It only creates a heap buffer if it needs to.
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2002-10-04 16:35:10 +00:00
|
|
|
class U_COMMON_API CharString : public UMemory {
|
|
|
|
public:
|
2003-10-23 18:34:43 +00:00
|
|
|
// Constructor
|
|
|
|
// @param str The unicode string to be converted to char *
|
|
|
|
// @param codepage The char * code page. "" for invariant conversion.
|
|
|
|
// NULL for default code page.
|
|
|
|
inline CharString(const UnicodeString& str, const char *codepage = "");
|
2001-11-19 21:24:29 +00:00
|
|
|
inline ~CharString();
|
2002-10-30 18:07:23 +00:00
|
|
|
inline operator const char*() const { return ptr; }
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-10-04 16:35:10 +00:00
|
|
|
private:
|
2001-11-19 21:24:29 +00:00
|
|
|
char buf[128];
|
|
|
|
char* ptr;
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-10-04 16:35:10 +00:00
|
|
|
CharString(const CharString &other); // forbid copying of this class
|
|
|
|
CharString &operator=(const CharString &other); // forbid copying of this class
|
2001-11-19 21:24:29 +00:00
|
|
|
};
|
|
|
|
|
2003-10-23 18:34:43 +00:00
|
|
|
inline CharString::CharString(const UnicodeString& str, const char *codepage) {
|
|
|
|
int32_t len;
|
|
|
|
ptr = buf;
|
|
|
|
len = str.extract(0, 0x7FFFFFFF, buf ,sizeof(buf)-1, codepage);
|
|
|
|
buf[sizeof(buf)-1] = 0; // extract does not add null if it thinks there is no space for it.
|
2003-11-27 01:14:37 +00:00
|
|
|
if (len >= (int32_t)(sizeof(buf)-1)) {
|
2003-10-23 18:34:43 +00:00
|
|
|
ptr = (char *)uprv_malloc(len+1);
|
|
|
|
str.extract(0, 0x7FFFFFFF, ptr, len+1, codepage);
|
2001-11-19 21:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CharString::~CharString() {
|
|
|
|
if (ptr != buf) {
|
2002-07-16 01:55:55 +00:00
|
|
|
uprv_free(ptr);
|
2001-11-19 21:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
//eof
|