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
This commit is contained in:
Georg Baum 2004-11-09 19:08:34 +00:00
parent a899dc13c9
commit dacd4cea26
18 changed files with 112 additions and 71 deletions

View 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)

View 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

View 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)

View 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);

View 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 =

View 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;
///

View 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):

View 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

View 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/

View 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);

View 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]:

View 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

View 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));
}

View 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.

View 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();
}

View 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

View 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

View 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);