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;
34 void Counter::set(int v)
40 void Counter::addto(int v)
46 int Counter::value() const
64 string Counter::master() const
70 void Counter::setMaster(string const & m)
76 void Counters::newCounter(string const & newc)
78 // First check if newc already exist
79 CounterList::iterator cit = counterList.find(newc);
80 // if already exist give warning and return
81 if (cit != counterList.end()) {
82 lyxerr << "The new counter already exists." << endl;
89 void Counters::newCounter(string const & newc, string const & masterc)
91 // First check if newc already exists
92 CounterList::iterator cit = counterList.find(newc);
93 // if already existant give warning and return
94 if (cit != counterList.end()) {
95 lyxerr << "The new counter already exists." << endl;
98 // then check if masterc exists
99 CounterList::iterator it = counterList.find(masterc);
100 // if not give warning and return
101 if (it == counterList.end()) {
102 lyxerr << "The master counter does not exist." << endl;
106 counterList[newc].setMaster(masterc);
110 void Counters::set(string const & ctr, int val)
112 CounterList::iterator it = counterList.find(ctr);
113 if (it == counterList.end()) {
114 lyxerr << "set: Counter does not exist: " << ctr << endl;
121 void Counters::addto(string const & ctr, int val)
123 CounterList::iterator it = counterList.find(ctr);
124 if (it == counterList.end()) {
125 lyxerr << "addto: Counter does not exist: " << ctr << endl;
128 it->second.addto(val);
132 int Counters::value(string const & ctr) const
134 CounterList::const_iterator cit = counterList.find(ctr);
135 if (cit == counterList.end()) {
136 lyxerr << "value: Counter does not exist: " << ctr << endl;
139 return cit->second.value();
143 void Counters::step(string const & ctr)
145 CounterList::iterator it = counterList.find(ctr);
146 if (it == counterList.end()) {
147 lyxerr << "step: Counter does not exist: " << ctr << endl;
152 it = counterList.begin();
153 CounterList::iterator end = counterList.end();
154 for (; it != end; ++it) {
155 if (it->second.master() == ctr) {
162 void Counters::reset()
164 CounterList::iterator it = counterList.begin();
165 CounterList::iterator end = counterList.end();
166 for (; it != end; ++it) {
172 void Counters::reset(string const & match)
174 BOOST_ASSERT(!match.empty());
176 CounterList::iterator it = counterList.begin();
177 CounterList::iterator end = counterList.end();
178 for (; it != end; ++it) {
179 if (it->first.find(match) != string::npos)
185 void Counters::copy(Counters & from, Counters & to, string const & match)
187 CounterList::iterator it = counterList.begin();
188 CounterList::iterator end = counterList.end();
189 for (; it != end; ++it) {
190 if (it->first.find(match) != string::npos || match == "") {
191 to.set(it->first, from.value(it->first));
199 char loweralphaCounter(int n)
207 char alphaCounter(int n)
215 char hebrewCounter(int n)
217 static const char hebrew[22] = {
218 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
219 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
225 return hebrew[n - 1];
229 string const lowerromanCounter(int n)
231 static char const * roman[20] = {
232 "i", "ii", "iii", "iv", "v",
233 "vi", "vii", "viii", "ix", "x",
234 "xi", "xii", "xiii", "xiv", "xv",
235 "xvi", "xvii", "xviii", "xix", "xx"
244 string const romanCounter(int n)
246 static char 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"
261 string Counters::labelItem(string const & ctr, string const & numbertype)
263 if (counterList.find(ctr) == counterList.end()) {
264 lyxerr << "Counter " << ctr << " does not exist." << endl;
268 if (numbertype == "hebrew")
269 return string(1, hebrewCounter(value(ctr)));
271 if (numbertype == "alph")
272 return string(1, loweralphaCounter(value(ctr)));
274 if (numbertype == "Alph")
275 return string(1, alphaCounter(value(ctr)));
277 if (numbertype == "roman")
278 return lowerromanCounter(value(ctr));
280 if (numbertype == "Roman")
281 return romanCounter(value(ctr));
283 return tostr(value(ctr));
287 string Counters::counterLabel(string const & format)
289 string label = format;
291 #warning Using boost::regex would make this code a lot simpler... (Lgb)
293 size_t const i = label.find('\\', 0);
294 if (i == string::npos)
296 size_t const j = label.find('{', i + 1);
297 if (j == string::npos)
299 size_t const k = label.find('}', j + 1);
300 if (k == string::npos)
302 string const numbertype(label, i + 1, j - i - 1);
303 string const counter(label, j + 1, k - j - 1);
304 string const rep = labelItem(counter, numbertype);
305 label = string(label, 0, i) + rep + string(label, k + 1, string::npos);
306 //lyxerr << " : " << " (" << counter << ","
307 // << numbertype << ") -> " << label << endl;
309 //lyxerr << "counterLabel: " << format << " -> " << label << endl;
314 string Counters::enumLabel(string const & ctr, string const & langtype)
318 if (langtype == "hebrew") {
320 os << '.' << value("enumi");
321 else if (ctr == "enumii")
322 os << '(' << hebrewCounter(value("enumii")) << ')';
323 else if (ctr == "enumiii")
324 os << '.' << lowerromanCounter(value("enumiii"));
325 else if (ctr == "enumiv")
326 os << '.' << alphaCounter(value("enumiv"));
329 os << value("enumi") << '.';
330 else if (ctr == "enumii")
331 os << '(' << loweralphaCounter(value("enumii")) << ')';
332 else if (ctr == "enumiii")
333 os << lowerromanCounter(value("enumiii")) << '.';
334 else if (ctr == "enumiv")
335 os << alphaCounter(value("enumiv")) << '.';