1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: utility for ustring |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <osn/ustring.h> |
|
20 #include <glib/gstring.h> |
|
21 |
|
22 #include <stdlib.h> |
|
23 #include <errno.h> |
|
24 #include <stdexcept> |
|
25 #include <osn/osnnew.h> |
|
26 |
|
27 namespace osncore |
|
28 { |
|
29 |
|
30 const int KBase = 10; |
|
31 |
|
32 // --------------------------------------------------------------------------- |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 template <typename T> |
|
36 OSN_EXPORT UtfProxy<T>::~UtfProxy() |
|
37 { |
|
38 if(iUtf) |
|
39 { |
|
40 free(iUtf); |
|
41 } |
|
42 } |
|
43 |
|
44 // --------------------------------------------------------------------------- |
|
45 // Conversion to Utf16 from Utf8 using g_utf8_to_utf16 |
|
46 // --------------------------------------------------------------------------- |
|
47 // |
|
48 OSN_EXPORT auto_ptr<Utf16Proxy> toUtf16(const UString& aSourceUtf8) |
|
49 { |
|
50 Utf16* ret = g_utf8_to_utf16(aSourceUtf8.getUtf8(),-1,0,0,0); |
|
51 |
|
52 auto_ptr<Utf16Proxy> tmp; |
|
53 try |
|
54 { |
|
55 tmp.reset(new (EMM)Utf16Proxy(ret)); |
|
56 } |
|
57 catch(std::bad_alloc& e) |
|
58 { |
|
59 free(ret); |
|
60 } |
|
61 return tmp; |
|
62 } |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // --------------------------------------------------------------------------- |
|
66 // |
|
67 OSN_EXPORT int toInt(const UString& aSource) |
|
68 { |
|
69 int ret(0); |
|
70 const Utf8* string = aSource.getUtf8(); |
|
71 if (!string) |
|
72 { |
|
73 throw UString::InvalidUtf8(); |
|
74 } |
|
75 |
|
76 ret = strtol(string, (char **)NULL, KBase); |
|
77 if( errno == EINVAL || errno == ERANGE) |
|
78 { |
|
79 errno = 0; |
|
80 throw UString::InvalidUtf8(); |
|
81 } |
|
82 return ret; |
|
83 } |
|
84 |
|
85 // --------------------------------------------------------------------------- |
|
86 // --------------------------------------------------------------------------- |
|
87 // |
|
88 static auto_ptr<UString> normalize(const UString& aSource, GNormalizeMode aMode) |
|
89 { |
|
90 const Utf8* string = aSource.getUtf8(); |
|
91 if (!string) |
|
92 { |
|
93 throw UString::InvalidUtf8(); |
|
94 } |
|
95 |
|
96 Utf8* ret = g_utf8_normalize(string, -1, aMode); |
|
97 |
|
98 if(!ret) |
|
99 { |
|
100 throw UString::InvalidUtf8(); |
|
101 } |
|
102 auto_ptr<UString> tmp; |
|
103 try |
|
104 { |
|
105 tmp.reset(new (EMM)UString(ret)); |
|
106 // g_free(ret); |
|
107 free(ret); |
|
108 } |
|
109 catch(std::bad_alloc& e) |
|
110 { |
|
111 // g_free(ret); |
|
112 free(ret); |
|
113 } |
|
114 return tmp; |
|
115 } |
|
116 |
|
117 // --------------------------------------------------------------------------- |
|
118 // --------------------------------------------------------------------------- |
|
119 // |
|
120 OSN_EXPORT auto_ptr<UString> normalizeNFD(const UString& aSource) |
|
121 { |
|
122 return normalize(aSource,G_NORMALIZE_DEFAULT); |
|
123 } |
|
124 |
|
125 // --------------------------------------------------------------------------- |
|
126 // --------------------------------------------------------------------------- |
|
127 // |
|
128 OSN_EXPORT auto_ptr<UString> normalizeNFC(const UString& aSource) |
|
129 { |
|
130 return normalize(aSource,G_NORMALIZE_DEFAULT_COMPOSE); |
|
131 } |
|
132 |
|
133 // --------------------------------------------------------------------------- |
|
134 // --------------------------------------------------------------------------- |
|
135 // |
|
136 OSN_EXPORT auto_ptr<UString> normalizeNFKD(const UString& aSource) |
|
137 { |
|
138 return normalize(aSource,G_NORMALIZE_ALL); |
|
139 } |
|
140 |
|
141 // --------------------------------------------------------------------------- |
|
142 // --------------------------------------------------------------------------- |
|
143 // |
|
144 OSN_EXPORT auto_ptr<UString> normalizeNFKC(const UString& aSource) |
|
145 { |
|
146 return normalize(aSource,G_NORMALIZE_ALL_COMPOSE); |
|
147 } |
|
148 |
|
149 } |
|