]> git.lyx.org Git - lyx.git/blobdiff - src/Format.cpp
tex2lyx/text.cpp: fix 2 typos
[lyx.git] / src / Format.cpp
index 4219174df7f5089786c3b3cf60bceaf2e66adf24..1ae3960163de695bcb9457bf14bec40842ecf342 100644 (file)
@@ -29,6 +29,8 @@
 #include "support/Translator.h"
 
 #include <algorithm>
+#include <map>
+#include <ctime>
 
 // FIXME: Q_WS_MACX is not available, it's in Qt
 #ifdef USE_MACOSX_PACKAGING
@@ -75,26 +77,36 @@ private:
        string extension_;
 };
 
+
+class FormatPrettyNameEqual : public unary_function<Format, bool> {
+public:
+       FormatPrettyNameEqual(string const & prettyname)
+               : prettyname_(prettyname) {}
+       bool operator()(Format const & f) const
+       {
+               return f.prettyname() == prettyname_;
+       }
+private:
+       string prettyname_;
+};
+
 } //namespace anon
 
 
 bool operator<(Format const & a, Format const & b)
 {
-       // use the compare_ascii_no_case instead of compare_no_case,
-       // because in turkish, 'i' is not the lowercase version of 'I',
-       // and thus turkish locale breaks parsing of tags.
-
-       return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
+       return _(a.prettyname()) < _(b.prettyname());
 }
 
 
 Format::Format(string const & n, string const & e, string const & p,
               string const & s, string const & v, string const & ed,
               int flags)
-       : name_(n), extensions_(e), prettyname_(p), shortcut_(s), viewer_(v),
+       : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
          editor_(ed), flags_(flags)
 {
        extension_list_ = getVectorFromString(e, ",");
+       LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
 }
 
 
@@ -104,6 +116,12 @@ bool Format::dummy() const
 }
 
 
+string const Format::extensions() const
+{
+       return getStringFromVector(extension_list_, ", ");
+}
+
+
 bool Format::hasExtension(string const & e) const
 {
        return (find(extension_list_.begin(), extension_list_.end(), e)
@@ -127,7 +145,6 @@ string const Format::parentFormat() const
 
 void Format::setExtensions(string const & e)
 {
-       extensions_ = e;
        extension_list_ = getVectorFromString(e, ",");
 }
 
@@ -152,11 +169,26 @@ string Formats::getFormatFromFile(FileName const & filename) const
                return string();
 
        string const format = filename.guessFormatFromContents();
+       string const ext = getExtension(filename.absFileName());
+       if ((format == "gzip" || format == "zip" || format == "compress")
+           && !ext.empty()) {
+               string const & fmt_name = formats.getFormatFromExtension(ext);
+               if (!fmt_name.empty()) {
+                       Format const * p_format = formats.getFormat(fmt_name);
+                       if (p_format && p_format->zippedNative())
+                               return p_format->name();
+               }
+       }
        if (!format.empty())
                return format;
 
        // try to find a format from the file extension.
-       string const ext = getExtension(filename.absFileName());
+       return getFormatFromExtension(ext);
+}
+
+
+string Formats::getFormatFromExtension(string const & ext) const
+{
        if (!ext.empty()) {
                // this is ambigous if two formats have the same extension,
                // but better than nothing
@@ -173,6 +205,45 @@ string Formats::getFormatFromFile(FileName const & filename) const
 }
 
 
+string Formats::getFormatFromPrettyName(string const & prettyname) const
+{
+       if (!prettyname.empty()) {
+               Formats::const_iterator cit =
+                       find_if(formatlist.begin(), formatlist.end(),
+                               FormatPrettyNameEqual(prettyname));
+               if (cit != formats.end())
+                       return cit->name();
+       }
+       return string();
+}
+
+
+/// Used to store last timestamp of file and whether it is (was) zipped
+struct ZippedInfo {
+       bool zipped;
+       std::time_t timestamp;
+       ZippedInfo(bool zipped, std::time_t timestamp)
+       : zipped(zipped), timestamp(timestamp) { }
+};
+
+
+/// Mapping absolute pathnames of files to their ZippedInfo metadata.
+static std::map<std::string, ZippedInfo> zipped_;
+
+
+bool Formats::isZippedFile(support::FileName const & filename) const {
+       string const & fname = filename.absFileName();
+       time_t timestamp = filename.lastModified();
+       map<string, ZippedInfo>::iterator it = zipped_.find(fname);
+       if (it != zipped_.end() && it->second.timestamp == timestamp)
+               return it->second.zipped;
+       string const & format = getFormatFromFile(filename);
+       bool zipped = (format == "gzip" || format == "zip");
+       zipped_.insert(pair<string, ZippedInfo>(fname, ZippedInfo(zipped, timestamp)));
+       return zipped;
+}
+
+
 static string fixCommand(string const & cmd, string const & ext,
                  os::auto_open_mode mode)
 {