rename getExtFromContents() to getFormatFromContents()

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9141 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2004-10-29 15:47:55 +00:00
parent a86b3a1c84
commit 6fe9e4e30d
19 changed files with 122 additions and 48 deletions

View File

@ -1,3 +1,9 @@
2004-10-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* buffer.C, lyxlex_pimpl.C:
* lyxlex_pimpl.C (setFile):
s/getExtFromContents/getFormatFromContents/
2004-10-28 José Matos <jamatos@lyx.org>
* output_docbook.C (makeEnvironment): move id to broadest possible scope.

View File

@ -93,7 +93,7 @@ using lyx::support::createBufferTmpDir;
using lyx::support::destroyDir;
using lyx::support::FileInfo;
using lyx::support::FileInfo;
using lyx::support::getExtFromContents;
using lyx::support::getFormatFromContents;
using lyx::support::IsDirWriteable;
using lyx::support::IsFileWriteable;
using lyx::support::LibFileSearch;
@ -532,7 +532,7 @@ void Buffer::insertStringAsLines(ParagraphList & pars,
bool Buffer::readFile(string const & filename)
{
// Check if the file is compressed.
string const format = getExtFromContents(filename);
string const format = getFormatFromContents(filename);
if (format == "gzip" || format == "zip" || format == "compress") {
params().compressed = true;
}

View File

@ -1,3 +1,7 @@
2004-10-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* client.C (formats): new, needed for libsupport.a
2004-09-17 Lars Gullik Bjonnes <larsbj@gullik.net>
* client.C (connect): don't use SUN_PATH, just use sizeof instead.

View File

@ -13,6 +13,7 @@
#include <config.h>
#include "debug.h"
#include "format.h"
#include "support/lstrings.h"
#include <boost/filesystem/operations.hpp>
@ -53,6 +54,10 @@ using std::cin;
using std::endl;
// hack to link in libsupport
Formats formats;
namespace support {
string itoa(unsigned int i)

View File

@ -84,7 +84,7 @@ public:
Format const & get(FormatList::size_type i) const {
return formatlist[i];
}
///
/// \returns format named \p name if it exists, otherwise 0
Format const * getFormat(std::string const & name) const;
///
int getNumber(std::string const & name) const;

View File

@ -1,3 +1,7 @@
2004-10-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* ControlInclude.C: s/getExtFromContents/getFormatFromContents/
2004-10-26 Angus Leeming <leeming@lyx.org>
* ControlPrefs.[Ch]: add code to interface with the Movers.

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 ext = support::getExtFromContents(file);
if (ext == "lyx")
string const format = support::getFormatFromContents(file);
if (format == "lyx")
kernel().dispatch(FuncRequest(LFUN_CHILDOPEN, file));
else
// tex file or other text file in verbatim mode

View File

@ -1,3 +1,8 @@
2004-10-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* GraphicsCacheItem.C: s/getExtFromContents/getFormatFromContents/
* GraphicsImage.h (loadableFormats): correct documentation
2004-10-01 Angus Leeming <leeming@lyx.org>
* GraphicsConverter.C (move_file, build_script): protect

View File

@ -32,7 +32,7 @@ using support::FileMonitor;
using support::IsFileReadable;
using support::MakeDisplayPath;
using support::OnlyFilename;
using support::getExtFromContents;
using support::getFormatFromContents;
using support::tempName;
using support::unlink;
using support::unzipFile;
@ -401,7 +401,7 @@ void CacheItem::Impl::convertToDisplayFormat()
<< "\n\twith displayed filename: " << displayed_filename
<< endl;
string from = getExtFromContents(filename);
string from = getFormatFromContents(filename);
lyxerr[Debug::GRAPHICS]
<< "\n\tThe file contains " << from << " format data." << endl;
string const to = findTargetFormat(from);

View File

@ -45,9 +45,9 @@ public:
///
static boost::function<ImagePtr()> newImage;
/// Return the list of loadable formats.
typedef std::vector<std::string> FormatList;
///
typedef std::vector<std::string> FormatList;
/// Return the list of loadable formats.
static boost::function<FormatList()> loadableFormats;
///

View File

@ -1,3 +1,9 @@
2004-10-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* ExternalSupport.C: s/getExtFromContents/getFormatFromContents/
* insetgraphics.C: s/getExtFromContents/getFormatFromContents/
* insetgraphics.C (findTargetFormat): reformat
2004-10-28 José Matos <jamatos@lyx.org>
* insetcharstyle.C (linuxdoc, docbook): apply opentTag and closeTag.

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::getExtFromContents(file_with_path));
support::getFormatFromContents(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::getExtFromContents(abs_from_file);
from_format = support::getFormatFromContents(abs_from_file);
if (from_format.empty())
return; // FAILURE

View File

@ -95,7 +95,7 @@ using lyx::support::contains;
using lyx::support::FileName;
using lyx::support::float_equal;
using lyx::support::GetExtension;
using lyx::support::getExtFromContents;
using lyx::support::getFormatFromContents;
using lyx::support::IsFileReadable;
using lyx::support::LibFileSearch;
using lyx::support::OnlyFilename;
@ -133,22 +133,27 @@ string const uniqueID()
}
string findTargetFormat(string const & suffix, OutputParams const & runparams)
string findTargetFormat(string const & format, OutputParams const & runparams)
{
// Are we using latex or pdflatex).
// Are we using latex or pdflatex?
if (runparams.flavor == OutputParams::PDFLATEX) {
lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode" << endl;
if (contains(suffix, "ps") || suffix == "pdf")
// Convert postscript to pdf
if (format == "eps" || format == "ps")
return "pdf";
if (suffix == "jpg") // pdflatex can use jpeg
return suffix;
return "png"; // and also png
// pdflatex can use jpeg, png and pdf directly
if (format == "jpg" || format == "pdf")
return format;
// Convert everything else to png
return "png";
}
// If it's postscript, we always do eps.
lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode" << endl;
if (suffix != "ps") // any other than ps
return "eps"; // is changed to eps
return suffix; // let ps untouched
if (format != "ps")
// any other than ps is changed to eps
return "eps";
// let ps untouched
return format;
}
} // namespace anon
@ -455,7 +460,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(getExtFromContents(file_in));
Mover const & mover = movers(getFormatFromContents(file_in));
bool const success = mover.copy(file_in, file_out);
if (!success) {
lyxerr[Debug::GRAPHICS]
@ -619,8 +624,9 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
}
}
string const from = getExtFromContents(temp_file);
string const from = getFormatFromContents(temp_file);
string const to = findTargetFormat(from, runparams);
string const ext = formats.extension(to);
lyxerr[Debug::GRAPHICS]
<< "\t we have: from " << from << " to " << to << '\n';
@ -632,7 +638,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
<< "\tthe orig file is: " << orig_file << endl;
if (from == to) {
// The extension of temp_file might be != to!
// The extension of temp_file might be != ext!
runparams.exportdata->addExternalFile("latex", source_file,
output_file);
runparams.exportdata->addExternalFile("dvi", source_file,
@ -640,8 +646,8 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
return stripExtensionIfPossible(output_file, to);
}
string const to_file = ChangeExtension(temp_file, to);
string const output_to_file = ChangeExtension(output_file, to);
string const to_file = ChangeExtension(temp_file, ext);
string const output_to_file = ChangeExtension(output_file, ext);
// Do we need to perform the conversion?
// Yes if to_file does not exist or if temp_file is newer than to_file
@ -668,8 +674,8 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
if (!converters.convert(&buf, temp_file, temp_file, from, to)) {
string const command =
"sh " + LibFileSearch("scripts", "convertDefault.sh") +
' ' + from + ':' + temp_file + ' ' +
to + ':' + to_file;
' ' + formats.extension(from) + ':' + temp_file +
' ' + ext + ':' + to_file;
lyxerr[Debug::GRAPHICS]
<< "No converter defined! I use convertDefault.sh:\n\t"
<< command << endl;
@ -899,7 +905,7 @@ InsetGraphicsParams const & InsetGraphics::params() 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, getExtFromContents(file_with_path));
formats.edit(buffer, file_with_path, getFormatFromContents(file_with_path));
}

View File

@ -21,7 +21,7 @@
#include "support/lstrings.h"
using lyx::support::compare_ascii_no_case;
using lyx::support::getExtFromContents;
using lyx::support::getFormatFromContents;
using lyx::support::MakeDisplayPath;
using lyx::support::split;
using lyx::support::subst;
@ -131,9 +131,8 @@ void LyXLex::Pimpl::popTable()
bool LyXLex::Pimpl::setFile(string const & filename)
{
// Check the format of the file.
string const format = getExtFromContents(filename);
string const format = getFormatFromContents(filename);
if (format == "gzip" || format == "zip" || format == "compress") {
lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;

View File

@ -1,3 +1,8 @@
2004-10-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* filetools.[Ch] (getExtFromContents): rename to
getFormatFromContents and return always a format
2004-10-25 Angus Leeming <leeming@lyx.org>
* forkedcall.C (generateChild): strip quotes from each argument

View File

@ -26,6 +26,7 @@
#include "support/systemcall.h"
#include "filetools.h"
#include "format.h"
#include "lstrings.h"
#include "FileInfo.h"
#include "forkedcontr.h"
@ -893,6 +894,24 @@ string const GetExtension(string const & name)
return string();
}
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
// the different filetypes and what they contain in one of the first lines
// (dots are any characters). (Herbert 20020131)
// AGR Grace...
@ -923,7 +942,7 @@ string const GetExtension(string const & name)
/// 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 getExtFromContents(string const & filename)
string const getFormatFromContents(string const & filename)
{
// paranoia check
if (filename.empty() || !IsFileReadable(filename))
@ -953,7 +972,7 @@ string const getExtFromContents(string const & filename)
while ((count++ < max_count) && format.empty()) {
if (ifs.eof()) {
lyxerr[Debug::GRAPHICS]
<< "filetools(getExtFromContents)\n"
<< "filetools(getFormatFromContents)\n"
<< "\tFile type not recognised before EOF!"
<< endl;
break;
@ -1070,25 +1089,31 @@ string const getExtFromContents(string const & filename)
string const ext(GetExtension(filename));
lyxerr[Debug::GRAPHICS]
<< "filetools(getExtFromContents)\n"
<< "\tCouldn't find a known Type!\n";
<< "filetools(getFormatFromContents)\n"
<< "\tCouldn't find a known format!\n";
if (!ext.empty()) {
lyxerr[Debug::GRAPHICS]
<< "\twill take the file extension -> "
<< ext << endl;
return ext;
} else {
lyxerr[Debug::GRAPHICS]
<< "\twill use ext or a \"user\" defined format" << endl;
return "user";
// 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();
}
}
lyxerr[Debug::GRAPHICS]
<< "\twill use a \"user\" defined format" << endl;
return "user";
}
/// check for zipped file
bool zippedFile(string const & name)
{
string const type = getExtFromContents(name);
string const type = getFormatFromContents(name);
if (contains("gzip zip compress", type) && !type.empty())
return true;
return false;
@ -1303,7 +1328,7 @@ string const readBB_from_PSFile(string const & file)
bool zipped = zippedFile(file);
string const file_ = zipped ?
string(unzipFile(file)) : string(file);
string const format = getExtFromContents(file_);
string const format = getFormatFromContents(file_);
if (format != "eps" && format != "ps") {
readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");

View File

@ -140,8 +140,8 @@ 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);
/// Return the type of the file as an extension from contents
std::string const getExtFromContents(std::string const & name);
/// Guess the file format name (as in Format::name()) from contents
std::string const getFormatFromContents(std::string const & name);
/// check for zipped file
bool zippedFile(std::string const & name);

View File

@ -1,3 +1,7 @@
2004-10-29 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* tex2lyx.C (formats): new, needed for libsupport.a
2004-10-20 Georg Baum <Georg.Baum@post.rwth-aachen.de>
* text.C (parse_text): replace newlines with spaces in arguments to

View File

@ -16,6 +16,7 @@
#include "context.h"
#include "debug.h"
#include "format.h"
#include "lyxtextclass.h"
#include "support/path_defines.h"
#include "support/filetools.h"
@ -55,6 +56,10 @@ 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);