Skip to content

Commit

Permalink
Use file names in UTF-8. Fix issue #30.
Browse files Browse the repository at this point in the history
  • Loading branch information
Abs62 committed Sep 9, 2011
1 parent 8b55121 commit 2763b74
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 33 deletions.
5 changes: 3 additions & 2 deletions bgl_babylon.cc
Expand Up @@ -29,6 +29,7 @@
#include<iconv.h>
#include <QTextDocument>
#include "dprintf.hh"
#include "ufile.hh"

#ifdef _WIN32
#include <io.h>
Expand Down Expand Up @@ -58,7 +59,7 @@ bool Babylon::open()
unsigned char buf[6];
int i;

f = fopen( m_filename.c_str(), "rb" );
f = gd_fopen( m_filename.c_str(), "rb" );
if( f == NULL )
return false;

Expand Down Expand Up @@ -325,7 +326,7 @@ bgl_entry Babylon::readEntry( ResourceHandler * resourceHandler )
block.data + pos,
block.length - pos );
#if 0
FILE *ifile = fopen(filename.c_str(), "w");
FILE *ifile = gd_fopen(filename.c_str(), "w");
fwrite(block.data + pos, 1, block.length -pos, ifile);
fclose(ifile);
#endif
Expand Down
9 changes: 5 additions & 4 deletions dictionary.cc
Expand Up @@ -17,6 +17,7 @@
#include <QFileInfo>
#include <QCryptographicHash>
#include <QDateTime>
#include "fsencoding.hh"

namespace Dictionary {

Expand Down Expand Up @@ -163,10 +164,10 @@ string makeDictionaryId( vector< string > const & dictionaryFiles ) throw()
{
string const & full( dictionaryFiles[ x ] );

QFileInfo fileInfo( QString::fromLocal8Bit( full.c_str() ) );
QFileInfo fileInfo( FsEncoding::decode( full.c_str() ) );

if ( fileInfo.isAbsolute() )
sortedList.push_back( dictionariesDir.relativeFilePath( fileInfo.filePath() ).toLocal8Bit().data() );
sortedList.push_back( FsEncoding::encode( dictionariesDir.relativeFilePath( fileInfo.filePath() ) ) );
else
{
// Well, it's relative. We don't technically support those, but
Expand Down Expand Up @@ -201,7 +202,7 @@ bool needToRebuildIndex( vector< string > const & dictionaryFiles,
for( std::vector< string >::const_iterator i = dictionaryFiles.begin();
i != dictionaryFiles.end(); ++i )
{
QFileInfo fileInfo( QString::fromLocal8Bit( i->c_str() ) );
QFileInfo fileInfo( FsEncoding::decode( i->c_str() ) );

if ( !fileInfo.exists() )
return true;
Expand All @@ -212,7 +213,7 @@ bool needToRebuildIndex( vector< string > const & dictionaryFiles,
lastModified = ts;
}

QFileInfo fileInfo( QString::fromLocal8Bit( indexFile.c_str() ) );
QFileInfo fileInfo( FsEncoding::decode( indexFile.c_str() ) );

if ( !fileInfo.exists() )
return true;
Expand Down
6 changes: 4 additions & 2 deletions dictzip.c
Expand Up @@ -31,6 +31,8 @@
#include <stdlib.h>
#include <string.h>

#include "ufile.hh"

#define BUFFERSIZE 10240

#define OUT_BUFFER_SIZE 0xffffL
Expand Down Expand Up @@ -270,7 +272,7 @@ static int dict_read_header( const char *filename,
int count;
unsigned long offset;

if (!(str = fopen( filename, "rb" )))
if (!(str = gd_fopen( filename, "rb" )))
err_fatal_errno( __func__,
"Cannot open data file \"%s\" for read\n", filename );

Expand Down Expand Up @@ -444,7 +446,7 @@ dictData *dict_data_open( const char *filename, int computeCRC )
"\"%s\" not in text or dzip format\n", filename );*/
}

h->fd = fopen( filename, "rb" );
h->fd = gd_fopen( filename, "rb" );

if ( !h->fd )
{
Expand Down
6 changes: 3 additions & 3 deletions dsl.cc
Expand Up @@ -375,7 +375,7 @@ void DslDictionary::doDeferredInit()
idx, idxMutex );

QString zipName = QDir::fromNativeSeparators(
QFile::decodeName( getDictionaryFilenames().back().c_str() ) );
FsEncoding::decode( getDictionaryFilenames().back().c_str() ) );

if ( zipName.endsWith( ".zip", Qt::CaseInsensitive ) ) // Sanity check
resourceZip.openZipFile( zipName );
Expand Down Expand Up @@ -416,7 +416,7 @@ void DslDictionary::loadIcon()
return;

QString fileName =
QDir::fromNativeSeparators( QString::fromLocal8Bit( getDictionaryFilenames()[ 0 ].c_str() ) );
QDir::fromNativeSeparators( FsEncoding::decode( getDictionaryFilenames()[ 0 ].c_str() ) );

// Remove the extension

Expand Down Expand Up @@ -1650,7 +1650,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
idxHeader.hasZipFile = 1;

QFile zipFile( QDir::fromNativeSeparators(
QFile::decodeName( zipFileName.c_str() ) ) );
FsEncoding::decode( zipFileName.c_str() ) ) );

if ( !zipFile.open( QFile::ReadOnly ) )
throw exCantReadFile( zipFileName );
Expand Down
14 changes: 13 additions & 1 deletion dsl_details.cc
Expand Up @@ -7,6 +7,7 @@
#include <wctype.h>
#include <stdio.h>
#include "dprintf.hh"
#include "ufile.hh"

namespace Dsl {
namespace Details {
Expand Down Expand Up @@ -499,10 +500,21 @@ DslScanner::DslScanner( string const & fileName ) throw( Ex, Iconv::Ex ):
{
// Since .dz is backwards-compatible with .gz, we use gz- functions to
// read it -- they are much nicer than the dict_data- ones.
#ifdef __WIN32
int id = gd_open( fileName.c_str() );
if( id == -1 )
throw exCantOpen( fileName );
f = gzdopen( id, "rb");
if ( !f )
{
_close( id );
throw exCantOpen( fileName );
}
#else
f = gzopen( fileName.c_str(), "rb");

if ( !f )
throw exCantOpen( fileName );
#endif

// Now try guessing the encoding by reading the first two bytes

Expand Down
12 changes: 10 additions & 2 deletions file.cc
Expand Up @@ -10,6 +10,12 @@
#include <sys/stat.h>
#include <unistd.h>

#ifdef __WIN32
#include <windows.h>
#endif

#include "ufile.hh"

namespace File {

enum
Expand All @@ -23,7 +29,9 @@ bool exists( char const * filename ) throw()
{
#ifdef __WIN32
struct _stat buf;
return _stat( filename, &buf ) == 0;
wchar_t wname[16384];
MultiByteToWideChar( CP_UTF8, 0, filename, -1, wname, 16384 );
return _wstat( wname, &buf ) == 0;
#else
struct stat buf;

Expand All @@ -34,7 +42,7 @@ bool exists( char const * filename ) throw()

void Class::open( char const * filename, char const * mode ) throw( exCantOpen )
{
f = fopen( filename, mode );
f = gd_fopen( filename, mode );

if ( !f )
throw exCantOpen( std::string( filename ) + ": " + strerror( errno ) );
Expand Down
30 changes: 30 additions & 0 deletions fsencoding.cc
Expand Up @@ -11,17 +11,47 @@ namespace FsEncoding {

string encode( wstring const & str )
{
#ifdef __WIN32
return string( gd::toQString( str ).toUtf8().data() );
#else
return string( gd::toQString( str ).toLocal8Bit().data() );
#endif
}

string encode( string const & str )
{
#ifdef __WIN32
return string( str );
#else
return string( QString::fromUtf8( str.c_str() ).toLocal8Bit().data() );
#endif
}

string encode( QString const & str )
{
#ifdef __WIN32
return string( str.toUtf8().data() );
#else
return string( str.toLocal8Bit().data() );
#endif
}

wstring decode( string const & str )
{
#ifdef __WIN32
return gd::toWString( QString::fromUtf8( str.c_str() ) );
#else
return gd::toWString( QString::fromLocal8Bit( str.c_str() ) );
#endif
}

QString decode( const char *str )
{
#ifdef __WIN32
return QString::fromUtf8( str );
#else
return QString::fromLocal8Bit( str );
#endif
}

char separator()
Expand Down
11 changes: 9 additions & 2 deletions fsencoding.hh
Expand Up @@ -5,6 +5,7 @@
#define __FSENCODING_HH_INCLUDED__

#include "wstring.hh"
#include <QString>

/// Utilities to convert a wide string or an utf8 string to the local 8bit
/// encoding of the file system, and to do other manipulations on the file
Expand All @@ -14,15 +15,21 @@ namespace FsEncoding {
using std::string;
using gd::wstring;

/// Encodes the given wide string to the system 8bit encoding.
/// Encodes the given wide string to the utf8 encoding.
string encode( wstring const & );

/// Encodes the given string in utf8 to the system 8bit encoding.
string encode( string const & );

/// Decodes the given 8bit-encoded string to a wide string.
/// Encodes the QString to the utf8/local 8-bit encoding.
string encode( QString const & );

/// Decodes the given utf8-encoded string to a wide string.
wstring decode( string const & str );

/// Decodes the given utf8/local 8-bit string to a QString.
QString decode( const char *str );

/// Returns the filesystem separator (/ on Unix and clones, \ on Windows).
char separator();

Expand Down
10 changes: 8 additions & 2 deletions goldendict.pro
Expand Up @@ -188,7 +188,8 @@ HEADERS += folding.hh \
maintabwidget.hh \
dprintf.hh \
mainstatusbar.hh \
gdappstyle.hh
gdappstyle.hh \
ufile.hh
FORMS += groups.ui \
dictgroupwidget.ui \
mainwindow.ui \
Expand Down Expand Up @@ -275,7 +276,8 @@ SOURCES += folding.cc \
parsecmdline.cc \
maintabwidget.cc \
mainstatusbar.cc \
gdappstyle.cc
gdappstyle.cc \
ufile.cc
win32 {
SOURCES += mouseover_win32/ThTypes.c \
wordbyauto.cc \
Expand Down Expand Up @@ -330,3 +332,7 @@ TS_OUT ~= s/.ts/.qm/g
PRE_TARGETDEPS += $$TS_OUT

include( qtsingleapplication/src/qtsingleapplication.pri )




3 changes: 2 additions & 1 deletion groups_widgets.cc
Expand Up @@ -7,6 +7,7 @@
#include "config.hh"
#include "langcoder.hh"
#include "language.hh"
#include "fsencoding.hh"

//#include "initializing.hh"

Expand Down Expand Up @@ -191,7 +192,7 @@ QVariant DictListModel::data( QModelIndex const & index, int role ) const
if ( dirs.size() )
{
tt += "<hr>";
tt += QString::fromLocal8Bit( dirs.at( 0 ).c_str() );
tt += FsEncoding::decode( dirs.at( 0 ).c_str() );
}

tt.replace( " ", "&nbsp;" );
Expand Down
5 changes: 3 additions & 2 deletions hunspell.cc
Expand Up @@ -18,6 +18,7 @@
#include <set>
#include <hunspell/hunspell.hxx>
#include "dprintf.hh"
#include "fsencoding.hh"

namespace HunspellMorpho {

Expand Down Expand Up @@ -673,9 +674,9 @@ vector< sptr< Dictionary::Class > > makeDictionaries( Config::Hunspell const & c
vector< string > dictFiles;

dictFiles.push_back(
QDir::toNativeSeparators( dataFiles[ d ].affFileName ).toLocal8Bit().data() );
FsEncoding::encode( QDir::toNativeSeparators( dataFiles[ d ].affFileName ) ) );
dictFiles.push_back(
QDir::toNativeSeparators( dataFiles[ d ].dicFileName ).toLocal8Bit().data() );
FsEncoding::encode( QDir::toNativeSeparators( dataFiles[ d ].dicFileName ) ) );

result.push_back(
new HunspellDictionary( Dictionary::makeDictionaryId( dictFiles ),
Expand Down

0 comments on commit 2763b74

Please sign in to comment.