]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / tex_helpers.C
1 /**
2  * \file tex_helpers.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Herbert Voß
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "tex_helpers.h"
14
15 #include "debug.h"
16
17 #include "support/filetools.h"
18 #include "support/lstrings.h"
19 #include "support/lyxalgo.h"
20 #include "support/package.h"
21 #include "support/path.h"
22 #include "support/systemcall.h"
23
24 #include <boost/cregex.hpp>
25 #include <fstream>
26
27 using std::string;
28 using std::endl;
29
30 namespace lyx {
31
32 using support::contains;
33 using support::GetExtension;
34 using support::GetFileContents;
35 using support::getVectorFromString;
36 using support::LibFileSearch;
37 using support::OnlyFilename;
38 using support::package;
39 using support::Path;
40 using support::QuoteName;
41 using support::split;
42 using support::Systemcall;
43 using support::token;
44
45 namespace frontend {
46
47 // build filelists of all availabe bst/cls/sty-files. done through
48 // kpsewhich and an external script, saved in *Files.lst
49 void rescanTexStyles()
50 {
51         // Run rescan in user lyx directory
52         Path p(package().user_support());
53         Systemcall one;
54         one.startscript(Systemcall::Wait,
55                         "sh " +
56                         QuoteName(LibFileSearch("scripts", "TeXFiles.sh")));
57 }
58
59
60 void texhash()
61 {
62         // Run texhash in user lyx directory
63         Path p(package().user_support());
64
65         //path to texhash through system
66         Systemcall one;
67         one.startscript(Systemcall::Wait,"texhash");
68 }
69
70
71 void getTexFileList(string const & filename, std::vector<string> & list)
72 {
73         list.clear();
74         string const file = LibFileSearch("", filename);
75         if (file.empty())
76                 return;
77
78         list = getVectorFromString(GetFileContents(file), "\n");
79
80         // Normalise paths like /foo//bar ==> /foo/bar
81         boost::RegEx regex("/{2,}");
82         std::vector<string>::iterator it  = list.begin();
83         std::vector<string>::iterator end = list.end();
84         for (; it != end; ++it) {
85                 *it = regex.Merge((*it), "/");
86         }
87
88         // remove empty items and duplicates
89         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
90         eliminate_duplicates(list);
91 }
92
93
94 string const getListOfOptions(string const & classname, string const & type)
95 {
96         string const filename = getTexFileFromList(classname,type);
97         string optionList = string();
98         std::ifstream is(filename.c_str());
99         while (is) {
100                 string s;
101                 is >> s;
102                 if (contains(s,"DeclareOption")) {
103                         s = s.substr(s.find("DeclareOption"));
104                         s = split(s,'{');               // cut front
105                         s = token(s,'}',0);             // cut end
106                         optionList += (s + '\n');
107                 }
108         }
109         return optionList;
110 }
111
112
113 string const getTexFileFromList(string const & file,
114                             string const & type)
115 {
116         string file_ = file;
117         // do we need to add the suffix?
118         if (!(GetExtension(file) == type))
119                 file_ += '.' + type;
120
121         lyxerr << "Searching for file " << file_ << endl;
122
123         string lstfile;
124         if (type == "cls")
125                 lstfile = "clsFiles.lst";
126         else if (type == "sty")
127                 lstfile = "styFiles.lst";
128         else if (type == "bst")
129                 lstfile = "bstFiles.lst";
130         else if (type == "bib")
131                 lstfile = "bibFiles.lst";
132         string const allClasses = GetFileContents(LibFileSearch(string(),
133                                                                 lstfile));
134         int entries = 0;
135         string classfile = token(allClasses, '\n', entries);
136         int count = 0;
137         while ((!contains(classfile, file) ||
138                 (OnlyFilename(classfile) != file)) &&
139                 (++count < 1000)) {
140                 classfile = token(allClasses, '\n', ++entries);
141         }
142
143         // now we have filename with full path
144         lyxerr << "with full path: " << classfile << endl;
145
146         return classfile;
147 }
148
149 } // namespace frontend
150 } // namespace lyx