diff --git a/src/ChangeLog b/src/ChangeLog index dc4fac761f..72e7e17643 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2004-10-29 Georg Baum + + * buffer.C, lyxlex_pimpl.C: + * lyxlex_pimpl.C (setFile): + s/getExtFromContents/getFormatFromContents/ + 2004-10-28 José Matos * output_docbook.C (makeEnvironment): move id to broadest possible scope. diff --git a/src/buffer.C b/src/buffer.C index 0f400fbd12..2fdf62c6d1 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -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; } diff --git a/src/client/ChangeLog b/src/client/ChangeLog index e3b55c7bc2..92187295d7 100644 --- a/src/client/ChangeLog +++ b/src/client/ChangeLog @@ -1,3 +1,7 @@ +2004-10-29 Georg Baum + + * client.C (formats): new, needed for libsupport.a + 2004-09-17 Lars Gullik Bjonnes * client.C (connect): don't use SUN_PATH, just use sizeof instead. diff --git a/src/client/client.C b/src/client/client.C index 8339c1fe59..64c2fcc711 100644 --- a/src/client/client.C +++ b/src/client/client.C @@ -13,6 +13,7 @@ #include #include "debug.h" +#include "format.h" #include "support/lstrings.h" #include @@ -53,6 +54,10 @@ using std::cin; using std::endl; +// hack to link in libsupport +Formats formats; + + namespace support { string itoa(unsigned int i) diff --git a/src/format.h b/src/format.h index 3751b6d0af..9b5cdec982 100644 --- a/src/format.h +++ b/src/format.h @@ -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; diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 2e044193e9..58ffde7965 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,7 @@ +2004-10-29 Georg Baum + + * ControlInclude.C: s/getExtFromContents/getFormatFromContents/ + 2004-10-26 Angus Leeming * ControlPrefs.[Ch]: add code to interface with the Movers. diff --git a/src/frontends/controllers/ControlInclude.C b/src/frontends/controllers/ControlInclude.C index d5c52ffb34..d09bf7ac3c 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 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 diff --git a/src/graphics/ChangeLog b/src/graphics/ChangeLog index 291d5fc33b..c5435fd2a3 100644 --- a/src/graphics/ChangeLog +++ b/src/graphics/ChangeLog @@ -1,3 +1,8 @@ +2004-10-29 Georg Baum + + * GraphicsCacheItem.C: s/getExtFromContents/getFormatFromContents/ + * GraphicsImage.h (loadableFormats): correct documentation + 2004-10-01 Angus Leeming * GraphicsConverter.C (move_file, build_script): protect diff --git a/src/graphics/GraphicsCacheItem.C b/src/graphics/GraphicsCacheItem.C index d58057665f..5071e0d5e0 100644 --- a/src/graphics/GraphicsCacheItem.C +++ b/src/graphics/GraphicsCacheItem.C @@ -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); diff --git a/src/graphics/GraphicsImage.h b/src/graphics/GraphicsImage.h index 01d53b41cf..5364aa1dbb 100644 --- a/src/graphics/GraphicsImage.h +++ b/src/graphics/GraphicsImage.h @@ -45,9 +45,9 @@ public: /// static boost::function newImage; - /// Return the list of loadable formats. - typedef std::vector FormatList; /// + typedef std::vector FormatList; + /// Return the list of loadable formats. static boost::function loadableFormats; /// diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 9aeb767886..0bd9e7c3d0 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,9 @@ +2004-10-29 Georg Baum + + * ExternalSupport.C: s/getExtFromContents/getFormatFromContents/ + * insetgraphics.C: s/getExtFromContents/getFormatFromContents/ + * insetgraphics.C (findTargetFormat): reformat + 2004-10-28 José Matos * insetcharstyle.C (linuxdoc, docbook): apply opentTag and closeTag. diff --git a/src/insets/ExternalSupport.C b/src/insets/ExternalSupport.C index c12ebaaa15..b60fee1f02 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::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 diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index ec239f1b45..956819d798 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -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)); } diff --git a/src/lyxlex_pimpl.C b/src/lyxlex_pimpl.C index a13a9fa6d6..af2b2577a4 100644 --- a/src/lyxlex_pimpl.C +++ b/src/lyxlex_pimpl.C @@ -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; diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 03836db8a2..e03a459cb8 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,8 @@ +2004-10-29 Georg Baum + + * filetools.[Ch] (getExtFromContents): rename to + getFormatFromContents and return always a format + 2004-10-25 Angus Leeming * forkedcall.C (generateChild): strip quotes from each argument diff --git a/src/support/filetools.C b/src/support/filetools.C index d842690185..265583d074 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -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 { +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"); diff --git a/src/support/filetools.h b/src/support/filetools.h index b145082dd0..f7b80c6c18 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -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); diff --git a/src/tex2lyx/ChangeLog b/src/tex2lyx/ChangeLog index 93dc72013d..43a069f19c 100644 --- a/src/tex2lyx/ChangeLog +++ b/src/tex2lyx/ChangeLog @@ -1,3 +1,7 @@ +2004-10-29 Georg Baum + + * tex2lyx.C (formats): new, needed for libsupport.a + 2004-10-20 Georg Baum * text.C (parse_text): replace newlines with spaces in arguments to diff --git a/src/tex2lyx/tex2lyx.C b/src/tex2lyx/tex2lyx.C index 264ba04912..0d44d989d1 100644 --- a/src/tex2lyx/tex2lyx.C +++ b/src/tex2lyx/tex2lyx.C @@ -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);