From 95579c2a71cfff99e4bc01c9193dc3f7d4e3fda8 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Tue, 5 Dec 2006 12:57:01 +0000 Subject: [PATCH] more unicode filenames (+ fix one possible initialization order bug in needsUpdate() in src/lyx_main.C). toFilesystemEncoding() is now used in all places where it is needed if I did not forget anything. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16184 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ConverterCache.C | 2 +- src/buffer.C | 2 +- src/converter.C | 4 ++- src/frontends/controllers/ControlGraphics.C | 9 ++++--- src/lyx_cb.C | 9 ++++--- src/lyx_main.C | 28 ++++++++++++++------- src/lyxfunc.C | 9 ++++--- src/support/filetools.C | 12 +++++---- src/support/filetools.h | 4 +-- src/tex2lyx/text.C | 10 ++++---- 10 files changed, 53 insertions(+), 36 deletions(-) diff --git a/src/ConverterCache.C b/src/ConverterCache.C index 9fca48e21d..b5e0c12d8c 100644 --- a/src/ConverterCache.C +++ b/src/ConverterCache.C @@ -115,7 +115,7 @@ void ConverterCache::Impl::readIndex() CacheItem item(orig_from_name, to_format, timestamp, checksum); // Don't cache files that do not exist anymore - if (!fs::exists(orig_from)) { + if (!fs::exists(orig_from_name.toFilesystemEncoding())) { lyxerr[Debug::FILES] << "Not caching file `" << orig_from << "' (does not exist anymore)." << std::endl; diff --git a/src/buffer.C b/src/buffer.C index e3e2ea27e5..a6459d1ffd 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -229,7 +229,7 @@ Buffer::~Buffer() closing(); - if (!temppath().empty() && !destroyDir(temppath())) { + if (!temppath().empty() && !destroyDir(FileName(temppath()))) { Alert::warning(_("Could not remove temporary directory"), bformat(_("Could not remove the temporary directory %1$s"), from_utf8(temppath()))); diff --git a/src/converter.C b/src/converter.C index ce69aa7c0c..a7a061f0fe 100644 --- a/src/converter.C +++ b/src/converter.C @@ -511,7 +511,9 @@ bool Converters::move(string const & fmt, string const to_base = changeExtension(to.absFilename(), string()); string const to_extension = getExtension(to.absFilename()); - vector files = dirList(onlyPath(from.absFilename()), getExtension(from.absFilename())); + vector const files = dirList( + FileName(onlyPath(from.absFilename())), + getExtension(from.absFilename())); for (vector::const_iterator it = files.begin(); it != files.end(); ++it) if (prefixIs(*it, base)) { diff --git a/src/frontends/controllers/ControlGraphics.C b/src/frontends/controllers/ControlGraphics.C index 6a642bbde5..a1b90cd20b 100644 --- a/src/frontends/controllers/ControlGraphics.C +++ b/src/frontends/controllers/ControlGraphics.C @@ -88,13 +88,14 @@ docstring const ControlGraphics::browse(docstring const & in_name) const // Does user clipart directory exist? string clipdir = addName(package().user_support(), "clipart"); - if (!(fs::exists(clipdir) && fs::is_directory(clipdir))) + string const encoded_clipdir = FileName(clipdir).toFilesystemEncoding(); + if (!(fs::exists(encoded_clipdir) && fs::is_directory(encoded_clipdir))) // No - bail out to system clipart directory clipdir = addName(package().system_support(), "clipart"); - pair dir1(_("Clipart|#C#c"), lyx::from_utf8(clipdir)); - pair dir2(_("Documents|#o#O"), lyx::from_utf8(lyxrc.document_path)); + pair dir1(_("Clipart|#C#c"), from_utf8(clipdir)); + pair dir2(_("Documents|#o#O"), from_utf8(lyxrc.document_path)); // Show the file browser dialog - return browseRelFile(in_name, lyx::from_utf8(kernel().bufferFilepath()), + return browseRelFile(in_name, from_utf8(kernel().bufferFilepath()), title, FileFilterList(), false, dir1, dir2); diff --git a/src/lyx_cb.C b/src/lyx_cb.C index 5c307126bf..30f53dee5e 100644 --- a/src/lyx_cb.C +++ b/src/lyx_cb.C @@ -123,12 +123,12 @@ bool menuWrite(Buffer * buffer) -bool writeAs(Buffer * buffer, string const & filename) +bool writeAs(Buffer * buffer, string const & newname) { string fname = buffer->fileName(); string const oldname = fname; - if (filename.empty()) { + if (newname.empty()) { // FIXME UNICODE FileDialog fileDlg(_("Choose a filename to save document as"), @@ -159,9 +159,10 @@ bool writeAs(Buffer * buffer, string const & filename) if (!isLyXFilename(fname)) fname += ".lyx"; } else - fname = filename; + fname = newname; - if (fs::exists(fname)) { + FileName const filename(fname); + if (fs::exists(filename.toFilesystemEncoding())) { docstring const file = makeDisplayPath(fname, 30); docstring text = bformat(_("The document %1$s already exists.\n\n" "Do you want to over-write that document?"), file); diff --git a/src/lyx_main.C b/src/lyx_main.C index fbb4bd7164..f7d0c98eab 100644 --- a/src/lyx_main.C +++ b/src/lyx_main.C @@ -426,7 +426,7 @@ void LyX::prepareExit() return; } - if (!destroyDir(package().temp_dir())) { + if (!destroyDir(FileName(package().temp_dir()))) { docstring const msg = bformat(_("Unable to remove the temporary directory %1$s"), from_utf8(package().temp_dir())); @@ -841,8 +841,9 @@ bool LyX::init() if (!lyxrc.path_prefix.empty()) prependEnvPath("PATH", lyxrc.path_prefix); - if (fs::exists(lyxrc.document_path) && - fs::is_directory(lyxrc.document_path)) + FileName const document_path(lyxrc.document_path); + if (fs::exists(document_path.toFilesystemEncoding()) && + fs::is_directory(document_path.toFilesystemEncoding())) package().document_dir() = lyxrc.document_path; package().temp_dir() = createLyXTmpDir(FileName(lyxrc.tempdir_path)).absFilename(); @@ -975,11 +976,19 @@ namespace { // return true if file does not exist or is older than configure.py. bool needsUpdate(string const & file) { - static string const configure_script = - addName(package().system_support(), "configure.py"); - string const absfile = - addName(package().user_support(), file); + // We cannot initialize configure_script directly because the package + // is not initialized yet when static objects are constructed. + static string configure_script; + static bool firstrun = true; + if (firstrun) { + configure_script = FileName(addName( + package().system_support(), + "configure.py")).toFilesystemEncoding(); + firstrun = false; + } + string const absfile = FileName(addName( + package().user_support(), file)).toFilesystemEncoding(); return (! fs::exists(absfile)) || (fs::last_write_time(configure_script) > fs::last_write_time(absfile)); @@ -991,8 +1000,9 @@ bool needsUpdate(string const & file) bool LyX::queryUserLyXDir(bool explicit_userdir) { // Does user directory exist? - if (fs::exists(package().user_support()) && - fs::is_directory(package().user_support())) { + string const user_support = + FileName(package().user_support()).toFilesystemEncoding(); + if (fs::exists(user_support) && fs::is_directory(user_support)) { first_start = false; return needsUpdate("lyxrc.defaults") diff --git a/src/lyxfunc.C b/src/lyxfunc.C index 5a3984d47a..2a9f579ae9 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -1808,7 +1808,8 @@ void LyXFunc::menuNew(string const & name, bool fromTemplate) if (filename.empty()) { filename = addName(lyxrc.document_path, "newfile" + convert(++newfile_number) + ".lyx"); - while (theBufferList().exists(filename) || fs::is_readable(filename)) { + while (theBufferList().exists(filename) || + fs::is_readable(FileName(filename).toFilesystemEncoding())) { ++newfile_number; filename = addName(lyxrc.document_path, "newfile" + convert(newfile_number) + @@ -2101,10 +2102,10 @@ void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new) case LyXRC::RC_DISPLAY_GRAPHICS: case LyXRC::RC_DOCUMENTPATH: if (lyxrc_orig.document_path != lyxrc_new.document_path) { - if (fs::exists(lyxrc_new.document_path) && - fs::is_directory(lyxrc_new.document_path)) { + string const encoded = FileName( + lyxrc_new.document_path).toFilesystemEncoding(); + if (fs::exists(encoded) && fs::is_directory(encoded)) support::package().document_dir() = lyxrc.document_path; - } } case LyXRC::RC_ESC_CHARS: case LyXRC::RC_FONT_ENCODING: diff --git a/src/support/filetools.C b/src/support/filetools.C index 825c508a36..2eab4b38cf 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -216,12 +216,13 @@ FileName const fileOpenSearch(string const & path, string const & name, /// Returns a vector of all files in directory dir having extension ext. -vector const dirList(string 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; - if (!(fs::exists(dir) && fs::is_directory(dir))) { + string const encoded_dir = dir.toFilesystemEncoding(); + if (!(fs::exists(encoded_dir) && fs::is_directory(encoded_dir))) { lyxerr[Debug::FILES] << "Directory \"" << dir << "\" does not exist to DirList." << endl; @@ -233,11 +234,12 @@ vector const dirList(string const & dir, string const & ext) extension += '.'; extension += ext; - fs::directory_iterator dit(dir); + fs::directory_iterator dit(encoded_dir); 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); } } @@ -386,10 +388,10 @@ FileName const createTmpDir(FileName const & tempdir, string const & mask) } // namespace anon -bool destroyDir(string const & tmpdir) +bool destroyDir(FileName const & tmpdir) { try { - return fs::remove_all(tmpdir) > 0; + return fs::remove_all(tmpdir.toFilesystemEncoding()) > 0; } catch (fs::filesystem_error const & fe){ lyxerr << "Could not delete " << tmpdir << ". (" << fe.what() << ")" << std::endl; return false; diff --git a/src/support/filetools.h b/src/support/filetools.h index d6bf08897c..abf637ca84 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -23,7 +23,7 @@ namespace lyx { namespace support { /// remove directory and all contents, returns true on success -bool destroyDir(std::string const & tmpdir); +bool destroyDir(FileName const & tmpdir); /// Creates the per buffer temporary directory std::string const createBufferTmpDir(); @@ -60,7 +60,7 @@ FileName const fileSearch(std::string const & path, std::string const & ext = std::string()); /// Returns a vector of all files in directory dir having extension ext. -std::vector const dirList(std::string const & dir, +std::vector const dirList(FileName const & dir, std::string const & ext = std::string()); /** Is directory read only? diff --git a/src/tex2lyx/text.C b/src/tex2lyx/text.C index bf5ae5f152..17003c01d5 100644 --- a/src/tex2lyx/text.C +++ b/src/tex2lyx/text.C @@ -358,7 +358,7 @@ string find_file(string const & name, string const & path, // We don't use ChangeExtension() because it does the wrong // thing if name contains a dot. string const trial = name + '.' + (*what); - if (fs::exists(makeAbsPath(trial, path))) + if (fs::exists(FileName(makeAbsPath(trial, path)).toFilesystemEncoding())) return trial; } return string(); @@ -1480,7 +1480,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, string const path = getMasterFilePath(); // We want to preserve relative / absolute filenames, // therefore path is only used for testing - if (!fs::exists(makeAbsPath(name, path))) { + if (!fs::exists(FileName(makeAbsPath(name, path)).toFilesystemEncoding())) { // The file extension is probably missing. // Now try to find it out. string const dvips_name = @@ -1510,7 +1510,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, name = pdftex_name; } - if (fs::exists(makeAbsPath(name, path))) + if (fs::exists(FileName(makeAbsPath(name, path)).toFilesystemEncoding())) fix_relative_filename(name); else cerr << "Warning: Could not find graphics file '" @@ -2132,7 +2132,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, // We want to preserve relative / absolute filenames, // therefore path is only used for testing if (t.cs() == "include" && - !fs::exists(makeAbsPath(filename, path))) { + !fs::exists(FileName(makeAbsPath(filename, path)).toFilesystemEncoding())) { // The file extension is probably missing. // Now try to find it out. string const tex_name = @@ -2141,7 +2141,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, if (!tex_name.empty()) filename = tex_name; } - if (fs::exists(makeAbsPath(filename, path))) { + if (fs::exists(FileName(makeAbsPath(filename, path)).toFilesystemEncoding())) { string const abstexname = makeAbsPath(filename, path); string const abslyxname =