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/std_sstream.h"
20 #include "support/tostr.h"
22 #include <boost/assert.hpp>
25 using std::ostringstream;
35 void Counter::set(int v)
41 void Counter::addto(int v)
47 int Counter::value() const
65 string Counter::master() const
71 void Counter::setMaster(string const & m)
77 void Counters::newCounter(string const & newc)
79 // First check if newc already exist
80 CounterList::iterator cit = counterList.find(newc);
81 // if already exist give warning and return
82 if (cit != counterList.end()) {
83 lyxerr << "The new counter already exists." << endl;
90 void Counters::newCounter(string const & newc, string const & masterc)
92 // First check if newc already exists
93 CounterList::iterator cit = counterList.find(newc);
94 // if already existant give warning and return
95 if (cit != counterList.end()) {
96 lyxerr << "The new counter already exists." << endl;
99 // then check if masterc exists
100 CounterList::iterator it = counterList.find(masterc);
101 // if not give warning and return
102 if (it == counterList.end()) {
103 lyxerr << "The master counter does not exist." << endl;
107 counterList[newc].setMaster(masterc);
111 void Counters::set(string const & ctr, int val)
113 CounterList::iterator it = counterList.find(ctr);
114 if (it == counterList.end()) {
115 lyxerr << "set: Counter does not exist: " << ctr << endl;
122 void Counters::addto(string const & ctr, int val)
124 CounterList::iterator it = counterList.find(ctr);
125 if (it == counterList.end()) {
126 lyxerr << "addto: Counter does not exist: " << ctr << endl;
129 it->second.addto(val);
133 int Counters::value(string const & ctr) const
135 CounterList::const_iterator cit = counterList.find(ctr);
136 if (cit == counterList.end()) {
137 lyxerr << "value: Counter does not exist: " << ctr << endl;
140 return cit->second.value();
144 void Counters::step(string const & ctr)
146 CounterList::iterator it = counterList.find(ctr);
147 if (it == counterList.end()) {
148 lyxerr << "step: Counter does not exist: " << ctr << endl;
153 it = counterList.begin();
154 CounterList::iterator end = counterList.end();
155 for (; it != end; ++it) {
156 if (it->second.master() == ctr) {
163 void Counters::reset()
165 CounterList::iterator it = counterList.begin();
166 CounterList::iterator end = counterList.end();
167 for (; it != end; ++it) {
173 void Counters::reset(string const & match)
175 BOOST_ASSERT(!match.empty());
177 CounterList::iterator it = counterList.begin();
178 CounterList::iterator end = counterList.end();
179 for (; it != end; ++it) {
180 if (it->first.find(match) != string::npos)
186 void Counters::copy(Counters & from, Counters & to, string const & match)
188 CounterList::iterator it = counterList.begin();
189 CounterList::iterator end = counterList.end();
190 for (; it != end; ++it) {
191 if (it->first.find(match) != string::npos || match == "") {
192 to.set(it->first, from.value(it->first));
200 char loweralphaCounter(int n)
208 char alphaCounter(int n)
216 char hebrewCounter(int n)
218 static const char hebrew[22] = {
219 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
220 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
226 return hebrew[n - 1];
230 string const lowerromanCounter(int n)
232 static char const * roman[20] = {
233 "i", "ii", "iii", "iv", "v",
234 "vi", "vii", "viii", "ix", "x",
235 "xi", "xii", "xiii", "xiv", "xv",
236 "xvi", "xvii", "xviii", "xix", "xx"
245 string const romanCounter(int n)
247 static char 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"
262 string Counters::labelItem(string const & ctr, string const & numbertype)
264 if (counterList.find(ctr) == counterList.end()) {
265 lyxerr << "Counter " << ctr << " does not exist." << endl;
269 if (numbertype == "hebrew")
270 return string(1, hebrewCounter(value(ctr)));
272 if (numbertype == "alph")
273 return string(1, loweralphaCounter(value(ctr)));
275 if (numbertype == "Alph")
276 return string(1, alphaCounter(value(ctr)));
278 if (numbertype == "roman")
279 return lowerromanCounter(value(ctr));
281 if (numbertype == "Roman")
282 return romanCounter(value(ctr));
284 return tostr(value(ctr));
288 string Counters::counterLabel(string const & format)
290 string label = format;
292 #warning Using boost::regex would make this code a lot simpler... (Lgb)
294 size_t const i = label.find('\\', 0);
295 if (i == string::npos)
297 size_t const j = label.find('{', i + 1);
298 if (j == string::npos)
300 size_t const k = label.find('}', j + 1);
301 if (k == string::npos)
303 string const numbertype(label, i + 1, j - i - 1);
304 string const counter(label, j + 1, k - j - 1);
305 string const rep = labelItem(counter, numbertype);
306 label = string(label, 0, i) + rep + string(label, k + 1, string::npos);
307 //lyxerr << " : " << " (" << counter << ","
308 // << numbertype << ") -> " << label << endl;
310 //lyxerr << "counterLabel: " << format << " -> " << label << endl;
315 string Counters::enumLabel(string const & ctr, string const & langtype)
319 if (langtype == "hebrew") {
321 os << '.' << value("enumi");
322 else if (ctr == "enumii")
323 os << '(' << hebrewCounter(value("enumii")) << ')';
324 else if (ctr == "enumiii")
325 os << '.' << lowerromanCounter(value("enumiii"));
326 else if (ctr == "enumiv")
327 os << '.' << alphaCounter(value("enumiv"));
330 os << value("enumi") << '.';
331 else if (ctr == "enumii")
332 os << '(' << loweralphaCounter(value("enumii")) << ')';
333 else if (ctr == "enumiii")
334 os << lowerromanCounter(value("enumiii")) << '.';
335 else if (ctr == "enumiv")
336 os << alphaCounter(value("enumiv")) << '.';