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>
28 using std::ostringstream;
38 void Counter::set(int v)
44 void Counter::addto(int v)
50 int Counter::value() const
68 docstring Counter::master() const
74 void Counter::setMaster(docstring const & m)
80 void Counters::newCounter(docstring const & newc)
82 // First check if newc already exist
83 CounterList::iterator const cit = counterList.find(newc);
84 // if already exist give warning and return
85 if (cit != counterList.end()) {
86 lyxerr << "New counter already exists: "
95 void Counters::newCounter(docstring const & newc,
96 docstring const & masterc)
98 // First check if newc already exists
99 CounterList::iterator const cit = counterList.find(newc);
100 // if already existant give warning and return
101 if (cit != counterList.end()) {
102 lyxerr << "New counter already exists: "
103 << lyx::to_utf8(newc)
107 // then check if masterc exists
108 CounterList::iterator const it = counterList.find(masterc);
109 // if not give warning and return
110 if (it == counterList.end()) {
111 lyxerr << "Master counter does not exist: "
112 << lyx::to_utf8(masterc)
117 counterList[newc].setMaster(masterc);
121 void Counters::set(docstring const & ctr, int const val)
123 CounterList::iterator const it = counterList.find(ctr);
124 if (it == counterList.end()) {
125 lyxerr << "set: Counter does not exist: "
126 << lyx::to_utf8(ctr) << endl;
133 void Counters::addto(docstring const & ctr, int const val)
135 CounterList::iterator const it = counterList.find(ctr);
136 if (it == counterList.end()) {
137 lyxerr << "addto: Counter does not exist: "
138 << lyx::to_utf8(ctr) << endl;
141 it->second.addto(val);
145 int Counters::value(docstring const & ctr) const
147 CounterList::const_iterator const cit = counterList.find(ctr);
148 if (cit == counterList.end()) {
149 lyxerr << "value: Counter does not exist: "
150 << lyx::to_utf8(ctr) << endl;
153 return cit->second.value();
157 void Counters::step(docstring const & ctr)
159 CounterList::iterator it = counterList.find(ctr);
160 if (it == counterList.end()) {
161 lyxerr << "step: Counter does not exist: "
162 << lyx::to_utf8(ctr) << endl;
167 it = counterList.begin();
168 CounterList::iterator const end = counterList.end();
169 for (; it != end; ++it) {
170 if (it->second.master() == ctr) {
177 void Counters::reset()
179 CounterList::iterator it = counterList.begin();
180 CounterList::iterator const end = counterList.end();
181 for (; it != end; ++it) {
187 void Counters::reset(docstring const & match)
189 BOOST_ASSERT(!match.empty());
191 CounterList::iterator it = counterList.begin();
192 CounterList::iterator end = counterList.end();
193 for (; it != end; ++it) {
194 if (it->first.find(match) != string::npos)
200 void Counters::copy(Counters & from, Counters & to, docstring const & match)
202 CounterList::iterator it = counterList.begin();
203 CounterList::iterator end = counterList.end();
204 for (; it != end; ++it) {
205 if (it->first.find(match) != string::npos || match == "") {
206 to.set(it->first, from.value(it->first));
214 char loweralphaCounter(int const n)
222 char alphaCounter(int const n)
230 char hebrewCounter(int const n)
232 static const char hebrew[22] = {
233 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
234 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
240 return hebrew[n - 1];
244 docstring const lowerromanCounter(int const n)
246 static char const * const roman[20] = {
247 "i", "ii", "iii", "iv", "v",
248 "vi", "vii", "viii", "ix", "x",
249 "xi", "xii", "xiii", "xiv", "xv",
250 "xvi", "xvii", "xviii", "xix", "xx"
254 return lyx::from_ascii("??");
255 return lyx::from_ascii(roman[n - 1]);
259 docstring const romanCounter(int const n)
261 static char const * const roman[20] = {
262 "I", "II", "III", "IV", "V",
263 "VI", "VII", "VIII", "IX", "X",
264 "XI", "XII", "XIII", "XIV", "XV",
265 "XVI", "XVII", "XVIII", "XIX", "XX"
269 return lyx::from_ascii("??");
270 return lyx::from_ascii(roman[n - 1]);
276 docstring Counters::labelItem(docstring const & ctr,
277 docstring const & numbertype)
279 if (counterList.find(ctr) == counterList.end()) {
282 << " does not exist." << endl;
286 if (numbertype == "hebrew")
287 return docstring(1, hebrewCounter(value(ctr)));
289 if (numbertype == "alph")
290 return docstring(1, loweralphaCounter(value(ctr)));
292 if (numbertype == "Alph")
293 return docstring(1, alphaCounter(value(ctr)));
295 if (numbertype == "roman")
296 return lowerromanCounter(value(ctr));
298 if (numbertype == "Roman")
299 return romanCounter(value(ctr));
301 return convert<docstring>(value(ctr));
305 docstring Counters::counterLabel(docstring const & format)
307 docstring label = format;
310 #warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
313 size_t const i = label.find('\\', 0);
314 if (i == docstring::npos)
316 size_t const j = label.find('{', i + 1);
317 if (j == docstring::npos)
319 size_t const k = label.find('}', j + 1);
320 if (k == string::npos)
322 docstring const numbertype(label, i + 1, j - i - 1);
323 docstring const counter(label, j + 1, k - j - 1);
324 docstring const rep = labelItem(counter, numbertype);
325 label = docstring(label, 0, i) + rep + docstring(label, k + 1, string::npos);
326 //lyxerr << " : " << " (" << counter << ","
327 // << numbertype << ") -> " << label << endl;
329 //lyxerr << "counterLabel: " << format << " -> " << label << endl;