]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
remove some very old dead code
[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 #ifndef CXX_GLOBAL_CSTD
28 using std::exit;
29 #endif
30
31 using lyx::textclass_type;
32 using std::pair;
33 using std::make_pair;
34 using std::endl;
35 using std::find_if;
36 using std::sort;
37
38
39 // Gets textclass number from name
40 pair<bool, textclass_type> const
41 LyXTextClassList::NumberOfClass(string const & textclass) const
42 {
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));
49 }
50
51
52 // Gets a textclass structure from number
53 LyXTextClass const &
54 LyXTextClassList::operator[](textclass_type textclass) const
55 {
56         classlist_[textclass].load();
57         if (textclass < classlist_.size())
58                 return classlist_[textclass];
59         else
60                 return classlist_[0];
61 }
62
63
64 // used when sorting the textclass list.
65 class less_textclass_desc {
66 public:
67         int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
68                 return tc1.description() < tc2.description();
69         }
70 };
71
72
73 // Reads LyX textclass definitions according to textclass config file
74 bool LyXTextClassList::Read ()
75 {
76         LyXLex lex(0, 0);
77         string real_file = LibFileSearch("", "textclass.lst");
78         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
79                               << real_file << '\'' << endl;
80
81         if (real_file.empty()) {
82                 lyxerr << "LyXTextClassList::Read: unable to find "
83                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
84                        << "'. Exiting." << endl;
85
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 :-("));
89                 return false;
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)
95         }
96
97         if (!lex.setFile(real_file)) {
98                 lyxerr << "LyXTextClassList::Read: "
99                         "lyxlex was not able to set file: "
100                        << real_file << endl;
101         }
102
103         if (!lex.isOK()) {
104                 lyxerr << "LyXTextClassList::Read: unable to open "
105                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
106                        << "'\nCheck your installation. LyX can't continue."
107                        << endl;
108                 return false;
109         }
110
111         bool finished = false;
112         // Parse config-file
113         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
114         while (lex.isOK() && !finished) {
115                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
116                 switch (lex.lex()) {
117                 case LyXLex::LEX_FEOF:
118                         finished = true;
119                         break;
120                 default:
121                         string const fname = lex.getString();
122                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
123                         if (lex.next()) {
124                                 string const clname = lex.getString();
125                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
126                                 if (lex.next()) {
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)) {
133                                                 tmpl.load();
134                                         }
135                                         classlist_.push_back(tmpl);
136                                 }
137                         }
138                 }
139         }
140         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
141
142         if (classlist_.empty()) {
143                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
144                        << endl;
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 :-("));
148                 return false;
149         }
150         // Ok everything loaded ok, now sort the list.
151         sort(classlist_.begin(), classlist_.end(), less_textclass_desc());
152         return true;
153 }
154
155
156 // Global variable: textclass table.
157 LyXTextClassList textclasslist;
158
159 // Reads the style files
160 void LyXSetStyle()
161 {
162         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
163
164         if (!textclasslist.Read()) {
165                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
166                         "during parsing.\n             Exiting." << endl;
167                 exit(1);
168         }
169
170         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
171 }