From 49b753f60330fe78a106ec55d8b99d0372464641 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Thu, 21 Dec 2006 13:01:47 +0000 Subject: [PATCH] Fix another instance of filename encoding problems * src/support/filetools.h (fileSearch): change return value type to vector * src/support/filetools.C (dirList): Convert filenames from the file system encoding * src/converter.C (Converters::move): Adjust to dirList interface change * src/support/filename.[Ch] (FileName::fromFilesystemEncoding): New static method * src/client/client.C: Add comments about filename encoding git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16362 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/client/client.C | 8 ++++++-- src/converter.C | 23 +++++++++++++---------- src/support/filename.C | 7 +++++++ src/support/filename.h | 7 +++++++ src/support/filetools.C | 11 +++++------ src/support/filetools.h | 2 +- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/client/client.C b/src/client/client.C index 5b116d4103..b4dc9ab435 100644 --- a/src/client/client.C +++ b/src/client/client.C @@ -85,8 +85,9 @@ string itoa(unsigned int i) } -// Parts stolen from lyx::support::DirList() -// Returns the absolute pathnames of all lyx local sockets +/// Returns the absolute pathnames of all lyx local sockets in +/// file system encoding. +/// Parts stolen from lyx::support::DirList(). vector lyxSockets(string const & dir, string const & pid) { vector dirlist; @@ -118,6 +119,8 @@ vector lyxSockets(string const & dir, string const & pid) namespace socktools { +/// Connect to the socket \p name. +/// Caution: \p name is in filesystem encoding int connect(string const & name) { int fd; // File descriptor for the socket @@ -551,6 +554,7 @@ int main(int argc, char * argv[]) vector::const_iterator addr = addrs.begin(); vector::const_iterator end = addrs.end(); for (; addr != end; ++addr) { + // Caution: addr->string() is in filesystem encoding server.reset(new LyXDataSocket(addr->string())); if (server->connected()) break; diff --git a/src/converter.C b/src/converter.C index 7c4ad3c3d0..37edc5dde3 100644 --- a/src/converter.C +++ b/src/converter.C @@ -512,30 +512,33 @@ bool Converters::move(string const & fmt, string const to_base = removeExtension(to.absFilename()); string const to_extension = getExtension(to.absFilename()); - vector const files = dirList(FileName(path), + vector const files = dirList(FileName(path), getExtension(from.absFilename())); - for (vector::const_iterator it = files.begin(); - it != files.end(); ++it) - if (prefixIs(*it, base)) { - string const from2 = path + *it; - string to2 = to_base + it->substr(base.length()); - to2 = changeExtension(to2, to_extension); + for (vector::const_iterator it = files.begin(); + it != files.end(); ++it) { + string const from2 = it->absFilename(); + string const file2 = onlyFilename(from2); + if (prefixIs(file2, base)) { + string const to2 = changeExtension( + to_base + file2.substr(base.length()), + to_extension); lyxerr[Debug::FILES] << "moving " << from2 << " to " << to2 << endl; Mover const & mover = movers(fmt); bool const moved = copy - ? mover.copy(FileName(from2), FileName(to2)) - : mover.rename(FileName(from2), FileName(to2)); + ? mover.copy(*it, FileName(to2)) + : mover.rename(*it, FileName(to2)); if (!moved && no_errors) { Alert::error(_("Cannot convert file"), bformat(copy ? _("Could not copy a temporary file from %1$s to %2$s.") : _("Could not move a temporary file from %1$s to %2$s."), - from_ascii(from2), from_ascii(to2))); + from_utf8(from2), from_utf8(to2))); no_errors = false; } } + } return no_errors; } diff --git a/src/support/filename.C b/src/support/filename.C index 8b8a4aee57..560aaf576e 100644 --- a/src/support/filename.C +++ b/src/support/filename.C @@ -70,6 +70,13 @@ string const FileName::toFilesystemEncoding() const } +FileName const FileName::fromFilesystemEncoding(string const & name) +{ + QByteArray const encoded(name.c_str(), name.length()); + return FileName(fromqstr(QFile::decodeName(encoded))); +} + + bool operator==(FileName const & lhs, FileName const & rhs) { return lhs.absFilename() == rhs.absFilename(); diff --git a/src/support/filename.h b/src/support/filename.h index 8683e2dda2..71b422b375 100644 --- a/src/support/filename.h +++ b/src/support/filename.h @@ -46,6 +46,13 @@ public: * Only use this for accessing the file, e.g. with an fstream. */ std::string const toFilesystemEncoding() const; + /** + * Get a FileName from \p name in the encoding used by the file system. + * Only use this for filenames you got directly from the file system, + * e.g. from reading a directory. + * \p name must have an absolute path. + */ + static FileName const fromFilesystemEncoding(std::string const & name); protected: /// The absolute file name. /// The encoding is currently unspecified, anything else than ASCII diff --git a/src/support/filetools.C b/src/support/filetools.C index 8ba5c0f595..105a45d560 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -218,10 +218,10 @@ FileName const fileOpenSearch(string const & path, string const & name, /// Returns a vector of all files in directory dir having extension ext. -vector const dirList(FileName const & dir, string const & ext) +vector const dirList(FileName const & dir, string const & ext) { // EXCEPTIONS FIXME. Rewrite needed when we turn on exceptions. (Lgb) - vector dirlist; + vector dirlist; string const encoded_dir = dir.toFilesystemEncoding(); if (!(fs::exists(encoded_dir) && fs::is_directory(encoded_dir))) { @@ -240,10 +240,9 @@ vector const dirList(FileName const & dir, string const & ext) fs::directory_iterator end; for (; dit != end; ++dit) { string const & fil = dit->leaf(); - if (suffixIs(fil, extension)) { - // FIXME UNICODE: We need to convert from filesystem encoding to utf8 - dirlist.push_back(fil); - } + if (suffixIs(fil, extension)) + dirlist.push_back(FileName::fromFilesystemEncoding( + makeAbsPath(fil, encoded_dir))); } return dirlist; } diff --git a/src/support/filetools.h b/src/support/filetools.h index aadb39c4b6..86abc47b4a 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -73,7 +73,7 @@ FileName const fileSearch(std::string const & path, search_mode mode = standard_mode); /// Returns a vector of all files in directory dir having extension ext. -std::vector const dirList(FileName const & dir, +std::vector const dirList(FileName const & dir, std::string const & ext = std::string()); /** Is directory read only? -- 2.39.2