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 using namespace lyx::support;
29 #ifndef CXX_GLOBAL_CSTD
33 using lyx::textclass_type;
41 // Gets textclass number from name
42 pair<bool, textclass_type> const
43 LyXTextClassList::NumberOfClass(string const & textclass) const
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));
54 // Gets a textclass structure from number
56 LyXTextClassList::operator[](textclass_type textclass) const
58 classlist_[textclass].load();
59 if (textclass < classlist_.size())
60 return classlist_[textclass];
66 // used when sorting the textclass list.
67 class less_textclass_avail_desc {
69 int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
71 // 1. Availability of text class
72 // 2. Description (lexicographic)
74 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
75 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
76 tc1.description() < tc2.description());
81 // Reads LyX textclass definitions according to textclass config file
82 bool LyXTextClassList::Read()
85 string real_file = LibFileSearch("", "textclass.lst");
86 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
87 << real_file << '\'' << endl;
89 if (real_file.empty()) {
90 lyxerr << "LyXTextClassList::Read: unable to find "
91 "textclass file `" << MakeDisplayPath(real_file, 1000)
92 << "'. Exiting." << endl;
94 // This causes LyX to end... Not a desirable behaviour. Lgb
95 // What do you propose? That the user gets a file dialog
96 // and is allowed to hunt for the file? (Asger)
97 // more that we have a layout for minimal.cls statically
98 // compiled in... (Lgb)
101 if (!lex.setFile(real_file)) {
102 lyxerr << "LyXTextClassList::Read: "
103 "lyxlex was not able to set file: "
104 << real_file << endl;
108 lyxerr << "LyXTextClassList::Read: unable to open "
109 "textclass file `" << MakeDisplayPath(real_file, 1000)
110 << "'\nCheck your installation. LyX can't continue."
115 bool finished = false;
117 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
118 while (lex.isOK() && !finished) {
119 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
121 case LyXLex::LEX_FEOF:
125 string const fname = lex.getString();
126 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
128 string const clname = lex.getString();
129 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
131 string const desc = lex.getString();
132 lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
134 bool avail = lex.getBool();
135 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
136 // This code is run when we have
137 // fname, clname, desc, and avail
138 LyXTextClass tmpl(fname, clname, desc, avail);
139 if (lyxerr.debugging(Debug::TCLASS)) {
142 classlist_.push_back(tmpl);
148 lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
150 if (classlist_.empty()) {
151 lyxerr << "LyXTextClassList::Read: no textclasses found!"
155 // Ok everything loaded ok, now sort the list.
156 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_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;