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