/**
* \file tex_helpers.C
- * Copyright 1995-2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * \author Herbert Voss <voss@lyx.org>
+ * \author Herbert Voß
+ *
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-#include <vector.h>
-#include <fstream>
-#include <algorithm>
+#include "tex_helpers.h"
+
+#include "debug.h"
#include "gettext.h"
-#include "tex_helpers.h"
+#include "frontends/Alert.h"
+
#include "support/filetools.h"
-#include "debug.h"
#include "support/lstrings.h"
-#include "support/systemcall.h"
-#include "support/path.h"
#include "support/lyxalgo.h"
+#include "support/os.h"
+#include "support/package.h"
+#include "support/path.h"
+#include "support/systemcall.h"
-using std::vector;
-using std::sort;
-using std::unique;
+#include <boost/cregex.hpp>
+#include <fstream>
-extern string user_lyxdir; // home of *Files.lst
+using std::string;
+using std::endl;
-namespace {
+namespace lyx {
-vector<string> listWithoutPath(vector<string> & dbase)
-{
- vector<string>::iterator it = dbase.begin();
- vector<string>::iterator end = dbase.end();
- for (; it != end; ++it)
- *it = OnlyFilename(*it);
- return dbase;
-}
+using support::bformat;
+using support::contains;
+using support::FileName;
+using support::getExtension;
+using support::getFileContents;
+using support::getVectorFromString;
+using support::libFileSearch;
+using support::onlyFilename;
+using support::package;
+using support::Path;
+using support::quoteName;
+using support::split;
+using support::Systemcall;
+using support::token;
-}
+namespace frontend {
-// build filelists of all availabe bst/cls/sty-files. done through
-// kpsewhich and an external script, saved in *Files.lst
void rescanTexStyles()
{
// Run rescan in user lyx directory
- Path p(user_lyxdir);
+ Path p(package().user_support());
+ FileName const command = libFileSearch("scripts", "TeXFiles.py");
Systemcall one;
- one.startscript(Systemcall::Wait,
- LibFileSearch("scripts", "TeXFiles.sh"));
- p.pop();
+ int const status = one.startscript(Systemcall::Wait,
+ lyx::support::os::python() + ' ' +
+ quoteName(command.toFilesystemEncoding()));
+ if (status == 0)
+ return;
+ // FIXME UNICODE
+ Alert::error(_("Could not update TeX information"),
+ bformat(_("The script `%s' failed."), lyx::from_utf8(command.absFilename())));
}
void texhash()
{
// Run texhash in user lyx directory
- Path p(user_lyxdir);
+ Path p(package().user_support());
//path to texhash through system
Systemcall one;
- one.startscript(Systemcall::Wait,"texhash");
- p.pop();
+ one.startscript(Systemcall::Wait,"texhash");
}
-string const getTexFileList(string const & filename, bool withFullPath)
+
+void getTexFileList(string const & filename, std::vector<string> & list)
{
- vector<string> dbase = getVectorFromString(
- GetFileContents(LibFileSearch(string(),filename)), "\n");
- lyx::eliminate_duplicates(dbase);
- string const str_out = withFullPath ?
- getStringFromVector(dbase, "\n") :
- getStringFromVector(listWithoutPath(dbase), "\n");
- // everything ok?
- if (str_out.empty())
- return _("Missing filelist. try Rescan");
- return str_out;
+ list.clear();
+ FileName const file = libFileSearch("", filename);
+ if (file.empty())
+ return;
+
+ list = getVectorFromString(getFileContents(file), "\n");
+
+ // Normalise paths like /foo//bar ==> /foo/bar
+ boost::RegEx regex("/{2,}");
+ std::vector<string>::iterator it = list.begin();
+ std::vector<string>::iterator end = list.end();
+ for (; it != end; ++it) {
+ *it = regex.Merge((*it), "/");
+ }
+
+ // remove empty items and duplicates
+ list.erase(std::remove(list.begin(), list.end(), ""), list.end());
+ eliminate_duplicates(list);
}
-string const getListOfOptions(string const & classname,
- string const & type)
+string const getListOfOptions(string const & classname, string const & type)
{
- string const filename = getTexFileFromList(classname,type);
+ FileName const filename(getTexFileFromList(classname, type));
+ if (filename.empty())
+ return string();
string optionList = string();
- std::ifstream is(filename.c_str());
+ std::ifstream is(filename.toFilesystemEncoding().c_str());
while (is) {
- string s;
- is >> s;
- if (contains(s,"DeclareOption")) {
- s = s.substr(s.find("DeclareOption"));
- s = split(s,'{'); // cut front
- s = token(s,'}',0); // cut end
-// FIXME: why is this commented out ?
-// s = s.substr(0, s.find('}')+1); // format entry
- optionList += (s + '\n');
- }
+ string s;
+ is >> s;
+ if (contains(s,"DeclareOption")) {
+ s = s.substr(s.find("DeclareOption"));
+ s = split(s,'{'); // cut front
+ s = token(s,'}',0); // cut end
+ optionList += (s + '\n');
+ }
}
return optionList;
}
-string const getTexFileFromList(string const & file,
+string const getTexFileFromList(string const & file,
string const & type)
{
- string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
-
- lyxerr << "Search for classfile " << file_ << endl;
-
- string const lstfile = (type == "cls") ? "clsFiles.lst" : "styFiles.lst";
- string const allClasses = GetFileContents(LibFileSearch(string(), lstfile));
+ string file_ = file;
+ // do we need to add the suffix?
+ if (!(getExtension(file) == type))
+ file_ += '.' + type;
+
+ lyxerr << "Searching for file " << file_ << endl;
+
+ string lstfile;
+ if (type == "cls")
+ lstfile = "clsFiles.lst";
+ else if (type == "sty")
+ lstfile = "styFiles.lst";
+ else if (type == "bst")
+ lstfile = "bstFiles.lst";
+ else if (type == "bib")
+ lstfile = "bibFiles.lst";
+ FileName const abslstfile = libFileSearch(string(), lstfile);
+ if (abslstfile.empty()) {
+ lyxerr << "File `'" << lstfile << "' not found." << endl;
+ return string();
+ }
+ string const allClasses = getFileContents(abslstfile);
int entries = 0;
string classfile = token(allClasses, '\n', entries);
int count = 0;
- while ((!contains(classfile, file) ||
- (OnlyFilename(classfile) != file)) &&
+ while ((!contains(classfile, file) ||
+ (onlyFilename(classfile) != file)) &&
(++count < 1000)) {
classfile = token(allClasses, '\n', ++entries);
}
-
+
// now we have filename with full path
lyxerr << "with full path: " << classfile << endl;
+
return classfile;
}
+
+} // namespace frontend
+} // namespace lyx