2001-11-19 21:24:29 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2004-08-26 22:51:40 +00:00
|
|
|
* Copyright (c) 2001-2004, 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.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2004-10-15 22:56:26 +00:00
|
|
|
#ifndef CHARSTRING_H
|
|
|
|
#define CHARSTRING_H
|
|
|
|
|
2001-11-19 21:24:29 +00:00
|
|
|
#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:
|
2004-08-26 22:51:40 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_CONVERSION
|
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.
|
2004-08-26 22:51:40 +00:00
|
|
|
inline CharString(const UnicodeString& str, const char *codepage);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline CharString(const UnicodeString& str);
|
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
|
|
|
};
|
|
|
|
|
2004-08-26 22:51:40 +00:00
|
|
|
#if !UCONFIG_NO_CONVERSION
|
|
|
|
|
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);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-26 22:51:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
inline CharString::CharString(const UnicodeString& str) {
|
|
|
|
int32_t len;
|
|
|
|
ptr = buf;
|
|
|
|
len = str.extract(0, 0x7FFFFFFF, buf, (int32_t)(sizeof(buf)-1), US_INV);
|
|
|
|
if (len >= (int32_t)(sizeof(buf)-1)) {
|
|
|
|
ptr = (char *)uprv_malloc(len+1);
|
|
|
|
str.extract(0, 0x7FFFFFFF, ptr, len+1, US_INV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2004-10-15 22:56:26 +00:00
|
|
|
#endif
|
2001-11-19 21:24:29 +00:00
|
|
|
//eof
|