From: Abdelrazak Younes Date: Wed, 28 Nov 2007 15:25:07 +0000 (+0000) Subject: * FileName: X-Git-Tag: 1.6.10~7147 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=c886bc68c2ab948e1219dddcfe96bc393e788f9c;p=features.git * FileName: - introduce changeExtension(): the goal is to replace progressively the one in filetools.cpp - get rid of internal_path() checking for Win32 as the path is always normalized with slashes with QFileInfo. * BufferParams: get rid of filepath member. This was used only once and was fixing a path in BufferParam was wrong anyway. * Buffer::filePath(): always regenerate instead of caching it in BufferParams. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21847 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 82f5b8fe40..ef57cec784 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -233,7 +233,7 @@ Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_) inset.setAutoBreakRows(true); lyxvc.setBuffer(&parent); temppath = createBufferTmpDir(); - params.filepath = onlyPath(file.absFilename()); + // FIXME: And now do something if temppath == string(), because we // assume from now on that temppath points to a valid temp dir. // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html @@ -445,7 +445,6 @@ void Buffer::setReadonly(bool const flag) void Buffer::setFileName(string const & newfile) { pimpl_->filename = makeAbsPath(newfile); - params().filepath = onlyPath(pimpl_->filename.absFilename()); setReadonly(pimpl_->filename.isReadOnly()); updateTitles(); } @@ -501,7 +500,7 @@ int Buffer::readHeader(Lexer & lex) LYXERR(Debug::PARSER, "Handling document header token: `" << token << '\''); - string unknown = params().readToken(lex, token); + string unknown = params().readToken(lex, token, pimpl_->filename.onlyPath()); if (!unknown.empty()) { if (unknown[0] != '\\' && token == "\\textclass") { Alert::warning(_("Unknown document class"), @@ -848,8 +847,7 @@ bool Buffer::save() const backupName = FileName(absFileName() + '~'); if (!lyxrc.backupdir_path.empty()) { string const mangledName = - subst(subst(os::internal_path( - backupName.absFilename()), '/', '!'), ':', '!'); + subst(subst(backupName.absFilename(), '/', '!'), ':', '!'); backupName = FileName(addName(lyxrc.backupdir_path, mangledName)); } @@ -1701,9 +1699,9 @@ string Buffer::absFileName() const } -string const & Buffer::filePath() const +string Buffer::filePath() const { - return params().filepath; + return pimpl_->filename.onlyPath().absFilename(); } diff --git a/src/Buffer.h b/src/Buffer.h index c3ea3ea4bd..d5703ba34a 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -244,7 +244,7 @@ public: /// Returns the the path where the buffer lives. /// It is always an absolute path. - std::string const & filePath() const; + std::string filePath() const; /** A transformed version of the file name, adequate for LaTeX. \param no_path optional if \c true then the path is stripped. diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index 8c30839cd7..946a781a07 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -44,6 +44,7 @@ #include "support/convert.h" #include "support/docstream.h" +#include "support/FileName.h" #include "support/filetools.h" #include "support/Translator.h" #include "support/lstrings.h" @@ -464,14 +465,16 @@ void BufferParams::setDefSkip(VSpace const & vs) } -string const BufferParams::readToken(Lexer & lex, string const & token) +string const BufferParams::readToken(Lexer & lex, string const & token, + FileName const & filepath) { if (token == "\\textclass") { lex.next(); string const classname = lex.getString(); // if there exists a local layout file, ignore the system one // NOTE: in this case, the textclass (.cls file) is assumed to be available. - pair pp = textclasslist.addTextClass(classname, filepath); + pair pp = textclasslist.addTextClass( + classname, filepath.absFilename()); if (pp.first) setBaseClass(pp.second); else { diff --git a/src/BufferParams.h b/src/BufferParams.h index c24017483d..cbfa01c782 100644 --- a/src/BufferParams.h +++ b/src/BufferParams.h @@ -30,6 +30,10 @@ namespace lyx { +namespace support { +class FileName; +} + class AuthorList; class BranchList; class Bullet; @@ -65,7 +69,9 @@ public: docstring const B_(std::string const & l10n) const; /// read a header token, if unrecognised, return it or an unknown class name - std::string const readToken(Lexer & lex, std::string const & token); + std::string const readToken(Lexer & lex, + std::string const & token, ///< token to read. + support::FileName const & filepath); ///< where to look for local layout file. /// void writeFile(std::ostream &) const; @@ -284,8 +290,7 @@ public: std::string const & sf, std::string const & tt, bool const & sc, bool const & osf, int const & sfscale, int const & ttscale) const; - /// path of the current buffer - std::string filepath; + /// get the appropriate cite engine (natbib handling) biblio::CiteEngine getEngine() const; diff --git a/src/frontends/qt4/GuiExternal.cpp b/src/frontends/qt4/GuiExternal.cpp index b053b064e0..1e4abef288 100644 --- a/src/frontends/qt4/GuiExternal.cpp +++ b/src/frontends/qt4/GuiExternal.cpp @@ -650,8 +650,7 @@ void GuiExternal::updateTemplate() void GuiExternal::applyView() { - params_.filename.set(internal_path(fromqstr(fileED->text())), - bufferFilepath()); + params_.filename.set(fromqstr(fileED->text()), bufferFilepath()); params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName); diff --git a/src/frontends/qt4/GuiGraphics.cpp b/src/frontends/qt4/GuiGraphics.cpp index f9a3484dd2..026e2c5fde 100644 --- a/src/frontends/qt4/GuiGraphics.cpp +++ b/src/frontends/qt4/GuiGraphics.cpp @@ -614,8 +614,7 @@ void GuiGraphics::applyView() { InsetGraphicsParams & igp = params_; - igp.filename.set(internal_path(fromqstr(filename->text())), - bufferFilepath()); + igp.filename.set(fromqstr(filename->text()), bufferFilepath()); igp.filename.setEmbed(embedCB->checkState() == Qt::Checked); // the bb section diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 27a7dd2247..c6255ce01e 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -79,12 +79,10 @@ FileName::FileName() : d(new Private) { } + FileName::FileName(string const & abs_filename) : d(abs_filename.empty() ? new Private : new Private(abs_filename)) { -#if defined(_WIN32) - BOOST_ASSERT(!contains(abs_filename, '\\')); -#endif } @@ -117,9 +115,6 @@ void FileName::set(string const & name) { d->fi.setFile(toqstr(name)); BOOST_ASSERT(d->fi.isAbsolute()); -#if defined(_WIN32) - BOOST_ASSERT(!contains(name, '\\')); -#endif } @@ -360,6 +355,26 @@ string FileName::fileContents() const } +void FileName::changeExtension(std::string const & extension) +{ + // FIXME: use Qt native methods... + string const oldname = absFilename(); + string::size_type const last_slash = oldname.rfind('/'); + string::size_type last_dot = oldname.rfind('.'); + if (last_dot < last_slash && last_slash != string::npos) + last_dot = string::npos; + + string ext; + // Make sure the extension starts with a dot + if (!extension.empty() && extension[0] != '.') + ext= '.' + extension; + else + ext = extension; + + set(oldname.substr(0, last_dot) + ext); +} + + string FileName::guessFormatFromContents() const { // the different filetypes and what they contain in one of the first lines @@ -636,7 +651,7 @@ string const DocFileName::mangledFilename(std::string const & dir) const // Now the real work string mname = os::internal_path(name); // Remove the extension. - mname = changeExtension(name, string()); + mname = support::changeExtension(name, string()); // The mangled name must be a valid LaTeX name. // The list of characters to keep is probably over-restrictive, // but it is not really a problem. @@ -651,7 +666,7 @@ string const DocFileName::mangledFilename(std::string const & dir) const while ((pos = mname.find_first_not_of(keep, pos)) != string::npos) mname[pos++] = '_'; // Add the extension back on - mname = changeExtension(mname, getExtension(name)); + mname = support::changeExtension(mname, getExtension(name)); // Prepend a counter to the filename. This is necessary to make // the mangled name unique. diff --git a/src/support/FileName.h b/src/support/FileName.h index 251cfdbebb..b7583a390c 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -112,6 +112,13 @@ public: * \p name must have an absolute path. */ + /// Change extension. + /** + * If oldname does not have an extension, it is appended. + * If the extension is empty, any extension is removed from the name. + */ + void changeExtension(std::string const & extension); + /** Guess the file format name (as in Format::name()) from contents. Normally you don't want to use this directly, but rather Formats::getFormatFromFile(). diff --git a/src/support/Package.cpp b/src/support/Package.cpp index cad22eced3..1d54cd1d56 100644 --- a/src/support/Package.cpp +++ b/src/support/Package.cpp @@ -367,7 +367,7 @@ FileName const get_temp_dir() // Typical example: C:/TEMP/. char path[MAX_PATH]; GetTempPath(MAX_PATH, path); - return FileName(os::internal_path(to_utf8(from_local8bit(path)))); + return FileName(to_utf8(from_local8bit(path))); #else // Posix-like. return FileName("/tmp"); #endif diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index e009fcc7b6..6321344aa4 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -448,7 +448,7 @@ FileName const makeAbsPath(string const & relPath, string const & basePath) } // returns absolute path - return FileName(os::internal_path(tempBase)); + return FileName(tempBase); } @@ -840,8 +840,7 @@ FileName const findtexfile(string const & fil, string const & /*format*/) LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n' << "kpse result = `" << rtrim(c.second, "\n\r") << '\''); if (c.first != -1) - return FileName(os::internal_path(rtrim(to_utf8(from_filesystem8bit(c.second)), - "\n\r"))); + return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r")); else return FileName(); }