Lars Gullik Bjønnes wrote:

> Georg Baum <[EMAIL PROTECTED]>
> writes:
> | Would it be ok with you to have something like
> | Format const *Formats::getFormatFromFile(string filename)
> | that does what getFormatFromContents() did before your patch?
> 
> Yes.
> 
> One solution will be a wrapper function
> 
> string discoverFormat(string const & filename)
> {      
>        string fmt = getFormatFromFile(filename);
>        if (fmt == "user) {
>           // try to get format from the Formats
>        }
>        return fmt;
> }

Why a wrapper function? We never need the part that gets the format from
file extension alone. I made getFormatFromFile() a member of Formats. It
could as well be a freestanding function, but then we could not distinguish
between formats and systemformats (not needed now, but who knows...).
We never need the variant returning a Format * either, so I changed that to
string.

Here comes the patch. I removed the useless "user" format in the process.
Now getFormatFromContents() either returns a format name or an empty string
if it cannot determine the format. I even found two cases where the return
value of getFormatFromContents() needed to be checked.
Ok to apply?


Georg
Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.2029
diff -u -p -r1.2029 ChangeLog
--- src/ChangeLog	8 Nov 2004 10:54:27 -0000	1.2029
+++ src/ChangeLog	8 Nov 2004 18:22:33 -0000
@@ -1,3 +1,8 @@
+2004-11-08  Georg Baum  <[EMAIL PROTECTED]>
+
+	* format.[Ch] (getFormatFromFile): new method
+	* exporter.C: s/getFormatFromContents/formats.getFormatFromFile/
+
 2004-11-05  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
 
 	* lyxfunc.C (dispatch): remove the verbose argument
Index: src/exporter.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/exporter.C,v
retrieving revision 1.55
diff -u -p -r1.55 exporter.C
--- src/exporter.C	1 Nov 2004 08:50:42 -0000	1.55
+++ src/exporter.C	8 Nov 2004 18:22:33 -0000
@@ -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 +210,8 @@ bool Exporter::Export(Buffer * buffer, s
 		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: src/format.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/format.C,v
retrieving revision 1.24
diff -u -p -r1.24 format.C
--- src/format.C	5 Oct 2004 10:11:27 -0000	1.24
+++ src/format.C	8 Nov 2004 18:22:33 -0000
@@ -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)
@@ -107,6 +120,34 @@ Format const * Formats::getFormat(string
 		return &(*cit);
 	else
 		return 0;
+}
+
+
+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();
 }
 
 
Index: src/format.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/format.h,v
retrieving revision 1.9
diff -u -p -r1.9 format.h
--- src/format.h	29 Oct 2004 15:47:53 -0000	1.9
+++ src/format.h	8 Nov 2004 18:22:33 -0000
@@ -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: src/client/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/client/ChangeLog,v
retrieving revision 1.5
diff -u -p -r1.5 ChangeLog
--- src/client/ChangeLog	29 Oct 2004 15:47:53 -0000	1.5
+++ src/client/ChangeLog	8 Nov 2004 18:22:34 -0000
@@ -1,3 +1,7 @@
+2004-11-08  Georg Baum  <[EMAIL PROTECTED]>
+
+	* client.C: remove format hack
+
 2004-10-29  Georg Baum  <[EMAIL PROTECTED]>
 
 	* client.C (formats): new, needed for libsupport.a
Index: src/client/client.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/client/client.C,v
retrieving revision 1.4
diff -u -p -r1.4 client.C
--- src/client/client.C	29 Oct 2004 15:47:53 -0000	1.4
+++ src/client/client.C	8 Nov 2004 18:22:34 -0000
@@ -13,7 +13,6 @@
 #include <config.h>
 
 #include "debug.h"
-#include "format.h"
 #include "support/lstrings.h"
 
 #include <boost/filesystem/operations.hpp>
@@ -54,10 +54,6 @@ using std::cin;
 using std::endl;
 
 
-// hack to link in libsupport 
-Formats formats;
-
-
 namespace support {
 
 string itoa(unsigned int i)
Index: src/frontends/controllers/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ChangeLog,v
retrieving revision 1.454
diff -u -p -r1.454 ChangeLog
--- src/frontends/controllers/ChangeLog	8 Nov 2004 10:54:28 -0000	1.454
+++ src/frontends/controllers/ChangeLog	8 Nov 2004 18:22:35 -0000
@@ -1,3 +1,12 @@
+2002-06-21  Herbert Voss  <[EMAIL PROTECTED]>
+
+	* ControlListing.[Ch]:
+	* Makefile.am: new gui for inserting sourcecode into a lyxdoc
+
+2004-11-08  Georg Baum  <[EMAIL PROTECTED]>
+
+	* ControlMath.C: add mod, pmod and pod
+
 2004-11-05  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
 
 	* ControlRef.C (gotoRef, gotoBookmark): 
Index: src/frontends/controllers/ControlInclude.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ControlInclude.C,v
retrieving revision 1.47
diff -u -p -r1.47 ControlInclude.C
--- src/frontends/controllers/ControlInclude.C	29 Oct 2004 15:47:54 -0000	1.47
+++ src/frontends/controllers/ControlInclude.C	8 Nov 2004 18:22:35 -0000
@@ -99,8 +101,8 @@ string const ControlInclude::browse(stri
 
 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: src/graphics/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/graphics/ChangeLog,v
retrieving revision 1.210
diff -u -p -r1.210 ChangeLog
--- src/graphics/ChangeLog	29 Oct 2004 15:47:54 -0000	1.210
+++ src/graphics/ChangeLog	8 Nov 2004 18:22:41 -0000
@@ -1,3 +1,8 @@
+2004-11-08  Georg Baum  <[EMAIL PROTECTED]>
+
+	* GraphicsCacheItem.C:
+	s/getFormatFromContents/formats.getFormatFromFile/
+
 2004-10-29  Georg Baum  <[EMAIL PROTECTED]>
 
 	* GraphicsCacheItem.C: s/getExtFromContents/getFormatFromContents/
Index: src/graphics/GraphicsCacheItem.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/graphics/GraphicsCacheItem.C,v
retrieving revision 1.63
diff -u -p -r1.63 GraphicsCacheItem.C
--- src/graphics/GraphicsCacheItem.C	29 Oct 2004 15:47:54 -0000	1.63
+++ src/graphics/GraphicsCacheItem.C	8 Nov 2004 18:22:41 -0000
@@ -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::convertToDisplayFo
 		<< "\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: src/insets/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/ChangeLog,v
retrieving revision 1.1072
diff -u -p -r1.1072 ChangeLog
--- src/insets/ChangeLog	5 Nov 2004 15:08:58 -0000	1.1072
+++ src/insets/ChangeLog	8 Nov 2004 18:22:43 -0000
@@ -1,3 +1,8 @@
+2004-11-08  Georg Baum  <[EMAIL PROTECTED]>
+
+	* ExternalSupport.C, insetgraphics.C:
+	s/getFormatFromContents/formats.getFormatFromFile/
+
 2004-11-04  Jürgen Spitzmüller  <[EMAIL PROTECTED]>
 
 	* insetcharstyle.[Ch]:
Index: src/insets/ExternalSupport.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/ExternalSupport.C,v
retrieving revision 1.12
diff -u -p -r1.12 ExternalSupport.C
--- src/insets/ExternalSupport.C	1 Nov 2004 08:50:42 -0000	1.12
+++ src/insets/ExternalSupport.C	8 Nov 2004 18:22:43 -0000
@@ -56,7 +56,7 @@ void editExternal(InsetExternalParams co
 {
 	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 
 			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: src/insets/insetgraphics.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetgraphics.C,v
retrieving revision 1.262
diff -u -p -r1.262 insetgraphics.C
--- src/insets/insetgraphics.C	29 Oct 2004 23:08:03 -0000	1.262
+++ src/insets/insetgraphics.C	8 Nov 2004 18:22:43 -0000
@@ -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,
 		// 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(
 		}
 	}
 
-	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 & InsetGraphic
 }
 
 
-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: src/support/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/ChangeLog,v
retrieving revision 1.272
diff -u -p -r1.272 ChangeLog
--- src/support/ChangeLog	7 Nov 2004 13:22:50 -0000	1.272
+++ src/support/ChangeLog	8 Nov 2004 18:22:46 -0000
@@ -1,3 +1,9 @@
+2004-11-08  Georg Baum  <[EMAIL PROTECTED]>
+
+	* 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  <[EMAIL PROTECTED]>
 
 	* Make it clearer where include files are comming from. 
Index: src/support/filetools.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/filetools.C,v
retrieving revision 1.190
diff -u -p -r1.190 filetools.C
--- src/support/filetools.C	7 Nov 2004 13:22:51 -0000	1.190
+++ src/support/filetools.C	8 Nov 2004 18:22:46 -0000
@@ -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 +926,6 @@ string const GetExtension(string const &
 }
 
 
-#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 +953,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 +1098,10 @@ string const getFormatFromContents(strin
 		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: src/support/filetools.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/filetools.h,v
retrieving revision 1.55
diff -u -p -r1.55 filetools.h
--- src/support/filetools.h	29 Oct 2004 15:47:55 -0000	1.55
+++ src/support/filetools.h	8 Nov 2004 18:22:47 -0000
@@ -140,7 +150,10 @@ ChangeExtension(std::string const & oldn
 /// 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: src/tex2lyx/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/ChangeLog,v
retrieving revision 1.62
diff -u -p -r1.62 ChangeLog
--- src/tex2lyx/ChangeLog	8 Nov 2004 08:24:43 -0000	1.62
+++ src/tex2lyx/ChangeLog	8 Nov 2004 18:22:47 -0000
@@ -1,3 +1,7 @@
+2004-11-08  Georg Baum  <[EMAIL PROTECTED]>
+
+	* tex2lyx.C: remove format hack
+
 2004-11-07  Georg Baum  <[EMAIL PROTECTED]>
 
 	* texparser.C (getNewline): new
Index: src/tex2lyx/tex2lyx.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/tex2lyx.C,v
retrieving revision 1.58
diff -u -p -r1.58 tex2lyx.C
--- src/tex2lyx/tex2lyx.C	29 Oct 2004 15:47:55 -0000	1.58
+++ src/tex2lyx/tex2lyx.C	8 Nov 2004 18:22:48 -0000
@@ -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 +56,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);

Reply via email to