]> git.lyx.org Git - features.git/commitdiff
make gettting the file format from file contents work in the correct way ;-)
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Tue, 9 Nov 2004 19:08:34 +0000 (19:08 +0000)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Tue, 9 Nov 2004 19:08:34 +0000 (19:08 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9218 a592a061-630c-0410-9148-cb99ea01b6c8

18 files changed:
src/ChangeLog
src/client/ChangeLog
src/client/client.C
src/exporter.C
src/format.C
src/format.h
src/frontends/controllers/ChangeLog
src/frontends/controllers/ControlInclude.C
src/graphics/ChangeLog
src/graphics/GraphicsCacheItem.C
src/insets/ChangeLog
src/insets/ExternalSupport.C
src/insets/insetgraphics.C
src/support/ChangeLog
src/support/filetools.C
src/support/filetools.h
src/tex2lyx/ChangeLog
src/tex2lyx/tex2lyx.C

index 69d28446697b5bd82e2adcabaca955da5b07fc7f..5dc94c8184f516c386b72875089965cdd8ef8c41 100644 (file)
@@ -1,3 +1,8 @@
+2004-11-09  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * format.[Ch] (getFormatFromFile): new method
+       * exporter.C: s/getFormatFromContents/formats.getFormatFromFile/
+
 2004-11-09  Jean-Marc Lasgouttes  <lasgouttes@lyx.org>
 
        * lengthcommon.C (unitFromString): fix off-by-one error (bug 1682)
index 92187295d74b2fc14b2cae142d5f0137e6ffaaed..f5fba9bb4fe53181531f1201ae3aa425517a64e8 100644 (file)
@@ -1,3 +1,7 @@
+2004-11-09  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * client.C: remove format hack
+
 2004-10-29  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
 
        * client.C (formats): new, needed for libsupport.a
index 64c2fcc711d76df3e2bb7f4b51bd4645806f557c..8339c1fe598b0aadc3ca4346fd1ae2006fd57bc9 100644 (file)
@@ -13,7 +13,6 @@
 #include <config.h>
 
 #include "debug.h"
-#include "format.h"
 #include "support/lstrings.h"
 
 #include <boost/filesystem/operations.hpp>
@@ -54,10 +53,6 @@ using std::cin;
 using std::endl;
 
 
-// hack to link in libsupport 
-Formats formats;
-
-
 namespace support {
 
 string itoa(unsigned int i)
index 1c12aa2d2fd723ace7bbdba4828bd9f537a48ebc..ba090ea8af942df6bf915f4985e564bea679da86 100644 (file)
@@ -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<ExportedFile>::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);
index 0e83306e2506a0ed74b6b8f270f985a5d1040da3..5215e34bb31dc4b9a2ac4341b3c52b349349a12d 100644 (file)
@@ -59,6 +59,19 @@ private:
        string name_;
 };
 
+
+class FormatExtensionsEqual : public std::unary_function<Format, bool> {
+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 =
index 9b5cdec9821cea143bdc399718af14ba2713dddd..54e337ff0abd99b6326561dea7095d1f8f92dd3e 100644 (file)
@@ -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;
        ///
index c9ef9cc8a672d24ef25adccd9db98bf8174f580d..ac35000f99fb384ef331d4e98c73ce93acd7a784 100644 (file)
@@ -1,3 +1,8 @@
+2004-11-09  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * ControlInclude.C (browse): Use GetExtension() instead of
+       getFormatFromContents()
+
 2004-11-05  Jean-Marc Lasgouttes  <lasgouttes@lyx.org>
 
        * ControlRef.C (gotoRef, gotoBookmark): 
index d09bf7ac3cc7e0a40dc2d6dc2a5e7460ed8c3ae8..841543e175eced896312e16933f3cce5b4b4c1b8 100644 (file)
@@ -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
index c5435fd2a3c78b66169a07626d3cb0bddfc4f5e8..6d2634d1a874088c0bbdec4a94b6b920d2bd7110 100644 (file)
@@ -1,3 +1,8 @@
+2004-11-09  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * GraphicsCacheItem.C:
+       s/getFormatFromContents/formats.getFormatFromFile/
+
 2004-10-29  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
 
        * GraphicsCacheItem.C: s/getExtFromContents/getFormatFromContents/
index 5071e0d5e0ed6a0259678f75a292d929cb4553a6..7359995c8c06c8768f6ccf341458dbbd8107ee2c 100644 (file)
@@ -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);
index 01cbcaf20575a2ec6b393cf864534edcc1306ade..b3bee1ce49e178e4e01f9038d3b373df4bd59aa6 100644 (file)
@@ -1,3 +1,8 @@
+2004-11-09  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * ExternalSupport.C, insetgraphics.C:
+       s/getFormatFromContents/formats.getFormatFromFile/
+
 2004-11-04  Jürgen Spitzmüller  <j.spitzmueller@gmx.de>
 
        * insetcharstyle.[Ch]:
index c2ea3fa767a6f2bf6b4333b79cf8f2ae81e8948c..510b02c4397b60f700abd1d43d4c50a111d9d529 100644 (file)
@@ -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
 
index 71f4a3d531edeaa72e11e8bad9961cab9f684450..017ab2a5c69fddd1fb135b0db3d2b66b0487eb42 100644 (file)
@@ -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));
 }
 
 
index 18eddf71bc8ee9b843fac302f274bcdd62751d37..182d341e79791b7a2fa8b5d4e73bb061614d0c75 100644 (file)
@@ -1,3 +1,9 @@
+2004-11-09  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * 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  <larsbj@gullik.net>
 
        * Make it clearer where include files are comming from. 
index c3cc31cd60eb548ef27403caa914340c5c0f2368..776f89c1136d3e42e647c676310ae3c97d99d61b 100644 (file)
@@ -35,7 +35,6 @@
 // FIXME Interface violation
 #include "gettext.h"
 #include "debug.h"
-//#include "format.h"
 
 #include <boost/assert.hpp>
 #include <boost/regex.hpp>
@@ -896,25 +895,6 @@ string const GetExtension(string const & name)
 }
 
 
-#if 0
-namespace {
-
-class FormatExtensionsEqual : public std::unary_function<Format, bool> {
-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();
 }
 
 
index f7b80c6c18fcc79b13cfa4de1e5c22368de8dbd2..6b4c4b1edfaa136151dfe7edf621f46f87d78a0b 100644 (file)
@@ -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
index 3dfd8ff984e36ecba51fd14f6b2162d26b303ff2..a064eed91d52363b46a40ca155816f5f35ad345f 100644 (file)
@@ -1,3 +1,7 @@
+2004-11-09  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * tex2lyx.C: remove format hack
+
 2004-11-07  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
 
        * texparser.C (getNewline): new
index 0d44d989d19c2f71c86717845a5f96d0414ba48a..264ba0491208d9620922e79a5f7a7fd2632b3941 100644 (file)
@@ -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);