summaryrefslogtreecommitdiff
path: root/cpp/include/Ice/StringConverter.h
blob: 5dd3c2cff8c8e6d1ac2671848cae9f33ad05e860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICE_STRING_CONVERTER_H
#define ICE_STRING_CONVERTER_H

#include <Ice/Config.h>
#include <IceUtil/Exception.h>
#include <IceUtil/Shared.h>
#include <IceUtil/Handle.h>

#include <string>

namespace Ice
{

//
// Provides bytes to toUTF8. Raises MemoryLimitException when too many
// bytes are requested.
//
class ICE_API UTF8Buffer
{
public:
    virtual Byte* getMoreBytes(size_t howMany, Byte* firstUnused) = 0;
    
    virtual ~UTF8Buffer() {}
};

//
// A StringConverter converts narrow or wide-strings to and from UTF-8 byte sequences.
// It's used by the communicator during marshaling (toUTF8) and unmarshaling (fromUTF8).
// It report errors by raising StringConversionFailed or MemoryLimitException.
//
template<typename charT>
class BasicStringConverter : public IceUtil::Shared
{
public:
    
    //
    // Returns a pointer to byte after the last written byte (which may be
    // past the last byte returned by getMoreBytes).
    //
    virtual Byte* toUTF8(const charT* sourceStart, const charT* sourceEnd,
			 UTF8Buffer&) const = 0;

    //
    // This fromUTF8 function allocates the result (targetStart and targetEnd are
    // out parameters); when it succeeds, the caller is responsible to free the 
    // allocated target with freeTarget.
    // This way, an implementation of fromUTF8 using iconv() can use a single iconv_t
    // even when it discovers during conversion that it needs a larger target buffer.
    //
    virtual void fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
			  const charT*& targetStart, const charT*& targetEnd) const = 0;

    virtual void freeTarget(const charT* targetStart) const = 0;


    //
    // You may want to override this fromUTF8 function to provide a more efficient 
    // implementation, without a temporary charT buffer.
    //
    virtual void fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
			  std::basic_string<charT>& target) const
    {
	const charT* targetStart = 0;
	const charT* targetEnd = 0;
	fromUTF8(sourceStart, sourceEnd, targetStart, targetEnd);
	std::basic_string<charT> s(targetStart, static_cast<size_t>(targetEnd - targetStart));
	freeTarget(targetStart);
	s.swap(target);
    } 
};

typedef BasicStringConverter<char> StringConverter;
typedef IceUtil::Handle<StringConverter> StringConverterPtr;

typedef BasicStringConverter<wchar_t> WstringConverter;
typedef IceUtil::Handle<WstringConverter> WstringConverterPtr;


//
// Converts to and from UTF-16 or UTF-32 depending on sizeof(wchar_t)
//
class ICE_API UnicodeWstringConverter : public WstringConverter
{
public:
    
    virtual Byte* toUTF8(const wchar_t* sourceStart, const wchar_t* sourceEnd,
			 UTF8Buffer&) const;

    virtual void fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
			  const wchar_t*& targetStart, const wchar_t*& targetEnd) const;

    virtual void freeTarget(const wchar_t* targetStart) const;

    virtual void fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
			  std::wstring& target) const;
};
}
#endif