Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/blahtexml/source/BlahtexXMLin/XercesString.cpp
blob: 69c8613fcea8def88d4084752ee8dbb22aa90547 (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
// File "XercesString.cpp"
//
// blahtexml (version 0.5)
// Copyright (C) 2007-2008, Gilles Van Assche
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include <iostream>
#include "XercesString.h"

XercesString::XercesString()
    : basic_string<XMLCh>()
{
}

XercesString::XercesString(const XMLCh *s)
    : basic_string<XMLCh>(s)
{
}

XercesString::XercesString(const char *s)
    : basic_string<XMLCh>()
{
    XMLCh *Xs = XMLString::transcode(s);
    assign(Xs);
    XMLString::release(&Xs);
}

XercesString::XercesString(const wstring& in)
    : basic_string<XMLCh>()
{
    for(unsigned int i=0; i<in.length(); ++i) {
        wchar_t utf32 = in[i];
        if (utf32 >= 0x10000UL) {
            push_back(0xD800 - 0x40 + (utf32 >> 10));
            push_back(0xDC00 + (utf32 & 0x3FF));
        }
        else
            push_back(utf32);
    }
}

wstring XercesString::convertTowstring()
{
    wstring out;
    wchar_t ucs4;
    bool firstSurrogate = false;
    for(unsigned int i=0; i<length(); ++i) {
        XMLCh utf16 = at(i);
        if (firstSurrogate) {
            if ((utf16 & 0xFC00) == 0xDC00) {
                ucs4 += utf16-0xDC00UL+0x10000UL;
                out.push_back(ucs4);
            }
            /*else
                throw (...);*/
            firstSurrogate = false;
        }
        else if ((utf16 & 0xFC00) == 0xD800) {
            ucs4 = (utf16-0xD800UL) << 10;
            firstSurrogate = true;
        }
        else {
            out.push_back(utf16);
        }
    }
    return out;
}

using namespace std;

ostream& operator<<(ostream& out, const XercesString& s)
{
    char *temp = XMLString::transcode(s.c_str());
    out << temp;
    XMLString::release(&temp);
    return out;
}