]> git.lyx.org Git - lyx.git/blob - src/Counters.cpp
Fixed some lines that were too long. It compiled afterwards.
[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
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 bool Counters::hasCounter(docstring const & c) const
124 {
125         return counterList.find(c) != counterList.end();
126 }
127
128
129 void Counters::set(docstring const & ctr, int const val)
130 {
131         CounterList::iterator const it = counterList.find(ctr);
132         if (it == counterList.end()) {
133                 lyxerr << "set: Counter does not exist: "
134                        << to_utf8(ctr) << endl;
135                 return;
136         }
137         it->second.set(val);
138 }
139
140
141 void Counters::addto(docstring const & ctr, int const val)
142 {
143         CounterList::iterator const it = counterList.find(ctr);
144         if (it == counterList.end()) {
145                 lyxerr << "addto: Counter does not exist: "
146                        << to_utf8(ctr) << endl;
147                 return;
148         }
149         it->second.addto(val);
150 }
151
152
153 int Counters::value(docstring const & ctr) const
154 {
155         CounterList::const_iterator const cit = counterList.find(ctr);
156         if (cit == counterList.end()) {
157                 lyxerr << "value: Counter does not exist: "
158                        << to_utf8(ctr) << endl;
159                 return 0;
160         }
161         return cit->second.value();
162 }
163
164
165 void Counters::step(docstring const & ctr)
166 {
167         CounterList::iterator it = counterList.find(ctr);
168         if (it == counterList.end()) {
169                 lyxerr << "step: Counter does not exist: "
170                        << to_utf8(ctr) << endl;
171                 return;
172         }
173
174         it->second.step();
175         it = counterList.begin();
176         CounterList::iterator const end = counterList.end();
177         for (; it != end; ++it) {
178                 if (it->second.master() == ctr) {
179                         it->second.reset();
180                 }
181         }
182 }
183
184
185 void Counters::reset()
186 {
187         appendix_ = false;
188         CounterList::iterator it = counterList.begin();
189         CounterList::iterator const end = counterList.end();
190         for (; it != end; ++it) {
191                 it->second.reset();
192         }
193 }
194
195
196 void Counters::reset(docstring const & match)
197 {
198         BOOST_ASSERT(!match.empty());
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)
204                         it->second.reset();
205         }
206 }
207
208
209 void Counters::copy(Counters & from, Counters & to, docstring const & match)
210 {
211         CounterList::iterator it = counterList.begin();
212         CounterList::iterator end = counterList.end();
213         for (; it != end; ++it) {
214                 if (it->first.find(match) != string::npos || match == "") {
215                         to.set(it->first, from.value(it->first));
216                 }
217         }
218 }
219
220
221 namespace {
222
223 char loweralphaCounter(int const n)
224 {
225         if (n < 1 || n > 26)
226                 return '?';
227         return 'a' + n - 1;
228 }
229
230
231 char alphaCounter(int const n)
232 {
233         if (n < 1 || n > 26)
234                 return '?';
235         return 'A' + n - 1;
236 }
237
238
239 char hebrewCounter(int const n)
240 {
241         static const char hebrew[22] = {
242                 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
243                 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
244                 '\xf7', '\xf8', '\xf9', '\xfa'
245         };
246
247         if (n < 1 || n > 22)
248                 return '?';
249         return hebrew[n - 1];
250 }
251
252
253
254 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
255 //and for a list of roman numerals up to and including 3999, see 
256 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
257 //for this info.)
258 docstring const romanCounter(int const n)
259 {
260         static char const * const ones[9] = {
261                 "I",   "II",  "III", "IV", "V",
262                 "VI",  "VII", "VIII", "IX"
263         };
264         
265         static char const * const tens[9] = {
266                 "X", "XX", "XXX", "XL", "L",
267                 "LX", "LXX", "LXXX", "XC"
268         };
269         
270         static char const * const hunds[9] = {
271                 "C", "CC", "CCC", "CD", "D",
272                 "DC", "DCC", "DCCC", "CM"
273         };
274         
275         if (n > 1000 || n < 1) 
276                 return from_ascii("??");
277         
278         int val = n;
279         string roman;
280         switch (n) {
281         //special cases
282         case 900: 
283                 roman = "CM";
284                 break;
285         case 400:
286                 roman = "CD";
287                 break;
288         default:
289                 if (val >= 100) {
290                         int hundreds = val / 100;
291                         roman = hunds[hundreds - 1];
292                         val = val % 100;
293                 }
294                 if (val >= 10) {
295                         switch (val) {
296                         //special case
297                         case 90:
298                                 roman = roman + "XC";
299                                 val = 0; //skip next
300                                 break;
301                         default:
302                                 int tensnum = val / 10;
303                                 roman = roman + tens[tensnum - 1];
304                                 val = val % 10;
305                         } // end switch
306                 } // end tens
307                 if (val > 0)
308                         roman = roman + ones[val -1];
309         }
310         return from_ascii(roman);
311 }
312
313
314 docstring const lowerromanCounter(int const n)
315 {
316         return support::lowercase(romanCounter(n));
317 }
318
319 } // namespace anon
320
321
322 docstring Counters::labelItem(docstring const & ctr,
323                               docstring const & numbertype)
324 {
325         CounterList::const_iterator const cit = counterList.find(ctr);
326         if (cit == counterList.end()) {
327                 lyxerr << "Counter "
328                        << to_utf8(ctr)
329                        << " does not exist." << endl;
330                 return docstring();
331         }
332
333         int val = cit->second.value();
334
335         if (numbertype == "hebrew")
336                 return docstring(1, hebrewCounter(val));
337
338         if (numbertype == "alph")
339                 return docstring(1, loweralphaCounter(val));
340
341         if (numbertype == "Alph")
342                 return docstring(1, alphaCounter(val));
343
344         if (numbertype == "roman")
345                 return lowerromanCounter(val);
346
347         if (numbertype == "Roman")
348                 return romanCounter(val);
349
350         return convert<docstring>(val);
351 }
352
353
354 docstring Counters::counterLabel(docstring const & format)
355 {
356         docstring label = format;
357         while (true) {
358                 // FIXME: Using boost::regex or boost::spirit would make
359                 // FIXME: this code a lot simpler... (Lgb)
360
361                 size_t const i = label.find('\\', 0);
362                 if (i == docstring::npos)
363                         break;
364                 size_t const j = label.find('{', i + 1);
365                 if (j == docstring::npos)
366                         break;
367                 size_t const k = label.find('}', j + 1);
368                 if (k == docstring::npos)
369                         break;
370                 docstring const numbertype(label, i + 1, j - i - 1);
371                 docstring const counter(label, j + 1, k - j - 1);
372                 docstring const rep = labelItem(counter, numbertype);
373                 label = docstring(label, 0, i) + rep
374                         + docstring(label, k + 1, docstring::npos);
375                 //lyxerr << "  : " << " (" << counter  << ","
376                 //      << numbertype << ") -> " << label << endl;
377         }
378         //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
379         return label;
380 }
381
382
383 } // namespace lyx