3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Martin Vermeer
9 * \author Richard Heck (roman numerals)
11 * Full author contact details are available in file CREDITS.
19 #include "support/lstrings.h"
20 #include "support/convert.h"
22 #include <boost/assert.hpp>
27 using std::ostringstream;
40 void Counter::set(int v)
46 void Counter::addto(int v)
52 int Counter::value() const
70 docstring const & Counter::master() const
76 void Counter::setMaster(docstring const & m)
82 void Counters::newCounter(docstring const & newc)
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: "
97 void Counters::newCounter(docstring const & newc,
98 docstring const & masterc)
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: "
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: "
119 counterList[newc].setMaster(masterc);
123 void Counters::set(docstring const & ctr, int const val)
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;
135 void Counters::addto(docstring const & ctr, int const val)
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;
143 it->second.addto(val);
147 int Counters::value(docstring const & ctr) const
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;
155 return cit->second.value();
159 void Counters::step(docstring const & ctr)
161 CounterList::iterator it = counterList.find(ctr);
162 if (it == counterList.end()) {
163 lyxerr << "step: Counter does not exist: "
164 << to_utf8(ctr) << endl;
169 it = counterList.begin();
170 CounterList::iterator const end = counterList.end();
171 for (; it != end; ++it) {
172 if (it->second.master() == ctr) {
179 void Counters::reset()
182 CounterList::iterator it = counterList.begin();
183 CounterList::iterator const end = counterList.end();
184 for (; it != end; ++it) {
190 void Counters::reset(docstring const & match)
192 BOOST_ASSERT(!match.empty());
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)
203 void Counters::copy(Counters & from, Counters & to, docstring const & match)
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));
217 char loweralphaCounter(int const n)
225 char alphaCounter(int const n)
233 char hebrewCounter(int const n)
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'
243 return hebrew[n - 1];
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
252 docstring const romanCounter(int const n)
254 static char const * const ones[9] = {
255 "I", "II", "III", "IV", "V",
256 "VI", "VII", "VIII", "IX"
259 static char const * const tens[9] = {
260 "X", "XX", "XXX", "XL", "L",
261 "LX", "LXX", "LXXX", "XC"
264 static char const * const hunds[9] = {
265 "C", "CC", "CCC", "CD", "D",
266 "DC", "DCC", "DCCC", "CM"
269 if (n > 1000 || n < 1)
270 return from_ascii("??");
284 int hundreds = val / 100;
285 roman = hunds[hundreds - 1];
292 roman = roman + "XC";
296 int tensnum = val / 10;
297 roman = roman + tens[tensnum - 1];
302 roman = roman + ones[val -1];
304 return from_ascii(roman);
308 docstring const lowerromanCounter(int const n)
310 return support::lowercase(romanCounter(n));
316 docstring Counters::labelItem(docstring const & ctr,
317 docstring const & numbertype)
319 CounterList::const_iterator const cit = counterList.find(ctr);
320 if (cit == counterList.end()) {
323 << " does not exist." << endl;
327 int val = cit->second.value();
329 if (numbertype == "hebrew")
330 return docstring(1, hebrewCounter(val));
332 if (numbertype == "alph")
333 return docstring(1, loweralphaCounter(val));
335 if (numbertype == "Alph")
336 return docstring(1, alphaCounter(val));
338 if (numbertype == "roman")
339 return lowerromanCounter(val);
341 if (numbertype == "Roman")
342 return romanCounter(val);
344 return convert<docstring>(val);
348 docstring Counters::counterLabel(docstring const & format)
350 docstring label = format;
352 // FIXME: Using boost::regex or boost::spirit would make
353 // FIXME: this code a lot simpler... (Lgb)
355 size_t const i = label.find('\\', 0);
356 if (i == docstring::npos)
358 size_t const j = label.find('{', i + 1);
359 if (j == docstring::npos)
361 size_t const k = label.find('}', j + 1);
362 if (k == docstring::npos)
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;
372 //lyxerr << "counterLabel: " << format << " -> " << label << endl;