1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 30*cdf0e10cSrcweir #include "precompiled_sal.hxx" 31*cdf0e10cSrcweir #include <stdlib.h> 32*cdf0e10cSrcweir #include <rtl/ustring.hxx> 33*cdf0e10cSrcweir #include <rtl/strbuf.hxx> 34*cdf0e10cSrcweir #include <rtl/uri.hxx> 35*cdf0e10cSrcweir #include <osl/thread.h> 36*cdf0e10cSrcweir #include <osl/file.hxx> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include <testshl/simpleheader.hxx> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir namespace Stringtest 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir rtl::OString toHex(unsigned char _c) 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir rtl::OStringBuffer sStrBuf; 47*cdf0e10cSrcweir static char cHex[] = "0123456789ABCDEF"; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir int nhigh = int(_c) >> 4 & 0xf; 50*cdf0e10cSrcweir int nlow = int(_c) & 0xf; 51*cdf0e10cSrcweir sStrBuf.append( cHex[nhigh] ); 52*cdf0e10cSrcweir sStrBuf.append( cHex[nlow] ); 53*cdf0e10cSrcweir return sStrBuf.makeStringAndClear(); 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir rtl::OString escapeString(rtl::OString const& _sStr) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir rtl::OStringBuffer sStrBuf; 59*cdf0e10cSrcweir sal_Int32 nLength = _sStr.getLength(); 60*cdf0e10cSrcweir for(int i=0;i<nLength;++i) 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir unsigned char c = (unsigned char)_sStr[i]; 63*cdf0e10cSrcweir if (c > 127) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir sStrBuf.append("%"); 66*cdf0e10cSrcweir sStrBuf.append(toHex(c)); 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir else 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir sStrBuf.append((char)c); 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir return sStrBuf.makeStringAndClear(); 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir class Convert : public CppUnit::TestFixture 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir rtl::OUString m_aStr; 81*cdf0e10cSrcweir public: 82*cdf0e10cSrcweir /* 83*cdf0e10cSrcweir rtl::OString toUTF8(rtl::OUString const& _suStr) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir rtl::OString sStrAsUTF8 = rtl::OUStringToOString(_suStr, RTL_TEXTENCODING_UTF8); 86*cdf0e10cSrcweir t_print("%s\n", escapeString(sStrAsUTF8).getStr()); 87*cdf0e10cSrcweir return sStrAsUTF8; 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir */ 90*cdf0e10cSrcweir rtl::OUString fromUTF8(rtl::OString const& _suStr) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir rtl::OUString suStr = rtl::OStringToOUString(_suStr, RTL_TEXTENCODING_UTF8); 93*cdf0e10cSrcweir return suStr; 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir rtl::OString convertToOString(rtl::OUString const& _suStr) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir return rtl::OUStringToOString(_suStr, osl_getThreadTextEncoding()/*RTL_TEXTENCODING_ASCII_US*/); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir void showContent(rtl::OUString const& _suStr) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir rtl::OString sStr = convertToOString(_suStr); 104*cdf0e10cSrcweir t_print("%s\n", sStr.getStr()); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir void toUTF8_mech(rtl::OUString const& _suStr, rtl_UriEncodeMechanism _eMechanism) 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir rtl::OUString suStr; 110*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassNone, _eMechanism, RTL_TEXTENCODING_UTF8); 111*cdf0e10cSrcweir showContent(suStr); 112*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUric, _eMechanism, RTL_TEXTENCODING_UTF8); 113*cdf0e10cSrcweir showContent(suStr); 114*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUricNoSlash, _eMechanism, RTL_TEXTENCODING_UTF8); 115*cdf0e10cSrcweir showContent(suStr); 116*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRelSegment, _eMechanism, RTL_TEXTENCODING_UTF8); 117*cdf0e10cSrcweir showContent(suStr); 118*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRegName, _eMechanism, RTL_TEXTENCODING_UTF8); 119*cdf0e10cSrcweir showContent(suStr); 120*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUserinfo, _eMechanism, RTL_TEXTENCODING_UTF8); 121*cdf0e10cSrcweir showContent(suStr); 122*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassPchar, _eMechanism, RTL_TEXTENCODING_UTF8); 123*cdf0e10cSrcweir showContent(suStr); 124*cdf0e10cSrcweir suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUnoParamValue, _eMechanism, RTL_TEXTENCODING_UTF8); 125*cdf0e10cSrcweir showContent(suStr); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir void toUTF8(rtl::OUString const& _suStr) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir t_print("rtl_UriEncodeIgnoreEscapes \n"); 131*cdf0e10cSrcweir toUTF8_mech(_suStr, rtl_UriEncodeIgnoreEscapes); 132*cdf0e10cSrcweir t_print("\n"); 133*cdf0e10cSrcweir t_print("# rtl_UriEncodeKeepEscapes\n"); 134*cdf0e10cSrcweir toUTF8_mech(_suStr, rtl_UriEncodeKeepEscapes); 135*cdf0e10cSrcweir t_print("\n"); 136*cdf0e10cSrcweir t_print("# rtl_UriEncodeCheckEscapes\n"); 137*cdf0e10cSrcweir toUTF8_mech(_suStr, rtl_UriEncodeCheckEscapes); 138*cdf0e10cSrcweir t_print("\n"); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir void test_FromUTF8_001() 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir // string --> ustring 144*cdf0e10cSrcweir rtl::OString sStrUTF8("h%C3%A4llo"); 145*cdf0e10cSrcweir rtl::OUString suStrUTF8 = rtl::OStringToOUString(sStrUTF8, RTL_TEXTENCODING_ASCII_US); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir // UTF8 --> real ustring 148*cdf0e10cSrcweir rtl::OUString suStr_UriDecodeToIuri = rtl::Uri::decode(suStrUTF8, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8); 149*cdf0e10cSrcweir showContent(suStr_UriDecodeToIuri); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir // string --> ustring 152*cdf0e10cSrcweir rtl::OString sStr("h\xE4llo"); 153*cdf0e10cSrcweir rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suString.equals(suStr_UriDecodeToIuri) == sal_True); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir // ustring --> ustring (UTF8) 158*cdf0e10cSrcweir rtl::OUString suStr2 = rtl::Uri::encode(suStr_UriDecodeToIuri, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8); 159*cdf0e10cSrcweir showContent(suStr2); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suStr2.equals(suStrUTF8) == sal_True); 162*cdf0e10cSrcweir // suStr should be equal to suStr2 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir // "%C3%84qypten"; 166*cdf0e10cSrcweir // testshl2 ../../../unxlngi4.pro/lib/libConvert.so "-onlyerrors" 167*cdf0e10cSrcweir // # Type: 'Directory' file name '%E6%89%8B%E6%9C%BA%E5%8F%B7%E7%A0%81' 168*cdf0e10cSrcweir // # Type: 'Directory' file name '%E6%9C%AA%E5%91%BD%E5%90%8Dzhgb18030' 169*cdf0e10cSrcweir // # Type: 'Regular file' file name '%E5%B7%A5%E4%BD%9C' 170*cdf0e10cSrcweir // # 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' 171*cdf0e10cSrcweir // ls 172*cdf0e10cSrcweir rtl::OString getFileTypeName(osl::FileStatus const& _aStatus) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir rtl::OString sType; 175*cdf0e10cSrcweir if (_aStatus.isValid(osl_FileStatus_Mask_Type)) 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir osl::FileStatus::Type aType = _aStatus.getFileType(); 178*cdf0e10cSrcweir if (aType == osl::FileStatus::Directory) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir sType = "Directory"; 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir else if (aType == osl::FileStatus::Regular) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir sType = "Regular file"; 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir else if (aType == osl::FileStatus::Volume) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir sType = "Volume"; 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir else if (aType == osl::FileStatus::Fifo) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir sType = "Fifo"; 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir else if (aType == osl::FileStatus::Socket) 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir sType = "Socket"; 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir else if (aType == osl::FileStatus::Link) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir sType = "Link"; 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir else if (aType == osl::FileStatus::Special) 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir sType = "Special"; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir else if (aType == osl::FileStatus::Unknown) 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir sType = "Unknown"; 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir else 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir sType = "Not handled yet"; 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir else 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir sType = "ERROR: osl_FileStatus_Mask_Type not set for FileStatus!"; 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir return sType; 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir void test_UTF8_files() 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir #ifdef UNX 226*cdf0e10cSrcweir rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///tmp/atestdir")); 227*cdf0e10cSrcweir #else /* Windows */ 228*cdf0e10cSrcweir rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/atestdir")); 229*cdf0e10cSrcweir #endif 230*cdf0e10cSrcweir osl::Directory aDir(suDirURL); 231*cdf0e10cSrcweir aDir.open(); 232*cdf0e10cSrcweir if (aDir.isOpen()) 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir osl::DirectoryItem aItem; 235*cdf0e10cSrcweir osl::FileStatus aStatus(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes | osl_FileStatus_Mask_Type); 236*cdf0e10cSrcweir while (aDir.getNextItem(aItem) == ::osl::FileBase::E_None) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir if (osl::FileBase::E_None == aItem.getFileStatus(aStatus) && 239*cdf0e10cSrcweir aStatus.isValid(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes)) 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir rtl::OString sType = getFileTypeName(aStatus); 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir rtl::OUString suFilename = aStatus.getFileName(); 244*cdf0e10cSrcweir // rtl::OUString suFullFileURL; 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir rtl::OUString suStrUTF8 = rtl::Uri::encode(suFilename, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8); 247*cdf0e10cSrcweir rtl::OString sStrUTF8 = convertToOString(suStrUTF8); 248*cdf0e10cSrcweir t_print("Type: '%s' file name '%s'\n", sType.getStr(), sStrUTF8.getStr()); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir aDir.close(); 252*cdf0e10cSrcweir } 253*cdf0e10cSrcweir else 254*cdf0e10cSrcweir { 255*cdf0e10cSrcweir rtl::OString sStr; 256*cdf0e10cSrcweir sStr = rtl::OUStringToOString(suDirURL, osl_getThreadTextEncoding()); 257*cdf0e10cSrcweir t_print("can't open dir:'%s'\n", sStr.getStr()); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir void test_FromUTF8() 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir rtl::OString sStr("h%C3%A4llo"); 264*cdf0e10cSrcweir rtl::OUString suStr = rtl::OStringToOUString(sStr, osl_getThreadTextEncoding()); 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir // rtl_UriEncodeIgnoreEscapes, 267*cdf0e10cSrcweir // rtl_UriEncodeKeepEscapes, 268*cdf0e10cSrcweir // rtl_UriEncodeCheckEscapes, 269*cdf0e10cSrcweir // rtl::OUString suStr2 = rtl::Uri::encode(suStr, rtl_UriCharClassRegName, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8); 270*cdf0e10cSrcweir rtl::OUString suStr_UriDecodeNone = rtl::Uri::decode(suStr, rtl_UriDecodeNone, RTL_TEXTENCODING_UTF8); 271*cdf0e10cSrcweir showContent(suStr_UriDecodeNone); 272*cdf0e10cSrcweir toUTF8(suStr_UriDecodeNone); 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir rtl::OUString suStr_UriDecodeToIuri = rtl::Uri::decode(suStr, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8); 275*cdf0e10cSrcweir showContent(suStr_UriDecodeToIuri); 276*cdf0e10cSrcweir toUTF8(suStr_UriDecodeToIuri); 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir rtl::OUString suStr_UriDecodeWithCharset = rtl::Uri::decode(suStr, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8); 279*cdf0e10cSrcweir showContent(suStr_UriDecodeWithCharset); 280*cdf0e10cSrcweir toUTF8(suStr_UriDecodeWithCharset); 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( Convert ); 284*cdf0e10cSrcweir CPPUNIT_TEST( test_FromUTF8_001 ); 285*cdf0e10cSrcweir // CPPUNIT_TEST( test_UTF8_files ); 286*cdf0e10cSrcweir // CPPUNIT_TEST( test_FromUTF8 ); 287*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 288*cdf0e10cSrcweir }; 289*cdf0e10cSrcweir 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Stringtest::Convert, "Stringtest" ); 294*cdf0e10cSrcweir 295*cdf0e10cSrcweir // LLA: doku anpassen!!! 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir NOADDITIONAL; 298