Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <stdlib.h>
22 : #include <rtl/strbuf.hxx>
23 : #include <rtl/uri.hxx>
24 : #include <osl/thread.h>
25 : #include <osl/file.hxx>
26 :
27 : #include <cppunit/TestFixture.h>
28 : #include <cppunit/extensions/HelperMacros.h>
29 : #include <cppunit/plugin/TestPlugIn.h>
30 :
31 : // -----------------------------------------------------------------------------
32 :
33 : namespace Stringtest
34 : {
35 : // -----------------------------------------------------------------------------
36 :
37 3 : class Convert : public CppUnit::TestFixture
38 : {
39 : rtl::OUString m_aStr;
40 : public:
41 : rtl::OUString fromUTF8(rtl::OString const& _suStr)
42 : {
43 : rtl::OUString suStr = rtl::OStringToOUString(_suStr, RTL_TEXTENCODING_UTF8);
44 : return suStr;
45 : }
46 :
47 2 : rtl::OString convertToOString(rtl::OUString const& _suStr)
48 : {
49 2 : return rtl::OUStringToOString(_suStr, osl_getThreadTextEncoding()/*RTL_TEXTENCODING_ASCII_US*/);
50 : }
51 :
52 2 : void showContent(rtl::OUString const& _suStr)
53 : {
54 2 : rtl::OString sStr = convertToOString(_suStr);
55 2 : printf("%s\n", sStr.getStr());
56 2 : }
57 :
58 : void toUTF8_mech(rtl::OUString const& _suStr, rtl_UriEncodeMechanism _eMechanism)
59 : {
60 : rtl::OUString suStr;
61 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassNone, _eMechanism, RTL_TEXTENCODING_UTF8);
62 : showContent(suStr);
63 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUric, _eMechanism, RTL_TEXTENCODING_UTF8);
64 : showContent(suStr);
65 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUricNoSlash, _eMechanism, RTL_TEXTENCODING_UTF8);
66 : showContent(suStr);
67 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRelSegment, _eMechanism, RTL_TEXTENCODING_UTF8);
68 : showContent(suStr);
69 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRegName, _eMechanism, RTL_TEXTENCODING_UTF8);
70 : showContent(suStr);
71 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUserinfo, _eMechanism, RTL_TEXTENCODING_UTF8);
72 : showContent(suStr);
73 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassPchar, _eMechanism, RTL_TEXTENCODING_UTF8);
74 : showContent(suStr);
75 : suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUnoParamValue, _eMechanism, RTL_TEXTENCODING_UTF8);
76 : showContent(suStr);
77 : }
78 :
79 : void toUTF8(rtl::OUString const& _suStr)
80 : {
81 : printf("rtl_UriEncodeIgnoreEscapes \n");
82 : toUTF8_mech(_suStr, rtl_UriEncodeIgnoreEscapes);
83 : printf("\n");
84 : printf("# rtl_UriEncodeKeepEscapes\n");
85 : toUTF8_mech(_suStr, rtl_UriEncodeKeepEscapes);
86 : printf("\n");
87 : printf("# rtl_UriEncodeCheckEscapes\n");
88 : toUTF8_mech(_suStr, rtl_UriEncodeCheckEscapes);
89 : printf("\n");
90 : }
91 :
92 1 : void test_FromUTF8_001()
93 : {
94 : // string --> ustring
95 1 : rtl::OString sStrUTF8("h%C3%A4llo");
96 1 : rtl::OUString suStrUTF8 = rtl::OStringToOUString(sStrUTF8, RTL_TEXTENCODING_ASCII_US);
97 :
98 : // UTF8 --> real ustring
99 1 : rtl::OUString suStr_UriDecodeToIuri = rtl::Uri::decode(suStrUTF8, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
100 1 : showContent(suStr_UriDecodeToIuri);
101 :
102 : // string --> ustring
103 1 : rtl::OString sStr("h\xE4llo", strlen("h\xE4llo"));
104 1 : rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15);
105 :
106 1 : CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suString.equals(suStr_UriDecodeToIuri) == sal_True);
107 :
108 : // ustring --> ustring (UTF8)
109 1 : rtl::OUString suStr2 = rtl::Uri::encode(suStr_UriDecodeToIuri, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
110 1 : showContent(suStr2);
111 :
112 1 : CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suStr2.equals(suStrUTF8) == sal_True);
113 : // suStr should be equal to suStr2
114 1 : }
115 :
116 : // "%C3%84qypten";
117 : // testshl2 ../../../unxlngi4.pro/lib/libConvert.so "-onlyerrors"
118 : // # Type: 'Directory' file name '%E6%89%8B%E6%9C%BA%E5%8F%B7%E7%A0%81'
119 : // # Type: 'Directory' file name '%E6%9C%AA%E5%91%BD%E5%90%8Dzhgb18030'
120 : // # Type: 'Regular file' file name '%E5%B7%A5%E4%BD%9C'
121 : // # Type: 'Regular file' file name '%E4%BA%8C%E6%89%8B%E6%88%BF%E4%B9%B0%E5%8D%96%E5%90%88%E5%90%8C%E8%8D%89%E7%A8%BF.doc'
122 : // ls
123 : rtl::OString getFileTypeName(osl::FileStatus const& _aStatus)
124 : {
125 : rtl::OString sType;
126 : if (_aStatus.isValid(osl_FileStatus_Mask_Type))
127 : {
128 : osl::FileStatus::Type aType = _aStatus.getFileType();
129 : if (aType == osl::FileStatus::Directory)
130 : {
131 : sType = "Directory";
132 : }
133 : else if (aType == osl::FileStatus::Regular)
134 : {
135 : sType = "Regular file";
136 : }
137 : else if (aType == osl::FileStatus::Volume)
138 : {
139 : sType = "Volume";
140 : }
141 : else if (aType == osl::FileStatus::Fifo)
142 : {
143 : sType = "Fifo";
144 : }
145 : else if (aType == osl::FileStatus::Socket)
146 : {
147 : sType = "Socket";
148 : }
149 : else if (aType == osl::FileStatus::Link)
150 : {
151 : sType = "Link";
152 : }
153 : else if (aType == osl::FileStatus::Special)
154 : {
155 : sType = "Special";
156 : }
157 : else if (aType == osl::FileStatus::Unknown)
158 : {
159 : sType = "Unknown";
160 : }
161 : else
162 : {
163 : sType = "Not handled yet";
164 : }
165 : }
166 : else
167 : {
168 : sType = "ERROR: osl_FileStatus_Mask_Type not set for FileStatus!";
169 : }
170 : return sType;
171 : }
172 :
173 :
174 : void test_UTF8_files()
175 : {
176 : #ifdef UNX
177 : rtl::OUString suDirURL(rtl::OUString("file:///tmp/atestdir"));
178 : #else /* Windows */
179 : rtl::OUString suDirURL(rtl::OUString("file:///c:/temp/atestdir"));
180 : #endif
181 : osl::Directory aDir(suDirURL);
182 : aDir.open();
183 : if (aDir.isOpen())
184 : {
185 : osl::DirectoryItem aItem;
186 : osl::FileStatus aStatus(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes | osl_FileStatus_Mask_Type);
187 : while (aDir.getNextItem(aItem) == ::osl::FileBase::E_None)
188 : {
189 : if (osl::FileBase::E_None == aItem.getFileStatus(aStatus) &&
190 : aStatus.isValid(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes))
191 : {
192 : rtl::OString sType = getFileTypeName(aStatus);
193 :
194 : rtl::OUString suFilename = aStatus.getFileName();
195 : // rtl::OUString suFullFileURL;
196 :
197 : rtl::OUString suStrUTF8 = rtl::Uri::encode(suFilename, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
198 : rtl::OString sStrUTF8 = convertToOString(suStrUTF8);
199 : printf("Type: '%s' file name '%s'\n", sType.getStr(), sStrUTF8.getStr());
200 : }
201 : }
202 : aDir.close();
203 : }
204 : else
205 : {
206 : rtl::OString sStr;
207 : sStr = rtl::OUStringToOString(suDirURL, osl_getThreadTextEncoding());
208 : printf("can't open dir:'%s'\n", sStr.getStr());
209 : }
210 : }
211 :
212 : void test_FromUTF8()
213 : {
214 : rtl::OString sStr("h%C3%A4llo");
215 : rtl::OUString suStr = rtl::OStringToOUString(sStr, osl_getThreadTextEncoding());
216 :
217 : // rtl_UriEncodeIgnoreEscapes,
218 : // rtl_UriEncodeKeepEscapes,
219 : // rtl_UriEncodeCheckEscapes,
220 : // rtl::OUString suStr2 = rtl::Uri::encode(suStr, rtl_UriCharClassRegName, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8);
221 : rtl::OUString suStr_UriDecodeNone = rtl::Uri::decode(suStr, rtl_UriDecodeNone, RTL_TEXTENCODING_UTF8);
222 : showContent(suStr_UriDecodeNone);
223 : toUTF8(suStr_UriDecodeNone);
224 :
225 : rtl::OUString suStr_UriDecodeToIuri = rtl::Uri::decode(suStr, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
226 : showContent(suStr_UriDecodeToIuri);
227 : toUTF8(suStr_UriDecodeToIuri);
228 :
229 : rtl::OUString suStr_UriDecodeWithCharset = rtl::Uri::decode(suStr, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8);
230 : showContent(suStr_UriDecodeWithCharset);
231 : toUTF8(suStr_UriDecodeWithCharset);
232 : }
233 :
234 2 : CPPUNIT_TEST_SUITE( Convert );
235 1 : CPPUNIT_TEST( test_FromUTF8_001 );
236 : // CPPUNIT_TEST( test_UTF8_files );
237 : // CPPUNIT_TEST( test_FromUTF8 );
238 2 : CPPUNIT_TEST_SUITE_END( );
239 : };
240 :
241 : }
242 :
243 :
244 1 : CPPUNIT_TEST_SUITE_REGISTRATION( Stringtest::Convert );
245 :
246 : // LLA: doku anpassen!!!
247 :
248 4 : CPPUNIT_PLUGIN_IMPLEMENT();
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|