]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
Always use std::endl with lyxerr.
[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 using namespace lyx::support;
28
29 #ifndef CXX_GLOBAL_CSTD
30 using std::exit;
31 #endif
32
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 a textclass structure from number
55 LyXTextClass const &
56 LyXTextClassList::operator[](textclass_type textclass) const
57 {
58         classlist_[textclass].load();
59         if (textclass < classlist_.size())
60                 return classlist_[textclass];
61         else
62                 return classlist_[0];
63 }
64
65
66 // used when sorting the textclass list.
67 class less_textclass_avail_desc {
68 public:
69         int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
70                 // Ordering criteria:
71                 //   1. Availability of text class
72                 //   2. Description (lexicographic)
73
74                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
75                        (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
76                         tc1.description() < tc2.description());
77         }
78 };
79
80
81 // Reads LyX textclass definitions according to textclass config file
82 bool LyXTextClassList::Read()
83 {
84         LyXLex lex(0, 0);
85         string real_file = LibFileSearch("", "textclass.lst");
86         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
87                               << real_file << '\'' << endl;
88
89         if (real_file.empty()) {
90                 lyxerr << "LyXTextClassList::Read: unable to find "
91                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
92                        << "'. Exiting." << endl;
93                 return false;
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)
99         }
100
101         if (!lex.setFile(real_file)) {
102                 lyxerr << "LyXTextClassList::Read: "
103                         "lyxlex was not able to set file: "
104                        << real_file << endl;
105         }
106
107         if (!lex.isOK()) {
108                 lyxerr << "LyXTextClassList::Read: unable to open "
109                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
110                        << "'\nCheck your installation. LyX can't continue."
111                        << endl;
112                 return false;
113         }
114
115         bool finished = false;
116         // Parse config-file
117         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
118         while (lex.isOK() && !finished) {
119                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
120                 switch (lex.lex()) {
121                 case LyXLex::LEX_FEOF:
122                         finished = true;
123                         break;
124                 default:
125                         string const fname = lex.getString();
126                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
127                         if (lex.next()) {
128                                 string const clname = lex.getString();
129                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
130                                 if (lex.next()) {
131                                         string const desc = lex.getString();
132                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
133                                         if (lex.next()) {
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)) {
140                                                         tmpl.load();
141                                                 }
142                                                 classlist_.push_back(tmpl);
143                                         }
144                                 }
145                         }
146                 }
147         }
148         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
149
150         if (classlist_.empty()) {
151                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
152                        << endl;
153                 return false;
154         }
155         // Ok everything loaded ok, now sort the list.
156         sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
157         return true;
158 }
159
160
161 // Global variable: textclass table.
162 LyXTextClassList textclasslist;
163
164 // Reads the style files
165 void LyXSetStyle()
166 {
167         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
168
169         if (!textclasslist.Read()) {
170                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
171                         "during parsing.\n             Exiting." << endl;
172                 exit(1);
173         }
174
175         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
176 }