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
10 * Full author contact details are available in file CREDITS.
18 #include "support/lstrings.h"
19 #include "support/convert.h"
21 #include <boost/assert.hpp>
26 using std::ostringstream;
36 void Counter::set(int v)
42 void Counter::addto(int v)
48 int Counter::value() const
66 string Counter::master() const
72 void Counter::setMaster(string const & m)
78 void Counters::newCounter(string const & newc)
80 // First check if newc already exist
81 CounterList::iterator const cit = counterList.find(newc);
82 // if already exist give warning and return
83 if (cit != counterList.end()) {
84 lyxerr << "New counter already exists: " << newc << endl;
91 void Counters::newCounter(string const & newc, string const & masterc)
93 // First check if newc already exists
94 CounterList::iterator const cit = counterList.find(newc);
95 // if already existant give warning and return
96 if (cit != counterList.end()) {
97 lyxerr << "New counter already exists: " << newc << endl;
100 // then check if masterc exists
101 CounterList::iterator const it = counterList.find(masterc);
102 // if not give warning and return
103 if (it == counterList.end()) {
104 lyxerr << "Master counter does not exist: " << masterc << endl;
108 counterList[newc].setMaster(masterc);
112 void Counters::set(string const & ctr, int const val)
114 CounterList::iterator const it = counterList.find(ctr);
115 if (it == counterList.end()) {
116 lyxerr << "set: Counter does not exist: " << ctr << endl;
123 void Counters::addto(string const & ctr, int const val)
125 CounterList::iterator const it = counterList.find(ctr);
126 if (it == counterList.end()) {
127 lyxerr << "addto: Counter does not exist: " << ctr << endl;
130 it->second.addto(val);
134 int Counters::value(string const & ctr) const
136 CounterList::const_iterator const cit = counterList.find(ctr);
137 if (cit == counterList.end()) {
138 lyxerr << "value: Counter does not exist: " << ctr << endl;
141 return cit->second.value();
145 void Counters::step(string const & ctr)
147 CounterList::iterator it = counterList.find(ctr);
148 if (it == counterList.end()) {
149 lyxerr << "step: Counter does not exist: " << ctr << endl;
154 it = counterList.begin();
155 CounterList::iterator const end = counterList.end();
156 for (; it != end; ++it) {
157 if (it->second.master() == ctr) {
164 void Counters::reset()
166 CounterList::iterator it = counterList.begin();
167 CounterList::iterator const end = counterList.end();
168 for (; it != end; ++it) {
174 void Counters::reset(string const & match)
176 BOOST_ASSERT(!match.empty());
178 CounterList::iterator it = counterList.begin();
179 CounterList::iterator end = counterList.end();
180 for (; it != end; ++it) {
181 if (it->first.find(match) != string::npos)
187 void Counters::copy(Counters & from, Counters & to, string const & match)
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 || match == "") {
193 to.set(it->first, from.value(it->first));
201 char loweralphaCounter(int const n)
209 char alphaCounter(int const n)
217 char hebrewCounter(int const n)
219 static const char hebrew[22] = {
220 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
221 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
227 return hebrew[n - 1];
231 string const lowerromanCounter(int const n)
233 static char const * const roman[20] = {
234 "i", "ii", "iii", "iv", "v",
235 "vi", "vii", "viii", "ix", "x",
236 "xi", "xii", "xiii", "xiv", "xv",
237 "xvi", "xvii", "xviii", "xix", "xx"
246 string const romanCounter(int const n)
248 static char const * const roman[20] = {
249 "I", "II", "III", "IV", "V",
250 "VI", "VII", "VIII", "IX", "X",
251 "XI", "XII", "XIII", "XIV", "XV",
252 "XVI", "XVII", "XVIII", "XIX", "XX"
263 string Counters::labelItem(string const & ctr, string const & numbertype)
265 if (counterList.find(ctr) == counterList.end()) {
266 lyxerr << "Counter " << ctr << " does not exist." << endl;
270 if (numbertype == "hebrew")
271 return string(1, hebrewCounter(value(ctr)));
273 if (numbertype == "alph")
274 return string(1, loweralphaCounter(value(ctr)));
276 if (numbertype == "Alph")
277 return string(1, alphaCounter(value(ctr)));
279 if (numbertype == "roman")
280 return lowerromanCounter(value(ctr));
282 if (numbertype == "Roman")
283 return romanCounter(value(ctr));
285 return convert<string>(value(ctr));
289 string Counters::counterLabel(string const & format)
291 string label = format;
294 #warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
297 size_t const i = label.find('\\', 0);
298 if (i == string::npos)
300 size_t const j = label.find('{', i + 1);
301 if (j == string::npos)
303 size_t const k = label.find('}', j + 1);
304 if (k == string::npos)
306 string const numbertype(label, i + 1, j - i - 1);
307 string const counter(label, j + 1, k - j - 1);
308 string const rep = labelItem(counter, numbertype);
309 label = string(label, 0, i) + rep + string(label, k + 1, string::npos);
310 //lyxerr << " : " << " (" << counter << ","
311 // << numbertype << ") -> " << label << endl;
313 //lyxerr << "counterLabel: " << format << " -> " << label << endl;