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 bool Counters::hasCounter(docstring const & c) const
125 return counterList.find(c) != counterList.end();
129 void Counters::set(docstring const & ctr, int const val)
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;
141 void Counters::addto(docstring const & ctr, int const val)
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;
149 it->second.addto(val);
153 int Counters::value(docstring const & ctr) const
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;
161 return cit->second.value();
165 void Counters::step(docstring const & ctr)
167 CounterList::iterator it = counterList.find(ctr);
168 if (it == counterList.end()) {
169 lyxerr << "step: Counter does not exist: "
170 << to_utf8(ctr) << endl;
175 it = counterList.begin();
176 CounterList::iterator const end = counterList.end();
177 for (; it != end; ++it) {
178 if (it->second.master() == ctr) {
185 void Counters::reset()
188 CounterList::iterator it = counterList.begin();
189 CounterList::iterator const end = counterList.end();
190 for (; it != end; ++it) {
196 void Counters::reset(docstring const & match)
198 BOOST_ASSERT(!match.empty());
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)
209 void Counters::copy(Counters & from, Counters & to, docstring const & match)
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));
223 char loweralphaCounter(int const n)
231 char alphaCounter(int const n)
239 char hebrewCounter(int const n)
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'
249 return hebrew[n - 1];
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
258 docstring const romanCounter(int const n)
260 static char const * const ones[9] = {
261 "I", "II", "III", "IV", "V",
262 "VI", "VII", "VIII", "IX"
265 static char const * const tens[9] = {
266 "X", "XX", "XXX", "XL", "L",
267 "LX", "LXX", "LXXX", "XC"
270 static char const * const hunds[9] = {
271 "C", "CC", "CCC", "CD", "D",
272 "DC", "DCC", "DCCC", "CM"
275 if (n > 1000 || n < 1)
276 return from_ascii("??");
290 int hundreds = val / 100;
291 roman = hunds[hundreds - 1];
298 roman = roman + "XC";
302 int tensnum = val / 10;
303 roman = roman + tens[tensnum - 1];
308 roman = roman + ones[val -1];
310 return from_ascii(roman);
314 docstring const lowerromanCounter(int const n)
316 return support::lowercase(romanCounter(n));
322 docstring Counters::labelItem(docstring const & ctr,
323 docstring const & numbertype)
325 CounterList::const_iterator const cit = counterList.find(ctr);
326 if (cit == counterList.end()) {
329 << " does not exist." << endl;
333 int val = cit->second.value();
335 if (numbertype == "hebrew")
336 return docstring(1, hebrewCounter(val));
338 if (numbertype == "alph")
339 return docstring(1, loweralphaCounter(val));
341 if (numbertype == "Alph")
342 return docstring(1, alphaCounter(val));
344 if (numbertype == "roman")
345 return lowerromanCounter(val);
347 if (numbertype == "Roman")
348 return romanCounter(val);
350 return convert<docstring>(val);
354 docstring Counters::counterLabel(docstring const & format)
356 docstring label = format;
358 // FIXME: Using boost::regex or boost::spirit would make
359 // FIXME: this code a lot simpler... (Lgb)
361 size_t const i = label.find('\\', 0);
362 if (i == docstring::npos)
364 size_t const j = label.find('{', i + 1);
365 if (j == docstring::npos)
367 size_t const k = label.find('}', j + 1);
368 if (k == docstring::npos)
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;
378 //lyxerr << "counterLabel: " << format << " -> " << label << endl;