1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <rtl/strbuf.hxx>
23 #include <rtl/uri.hxx>
24 #include <osl/thread.h>
25 #include <osl/file.hxx>
27 #include <cppunit/TestFixture.h>
28 #include <cppunit/extensions/HelperMacros.h>
29 #include <cppunit/plugin/TestPlugIn.h>
31 // -----------------------------------------------------------------------------
35 // -----------------------------------------------------------------------------
37 class Convert
: public CppUnit::TestFixture
41 rtl::OUString
fromUTF8(rtl::OString
const& _suStr
)
43 rtl::OUString suStr
= rtl::OStringToOUString(_suStr
, RTL_TEXTENCODING_UTF8
);
47 rtl::OString
convertToOString(rtl::OUString
const& _suStr
)
49 return rtl::OUStringToOString(_suStr
, osl_getThreadTextEncoding()/*RTL_TEXTENCODING_ASCII_US*/);
52 void showContent(rtl::OUString
const& _suStr
)
54 rtl::OString sStr
= convertToOString(_suStr
);
55 printf("%s\n", sStr
.getStr());
58 void toUTF8_mech(rtl::OUString
const& _suStr
, rtl_UriEncodeMechanism _eMechanism
)
61 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassNone
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
63 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassUric
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
65 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassUricNoSlash
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
67 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassRelSegment
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
69 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassRegName
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
71 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassUserinfo
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
73 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassPchar
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
75 suStr
= rtl::Uri::encode(_suStr
, rtl_UriCharClassUnoParamValue
, _eMechanism
, RTL_TEXTENCODING_UTF8
);
79 void toUTF8(rtl::OUString
const& _suStr
)
81 printf("rtl_UriEncodeIgnoreEscapes \n");
82 toUTF8_mech(_suStr
, rtl_UriEncodeIgnoreEscapes
);
84 printf("# rtl_UriEncodeKeepEscapes\n");
85 toUTF8_mech(_suStr
, rtl_UriEncodeKeepEscapes
);
87 printf("# rtl_UriEncodeCheckEscapes\n");
88 toUTF8_mech(_suStr
, rtl_UriEncodeCheckEscapes
);
92 void test_FromUTF8_001()
95 rtl::OString
sStrUTF8("h%C3%A4llo");
96 rtl::OUString suStrUTF8
= rtl::OStringToOUString(sStrUTF8
, RTL_TEXTENCODING_ASCII_US
);
98 // UTF8 --> real ustring
99 rtl::OUString suStr_UriDecodeToIuri
= rtl::Uri::decode(suStrUTF8
, rtl_UriDecodeToIuri
, RTL_TEXTENCODING_UTF8
);
100 showContent(suStr_UriDecodeToIuri
);
102 // string --> ustring
103 rtl::OString
sStr("h\xE4llo", strlen("h\xE4llo"));
104 rtl::OUString suString
= rtl::OStringToOUString(sStr
, RTL_TEXTENCODING_ISO_8859_15
);
106 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suString
.equals(suStr_UriDecodeToIuri
) == sal_True
);
108 // ustring --> ustring (UTF8)
109 rtl::OUString suStr2
= rtl::Uri::encode(suStr_UriDecodeToIuri
, rtl_UriCharClassUnoParamValue
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
112 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suStr2
.equals(suStrUTF8
) == sal_True
);
113 // suStr should be equal to suStr2
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'
123 rtl::OString
getFileTypeName(osl::FileStatus
const& _aStatus
)
126 if (_aStatus
.isValid(osl_FileStatus_Mask_Type
))
128 osl::FileStatus::Type aType
= _aStatus
.getFileType();
129 if (aType
== osl::FileStatus::Directory
)
133 else if (aType
== osl::FileStatus::Regular
)
135 sType
= "Regular file";
137 else if (aType
== osl::FileStatus::Volume
)
141 else if (aType
== osl::FileStatus::Fifo
)
145 else if (aType
== osl::FileStatus::Socket
)
149 else if (aType
== osl::FileStatus::Link
)
153 else if (aType
== osl::FileStatus::Special
)
157 else if (aType
== osl::FileStatus::Unknown
)
163 sType
= "Not handled yet";
168 sType
= "ERROR: osl_FileStatus_Mask_Type not set for FileStatus!";
174 void test_UTF8_files()
177 rtl::OUString
suDirURL(rtl::OUString("file:///tmp/atestdir"));
179 rtl::OUString
suDirURL(rtl::OUString("file:///c:/temp/atestdir"));
181 osl::Directory
aDir(suDirURL
);
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
)
189 if (osl::FileBase::E_None
== aItem
.getFileStatus(aStatus
) &&
190 aStatus
.isValid(osl_FileStatus_Mask_FileName
| osl_FileStatus_Mask_Attributes
))
192 rtl::OString sType
= getFileTypeName(aStatus
);
194 rtl::OUString suFilename
= aStatus
.getFileName();
195 // rtl::OUString suFullFileURL;
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());
207 sStr
= rtl::OUStringToOString(suDirURL
, osl_getThreadTextEncoding());
208 printf("can't open dir:'%s'\n", sStr
.getStr());
214 rtl::OString
sStr("h%C3%A4llo");
215 rtl::OUString suStr
= rtl::OStringToOUString(sStr
, osl_getThreadTextEncoding());
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
);
225 rtl::OUString suStr_UriDecodeToIuri
= rtl::Uri::decode(suStr
, rtl_UriDecodeToIuri
, RTL_TEXTENCODING_UTF8
);
226 showContent(suStr_UriDecodeToIuri
);
227 toUTF8(suStr_UriDecodeToIuri
);
229 rtl::OUString suStr_UriDecodeWithCharset
= rtl::Uri::decode(suStr
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
230 showContent(suStr_UriDecodeWithCharset
);
231 toUTF8(suStr_UriDecodeWithCharset
);
234 CPPUNIT_TEST_SUITE( Convert
);
235 CPPUNIT_TEST( test_FromUTF8_001
);
236 // CPPUNIT_TEST( test_UTF8_files );
237 // CPPUNIT_TEST( test_FromUTF8 );
238 CPPUNIT_TEST_SUITE_END( );
244 CPPUNIT_TEST_SUITE_REGISTRATION( Stringtest::Convert
);
246 // LLA: doku anpassen!!!
248 CPPUNIT_PLUGIN_IMPLEMENT();
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */