From: Georg Baum Date: Tue, 9 Nov 2004 19:08:34 +0000 (+0000) Subject: make gettting the file format from file contents work in the correct way ;-) X-Git-Tag: 1.6.10~14842 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=dacd4cea2649ebe32ed386eaa6628e47c68e7b6b;p=features.git make gettting the file format from file contents work in the correct way ;-) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9218 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/ChangeLog b/src/ChangeLog index 69d2844669..5dc94c8184 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2004-11-09 Georg Baum + + * format.[Ch] (getFormatFromFile): new method + * exporter.C: s/getFormatFromContents/formats.getFormatFromFile/ + 2004-11-09 Jean-Marc Lasgouttes * lengthcommon.C (unitFromString): fix off-by-one error (bug 1682) diff --git a/src/client/ChangeLog b/src/client/ChangeLog index 92187295d7..f5fba9bb4f 100644 --- a/src/client/ChangeLog +++ b/src/client/ChangeLog @@ -1,3 +1,7 @@ +2004-11-09 Georg Baum + + * client.C: remove format hack + 2004-10-29 Georg Baum * client.C (formats): new, needed for libsupport.a diff --git a/src/client/client.C b/src/client/client.C index 64c2fcc711..8339c1fe59 100644 --- a/src/client/client.C +++ b/src/client/client.C @@ -13,7 +13,6 @@ #include #include "debug.h" -#include "format.h" #include "support/lstrings.h" #include @@ -54,10 +53,6 @@ using std::cin; using std::endl; -// hack to link in libsupport -Formats formats; - - namespace support { string itoa(unsigned int i) diff --git a/src/exporter.C b/src/exporter.C index 1c12aa2d2f..ba090ea8af 100644 --- a/src/exporter.C +++ b/src/exporter.C @@ -39,7 +39,6 @@ using lyx::support::AddName; using lyx::support::bformat; using lyx::support::ChangeExtension; using lyx::support::contains; -using lyx::support::getFormatFromContents; using lyx::support::MakeAbsPath; using lyx::support::MakeDisplayPath; using lyx::support::OnlyFilename; @@ -208,7 +207,8 @@ bool Exporter::Export(Buffer * buffer, string const & format, CopyStatus status = SUCCESS; for (vector::const_iterator it = files.begin(); it != files.end() && status != CANCEL; ++it) { - string const fmt = getFormatFromContents(it->sourceName); + string const fmt = + formats.getFormatFromFile(it->sourceName); status = copyFile(fmt, it->sourceName, MakeAbsPath(it->exportName, dest), it->exportName, status == FORCE); 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 = diff --git a/src/format.h b/src/format.h index 9b5cdec982..54e337ff0a 100644 --- a/src/format.h +++ b/src/format.h @@ -86,6 +86,13 @@ public: } /// \returns format named \p name if it exists, otherwise 0 Format const * getFormat(std::string const & name) const; + /*! + * Get the format of \p filename from file contents or, if this + * fails, from file extension. + * \returns file format if it could be found, otherwise an empty + * string. + */ + std::string getFormatFromFile(std::string const & filename) const; /// int getNumber(std::string const & name) const; /// diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index c9ef9cc8a6..ac35000f99 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,8 @@ +2004-11-09 Georg Baum + + * ControlInclude.C (browse): Use GetExtension() instead of + getFormatFromContents() + 2004-11-05 Jean-Marc Lasgouttes * ControlRef.C (gotoRef, gotoBookmark): diff --git a/src/frontends/controllers/ControlInclude.C b/src/frontends/controllers/ControlInclude.C index d09bf7ac3c..841543e175 100644 --- a/src/frontends/controllers/ControlInclude.C +++ b/src/frontends/controllers/ControlInclude.C @@ -99,8 +99,8 @@ string const ControlInclude::browse(string const & in_name, Type in_type) const void ControlInclude::load(string const & file) { - string const format = support::getFormatFromContents(file); - if (format == "lyx") + string const ext = support::GetExtension(file); + if (ext == "lyx") kernel().dispatch(FuncRequest(LFUN_CHILDOPEN, file)); else // tex file or other text file in verbatim mode diff --git a/src/graphics/ChangeLog b/src/graphics/ChangeLog index c5435fd2a3..6d2634d1a8 100644 --- a/src/graphics/ChangeLog +++ b/src/graphics/ChangeLog @@ -1,3 +1,8 @@ +2004-11-09 Georg Baum + + * GraphicsCacheItem.C: + s/getFormatFromContents/formats.getFormatFromFile/ + 2004-10-29 Georg Baum * GraphicsCacheItem.C: s/getExtFromContents/getFormatFromContents/ diff --git a/src/graphics/GraphicsCacheItem.C b/src/graphics/GraphicsCacheItem.C index 5071e0d5e0..7359995c8c 100644 --- a/src/graphics/GraphicsCacheItem.C +++ b/src/graphics/GraphicsCacheItem.C @@ -17,6 +17,7 @@ #include "GraphicsImage.h" #include "debug.h" +#include "format.h" #include "support/filetools.h" #include "support/FileMonitor.h" @@ -32,7 +33,6 @@ using support::FileMonitor; using support::IsFileReadable; using support::MakeDisplayPath; using support::OnlyFilename; -using support::getFormatFromContents; using support::tempName; using support::unlink; using support::unzipFile; @@ -401,7 +401,13 @@ void CacheItem::Impl::convertToDisplayFormat() << "\n\twith displayed filename: " << displayed_filename << endl; - string from = getFormatFromContents(filename); + string const from = formats.getFormatFromFile(filename); + if (from.empty()) { + setStatus(ErrorConverting); + lyxerr[Debug::GRAPHICS] + << "\tCould not determine file format." << endl; + return; + } lyxerr[Debug::GRAPHICS] << "\n\tThe file contains " << from << " format data." << endl; string const to = findTargetFormat(from); diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 01cbcaf205..b3bee1ce49 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,8 @@ +2004-11-09 Georg Baum + + * ExternalSupport.C, insetgraphics.C: + s/getFormatFromContents/formats.getFormatFromFile/ + 2004-11-04 Jürgen Spitzmüller * insetcharstyle.[Ch]: diff --git a/src/insets/ExternalSupport.C b/src/insets/ExternalSupport.C index c2ea3fa767..510b02c439 100644 --- a/src/insets/ExternalSupport.C +++ b/src/insets/ExternalSupport.C @@ -56,7 +56,7 @@ void editExternal(InsetExternalParams const & params, Buffer const & buffer) { string const file_with_path = params.filename.absFilename(); formats.edit(buffer, file_with_path, - support::getFormatFromContents(file_with_path)); + formats.getFormatFromFile(file_with_path)); } @@ -174,7 +174,7 @@ void updateExternal(InsetExternalParams const & params, return; // NOT_NEEDED // Try and ascertain the file format from its contents. - from_format = support::getFormatFromContents(abs_from_file); + from_format = formats.getFormatFromFile(abs_from_file); if (from_format.empty()) return; // FAILURE diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index 71f4a3d531..017ab2a5c6 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -95,7 +95,6 @@ using lyx::support::contains; using lyx::support::FileName; using lyx::support::float_equal; using lyx::support::GetExtension; -using lyx::support::getFormatFromContents; using lyx::support::IsFileReadable; using lyx::support::LibFileSearch; using lyx::support::OnlyFilename; @@ -453,7 +452,7 @@ copyFileIfNeeded(string const & file_in, string const & file_out) // Nothing to do... return std::make_pair(IDENTICAL_CONTENTS, file_out); - Mover const & mover = movers(getFormatFromContents(file_in)); + Mover const & mover = movers(formats.getFormatFromFile(file_in)); bool const success = mover.copy(file_in, file_out); if (!success) { lyxerr[Debug::GRAPHICS] @@ -617,7 +616,12 @@ string const InsetGraphics::prepareFile(Buffer const & buf, } } - string const from = getFormatFromContents(temp_file); + string const from = formats.getFormatFromFile(temp_file); + if (from.empty()) { + lyxerr[Debug::GRAPHICS] + << "\tCould not get file format." << endl; + return orig_file; + } string const to = findTargetFormat(from, runparams); string const ext = formats.extension(to); lyxerr[Debug::GRAPHICS] @@ -895,10 +899,12 @@ InsetGraphicsParams const & InsetGraphics::params() const } -void InsetGraphics::editGraphics(InsetGraphicsParams const & p, Buffer const & buffer) const +void InsetGraphics::editGraphics(InsetGraphicsParams const & p, + Buffer const & buffer) const { string const file_with_path = p.filename.absFilename(); - formats.edit(buffer, file_with_path, getFormatFromContents(file_with_path)); + formats.edit(buffer, file_with_path, + formats.getFormatFromFile(file_with_path)); } diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 18eddf71bc..182d341e79 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,9 @@ +2004-11-09 Georg Baum + + * filetools.[Ch] (getFormatFromContents): don't guess format from + extension, return string() instead of "user" if the format could + not be determined + 2004-11-07 Lars Gullik Bjonnes * Make it clearer where include files are comming from. diff --git a/src/support/filetools.C b/src/support/filetools.C index c3cc31cd60..776f89c113 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -35,7 +35,6 @@ // FIXME Interface violation #include "gettext.h" #include "debug.h" -//#include "format.h" #include #include @@ -896,25 +895,6 @@ string const GetExtension(string const & name) } -#if 0 -namespace { - -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 -#endif - - // the different filetypes and what they contain in one of the first lines // (dots are any characters). (Herbert 20020131) // AGR Grace... @@ -942,9 +922,6 @@ private: // ZIP PK... http://www.halyava.ru/document/ind_arch.htm // Z \037\235 UNIX compress -/// return the "extension" which belongs to the contents. -/// for no knowing contents return the extension. Without -/// an extension and unknown contents we return "user" string const getFormatFromContents(string const & filename) { // paranoia check @@ -1090,33 +1067,10 @@ string const getFormatFromContents(string const & filename) return format; } - string const ext(GetExtension(filename)); lyxerr[Debug::GRAPHICS] << "filetools(getFormatFromContents)\n" << "\tCouldn't find a known format!\n"; -#if 0 - // This just cannot be here. It is a blatant violation of interfaces. - // Nothing in support should have any knowledge of internal structures - // in the rest of lyx. This case needs to be explictly checked for - // in the places where this function is called. Also it makes the - // function name a lie. (Lgb) - if (!ext.empty()) { - // this is ambigous if two formats have the same extension, - // but better than nothing - Formats::const_iterator cit = - find_if(formats.begin(), formats.end(), - FormatExtensionsEqual(ext)); - if (cit != formats.end()) { - lyxerr[Debug::GRAPHICS] - << "\twill guess format from file extension: " - << ext << " -> " << cit->name() << endl; - return cit->name(); - } - } -#endif - lyxerr[Debug::GRAPHICS] - << "\twill use a \"user\" defined format" << endl; - return "user"; + return string(); } diff --git a/src/support/filetools.h b/src/support/filetools.h index f7b80c6c18..6b4c4b1edf 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -140,7 +140,10 @@ ChangeExtension(std::string const & oldname, std::string const & extension); /// Return the extension of the file (not including the .) std::string const GetExtension(std::string const & name); -/// Guess the file format name (as in Format::name()) from contents +/** Guess the file format name (as in Format::name()) from contents. + Normally you don't want to use this directly, but rather + Formats::getFormatFromFile(). + */ std::string const getFormatFromContents(std::string const & name); /// check for zipped file diff --git a/src/tex2lyx/ChangeLog b/src/tex2lyx/ChangeLog index 3dfd8ff984..a064eed91d 100644 --- a/src/tex2lyx/ChangeLog +++ b/src/tex2lyx/ChangeLog @@ -1,3 +1,7 @@ +2004-11-09 Georg Baum + + * tex2lyx.C: remove format hack + 2004-11-07 Georg Baum * texparser.C (getNewline): new diff --git a/src/tex2lyx/tex2lyx.C b/src/tex2lyx/tex2lyx.C index 0d44d989d1..264ba04912 100644 --- a/src/tex2lyx/tex2lyx.C +++ b/src/tex2lyx/tex2lyx.C @@ -16,7 +16,6 @@ #include "context.h" #include "debug.h" -#include "format.h" #include "lyxtextclass.h" #include "support/path_defines.h" #include "support/filetools.h" @@ -56,10 +55,6 @@ using lyx::support::IsFileWriteable; LyXErr lyxerr(std::cerr.rdbuf()); -// hack to link in libsupport -Formats formats; - - string const trim(string const & a, char const * p) { // BOOST_ASSERT(p);