X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fformat.C;h=5215e34bb31dc4b9a2ac4341b3c52b349349a12d;hb=530749439472bddf13d9f4ee74ee6184ef76e3f9;hp=0e83306e2506a0ed74b6b8f270f985a5d1040da3;hpb=e978fa42e38be22e9312be963ba6512fcd4b45ef;p=lyx.git diff --git a/src/format.C b/src/format.C index 0e83306e25..5215e34bb3 100644 --- a/src/format.C +++ b/src/format.C @@ -59,6 +59,19 @@ private: string name_; }; + +class FormatExtensionsEqual : public std::unary_function { +public: + FormatExtensionsEqual(string const & extension) + : extension_(extension) {} + bool operator()(Format const & f) const + { + return f.extension() == extension_; + } +private: + string extension_; +}; + } //namespace anon bool operator<(Format const & a, Format const & b) @@ -110,6 +123,34 @@ Format const * Formats::getFormat(string const & name) const } +string Formats::getFormatFromFile(string const & filename) const +{ + if (filename.empty()) + return string(); + + string const format = lyx::support::getFormatFromContents(filename); + if (!format.empty()) + return format; + + // try to find a format from the file extension. + string const ext(lyx::support::GetExtension(filename)); + if (!ext.empty()) { + // this is ambigous if two formats have the same extension, + // but better than nothing + Formats::const_iterator cit = + find_if(formatlist.begin(), formatlist.end(), + FormatExtensionsEqual(ext)); + if (cit != formats.end()) { + lyxerr[Debug::GRAPHICS] + << "\twill guess format from file extension: " + << ext << " -> " << cit->name() << std::endl; + return cit->name(); + } + } + return string(); +} + + int Formats::getNumber(string const & name) const { FormatList::const_iterator cit =