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 * ======================================================
14 #include "lyxtextclasslist.h"
15 #include "lyxtextclass.h"
19 #include "frontends/Alert.h"
21 #include "support/lyxfunctional.h"
22 #include "support/LAssert.h"
23 #include "support/filetools.h"
27 #ifndef CXX_GLOBAL_CSTD
31 using lyx::textclass_type;
39 // Gets textclass number from name
40 pair<bool, textclass_type> const
41 LyXTextClassList::NumberOfClass(string const & textclass) const
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));
52 // Gets a textclass structure from number
54 LyXTextClassList::operator[](textclass_type textclass) const
56 classlist_[textclass].load();
57 if (textclass < classlist_.size())
58 return classlist_[textclass];
64 // used when sorting the textclass list.
65 class less_textclass_desc {
67 int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
68 return tc1.description() < tc2.description();
73 // Reads LyX textclass definitions according to textclass config file
74 bool LyXTextClassList::Read ()
77 string real_file = LibFileSearch("", "textclass.lst");
78 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
79 << real_file << '\'' << endl;
81 if (real_file.empty()) {
82 lyxerr << "LyXTextClassList::Read: unable to find "
83 "textclass file `" << MakeDisplayPath(real_file, 1000)
84 << "'. Exiting." << endl;
86 Alert::alert(_("LyX wasn't able to find its layout descriptions!"),
87 _("Check that the file \"textclass.lst\""),
88 _("is installed correctly. Sorry, has to exit :-("));
90 // This causes LyX to end... Not a desirable behaviour. Lgb
91 // What do you propose? That the user gets a file dialog
92 // and is allowed to hunt for the file? (Asger)
93 // more that we have a layout for minimal.cls statically
94 // compiled in... (Lgb)
97 if (!lex.setFile(real_file)) {
98 lyxerr << "LyXTextClassList::Read: "
99 "lyxlex was not able to set file: "
100 << real_file << endl;
104 lyxerr << "LyXTextClassList::Read: unable to open "
105 "textclass file `" << MakeDisplayPath(real_file, 1000)
106 << "'\nCheck your installation. LyX can't continue."
111 bool finished = false;
113 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
114 while (lex.isOK() && !finished) {
115 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
117 case LyXLex::LEX_FEOF:
121 string const fname = lex.getString();
122 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
124 string const clname = lex.getString();
125 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
127 string const desc = lex.getString();
128 lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
129 // This code is run when we have
130 // fname, clname and desc
131 LyXTextClass tmpl(fname, clname, desc);
132 if (lyxerr.debugging(Debug::TCLASS)) {
135 classlist_.push_back(tmpl);
140 lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
142 if (classlist_.empty()) {
143 lyxerr << "LyXTextClassList::Read: no textclasses found!"
145 Alert::alert(_("LyX wasn't able to find any layout description!"),
146 _("Check the contents of the file \"textclass.lst\""),
147 _("Sorry, has to exit :-("));
150 // Ok everything loaded ok, now sort the list.
151 sort(classlist_.begin(), classlist_.end(), less_textclass_desc());
156 // Global variable: textclass table.
157 LyXTextClassList textclasslist;
159 // Reads the style files
162 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
164 if (!textclasslist.Read()) {
165 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
166 "during parsing.\n Exiting." << endl;
170 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;