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>
29 using std::ostringstream;
39 void Counter::set(int v)
45 void Counter::addto(int v)
51 int Counter::value() const
69 docstring const & Counter::master() const
75 void Counter::setMaster(docstring const & m)
81 void Counters::newCounter(docstring const & newc)
83 // First check if newc already exist
84 CounterList::iterator const cit = counterList.find(newc);
85 // if already exist give warning and return
86 if (cit != counterList.end()) {
87 lyxerr << "New counter already exists: "
96 void Counters::newCounter(docstring const & newc,
97 docstring const & masterc)
99 // First check if newc already exists
100 CounterList::iterator const cit = counterList.find(newc);
101 // if already existant give warning and return
102 if (cit != counterList.end()) {
103 lyxerr << "New counter already exists: "
108 // then check if masterc exists
109 CounterList::iterator const it = counterList.find(masterc);
110 // if not give warning and return
111 if (it == counterList.end()) {
112 lyxerr << "Master counter does not exist: "
118 counterList[newc].setMaster(masterc);
122 void Counters::set(docstring const & ctr, int const val)
124 CounterList::iterator const it = counterList.find(ctr);
125 if (it == counterList.end()) {
126 lyxerr << "set: Counter does not exist: "
127 << to_utf8(ctr) << endl;
134 void Counters::addto(docstring const & ctr, int const val)
136 CounterList::iterator const it = counterList.find(ctr);
137 if (it == counterList.end()) {
138 lyxerr << "addto: Counter does not exist: "
139 << to_utf8(ctr) << endl;
142 it->second.addto(val);
146 int Counters::value(docstring const & ctr) const
148 CounterList::const_iterator const cit = counterList.find(ctr);
149 if (cit == counterList.end()) {
150 lyxerr << "value: Counter does not exist: "
151 << to_utf8(ctr) << endl;
154 return cit->second.value();
158 void Counters::step(docstring const & ctr)
160 CounterList::iterator it = counterList.find(ctr);
161 if (it == counterList.end()) {
162 lyxerr << "step: Counter does not exist: "
163 << to_utf8(ctr) << endl;
168 it = counterList.begin();
169 CounterList::iterator const end = counterList.end();
170 for (; it != end; ++it) {
171 if (it->second.master() == ctr) {
178 void Counters::reset()
180 CounterList::iterator it = counterList.begin();
181 CounterList::iterator const end = counterList.end();
182 for (; it != end; ++it) {
188 void Counters::reset(docstring const & match)
190 BOOST_ASSERT(!match.empty());
192 CounterList::iterator it = counterList.begin();
193 CounterList::iterator end = counterList.end();
194 for (; it != end; ++it) {
195 if (it->first.find(match) != string::npos)
201 void Counters::copy(Counters & from, Counters & to, docstring const & match)
203 CounterList::iterator it = counterList.begin();
204 CounterList::iterator end = counterList.end();
205 for (; it != end; ++it) {
206 if (it->first.find(match) != string::npos || match == "") {
207 to.set(it->first, from.value(it->first));
215 char loweralphaCounter(int const n)
223 char alphaCounter(int const n)
231 char hebrewCounter(int const n)
233 static const char hebrew[22] = {
234 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
235 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
241 return hebrew[n - 1];
245 docstring const lowerromanCounter(int const n)
247 static char const * const roman[20] = {
248 "i", "ii", "iii", "iv", "v",
249 "vi", "vii", "viii", "ix", "x",
250 "xi", "xii", "xiii", "xiv", "xv",
251 "xvi", "xvii", "xviii", "xix", "xx"
255 return from_ascii("??");
256 return from_ascii(roman[n - 1]);
260 docstring const romanCounter(int const n)
262 static char const * const roman[20] = {
263 "I", "II", "III", "IV", "V",
264 "VI", "VII", "VIII", "IX", "X",
265 "XI", "XII", "XIII", "XIV", "XV",
266 "XVI", "XVII", "XVIII", "XIX", "XX"
270 return from_ascii("??");
271 return from_ascii(roman[n - 1]);
277 docstring Counters::labelItem(docstring const & ctr,
278 docstring const & numbertype)
280 CounterList::const_iterator const cit = counterList.find(ctr);
281 if (cit == counterList.end()) {
284 << " does not exist." << endl;
288 int val = cit->second.value();
290 if (numbertype == "hebrew")
291 return docstring(1, hebrewCounter(val));
293 if (numbertype == "alph")
294 return docstring(1, loweralphaCounter(val));
296 if (numbertype == "Alph")
297 return docstring(1, alphaCounter(val));
299 if (numbertype == "roman")
300 return lowerromanCounter(val);
302 if (numbertype == "Roman")
303 return romanCounter(val);
305 return convert<docstring>(val);
309 docstring Counters::counterLabel(docstring const & format)
311 docstring label = format;
314 #warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
317 size_t const i = label.find('\\', 0);
318 if (i == docstring::npos)
320 size_t const j = label.find('{', i + 1);
321 if (j == docstring::npos)
323 size_t const k = label.find('}', j + 1);
324 if (k == docstring::npos)
326 docstring const numbertype(label, i + 1, j - i - 1);
327 docstring const counter(label, j + 1, k - j - 1);
328 docstring const rep = labelItem(counter, numbertype);
329 label = docstring(label, 0, i) + rep
330 + docstring(label, k + 1, docstring::npos);
331 //lyxerr << " : " << " (" << counter << ","
332 // << numbertype << ") -> " << label << endl;
334 //lyxerr << "counterLabel: " << format << " -> " << label << endl;