]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
use exclicit temp var
[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 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "lyxtextclasslist.h"
19 #include "lyxtextclass.h"
20 #include "debug.h"
21 #include "lyxlex.h"
22 #include "gettext.h"
23
24 #include "frontends/Alert.h"
25
26 #include "support/lyxfunctional.h"
27 #include "support/LAssert.h"
28 #include "support/filetools.h"
29
30 #include <utility>
31
32 using lyx::layout_type;
33 using lyx::textclass_type;
34 using std::pair;
35 using std::make_pair;
36 using std::endl;
37 using std::find_if;
38 using std::sort;
39
40
41 // Gets textclass number from name
42 pair<bool, textclass_type> const
43 LyXTextClassList::NumberOfClass(string const & textclass) const
44 {
45         ClassList::const_iterator cit =
46                 find_if(classlist.begin(), classlist.end(),
47                         lyx::compare_memfun(&LyXTextClass::name, textclass));
48         return cit != classlist.end() ?
49                 make_pair(true, textclass_type(cit - classlist.begin())) :
50                 make_pair(false, textclass_type(0));
51 }
52
53
54 // Gets layout structure from style number and textclass number
55 LyXLayout const &
56 LyXTextClassList::Style(textclass_type textclass,
57                         layout_type layout) const
58 {
59         classlist[textclass].load();
60         if (layout < classlist[textclass].numLayouts())
61                 return classlist[textclass][layout];
62         return classlist[textclass][0];
63 }
64
65
66 // Gets layout number from name and textclass number
67 pair<bool, layout_type> const
68 LyXTextClassList::NumberOfLayout(textclass_type textclass,
69                                  string const & name) const
70 {
71         classlist[textclass].load();
72         for (unsigned int i = 0; i < classlist[textclass].numLayouts(); ++i) {
73                 if (classlist[textclass][i].name() == name)
74                         return make_pair(true, i);
75         }
76         return make_pair(false, layout_type(0)); // not found
77 }
78
79
80 // Gets a layout (style) name from layout number and textclass number
81 string const &
82 LyXTextClassList::NameOfLayout(textclass_type textclass,
83                           layout_type layout) const
84 {
85         static string const dummy("dummy");
86         classlist[textclass].load();
87         if (layout < classlist[textclass].numLayouts())
88                 return classlist[textclass][layout].name();
89         return dummy;
90 }
91
92
93 // Gets a textclass name from number
94 string const &
95 LyXTextClassList::NameOfClass(textclass_type number) const
96 {
97         static string const dummy("dummy");
98         if (classlist.empty()) {
99                 return dummy;
100         }
101         lyx::Assert(number < classlist.size());
102         return classlist[number].name();
103 }
104
105
106 // Gets a textclass latexname from number
107 string const &
108 LyXTextClassList::LatexnameOfClass(textclass_type number) const
109 {
110         static string const dummy("dummy");
111         classlist[number].load();
112         if (classlist.empty()) {
113                 return dummy;
114         }
115         lyx::Assert(number < classlist.size());
116         return classlist[number].latexname();
117 }
118
119
120 // Gets a textclass description from number
121 string const &
122 LyXTextClassList::DescOfClass(textclass_type number) const
123 {
124         static string const dummy("dummy");
125         if (classlist.empty()) {
126                 return dummy;
127         }
128         lyx::Assert(number < classlist.size());
129         return classlist[number].description();
130 }
131
132
133 // Gets a textclass structure from number
134 LyXTextClass const &
135 LyXTextClassList::TextClass(textclass_type textclass) const
136 {
137         classlist[textclass].load();
138         if (textclass < classlist.size())
139                 return classlist[textclass];
140         else
141                 return classlist[0];
142 }
143
144
145 void LyXTextClassList::Add(LyXTextClass const & t)
146 {
147         classlist.push_back(t);
148 }
149
150
151 // used when sorting the textclass list.
152 class less_textclass_desc {
153 public:
154         int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
155                 return tc1.description() < tc2.description();
156         }
157 };
158
159
160 // Reads LyX textclass definitions according to textclass config file
161 bool LyXTextClassList::Read ()
162 {
163         LyXLex lex(0, 0);
164         string real_file = LibFileSearch("", "textclass.lst");
165         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
166                               << real_file << "'" << endl;
167
168         if (real_file.empty()) {
169                 lyxerr << "LyXTextClassList::Read: unable to find "
170                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
171                        << "'. Exiting." << endl;
172
173                 Alert::alert(_("LyX wasn't able to find its layout descriptions!"),
174                            _("Check that the file \"textclass.lst\""),
175                            _("is installed correctly. Sorry, has to exit :-("));
176                 return false;
177                 // This causes LyX to end... Not a desirable behaviour. Lgb
178                 // What do you propose? That the user gets a file dialog
179                 // and is allowed to hunt for the file? (Asger)
180                 // more that we have a layout for minimal.cls statically
181                 // compiled in... (Lgb)
182         }
183
184         if (!lex.setFile(real_file)) {
185                 lyxerr << "LyXTextClassList::Read: "
186                         "lyxlex was not able to set file: "
187                        << real_file << endl;
188         }
189         
190         if (!lex.isOK()) {
191                 lyxerr << "LyXTextClassList::Read: unable to open "
192                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
193                        << "'\nCheck your installation. LyX can't continue."
194                        << endl;
195                 return false;
196         }
197
198         bool finished = false;
199         // Parse config-file
200         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
201         while (lex.isOK() && !finished) {
202                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
203                 switch (lex.lex()) {
204                 case LyXLex::LEX_FEOF:
205                         finished = true;
206                         break;
207                 default:
208                         string const fname = lex.getString();
209                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
210                         if (lex.next()) {
211                                 string const clname = lex.getString();
212                                 lyxerr[Debug::TCLASS]
213                                         << "Clname: " << clname << endl;
214                                 if (lex.next()) {
215                                               string const desc = lex.getString();
216                                               lyxerr[Debug::TCLASS]
217                                                       << "Desc: " << desc << endl;
218                                               // This code is run when we have
219                                               // fname, clname and desc
220                                               LyXTextClass tmpl(fname,
221                                                                 clname,
222                                                                 desc);
223                                               if (lyxerr.
224                                                   debugging(Debug::TCLASS)) {
225                                                       tmpl.load();
226                                               }
227                                               Add (tmpl);
228                                 }
229                         }
230                 }
231         }
232         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
233
234         if (classlist.empty()) {
235                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
236                        << endl;
237                 Alert::alert(_("LyX wasn't able to find any layout description!"),
238                            _("Check the contents of the file \"textclass.lst\""),
239                            _("Sorry, has to exit :-("));
240                 return false;
241         }
242         // Ok everything loaded ok, now sort the list.
243         sort(classlist.begin(), classlist.end(), less_textclass_desc());
244         return true;
245 }
246
247         
248 /* Load textclass
249    Returns false if this fails
250 */
251 bool LyXTextClassList::Load(textclass_type number) const
252 {
253         bool result = true;
254         if (number < classlist.size()) {
255                 classlist[number].load();
256                 if (classlist[number].numLayouts() == 0) {
257                         result = false;
258                 }
259         } else {
260                 result = false;
261         }
262         return result;
263 }
264
265
266 // Global variable: textclass table.
267 LyXTextClassList textclasslist;
268
269 // Reads the style files
270 void LyXSetStyle()
271 {
272         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
273         
274         if (!textclasslist.Read()) {
275                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
276                         "during parsing.\n             Exiting." << endl;
277                 exit(1);
278         }
279
280         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
281 }