]> git.lyx.org Git - features.git/blob - src/Counters.cpp
Converted '#warning ...' into FIXME-comments
[features.git] / src / Counters.cpp
1 /**
2  * \file Counters.cpp
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  * \author Richard Heck (roman numerals)
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "Counters.h"
17 #include "debug.h"
18
19 #include "support/lstrings.h"
20 #include "support/convert.h"
21
22 #include <boost/assert.hpp>
23
24 #include <sstream>
25
26 using std::endl;
27 using std::ostringstream;
28 using std::string;
29
30
31 namespace lyx {
32
33
34 Counter::Counter()
35 {
36         reset();
37 }
38
39
40 void Counter::set(int v)
41 {
42         value_ = v;
43 }
44
45
46 void Counter::addto(int v)
47 {
48         value_ += v;
49 }
50
51
52 int Counter::value() const
53 {
54         return value_;
55 }
56
57
58 void Counter::step()
59 {
60         ++value_;
61 }
62
63
64 void Counter::reset()
65 {
66         value_ = 0;
67 }
68
69
70 docstring const & Counter::master() const
71 {
72         return master_;
73 }
74
75
76 void Counter::setMaster(docstring const & m)
77 {
78         master_ = m;
79 }
80
81
82 void Counters::newCounter(docstring const & newc)
83 {
84         // First check if newc already exist
85         CounterList::iterator const cit = counterList.find(newc);
86         // if already exist give warning and return
87         if (cit != counterList.end()) {
88                 lyxerr << "New counter already exists: "
89                        << to_utf8(newc)
90                        << endl;
91                 return;
92         }
93         counterList[newc];
94 }
95
96
97 void Counters::newCounter(docstring const & newc,
98                           docstring const & masterc)
99 {
100         // First check if newc already exists
101         CounterList::iterator const cit = counterList.find(newc);
102         // if already existant give warning and return
103         if (cit != counterList.end()) {
104                 lyxerr << "New counter already exists: "
105                        << to_utf8(newc)
106                        << endl;
107                 return;
108         }
109         // then check if masterc exists
110         CounterList::iterator const it = counterList.find(masterc);
111         // if not give warning and return
112         if (it == counterList.end()) {
113                 lyxerr << "Master counter does not exist: "
114                        << to_utf8(masterc)
115                        << endl;
116                 return;
117         }
118
119         counterList[newc].setMaster(masterc);
120 }
121
122
123 void Counters::set(docstring const & ctr, int const val)
124 {
125         CounterList::iterator const it = counterList.find(ctr);
126         if (it == counterList.end()) {
127                 lyxerr << "set: Counter does not exist: "
128                        << to_utf8(ctr) << endl;
129                 return;
130         }
131         it->second.set(val);
132 }
133
134
135 void Counters::addto(docstring const & ctr, int const val)
136 {
137         CounterList::iterator const it = counterList.find(ctr);
138         if (it == counterList.end()) {
139                 lyxerr << "addto: Counter does not exist: "
140                        << to_utf8(ctr) << endl;
141                 return;
142         }
143         it->second.addto(val);
144 }
145
146
147 int Counters::value(docstring const & ctr) const
148 {
149         CounterList::const_iterator const cit = counterList.find(ctr);
150         if (cit == counterList.end()) {
151                 lyxerr << "value: Counter does not exist: "
152                        << to_utf8(ctr) << endl;
153                 return 0;
154         }
155         return cit->second.value();
156 }
157
158
159 void Counters::step(docstring const & ctr)
160 {
161         CounterList::iterator it = counterList.find(ctr);
162         if (it == counterList.end()) {
163                 lyxerr << "step: Counter does not exist: "
164                        << to_utf8(ctr) << endl;
165                 return;
166         }
167
168         it->second.step();
169         it = counterList.begin();
170         CounterList::iterator const end = counterList.end();
171         for (; it != end; ++it) {
172                 if (it->second.master() == ctr) {
173                         it->second.reset();
174                 }
175         }
176 }
177
178
179 void Counters::reset()
180 {
181         appendix_ = false;
182         CounterList::iterator it = counterList.begin();
183         CounterList::iterator const end = counterList.end();
184         for (; it != end; ++it) {
185                 it->second.reset();
186         }
187 }
188
189
190 void Counters::reset(docstring const & match)
191 {
192         BOOST_ASSERT(!match.empty());
193
194         CounterList::iterator it = counterList.begin();
195         CounterList::iterator end = counterList.end();
196         for (; it != end; ++it) {
197                 if (it->first.find(match) != string::npos)
198                         it->second.reset();
199         }
200 }
201
202
203 void Counters::copy(Counters & from, Counters & to, docstring const & match)
204 {
205         CounterList::iterator it = counterList.begin();
206         CounterList::iterator end = counterList.end();
207         for (; it != end; ++it) {
208                 if (it->first.find(match) != string::npos || match == "") {
209                         to.set(it->first, from.value(it->first));
210                 }
211         }
212 }
213
214
215 namespace {
216
217 char loweralphaCounter(int const n)
218 {
219         if (n < 1 || n > 26)
220                 return '?';
221         return 'a' + n - 1;
222 }
223
224
225 char alphaCounter(int const n)
226 {
227         if (n < 1 || n > 26)
228                 return '?';
229         return 'A' + n - 1;
230 }
231
232
233 char hebrewCounter(int const n)
234 {
235         static const char hebrew[22] = {
236                 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
237                 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
238                 '\xf7', '\xf8', '\xf9', '\xfa'
239         };
240
241         if (n < 1 || n > 22)
242                 return '?';
243         return hebrew[n - 1];
244 }
245
246
247
248 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
249 //and for a list of roman numerals up to and including 3999, see 
250 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
251 //for this info.)
252 docstring const romanCounter(int const n)
253 {
254         static char const * const ones[9] = {
255                 "I",   "II",  "III", "IV", "V",
256                 "VI",  "VII", "VIII", "IX"
257         };
258         
259         static char const * const tens[9] = {
260                 "X", "XX", "XXX", "XL", "L",
261                 "LX", "LXX", "LXXX", "XC"
262         };
263         
264         static char const * const hunds[9] = {
265                 "C", "CC", "CCC", "CD", "D",
266                 "DC", "DCC", "DCCC", "CM"
267         };
268         
269         if (n > 1000 || n < 1) 
270                 return from_ascii("??");
271         
272         int val = n;
273         string roman;
274         switch (n) {
275         //special cases
276         case 900: 
277                 roman = "CM";
278                 break;
279         case 400:
280                 roman = "CD";
281                 break;
282         default:
283                 if (val >= 100) {
284                         int hundreds = val / 100;
285                         roman = hunds[hundreds - 1];
286                         val = val % 100;
287                 }
288                 if (val >= 10) {
289                         switch (val) {
290                         //special case
291                         case 90:
292                                 roman = roman + "XC";
293                                 val = 0; //skip next
294                                 break;
295                         default:
296                                 int tensnum = val / 10;
297                                 roman = roman + tens[tensnum - 1];
298                                 val = val % 10;
299                         } // end switch
300                 } // end tens
301                 if (val > 0)
302                         roman = roman + ones[val -1];
303         }
304         return from_ascii(roman);
305 }
306
307
308 docstring const lowerromanCounter(int const n)
309 {
310         return support::lowercase(romanCounter(n));
311 }
312
313 } // namespace anon
314
315
316 docstring Counters::labelItem(docstring const & ctr,
317                               docstring const & numbertype)
318 {
319         CounterList::const_iterator const cit = counterList.find(ctr);
320         if (cit == counterList.end()) {
321                 lyxerr << "Counter "
322                        << to_utf8(ctr)
323                        << " does not exist." << endl;
324                 return docstring();
325         }
326
327         int val = cit->second.value();
328
329         if (numbertype == "hebrew")
330                 return docstring(1, hebrewCounter(val));
331
332         if (numbertype == "alph")
333                 return docstring(1, loweralphaCounter(val));
334
335         if (numbertype == "Alph")
336                 return docstring(1, alphaCounter(val));
337
338         if (numbertype == "roman")
339                 return lowerromanCounter(val);
340
341         if (numbertype == "Roman")
342                 return romanCounter(val);
343
344         return convert<docstring>(val);
345 }
346
347
348 docstring Counters::counterLabel(docstring const & format)
349 {
350         docstring label = format;
351         while (true) {
352                 // FIXME: Using boost::regex or boost::spirit would make
353                 // FIXME: this code a lot simpler... (Lgb)
354
355                 size_t const i = label.find('\\', 0);
356                 if (i == docstring::npos)
357                         break;
358                 size_t const j = label.find('{', i + 1);
359                 if (j == docstring::npos)
360                         break;
361                 size_t const k = label.find('}', j + 1);
362                 if (k == docstring::npos)
363                         break;
364                 docstring const numbertype(label, i + 1, j - i - 1);
365                 docstring const counter(label, j + 1, k - j - 1);
366                 docstring const rep = labelItem(counter, numbertype);
367                 label = docstring(label, 0, i) + rep
368                         + docstring(label, k + 1, docstring::npos);
369                 //lyxerr << "  : " << " (" << counter  << ","
370                 //      << numbertype << ") -> " << label << endl;
371         }
372         //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
373         return label;
374 }
375
376
377 } // namespace lyx