1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ======================================================
15 #pragma implementation
18 #include "lyxtextclasslist.h"
19 #include "lyxtextclass.h"
24 #include "frontends/Alert.h"
26 #include "support/lyxfunctional.h"
27 #include "support/LAssert.h"
28 #include "support/filetools.h"
32 #ifndef CXX_GLOBAL_CSTD
36 using lyx::textclass_type;
44 // Gets textclass number from name
45 pair<bool, textclass_type> const
46 LyXTextClassList::NumberOfClass(string const & textclass) const
48 ClassList::const_iterator cit =
49 find_if(classlist_.begin(), classlist_.end(),
50 lyx::compare_memfun(&LyXTextClass::name, textclass));
51 return cit != classlist_.end() ?
52 make_pair(true, textclass_type(cit - classlist_.begin())) :
53 make_pair(false, textclass_type(0));
57 // Gets a textclass structure from number
59 LyXTextClassList::operator[](textclass_type textclass) const
61 classlist_[textclass].load();
62 if (textclass < classlist_.size())
63 return classlist_[textclass];
69 // used when sorting the textclass list.
70 class less_textclass_desc {
72 int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
73 return tc1.description() < tc2.description();
78 // Reads LyX textclass definitions according to textclass config file
79 bool LyXTextClassList::Read ()
82 string real_file = LibFileSearch("", "textclass.lst");
83 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
84 << real_file << "'" << endl;
86 if (real_file.empty()) {
87 lyxerr << "LyXTextClassList::Read: unable to find "
88 "textclass file `" << MakeDisplayPath(real_file, 1000)
89 << "'. Exiting." << endl;
91 Alert::alert(_("LyX wasn't able to find its layout descriptions!"),
92 _("Check that the file \"textclass.lst\""),
93 _("is installed correctly. Sorry, has to exit :-("));
95 // This causes LyX to end... Not a desirable behaviour. Lgb
96 // What do you propose? That the user gets a file dialog
97 // and is allowed to hunt for the file? (Asger)
98 // more that we have a layout for minimal.cls statically
99 // compiled in... (Lgb)
102 if (!lex.setFile(real_file)) {
103 lyxerr << "LyXTextClassList::Read: "
104 "lyxlex was not able to set file: "
105 << real_file << endl;
109 lyxerr << "LyXTextClassList::Read: unable to open "
110 "textclass file `" << MakeDisplayPath(real_file, 1000)
111 << "'\nCheck your installation. LyX can't continue."
116 bool finished = false;
118 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
119 while (lex.isOK() && !finished) {
120 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
122 case LyXLex::LEX_FEOF:
126 string const fname = lex.getString();
127 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
129 string const clname = lex.getString();
130 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
132 string const desc = lex.getString();
133 lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
134 // This code is run when we have
135 // fname, clname and desc
136 LyXTextClass tmpl(fname, clname, desc);
137 if (lyxerr.debugging(Debug::TCLASS)) {
140 classlist_.push_back(tmpl);
145 lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
147 if (classlist_.empty()) {
148 lyxerr << "LyXTextClassList::Read: no textclasses found!"
150 Alert::alert(_("LyX wasn't able to find any layout description!"),
151 _("Check the contents of the file \"textclass.lst\""),
152 _("Sorry, has to exit :-("));
155 // Ok everything loaded ok, now sort the list.
156 sort(classlist_.begin(), classlist_.end(), less_textclass_desc());
161 // Global variable: textclass table.
162 LyXTextClassList textclasslist;
164 // Reads the style files
167 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
169 if (!textclasslist.Read()) {
170 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
171 "during parsing.\n Exiting." << endl;
175 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;