os/textandloc/fontservices/textshaperplugin/IcuSource/common/charstr.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
**********************************************************************
sl@0
     3
*   Copyright (c) 2001-2004, International Business Machines
sl@0
     4
*   Corporation and others.  All Rights Reserved.
sl@0
     5
**********************************************************************
sl@0
     6
*   Date        Name        Description
sl@0
     7
*   11/19/2001  aliu        Creation.
sl@0
     8
**********************************************************************
sl@0
     9
*/
sl@0
    10
sl@0
    11
#ifndef CHARSTRING_H
sl@0
    12
#define CHARSTRING_H
sl@0
    13
sl@0
    14
#include "unicode/utypes.h"
sl@0
    15
#include "unicode/uobject.h"
sl@0
    16
#include "unicode/unistr.h"
sl@0
    17
#include "cmemory.h"
sl@0
    18
sl@0
    19
//--------------------------------------------------------------------
sl@0
    20
// class CharString
sl@0
    21
//
sl@0
    22
// This is a tiny wrapper class that is used internally to make a
sl@0
    23
// UnicodeString look like a const char*.  It can be allocated on the
sl@0
    24
// stack.  It only creates a heap buffer if it needs to.
sl@0
    25
//--------------------------------------------------------------------
sl@0
    26
sl@0
    27
U_NAMESPACE_BEGIN
sl@0
    28
sl@0
    29
class U_COMMON_API CharString : public UMemory {
sl@0
    30
public:
sl@0
    31
sl@0
    32
#if !UCONFIG_NO_CONVERSION
sl@0
    33
    // Constructor
sl@0
    34
    //     @param  str    The unicode string to be converted to char *
sl@0
    35
    //     @param  codepage   The char * code page.  ""   for invariant conversion.
sl@0
    36
    //                                               NULL for default code page.
sl@0
    37
//    inline CharString(const UnicodeString& str, const char *codepage);
sl@0
    38
#endif
sl@0
    39
sl@0
    40
    inline CharString(const UnicodeString& str);
sl@0
    41
    inline ~CharString();
sl@0
    42
    inline operator const char*() const { return ptr; }
sl@0
    43
sl@0
    44
private:
sl@0
    45
    char buf[128];
sl@0
    46
    char* ptr;
sl@0
    47
sl@0
    48
    CharString(const CharString &other); // forbid copying of this class
sl@0
    49
    CharString &operator=(const CharString &other); // forbid copying of this class
sl@0
    50
};
sl@0
    51
sl@0
    52
#if !UCONFIG_NO_CONVERSION
sl@0
    53
sl@0
    54
// PLEASE DON'T USE THIS FUNCTION.
sl@0
    55
// We don't want the static dependency on conversion or the performance hit that comes from a codepage conversion.
sl@0
    56
/*
sl@0
    57
inline CharString::CharString(const UnicodeString& str, const char *codepage) {
sl@0
    58
    int32_t    len;
sl@0
    59
    ptr = buf;
sl@0
    60
    len = str.extract(0, 0x7FFFFFFF, buf ,sizeof(buf)-1, codepage);
sl@0
    61
    if (len >= (int32_t)(sizeof(buf)-1)) {
sl@0
    62
        ptr = (char *)uprv_malloc(len+1);
sl@0
    63
        str.extract(0, 0x7FFFFFFF, ptr, len+1, codepage);
sl@0
    64
    }
sl@0
    65
}*/
sl@0
    66
sl@0
    67
#endif
sl@0
    68
sl@0
    69
inline CharString::CharString(const UnicodeString& str) {
sl@0
    70
    int32_t    len;
sl@0
    71
    ptr = buf;
sl@0
    72
    len = str.extract(0, 0x7FFFFFFF, buf, (int32_t)(sizeof(buf)-1), US_INV);
sl@0
    73
    if (len >= (int32_t)(sizeof(buf)-1)) {
sl@0
    74
        ptr = (char *)uprv_malloc(len+1);
sl@0
    75
        str.extract(0, 0x7FFFFFFF, ptr, len+1, US_INV);
sl@0
    76
    }
sl@0
    77
}
sl@0
    78
sl@0
    79
inline CharString::~CharString() {
sl@0
    80
    if (ptr != buf) {
sl@0
    81
        uprv_free(ptr);
sl@0
    82
    }
sl@0
    83
}
sl@0
    84
sl@0
    85
U_NAMESPACE_END
sl@0
    86
sl@0
    87
#endif
sl@0
    88
//eof