]> git.lyx.org Git - lyx.git/blob - src/Counters.cpp
Whitespace cleanup
[lyx.git] / src / Counters.cpp
1 /**
2  * \file Counters.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Martin Vermeer
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "Counters.h"
16 #include "debug.h"
17
18 #include "support/lstrings.h"
19 #include "support/convert.h"
20
21 #include <boost/assert.hpp>
22
23 #include <sstream>
24
25
26 namespace lyx {
27
28 using std::endl;
29 using std::ostringstream;
30 using std::string;
31
32
33 Counter::Counter()
34 {
35         reset();
36 }
37
38
39 void Counter::set(int v)
40 {
41         value_ = v;
42 }
43
44
45 void Counter::addto(int v)
46 {
47         value_ += v;
48 }
49
50
51 int Counter::value() const
52 {
53         return value_;
54 }
55
56
57 void Counter::step()
58 {
59         ++value_;
60 }
61
62
63 void Counter::reset()
64 {
65         value_ = 0;
66 }
67
68
69 docstring const & Counter::master() const
70 {
71         return master_;
72 }
73
74
75 void Counter::setMaster(docstring const & m)
76 {
77         master_ = m;
78 }
79
80
81 void Counters::newCounter(docstring const & newc)
82 {
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: "
88                        << to_utf8(newc)
89                        << endl;
90                 return;
91         }
92         counterList[newc];
93 }
94
95
96 void Counters::newCounter(docstring const & newc,
97                           docstring const & masterc)
98 {
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: "
104                        << to_utf8(newc)
105                        << endl;
106                 return;
107         }
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: "
113                        << to_utf8(masterc)
114                        << endl;
115                 return;
116         }
117
118         counterList[newc].setMaster(masterc);
119 }
120
121
122 void Counters::set(docstring const & ctr, int const val)
123 {
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;
128                 return;
129         }
130         it->second.set(val);
131 }
132
133
134 void Counters::addto(docstring const & ctr, int const val)
135 {
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;
140                 return;
141         }
142         it->second.addto(val);
143 }
144
145
146 int Counters::value(docstring const & ctr) const
147 {
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;
152                 return 0;
153         }
154         return cit->second.value();
155 }
156
157
158 void Counters::step(docstring const & ctr)
159 {
160         CounterList::iterator it = counterList.find(ctr);
161         if (it == counterList.end()) {
162                 lyxerr << "step: Counter does not exist: "
163                        << to_utf8(ctr) << endl;
164                 return;
165         }
166
167         it->second.step();
168         it = counterList.begin();
169         CounterList::iterator const end = counterList.end();
170         for (; it != end; ++it) {
171                 if (it->second.master() == ctr) {
172                         it->second.reset();
173                 }
174         }
175 }
176
177
178 void Counters::reset()
179 {
180         CounterList::iterator it = counterList.begin();
181         CounterList::iterator const end = counterList.end();
182         for (; it != end; ++it) {
183                 it->second.reset();
184         }
185 }
186
187
188 void Counters::reset(docstring const & match)
189 {
190         BOOST_ASSERT(!match.empty());
191
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)
196                         it->second.reset();
197         }
198 }
199
200
201 void Counters::copy(Counters & from, Counters & to, docstring const & match)
202 {
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));
208                 }
209         }
210 }
211
212
213 namespace {
214
215 char loweralphaCounter(int const n)
216 {
217         if (n < 1 || n > 26)
218                 return '?';
219         return 'a' + n - 1;
220 }
221
222
223 char alphaCounter(int const n)
224 {
225         if (n < 1 || n > 26)
226                 return '?';
227         return 'A' + n - 1;
228 }
229
230
231 char hebrewCounter(int const n)
232 {
233         static const char hebrew[22] = {
234                 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
235                 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
236                 '\xf7', '\xf8', '\xf9', '\xfa'
237         };
238
239         if (n < 1 || n > 22)
240                 return '?';
241         return hebrew[n - 1];
242 }
243
244
245 docstring const lowerromanCounter(int const n)
246 {
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"
252         };
253
254         if (n < 1 || n > 20)
255                 return from_ascii("??");
256         return from_ascii(roman[n - 1]);
257 }
258
259
260 docstring const romanCounter(int const n)
261 {
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"
267         };
268
269         if (n < 1 || n > 20)
270                 return from_ascii("??");
271         return from_ascii(roman[n - 1]);
272 }
273
274 } // namespace anon
275
276
277 docstring Counters::labelItem(docstring const & ctr,
278                               docstring const & numbertype)
279 {
280         CounterList::const_iterator const cit = counterList.find(ctr);
281         if (cit == counterList.end()) {
282                 lyxerr << "Counter "
283                        << to_utf8(ctr)
284                        << " does not exist." << endl;
285                 return docstring();
286         }
287
288         int val = cit->second.value();
289
290         if (numbertype == "hebrew")
291                 return docstring(1, hebrewCounter(val));
292
293         if (numbertype == "alph")
294                 return docstring(1, loweralphaCounter(val));
295
296         if (numbertype == "Alph")
297                 return docstring(1, alphaCounter(val));
298
299         if (numbertype == "roman")
300                 return lowerromanCounter(val);
301
302         if (numbertype == "Roman")
303                 return romanCounter(val);
304
305         return convert<docstring>(val);
306 }
307
308
309 docstring Counters::counterLabel(docstring const & format)
310 {
311         docstring label = format;
312         while (true) {
313 #ifdef WITH_WARNINGS
314 #warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
315 #endif
316
317                 size_t const i = label.find('\\', 0);
318                 if (i == docstring::npos)
319                         break;
320                 size_t const j = label.find('{', i + 1);
321                 if (j == docstring::npos)
322                         break;
323                 size_t const k = label.find('}', j + 1);
324                 if (k == docstring::npos)
325                         break;
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;
333         }
334         //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
335         return label;
336 }
337
338
339 } // namespace lyx