]> git.lyx.org Git - lyx.git/blob - src/Counters.cpp
update from Koji Yokota
[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         appendix_ = false;
181         CounterList::iterator it = counterList.begin();
182         CounterList::iterator const end = counterList.end();
183         for (; it != end; ++it) {
184                 it->second.reset();
185         }
186 }
187
188
189 void Counters::reset(docstring const & match)
190 {
191         BOOST_ASSERT(!match.empty());
192
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)
197                         it->second.reset();
198         }
199 }
200
201
202 void Counters::copy(Counters & from, Counters & to, docstring const & match)
203 {
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));
209                 }
210         }
211 }
212
213
214 namespace {
215
216 char loweralphaCounter(int const n)
217 {
218         if (n < 1 || n > 26)
219                 return '?';
220         return 'a' + n - 1;
221 }
222
223
224 char alphaCounter(int const n)
225 {
226         if (n < 1 || n > 26)
227                 return '?';
228         return 'A' + n - 1;
229 }
230
231
232 char hebrewCounter(int const n)
233 {
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'
238         };
239
240         if (n < 1 || n > 22)
241                 return '?';
242         return hebrew[n - 1];
243 }
244
245
246 docstring const lowerromanCounter(int const n)
247 {
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"
253         };
254
255         if (n < 1 || n > 20)
256                 return from_ascii("??");
257         return from_ascii(roman[n - 1]);
258 }
259
260
261 docstring const romanCounter(int const n)
262 {
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"
268         };
269
270         if (n < 1 || n > 20)
271                 return from_ascii("??");
272         return from_ascii(roman[n - 1]);
273 }
274
275 } // namespace anon
276
277
278 docstring Counters::labelItem(docstring const & ctr,
279                               docstring const & numbertype)
280 {
281         CounterList::const_iterator const cit = counterList.find(ctr);
282         if (cit == counterList.end()) {
283                 lyxerr << "Counter "
284                        << to_utf8(ctr)
285                        << " does not exist." << endl;
286                 return docstring();
287         }
288
289         int val = cit->second.value();
290
291         if (numbertype == "hebrew")
292                 return docstring(1, hebrewCounter(val));
293
294         if (numbertype == "alph")
295                 return docstring(1, loweralphaCounter(val));
296
297         if (numbertype == "Alph")
298                 return docstring(1, alphaCounter(val));
299
300         if (numbertype == "roman")
301                 return lowerromanCounter(val);
302
303         if (numbertype == "Roman")
304                 return romanCounter(val);
305
306         return convert<docstring>(val);
307 }
308
309
310 docstring Counters::counterLabel(docstring const & format)
311 {
312         docstring label = format;
313         while (true) {
314 #ifdef WITH_WARNINGS
315 #warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
316 #endif
317
318                 size_t const i = label.find('\\', 0);
319                 if (i == docstring::npos)
320                         break;
321                 size_t const j = label.find('{', i + 1);
322                 if (j == docstring::npos)
323                         break;
324                 size_t const k = label.find('}', j + 1);
325                 if (k == docstring::npos)
326                         break;
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;
334         }
335         //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
336         return label;
337 }
338
339
340 } // namespace lyx