]> git.lyx.org Git - lyx.git/blobdiff - src/support/filename.C
hopefully fix tex2lyx linking.
[lyx.git] / src / support / filename.C
index 3019db3d80aa85b7f266bfe7cfeff7d6751bee46..63c15df3fec9f87b9a2a04174bb877a42fa8a59a 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <map>
 #include <sstream>
+#include <algorithm>
 
 
 using std::map;
@@ -30,45 +31,68 @@ namespace support {
 
 
 FileName::FileName()
-       : save_abs_path_(true)
 {}
 
 
-FileName::FileName(string const & abs_filename, bool save_abs)
-       : name_(abs_filename), save_abs_path_(save_abs)
+FileName::FileName(string const & abs_filename)
+       : name_(abs_filename)
 {
-       BOOST_ASSERT(AbsolutePath(name_));
+       BOOST_ASSERT(absolutePath(name_));
 }
 
 
-void FileName::set(string const & name, string const & buffer_path)
+bool operator==(FileName const & lhs, FileName const & rhs)
 {
-       save_abs_path_ = AbsolutePath(name);
-       name_ = save_abs_path_ ? name : MakeAbsPath(name, buffer_path);
+       return lhs.absFilename() == rhs.absFilename();
 }
 
 
-void FileName::erase()
+bool operator!=(FileName const & lhs, FileName const & rhs)
+{
+       return lhs.absFilename() != rhs.absFilename();
+}
+
+
+DocFileName::DocFileName()
+       : save_abs_path_(true)
+{}
+
+
+DocFileName::DocFileName(string 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);
+       zipped_valid_ = false;
+}
+
+
+void DocFileName::erase()
 {
        name_.erase();
+       zipped_valid_ = false;
 }
 
 
-string const FileName::relFilename(string const & path) const
+string const DocFileName::relFilename(string const & path) const
 {
-       return MakeRelPath(name_, path);
+       return makeRelPath(name_, path);
 }
 
 
-string const FileName::outputFilename(string const & path) const
+string const DocFileName::outputFilename(string const & path) const
 {
-       return save_abs_path_ ? name_ : MakeRelPath(name_, path);
+       return save_abs_path_ ? name_ : makeRelPath(name_, path);
 }
 
 
-string const FileName::mangledFilename(std::string const & dir) const
+string const DocFileName::mangledFilename(std::string const & dir) const
 {
-       // We need to make sure that every FileName instance for a given
+       // We need to make sure that every DocFileName instance for a given
        // filename returns the same mangled name.
        typedef map<string, string> MangledMap;
        static MangledMap mangledNames;
@@ -79,7 +103,7 @@ string const FileName::mangledFilename(std::string const & dir) const
        // Now the real work
        string mname = os::internal_path(name_);
        // Remove the extension.
-       mname = ChangeExtension(name_, string());
+       mname = changeExtension(name_, string());
        // Replace '/' in the file name with '_'
        mname = subst(mname, "/", "_");
        // Replace '.' in the file name with '_'
@@ -89,7 +113,7 @@ string const FileName::mangledFilename(std::string const & dir) const
        // Replace ':' in the file name with '_'
        mname = subst(mname, ":", "_");
        // Add the extension back on
-       mname = ChangeExtension(mname, GetExtension(name_));
+       mname = changeExtension(mname, getExtension(name_));
 
        // Prepend a counter to the filename. This is necessary to make
        // the mangled name unique.
@@ -98,27 +122,22 @@ string const FileName::mangledFilename(std::string const & dir) const
        s << counter++ << mname;
        mname = s.str();
 
-       // Experiments show that MiKTeX's YAP (version 2.4.1803)
-       // will crash if the string referencing the file name in
-       // the .dvi file is longer than 220 characters.
-       // This string contains about 50 chars-worth of other data,
-       // leaving us, say, 160 characters for the file name itself.
-       // (Erring on the side of caution.)
-       string::size_type max_length = 160;
-       if (dir.size() - 1 < max_length) {
-               // If dir.size() > max_length, all bets are off anyway.
-               // "+ 1" for the directory separator.
-               max_length -= dir.size() + 1;
-
-               // If the mangled file name is too long, hack it to fit.
-               // We know we're guaranteed to have a unique file name because
-               // of the counter.
-               if (mname.size() > max_length) {
-                       int const half = (int(max_length) / 2) - 2;
-                       if (half > 0) {
-                               mname = mname.substr(0, half) + "___" +
-                                       mname.substr(mname.size() - half);
-                       }
+       // MiKTeX's YAP (version 2.4.1803) crashes if the file name
+       // is longer than about 160 characters. MiKTeX's pdflatex
+       // is even pickier. A maximum length of 100 has been proven to work.
+       // If dir.size() > max length, all bets are off for YAP. We truncate
+       // the filename nevertheless, keeping a minimum of 10 chars.
+
+       string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
+
+       // If the mangled file name is too long, hack it to fit.
+       // We know we're guaranteed to have a unique file name because
+       // of the counter.
+       if (mname.size() > max_length) {
+               int const half = (int(max_length) / 2) - 2;
+               if (half > 0) {
+                       mname = mname.substr(0, half) + "___" +
+                               mname.substr(mname.size() - half);
                }
        }
 
@@ -127,26 +146,30 @@ string const FileName::mangledFilename(std::string const & dir) const
 }
 
 
-bool FileName::isZipped() const
+bool DocFileName::isZipped() const
 {
-       return zippedFile(name_);
+       if (!zipped_valid_) {
+               zipped_ = zippedFile(name_);
+               zipped_valid_ = true;
+       }
+       return zipped_;
 }
 
 
-string const FileName::unzippedFilename() const
+string const DocFileName::unzippedFilename() const
 {
        return unzippedFileName(name_);
 }
 
 
-bool operator==(FileName const & lhs, FileName const & rhs)
+bool operator==(DocFileName const & lhs, DocFileName const & rhs)
 {
        return lhs.absFilename() == rhs.absFilename() &&
                lhs.saveAbsPath() == rhs.saveAbsPath();
 }
 
 
-bool operator!=(FileName const & lhs, FileName const & rhs)
+bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
 {
        return !(lhs == rhs);
 }