2010-12-09 12:31:50 +00:00
|
|
|
/* This file is (c) 2008-2011 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "fsencoding.hh"
|
2009-04-18 17:20:12 +00:00
|
|
|
#include "wstring_qt.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
#include <QString>
|
|
|
|
#include <QDir>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace FsEncoding {
|
|
|
|
|
|
|
|
string encode( wstring const & str )
|
|
|
|
{
|
2011-09-09 12:05:28 +00:00
|
|
|
#ifdef __WIN32
|
|
|
|
return string( gd::toQString( str ).toUtf8().data() );
|
|
|
|
#else
|
2009-04-18 17:20:12 +00:00
|
|
|
return string( gd::toQString( str ).toLocal8Bit().data() );
|
2011-09-09 12:05:28 +00:00
|
|
|
#endif
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string encode( string const & str )
|
|
|
|
{
|
2011-09-09 12:05:28 +00:00
|
|
|
#ifdef __WIN32
|
|
|
|
return string( str );
|
|
|
|
#else
|
2009-01-28 20:55:45 +00:00
|
|
|
return string( QString::fromUtf8( str.c_str() ).toLocal8Bit().data() );
|
2011-09-09 12:05:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
string encode( QString const & str )
|
|
|
|
{
|
|
|
|
#ifdef __WIN32
|
|
|
|
return string( str.toUtf8().data() );
|
|
|
|
#else
|
|
|
|
return string( str.toLocal8Bit().data() );
|
|
|
|
#endif
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:50:49 +00:00
|
|
|
wstring decode( string const & str )
|
|
|
|
{
|
2011-09-09 12:05:28 +00:00
|
|
|
#ifdef __WIN32
|
|
|
|
return gd::toWString( QString::fromUtf8( str.c_str() ) );
|
|
|
|
#else
|
2009-04-18 17:20:12 +00:00
|
|
|
return gd::toWString( QString::fromLocal8Bit( str.c_str() ) );
|
2011-09-09 12:05:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString decode( const char *str )
|
|
|
|
{
|
|
|
|
#ifdef __WIN32
|
|
|
|
return QString::fromUtf8( str );
|
|
|
|
#else
|
|
|
|
return QString::fromLocal8Bit( str );
|
|
|
|
#endif
|
2009-04-09 18:50:49 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
char separator()
|
|
|
|
{
|
|
|
|
return QDir::separator().toAscii();
|
|
|
|
}
|
|
|
|
|
|
|
|
string dirname( string const & str )
|
|
|
|
{
|
2009-02-02 15:01:48 +00:00
|
|
|
size_t x = str.rfind( separator() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-02-02 15:01:48 +00:00
|
|
|
if ( x == string::npos )
|
|
|
|
return string( "." );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-02-02 15:01:48 +00:00
|
|
|
return string( str, 0, x );
|
|
|
|
}
|
|
|
|
|
|
|
|
string basename( string const & str )
|
|
|
|
{
|
|
|
|
size_t x = str.rfind( separator() );
|
|
|
|
|
|
|
|
if ( x == string::npos )
|
|
|
|
return str;
|
|
|
|
|
|
|
|
return string( str, x + 1 );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|