]> git.lyx.org Git - lyx.git/blob - src/counters.C
Make string conversion work with non-ucs2-characters if using qt 4.2
[lyx.git] / src / counters.C
1 /**
2  * \file counters.C
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 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                 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è',
235                 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö',
236                 '÷', 'ø', 'ù', 'ú'
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         if (counterList.find(ctr) == counterList.end()) {
281                 lyxerr << "Counter "
282                        << to_utf8(ctr)
283                        << " does not exist." << endl;
284                 return docstring();
285         }
286
287         if (numbertype == "hebrew")
288                 return docstring(1, hebrewCounter(value(ctr)));
289
290         if (numbertype == "alph")
291                 return docstring(1, loweralphaCounter(value(ctr)));
292
293         if (numbertype == "Alph")
294                 return docstring(1, alphaCounter(value(ctr)));
295
296         if (numbertype == "roman")
297                 return lowerromanCounter(value(ctr));
298
299         if (numbertype == "Roman")
300                 return romanCounter(value(ctr));
301
302         return convert<docstring>(value(ctr));
303 }
304
305
306 docstring Counters::counterLabel(docstring const & format)
307 {
308         docstring label = format;
309         while (true) {
310 #ifdef WITH_WARNINGS
311 #warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb)
312 #endif
313
314                 size_t const i = label.find('\\', 0);
315                 if (i == docstring::npos)
316                         break;
317                 size_t const j = label.find('{', i + 1);
318                 if (j == docstring::npos)
319                         break;
320                 size_t const k = label.find('}', j + 1);
321                 if (k == string::npos)
322                         break;
323                 docstring const numbertype(label, i + 1, j - i - 1);
324                 docstring const counter(label, j + 1, k - j - 1);
325                 docstring const rep = labelItem(counter, numbertype);
326                 label = docstring(label, 0, i) + rep + docstring(label, k + 1, string::npos);
327                 //lyxerr << "  : " << " (" << counter  << ","
328                 //      << numbertype << ") -> " << label << endl;
329         }
330         //lyxerr << "counterLabel: " << format  << " -> "       << label << endl;
331         return label;
332 }
333
334
335 } // namespace lyx