]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
Alfredo's second patch
[lyx.git] / src / lyxtextclasslist.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include "lyxtextclasslist.h"
15 #include "lyxtextclass.h"
16 #include "debug.h"
17 #include "lyxlex.h"
18 #include "gettext.h"
19 #include "frontends/Alert.h"
20
21 #include "support/lyxfunctional.h"
22 #include "support/LAssert.h"
23 #include "support/filetools.h"
24
25 #include <utility>
26
27 #ifndef CXX_GLOBAL_CSTD
28 using std::exit;
29 #endif
30
31 using lyx::textclass_type;
32 using std::pair;
33 using std::make_pair;
34 using std::endl;
35 using std::find_if;
36 using std::sort;
37
38
39 // Gets textclass number from name
40 pair<bool, textclass_type> const
41 LyXTextClassList::NumberOfClass(string const & textclass) const
42 {
43         ClassList::const_iterator cit =
44                 find_if(classlist_.begin(), classlist_.end(),
45                         lyx::compare_memfun(&LyXTextClass::name, textclass));
46         return cit != classlist_.end() ?
47                 make_pair(true, textclass_type(cit - classlist_.begin())) :
48                 make_pair(false, textclass_type(0));
49 }
50
51
52 // Gets a textclass structure from number
53 LyXTextClass const &
54 LyXTextClassList::operator[](textclass_type textclass) const
55 {
56         classlist_[textclass].load();
57         if (textclass < classlist_.size())
58                 return classlist_[textclass];
59         else
60                 return classlist_[0];
61 }
62
63
64 // used when sorting the textclass list.
65 class less_textclass_desc {
66 public:
67         int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
68                 return tc1.description() < tc2.description();
69         }
70 };
71
72
73 // Reads LyX textclass definitions according to textclass config file
74 bool LyXTextClassList::Read()
75 {
76         LyXLex lex(0, 0);
77         string real_file = LibFileSearch("", "textclass.lst");
78         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
79                               << real_file << '\'' << endl;
80
81         if (real_file.empty()) {
82                 lyxerr << "LyXTextClassList::Read: unable to find "
83                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
84                        << "'. Exiting." << endl;
85                 return false;
86                 // This causes LyX to end... Not a desirable behaviour. Lgb
87                 // What do you propose? That the user gets a file dialog
88                 // and is allowed to hunt for the file? (Asger)
89                 // more that we have a layout for minimal.cls statically
90                 // compiled in... (Lgb)
91         }
92
93         if (!lex.setFile(real_file)) {
94                 lyxerr << "LyXTextClassList::Read: "
95                         "lyxlex was not able to set file: "
96                        << real_file << endl;
97         }
98
99         if (!lex.isOK()) {
100                 lyxerr << "LyXTextClassList::Read: unable to open "
101                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
102                        << "'\nCheck your installation. LyX can't continue."
103                        << endl;
104                 return false;
105         }
106
107         bool finished = false;
108         // Parse config-file
109         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
110         while (lex.isOK() && !finished) {
111                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
112                 switch (lex.lex()) {
113                 case LyXLex::LEX_FEOF:
114                         finished = true;
115                         break;
116                 default:
117                         string const fname = lex.getString();
118                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
119                         if (lex.next()) {
120                                 string const clname = lex.getString();
121                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
122                                 if (lex.next()) {
123                                         string const desc = lex.getString();
124                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
125                                         // This code is run when we have
126                                         // fname, clname and desc
127                                         LyXTextClass tmpl(fname, clname, desc);
128                                         if (lyxerr.debugging(Debug::TCLASS)) {
129                                                 tmpl.load();
130                                         }
131                                         classlist_.push_back(tmpl);
132                                 }
133                         }
134                 }
135         }
136         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
137
138         if (classlist_.empty()) {
139                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
140                        << endl;
141                 return false;
142         }
143         // Ok everything loaded ok, now sort the list.
144         sort(classlist_.begin(), classlist_.end(), less_textclass_desc());
145         return true;
146 }
147
148
149 // Global variable: textclass table.
150 LyXTextClassList textclasslist;
151
152 // Reads the style files
153 void LyXSetStyle()
154 {
155         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
156
157         if (!textclasslist.Read()) {
158                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
159                         "during parsing.\n             Exiting." << endl;
160                 exit(1);
161         }
162
163         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
164 }