]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
The std::string mammoth path.
[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/systemcall.h"
20 #include "support/path.h"
21 #include "support/lyxalgo.h"
22 #include "support/path_defines.h"
23
24 #include <boost/cregex.hpp>
25 #include <fstream>
26
27 using lyx::support::contains;
28 using lyx::support::GetFileContents;
29 using lyx::support::getVectorFromString;
30 using lyx::support::LibFileSearch;
31 using lyx::support::OnlyFilename;
32 using lyx::support::Path;
33 using lyx::support::Path;
34 using lyx::support::split;
35 using lyx::support::Systemcall;
36 using lyx::support::token;
37 using lyx::support::user_lyxdir;
38
39 using std::string;
40 using std::endl;
41
42
43 // build filelists of all availabe bst/cls/sty-files. done through
44 // kpsewhich and an external script, saved in *Files.lst
45 void rescanTexStyles()
46 {
47         // Run rescan in user lyx directory
48         Path p(user_lyxdir());
49         Systemcall one;
50         one.startscript(Systemcall::Wait,
51                         LibFileSearch("scripts", "TeXFiles.sh"));
52 }
53
54
55 void texhash()
56 {
57         // Run texhash in user lyx directory
58         Path p(user_lyxdir());
59
60         //path to texhash through system
61         Systemcall one;
62         one.startscript(Systemcall::Wait,"texhash");
63 }
64
65
66 void getTexFileList(string const & filename, std::vector<string> & list)
67 {
68         list.clear();
69         string const file = LibFileSearch("", filename);
70         if (file.empty())
71                 return;
72
73         list = getVectorFromString(GetFileContents(file), "\n");
74
75         // Normalise paths like /foo//bar ==> /foo/bar
76         boost::RegEx regex("/{2,}");
77         std::vector<string>::iterator it  = list.begin();
78         std::vector<string>::iterator end = list.end();
79         for (; it != end; ++it) {
80                 *it = regex.Merge((*it), "/");
81         }
82
83         lyx::eliminate_duplicates(list);
84 }
85
86
87 string const getListOfOptions(string const & classname, string const & type)
88 {
89         string const filename = getTexFileFromList(classname,type);
90         string optionList = string();
91         std::ifstream is(filename.c_str());
92         while (is) {
93                 string s;
94                 is >> s;
95                 if (contains(s,"DeclareOption")) {
96                         s = s.substr(s.find("DeclareOption"));
97                         s = split(s,'{');               // cut front
98                         s = token(s,'}',0);             // cut end
99                         optionList += (s + '\n');
100                 }
101         }
102         return optionList;
103 }
104
105
106 string const getTexFileFromList(string const & file,
107                             string const & type)
108 {
109         string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
110
111         lyxerr << "Search for classfile " << file_ << endl;
112
113         string const lstfile =
114                 ((type == "cls") ? "clsFiles.lst" : "styFiles.lst");
115         string const allClasses = GetFileContents(LibFileSearch(string(),
116                                                                 lstfile));
117         int entries = 0;
118         string classfile = token(allClasses, '\n', entries);
119         int count = 0;
120         while ((!contains(classfile, file) ||
121                 (OnlyFilename(classfile) != file)) &&
122                 (++count < 1000)) {
123                 classfile = token(allClasses, '\n', ++entries);
124         }
125
126         // now we have filename with full path
127         lyxerr << "with full path: " << classfile << endl;
128
129         return classfile;
130 }