]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/charstr.h
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / common / charstr.h
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
374ca955 3* Copyright (c) 2001-2004, International Business Machines
b75a7d8f
A
4* Corporation and others. All Rights Reserved.
5**********************************************************************
6* Date Name Description
7* 11/19/2001 aliu Creation.
8**********************************************************************
9*/
10
374ca955
A
11#ifndef CHARSTRING_H
12#define CHARSTRING_H
13
b75a7d8f
A
14#include "unicode/utypes.h"
15#include "unicode/uobject.h"
16#include "unicode/unistr.h"
17#include "cmemory.h"
18
19//--------------------------------------------------------------------
20// class CharString
21//
22// This is a tiny wrapper class that is used internally to make a
23// UnicodeString look like a const char*. It can be allocated on the
24// stack. It only creates a heap buffer if it needs to.
25//--------------------------------------------------------------------
26
27U_NAMESPACE_BEGIN
28
29class U_COMMON_API CharString : public UMemory {
30public:
374ca955
A
31
32#if !UCONFIG_NO_CONVERSION
33 // Constructor
34 // @param str The unicode string to be converted to char *
35 // @param codepage The char * code page. "" for invariant conversion.
36 // NULL for default code page.
37 inline CharString(const UnicodeString& str, const char *codepage);
38#endif
39
b75a7d8f
A
40 inline CharString(const UnicodeString& str);
41 inline ~CharString();
42 inline operator const char*() const { return ptr; }
43
44private:
45 char buf[128];
46 char* ptr;
47
48 CharString(const CharString &other); // forbid copying of this class
49 CharString &operator=(const CharString &other); // forbid copying of this class
50};
51
374ca955
A
52#if !UCONFIG_NO_CONVERSION
53
54inline CharString::CharString(const UnicodeString& str, const char *codepage) {
55 int32_t len;
56 ptr = buf;
57 len = str.extract(0, 0x7FFFFFFF, buf ,sizeof(buf)-1, codepage);
58 if (len >= (int32_t)(sizeof(buf)-1)) {
59 ptr = (char *)uprv_malloc(len+1);
60 str.extract(0, 0x7FFFFFFF, ptr, len+1, codepage);
61 }
62}
63
64#endif
65
b75a7d8f 66inline CharString::CharString(const UnicodeString& str) {
374ca955
A
67 int32_t len;
68 ptr = buf;
69 len = str.extract(0, 0x7FFFFFFF, buf, (int32_t)(sizeof(buf)-1), US_INV);
70 if (len >= (int32_t)(sizeof(buf)-1)) {
71 ptr = (char *)uprv_malloc(len+1);
72 str.extract(0, 0x7FFFFFFF, ptr, len+1, US_INV);
b75a7d8f 73 }
b75a7d8f
A
74}
75
76inline CharString::~CharString() {
77 if (ptr != buf) {
78 uprv_free(ptr);
79 }
80}
81
82U_NAMESPACE_END
83
374ca955 84#endif
b75a7d8f 85//eof