]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
More compilation fixes.
[lyx.git] / src / frontends / controllers / tex_helpers.C
1 /**
2  * \file tex_helpers.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Herbert Voss <voss@lyx.org>
7  */
8
9 #include <config.h>
10
11 #include "tex_helpers.h"
12
13 #include "debug.h"
14 #include "gettext.h"
15
16 #include "support/filetools.h"
17 #include "support/lstrings.h"
18 #include "support/systemcall.h"
19 #include "support/path.h"
20 #include "support/lyxalgo.h"
21
22 #include <vector>
23 #include <fstream>
24 #include <algorithm>
25
26 using std::vector;
27 using std::endl;
28 using std::sort;
29 using std::unique;
30
31 extern string user_lyxdir; // home of *Files.lst
32
33 namespace {
34
35 vector<string> listWithoutPath(vector<string> & dbase)
36 {
37         vector<string>::iterator it = dbase.begin();
38         vector<string>::iterator end = dbase.end();
39         for (; it != end; ++it) 
40                 *it = OnlyFilename(*it);
41         return dbase;
42 }
43
44 }
45
46 // build filelists of all availabe bst/cls/sty-files. done through
47 // kpsewhich and an external script, saved in *Files.lst
48 void rescanTexStyles()
49 {
50         // Run rescan in user lyx directory
51         Path p(user_lyxdir);
52         Systemcall one;
53         one.startscript(Systemcall::Wait,
54                         LibFileSearch("scripts", "TeXFiles.sh"));
55         p.pop();
56 }
57
58
59 void texhash()
60 {
61         // Run texhash in user lyx directory
62         Path p(user_lyxdir);
63
64         //path to texhash through system
65         Systemcall one;
66         one.startscript(Systemcall::Wait,"texhash"); 
67         p.pop();
68 }
69
70 string const getTexFileList(string const & filename, bool withFullPath)
71 {
72         string const file = LibFileSearch("", filename);
73         if (file.empty())
74                 return string();
75  
76         vector<string> dbase =
77                 getVectorFromString(GetFileContents(file), "\n");
78
79         lyx::eliminate_duplicates(dbase); 
80         string const str_out = withFullPath ?
81                 getStringFromVector(dbase, "\n") :
82                 getStringFromVector(listWithoutPath(dbase), "\n");
83         return str_out;
84 }
85
86
87 string const getListOfOptions(string const & classname,
88                             string const & type)
89 {
90         string const filename = getTexFileFromList(classname,type);
91         string optionList = string();
92         std::ifstream is(filename.c_str());
93         while (is) {
94             string s;
95             is >> s;
96             if (contains(s,"DeclareOption")) {
97                 s = s.substr(s.find("DeclareOption"));
98                 s = split(s,'{');               // cut front
99                 s = token(s,'}',0);             // cut end
100                 optionList += (s + '\n');
101             }
102         }
103         return optionList;
104 }
105
106
107 string const getTexFileFromList(string const & file, 
108                             string const & type)
109 {
110         string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
111  
112         lyxerr << "Search for classfile " << file_ << endl;
113  
114         string const lstfile = (type == "cls") ? "clsFiles.lst" : "styFiles.lst";
115         string const allClasses = GetFileContents(LibFileSearch(string(), lstfile));
116         int entries = 0;
117         string classfile = token(allClasses, '\n', entries);
118         int count = 0;
119         while ((!contains(classfile, file) || 
120                 (OnlyFilename(classfile) != file)) &&
121                 (++count < 1000)) {
122                 classfile = token(allClasses, '\n', ++entries);
123         }
124  
125         // now we have filename with full path
126         lyxerr << "with full path: " << classfile << endl;
127         return classfile;
128 }