]> git.lyx.org Git - lyx.git/blobdiff - src/support/filename.C
* src/encoding.C (latexChar,read):
[lyx.git] / src / support / filename.C
index 63c15df3fec9f87b9a2a04174bb877a42fa8a59a..533a14cd0bc1cf7ed30562cbffac6e58f68db9ec 100644 (file)
@@ -14,6 +14,9 @@
 #include "support/filetools.h"
 #include "support/lstrings.h"
 #include "support/os.h"
+#include "support/qstring_helpers.h"
+
+#include <QFile>
 
 #include <boost/assert.hpp>
 
@@ -34,10 +37,47 @@ FileName::FileName()
 {}
 
 
+FileName::~FileName()
+{}
+
+
 FileName::FileName(string const & abs_filename)
        : name_(abs_filename)
 {
+       BOOST_ASSERT(empty() || absolutePath(name_));
+#if defined(_WIN32)
+       BOOST_ASSERT(!contains(name_, '\\'));
+#endif
+}
+
+
+void FileName::set(string const & name)
+{
+       name_ = name;
        BOOST_ASSERT(absolutePath(name_));
+#if defined(_WIN32)
+       BOOST_ASSERT(!contains(name_, '\\'));
+#endif
+}
+
+
+void FileName::erase()
+{
+       name_.erase();
+}
+
+
+string const FileName::toFilesystemEncoding() const
+{
+       QByteArray const encoded = QFile::encodeName(toqstr(name_));
+       return string(encoded.begin(), encoded.end());
+}
+
+
+FileName const FileName::fromFilesystemEncoding(string const & name)
+{
+       QByteArray const encoded(name.c_str(), name.length());
+       return FileName(fromqstr(QFile::decodeName(encoded)));
 }
 
 
@@ -53,6 +93,24 @@ bool operator!=(FileName const & lhs, FileName const & rhs)
 }
 
 
+bool operator<(FileName const & lhs, FileName const & rhs)
+{
+       return lhs.absFilename() < rhs.absFilename();
+}
+
+
+bool operator>(FileName const & lhs, FileName const & rhs)
+{
+       return lhs.absFilename() > rhs.absFilename();
+}
+
+
+std::ostream & operator<<(std::ostream & os, FileName const & filename)
+{
+       return os << filename.absFilename();
+}
+
+
 DocFileName::DocFileName()
        : save_abs_path_(true)
 {}
@@ -63,10 +121,15 @@ DocFileName::DocFileName(string const & abs_filename, bool save_abs)
 {}
 
 
+DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
+       : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
+{}
+
+
 void DocFileName::set(string const & name, string const & buffer_path)
 {
        save_abs_path_ = absolutePath(name);
-       name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
+       name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
        zipped_valid_ = false;
 }
 
@@ -80,13 +143,15 @@ void DocFileName::erase()
 
 string const DocFileName::relFilename(string const & path) const
 {
-       return makeRelPath(name_, path);
+       // FIXME UNICODE
+       return to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
 }
 
 
 string const DocFileName::outputFilename(string const & path) const
 {
-       return save_abs_path_ ? name_ : makeRelPath(name_, path);
+       // FIXME UNICODE
+       return save_abs_path_ ? name_ : to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
 }
 
 
@@ -104,14 +169,19 @@ string const DocFileName::mangledFilename(std::string const & dir) const
        string mname = os::internal_path(name_);
        // Remove the extension.
        mname = changeExtension(name_, string());
-       // Replace '/' in the file name with '_'
-       mname = subst(mname, "/", "_");
-       // Replace '.' in the file name with '_'
-       mname = subst(mname, ".", "_");
-       // Replace ' ' in the file name with '_'
-       mname = subst(mname, " ", "_");
-       // Replace ':' in the file name with '_'
-       mname = subst(mname, ":", "_");
+       // 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.
+       // Apart from non-ASCII characters, at least the following characters
+       // are forbidden: '/', '.', ' ', and ':'.
+       // On windows it is not possible to create files with '<', '>' or '?'
+       // in the name.
+       static string const keep = "abcdefghijklmnopqrstuvwxyz"
+                                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+                                  "+,-0123456789;=";
+       string::size_type pos = 0;
+       while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
+               mname[pos++] = '_';
        // Add the extension back on
        mname = changeExtension(mname, getExtension(name_));
 
@@ -149,7 +219,7 @@ string const DocFileName::mangledFilename(std::string const & dir) const
 bool DocFileName::isZipped() const
 {
        if (!zipped_valid_) {
-               zipped_ = zippedFile(name_);
+               zipped_ = zippedFile(*this);
                zipped_valid_ = true;
        }
        return zipped_;