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;
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()
181 CounterList::iterator it = counterList.begin();
182 CounterList::iterator const end = counterList.end();
183 for (; it != end; ++it) {
189 void Counters::reset(docstring const & match)
191 BOOST_ASSERT(!match.empty());
193 CounterList::iterator it = counterList.begin();
194 CounterList::iterator end = counterList.end();
195 for (; it != end; ++it) {
196 if (it->first.find(match) != string::npos)
202 void Counters::copy(Counters & from, Counters & to, docstring const & match)
204 CounterList::iterator it = counterList.begin();
205 CounterList::iterator end = counterList.end();
206 for (; it != end; ++it) {
207 if (it->first.find(match) != string::npos || match == "") {
208 to.set(it->first, from.value(it->first));
216 char loweralphaCounter(int const n)
224 char alphaCounter(int const n)
232 char hebrewCounter(int const n)
234 static const char hebrew[22] = {
235 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
236 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
237 '\xf7', '\xf8', '\xf9', '\xfa'
242 return hebrew[n - 1];
246 docstring const lowerromanCounter(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"
256 return from_ascii("??");
257 return from_ascii(roman[n - 1]);
261 docstring const romanCounter(int const n)
263 static char const * const roman[20] = {
264 "I", "II", "III", "IV", "V",
265 "VI", "VII", "VIII", "IX", "X",
266 "XI", "XII", "XIII", "XIV", "XV",
267 "XVI", "XVII", "XVIII", "XIX", "XX"
271 return from_ascii("??");
272 return from_ascii(roman[n - 1]);
278 docstring Counters::labelItem(docstring const & ctr,
279 docstring const & numbertype)
281 CounterList::const_iterator const cit = counterList.find(ctr);
282 if (cit == counterList.end()) {
285 << " does not exist." << endl;
289 int val = cit->second.value();
291 if (numbertype == "hebrew")
292 return docstring(1, hebrewCounter(val));
294 if (numbertype == "alph")
295 return docstring(1, loweralphaCounter(val));
297 if (numbertype == "Alph")
298 return docstring(1, alphaCounter(val));
300 if (numbertype == "roman")
301 return lowerromanCounter(val);
303 if (numbertype == "Roman")
304 return romanCounter(val);
306 return convert<docstring>(val);
310 docstring Counters::counterLabel(docstring const & format)
312 docstring label = format;
315 #warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
318 size_t const i = label.find('\\', 0);
319 if (i == docstring::npos)
321 size_t const j = label.find('{', i + 1);
322 if (j == docstring::npos)
324 size_t const k = label.find('}', j + 1);
325 if (k == docstring::npos)
327 docstring const numbertype(label, i + 1, j - i - 1);
328 docstring const counter(label, j + 1, k - j - 1);
329 docstring const rep = labelItem(counter, numbertype);
330 label = docstring(label, 0, i) + rep
331 + docstring(label, k + 1, docstring::npos);
332 //lyxerr << " : " << " (" << counter << ","
333 // << numbertype << ") -> " << label << endl;
335 //lyxerr << "counterLabel: " << format << " -> " << label << endl;