]> git.lyx.org Git - lyx.git/blob - src/counters.C
remove unneeded functions
[lyx.git] / src / counters.C
1 /**
2  * \file counters.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Martin Vermeer
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "counters.h"
16 #include "debug.h"
17
18 #include "support/lstrings.h"
19 #include "support/std_sstream.h"
20 #include "support/tostr.h"
21
22 #include <boost/assert.hpp>
23
24 using std::endl;
25 using std::ostringstream;
26 using std::string;
27
28
29 Counter::Counter()
30 {
31         reset();
32 }
33
34
35 void Counter::set(int v)
36 {
37         value_ = v;
38 }
39
40
41 void Counter::addto(int v)
42 {
43         value_ += v;
44 }
45
46
47 int Counter::value() const
48 {
49         return value_;
50 }
51
52
53 void Counter::step()
54 {
55         ++value_;
56 }
57
58
59 void Counter::reset()
60 {
61         value_ = 0;
62 }
63
64
65 string Counter::master() const
66 {
67         return master_;
68 }
69
70
71 void Counter::setMaster(string const & m)
72 {
73         master_ = m;
74 }
75
76
77 void Counters::newCounter(string const & newc)
78 {
79         // First check if newc already exist
80         CounterList::iterator cit = counterList.find(newc);
81         // if already exist give warning and return
82         if (cit != counterList.end()) {
83                 lyxerr << "The new counter already exists." << endl;
84                 return;
85         }
86         counterList[newc];
87 }
88
89
90 void Counters::newCounter(string const & newc, string const & masterc)
91 {
92         // First check if newc already exists
93         CounterList::iterator cit = counterList.find(newc);
94         // if already existant give warning and return
95         if (cit != counterList.end()) {
96                 lyxerr << "The new counter already exists." << endl;
97                 return;
98         }
99         // then check if masterc exists
100         CounterList::iterator it = counterList.find(masterc);
101         // if not give warning and return
102         if (it == counterList.end()) {
103                 lyxerr << "The master counter does not exist." << endl;
104                 return;
105         }
106
107         counterList[newc].setMaster(masterc);
108 }
109
110
111 void Counters::set(string const & ctr, int val)
112 {
113         CounterList::iterator it = counterList.find(ctr);
114         if (it == counterList.end()) {
115                 lyxerr << "set: Counter does not exist: " << ctr << endl;
116                 return;
117         }
118         it->second.set(val);
119 }
120
121
122 void Counters::addto(string const & ctr, int val)
123 {
124         CounterList::iterator it = counterList.find(ctr);
125         if (it == counterList.end()) {
126                 lyxerr << "addto: Counter does not exist: " << ctr << endl;
127                 return;
128         }
129         it->second.addto(val);
130 }
131
132
133 int Counters::value(string const & ctr) const
134 {
135         CounterList::const_iterator cit = counterList.find(ctr);
136         if (cit == counterList.end()) {
137                 lyxerr << "value: Counter does not exist: " << ctr << endl;
138                 return 0;
139         }
140         return cit->second.value();
141 }
142
143
144 void Counters::step(string const & ctr)
145 {
146         CounterList::iterator it = counterList.find(ctr);
147         if (it == counterList.end()) {
148                 lyxerr << "step: Counter does not exist: " << ctr << endl;
149                 return;
150         }
151
152         it->second.step();
153         it = counterList.begin();
154         CounterList::iterator end = counterList.end();
155         for (; it != end; ++it) {
156                 if (it->second.master() == ctr) {
157                         it->second.reset();
158                 }
159         }
160 }
161
162
163 void Counters::reset()
164 {
165         CounterList::iterator it = counterList.begin();
166         CounterList::iterator end = counterList.end();
167         for (; it != end; ++it) {
168                 it->second.reset();
169         }
170 }
171
172
173 void Counters::reset(string const & match)
174 {
175         BOOST_ASSERT(!match.empty());
176
177         CounterList::iterator it = counterList.begin();
178         CounterList::iterator end = counterList.end();
179         for (; it != end; ++it) {
180                 if (it->first.find(match) != string::npos)
181                         it->second.reset();
182         }
183 }
184
185
186 void Counters::copy(Counters & from, Counters & to, string const & match)
187 {
188         CounterList::iterator it = counterList.begin();
189         CounterList::iterator end = counterList.end();
190         for (; it != end; ++it) {
191                 if (it->first.find(match) != string::npos || match == "") {
192                         to.set(it->first, from.value(it->first));
193                 }
194         }
195 }
196
197
198 namespace {
199
200 char loweralphaCounter(int n)
201 {
202         if (n < 1 || n > 26)
203                 return '?';
204         return 'a' + n - 1;
205 }
206
207
208 char alphaCounter(int n)
209 {
210         if (n < 1 || n > 26)
211                 return '?';
212         return 'A' + n - 1;
213 }
214
215
216 char hebrewCounter(int n)
217 {
218         static const char hebrew[22] = {
219                 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
220                 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
221                 '÷', 'ø', 'ù', 'ú'
222         };
223
224         if (n < 1 || n > 22)
225                 return '?';
226         return hebrew[n - 1];
227 }
228
229
230 string const lowerromanCounter(int n)
231 {
232         static char const * roman[20] = {
233                 "i",   "ii",  "iii", "iv", "v",
234                 "vi",  "vii", "viii", "ix", "x",
235                 "xi",  "xii", "xiii", "xiv", "xv",
236                 "xvi", "xvii", "xviii", "xix", "xx"
237         };
238
239         if (n < 1 || n > 20)
240                 return "??";
241         return roman[n - 1];
242 }
243
244
245 string const romanCounter(int n)
246 {
247         static char const * roman[20] = {
248                 "I",   "II",  "III", "IV", "V",
249                 "VI",  "VII", "VIII", "IX", "X",
250                 "XI",  "XII", "XIII", "XIV", "XV",
251                 "XVI", "XVII", "XVIII", "XIX", "XX"
252         };
253
254         if (n < 1 || n > 20)
255                 return "??";
256         return roman[n - 1];
257 }
258
259 } // namespace anon
260
261
262 string Counters::labelItem(string const & ctr, string const & numbertype)
263 {
264         if (counterList.find(ctr) == counterList.end()) {
265                 lyxerr << "Counter " << ctr << " does not exist." << endl;
266                 return string();
267         }
268
269         if (numbertype == "hebrew")
270                 return string(1, hebrewCounter(value(ctr)));
271
272         if (numbertype == "alph")
273                 return string(1, loweralphaCounter(value(ctr)));
274
275         if (numbertype == "Alph")
276                 return string(1, alphaCounter(value(ctr)));
277
278         if (numbertype == "roman")
279                 return lowerromanCounter(value(ctr));
280
281         if (numbertype == "Roman")
282                 return romanCounter(value(ctr));
283
284         return tostr(value(ctr));
285 }
286
287
288 string Counters::counterLabel(string const & format)
289 {
290         string label = format;
291         while (true) {
292 #ifdef WITH_WARNINGS
293 #warning Using boost::regex would make this code a lot simpler... (Lgb)
294 #endif
295
296                 size_t const i = label.find('\\', 0);
297                 if (i == string::npos)
298                         break;
299                 size_t const j = label.find('{', i + 1);
300                 if (j == string::npos)
301                         break;
302                 size_t const k = label.find('}', j + 1);
303                 if (k == string::npos)
304                         break;
305                 string const numbertype(label, i + 1, j - i - 1);
306                 string const counter(label, j + 1, k - j - 1);
307                 string const rep = labelItem(counter, numbertype);
308                 label = string(label, 0, i) + rep + string(label, k + 1, string::npos);
309                 //lyxerr << "  : " << " (" << counter  << ","
310                 //      << numbertype << ") -> " << label << endl;
311         }
312         //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
313         return label;
314 }
315
316
317 string Counters::enumLabel(string const & ctr, string const & langtype)
318 {
319         ostringstream os;
320
321         if (langtype == "hebrew") {
322                 if (ctr == "enumi")
323                         os << '.' << value("enumi");
324                 else if (ctr == "enumii")
325                         os << '(' << hebrewCounter(value("enumii")) << ')';
326                 else if (ctr == "enumiii")
327                         os << '.' << lowerromanCounter(value("enumiii"));
328                 else if (ctr == "enumiv")
329                         os << '.' << alphaCounter(value("enumiv"));
330         } else {
331                 if (ctr == "enumi")
332                         os << value("enumi") << '.';
333                 else if (ctr == "enumii")
334                         os << '(' << loweralphaCounter(value("enumii")) << ')';
335                 else if (ctr == "enumiii")
336                         os << lowerromanCounter(value("enumiii")) << '.';
337                 else if (ctr == "enumiv")
338                         os << alphaCounter(value("enumiv")) << '.';
339         }
340
341         return os.str();
342 }