]> git.lyx.org Git - features.git/commitdiff
* FileName:
authorAbdelrazak Younes <younes@lyx.org>
Wed, 28 Nov 2007 15:25:07 +0000 (15:25 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Wed, 28 Nov 2007 15:25:07 +0000 (15:25 +0000)
- 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

src/Buffer.cpp
src/Buffer.h
src/BufferParams.cpp
src/BufferParams.h
src/frontends/qt4/GuiExternal.cpp
src/frontends/qt4/GuiGraphics.cpp
src/support/FileName.cpp
src/support/FileName.h
src/support/Package.cpp
src/support/filetools.cpp

index 82f5b8fe4068debc4e8f45b358eaf5eca1ccb5f2..ef57cec7845f110a3fc918538c10f8fc0f6ca8b5 100644 (file)
@@ -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();
 }
 
 
index c3ea3ea4bd40098984763f0f35cdfee764c8a1ae..d5703ba34a64babc970fa368e2cf6973471b02f8 100644 (file)
@@ -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.
index 8c30839cd72ba5c9d82f4c9dee623bfa03f85daf..946a781a0726a79ce5160d0d0a89f347887ab7ff 100644 (file)
@@ -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<bool, lyx::textclass_type> pp = textclasslist.addTextClass(classname, filepath);
+               pair<bool, lyx::textclass_type> pp = textclasslist.addTextClass(
+                       classname, filepath.absFilename());
                if (pp.first)
                        setBaseClass(pp.second);
                else {
index c24017483dc9ee40afc649728bbec7b786dc5f76..cbfa01c782358c8bf66db336a7f8c131ef863f1b 100644 (file)
 
 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;
 
index b053b064e03768bf77a18d5455fc40dbd6809dd7..1e4abef288407cd339726f03f19899abe5ef989c 100644 (file)
@@ -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);
 
index f9a3484dd2e60a6d6998183f63002ad4adcacce0..026e2c5fde503e8ad757ad8336bc51c263eac2f0 100644 (file)
@@ -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
index 27a7dd224757bdad9e33793c7d7db68bf4b5adc3..c6255ce01e978bb2efa464a4bb1fbdfd12d1d5aa 100644 (file)
@@ -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.
index 251cfdbebb74cc340f1fe7d1078233a7ebc1c6ad..b7583a390c5c5cb3b514dc5c846959298e817b07 100644 (file)
@@ -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().
index cad22eced3f9f82fbf9e04dd0bdd8919cccebc71..1d54cd1d561be501516ebd71986ced52bb0dce40 100644 (file)
@@ -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
index e009fcc7b6d106c586bedc3847fc62581ceda3dc..6321344aa411e5586c2d50c9b857b064ac6d8ad8 100644 (file)
@@ -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();
 }