]> git.lyx.org Git - features.git/blob - src/Counters.cpp
compilation fix
[features.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  * \author Richard Heck (roman numerals)
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "Counters.h"
17 #include "Lexer.h"
18
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/lassert.h"
22 #include "support/lstrings.h"
23
24 #include <algorithm>
25 #include <sstream>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 Counter::Counter()
34 {
35         reset();
36 }
37
38
39 Counter::Counter(docstring const & mc, docstring const & ls, 
40                  docstring const & lsa)
41         : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
42 {
43         reset();
44 }
45
46
47 bool Counter::read(Lexer & lex)
48 {
49         enum {
50                 CT_WITHIN = 1,
51                 CT_LABELSTRING,
52                 CT_LABELSTRING_APPENDIX,
53                 CT_END
54         };
55
56         LexerKeyword counterTags[] = {
57                 { "end", CT_END },
58                 { "labelstring", CT_LABELSTRING },
59                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
60                 { "within", CT_WITHIN }
61         };
62
63         lex.pushTable(counterTags);
64
65         bool getout = false;
66         while (!getout && lex.isOK()) {
67                 int le = lex.lex();
68                 switch (le) {
69                         case Lexer::LEX_UNDEF:
70                                 lex.printError("Unknown counter tag `$$Token'");
71                                 continue;
72                         default: 
73                                 break;
74                 }
75                 switch (le) {
76                         case CT_WITHIN:
77                                 lex.next();
78                                 master_ = lex.getDocString();
79                                 if (master_ == "none")
80                                         master_.erase();
81                                 break;
82                         case CT_LABELSTRING:
83                                 lex.next();
84                                 labelstring_ = lex.getDocString();
85                                 labelstringappendix_ = labelstring_;
86                                 break;
87                         case CT_LABELSTRING_APPENDIX:
88                                 lex.next();
89                                 labelstringappendix_ = lex.getDocString();
90                                 break;
91                         case CT_END:
92                                 getout = true;
93                                 break;
94                 }
95         }
96
97         // Here if have a full counter if getout == true
98         if (!getout)
99                 LYXERR0("No End tag found for counter!");
100         lex.popTable();
101         return getout;
102 }
103
104 void Counter::set(int v)
105 {
106         value_ = v;
107 }
108
109
110 void Counter::addto(int v)
111 {
112         value_ += v;
113 }
114
115
116 int Counter::value() const
117 {
118         return value_;
119 }
120
121
122 void Counter::step()
123 {
124         ++value_;
125 }
126
127
128 void Counter::reset()
129 {
130         value_ = 0;
131 }
132
133
134 docstring const & Counter::master() const
135 {
136         return master_;
137 }
138
139
140 docstring const & Counter::labelString(bool in_appendix) const
141 {
142         return in_appendix ? labelstringappendix_ : labelstring_;
143 }
144
145
146 docstring const & Counter::flatLabelString(bool in_appendix) const
147 {
148         return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
149 }
150
151
152 docstring const & Counter::setFlatLabelStrings(docstring const & fls,
153                                                docstring const & flsa)
154 {
155         flatlabelstring_ = fls;
156         flatlabelstringappendix_ = flsa;
157 }
158
159
160 void Counters::newCounter(docstring const & newc,
161                           docstring const & masterc, 
162                           docstring const & ls,
163                           docstring const & lsa)
164 {
165         if (!masterc.empty() && !hasCounter(masterc)) {
166                 lyxerr << "Master counter does not exist: "
167                        << to_utf8(masterc)
168                        << endl;
169                 return;
170         }
171         counterList_[newc] = Counter(masterc, ls, lsa);
172 }
173
174
175 bool Counters::hasCounter(docstring const & c) const
176 {
177         return counterList_.find(c) != counterList_.end();
178 }
179
180
181 bool Counters::read(Lexer & lex, docstring const & name)
182 {
183         if (hasCounter(name)) {
184                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
185                 return counterList_[name].read(lex);
186         }
187         LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
188         Counter cnt;
189         bool success = cnt.read(lex);
190         if (success)
191                 counterList_[name] = cnt;
192         else
193                 LYXERR0("Error reading counter `" << name << "'!");
194         return success;
195 }
196
197
198 void Counters::set(docstring const & ctr, int const val)
199 {
200         CounterList::iterator const it = counterList_.find(ctr);
201         if (it == counterList_.end()) {
202                 lyxerr << "set: Counter does not exist: "
203                        << to_utf8(ctr) << endl;
204                 return;
205         }
206         it->second.set(val);
207 }
208
209
210 void Counters::addto(docstring const & ctr, int const val)
211 {
212         CounterList::iterator const it = counterList_.find(ctr);
213         if (it == counterList_.end()) {
214                 lyxerr << "addto: Counter does not exist: "
215                        << to_utf8(ctr) << endl;
216                 return;
217         }
218         it->second.addto(val);
219 }
220
221
222 int Counters::value(docstring const & ctr) const
223 {
224         CounterList::const_iterator const cit = counterList_.find(ctr);
225         if (cit == counterList_.end()) {
226                 lyxerr << "value: Counter does not exist: "
227                        << to_utf8(ctr) << endl;
228                 return 0;
229         }
230         return cit->second.value();
231 }
232
233
234 void Counters::step(docstring const & ctr)
235 {
236         CounterList::iterator it = counterList_.find(ctr);
237         if (it == counterList_.end()) {
238                 lyxerr << "step: Counter does not exist: "
239                        << to_utf8(ctr) << endl;
240                 return;
241         }
242
243         it->second.step();
244         it = counterList_.begin();
245         CounterList::iterator const end = counterList_.end();
246         for (; it != end; ++it) {
247                 if (it->second.master() == ctr) {
248                         it->second.reset();
249                 }
250         }
251 }
252
253
254 void Counters::reset()
255 {
256         appendix_ = false;
257         subfloat_ = false;
258         current_float_.erase();
259         CounterList::iterator it = counterList_.begin();
260         CounterList::iterator const end = counterList_.end();
261         std::vector<docstring> callers;
262         for (; it != end; ++it) {
263                 it->second.reset();
264                 // Compute the explicit counter labels without any
265                 // \thexxx strings, in order to avoid recursion.  
266                 // It only needs to be done when the textclass is
267                 // updated, but in practice the extra work is probably
268                 // not noticeable (JMarc)
269                 docstring const & fls = flattenLabelString(it->first, false, callers);
270                 docstring const & flsa = flattenLabelString(it->first, true, callers);
271                 it->second.setFlatLabelStrings(fls, flsa);
272         }
273 }
274
275
276 void Counters::reset(docstring const & match)
277 {
278         LASSERT(!match.empty(), /**/);
279
280         CounterList::iterator it = counterList_.begin();
281         CounterList::iterator end = counterList_.end();
282         for (; it != end; ++it) {
283                 if (it->first.find(match) != string::npos)
284                         it->second.reset();
285         }
286 }
287
288
289 void Counters::copy(Counters & from, Counters & to, docstring const & match)
290 {
291         CounterList::iterator it = counterList_.begin();
292         CounterList::iterator end = counterList_.end();
293         for (; it != end; ++it) {
294                 if (it->first.find(match) != string::npos || match == "") {
295                         to.set(it->first, from.value(it->first));
296                 }
297         }
298 }
299
300
301 namespace {
302
303 char loweralphaCounter(int const n)
304 {
305         if (n < 1 || n > 26)
306                 return '?';
307         return 'a' + n - 1;
308 }
309
310
311 char alphaCounter(int const n)
312 {
313         if (n < 1 || n > 26)
314                 return '?';
315         return 'A' + n - 1;
316 }
317
318
319 char hebrewCounter(int const n)
320 {
321         static const char hebrew[22] = {
322                 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
323                 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
324                 '\xf7', '\xf8', '\xf9', '\xfa'
325         };
326
327         if (n < 1 || n > 22)
328                 return '?';
329         return hebrew[n - 1];
330 }
331
332
333
334 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
335 //and for a list of roman numerals up to and including 3999, see 
336 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
337 //for this info.)
338 docstring const romanCounter(int const n)
339 {
340         static char const * const ones[9] = {
341                 "I",   "II",  "III", "IV", "V",
342                 "VI",  "VII", "VIII", "IX"
343         };
344         
345         static char const * const tens[9] = {
346                 "X", "XX", "XXX", "XL", "L",
347                 "LX", "LXX", "LXXX", "XC"
348         };
349         
350         static char const * const hunds[9] = {
351                 "C", "CC", "CCC", "CD", "D",
352                 "DC", "DCC", "DCCC", "CM"
353         };
354         
355         if (n > 1000 || n < 1) 
356                 return from_ascii("??");
357         
358         int val = n;
359         string roman;
360         switch (n) {
361         //special cases
362         case 900: 
363                 roman = "CM";
364                 break;
365         case 400:
366                 roman = "CD";
367                 break;
368         default:
369                 if (val >= 100) {
370                         int hundreds = val / 100;
371                         roman = hunds[hundreds - 1];
372                         val = val % 100;
373                 }
374                 if (val >= 10) {
375                         switch (val) {
376                         //special case
377                         case 90:
378                                 roman = roman + "XC";
379                                 val = 0; //skip next
380                                 break;
381                         default:
382                                 int tensnum = val / 10;
383                                 roman = roman + tens[tensnum - 1];
384                                 val = val % 10;
385                         } // end switch
386                 } // end tens
387                 if (val > 0)
388                         roman = roman + ones[val -1];
389         }
390         return from_ascii(roman);
391 }
392
393
394 docstring const lowerromanCounter(int const n)
395 {
396         return lowercase(romanCounter(n));
397 }
398
399 } // namespace anon
400
401
402 docstring Counters::labelItem(docstring const & ctr,
403                               docstring const & numbertype) const
404 {
405         CounterList::const_iterator const cit = counterList_.find(ctr);
406         if (cit == counterList_.end()) {
407                 lyxerr << "Counter "
408                        << to_utf8(ctr)
409                        << " does not exist." << endl;
410                 return docstring();
411         }
412
413         int val = cit->second.value();
414
415         if (numbertype == "hebrew")
416                 return docstring(1, hebrewCounter(val));
417
418         if (numbertype == "alph")
419                 return docstring(1, loweralphaCounter(val));
420
421         if (numbertype == "Alph")
422                 return docstring(1, alphaCounter(val));
423
424         if (numbertype == "roman")
425                 return lowerromanCounter(val);
426
427         if (numbertype == "Roman")
428                 return romanCounter(val);
429
430         return convert<docstring>(val);
431 }
432
433
434 docstring Counters::theCounter(docstring const & counter) const
435 {
436         CounterList::const_iterator it = counterList_.find(counter); 
437         if (it == counterList_.end())
438                 return from_ascii("??");
439         return counterLabel(it->second.flatLabelString(appendix()));
440 }
441
442
443 docstring Counters::flattenLabelString(docstring const & counter, bool in_appendix, 
444                                        vector<docstring> & callers) const
445 {
446         docstring label;
447
448         if (find(callers.begin(), callers.end(), counter) != callers.end()) {
449                 // recursion detected
450                 lyxerr << "Warning: Recursion in label for counter `"
451                        << counter << "' detected"
452                        << endl;
453                 return from_ascii("??");
454         }
455                 
456         CounterList::const_iterator it = counterList_.find(counter); 
457         if (it == counterList_.end())
458                 return from_ascii("??");
459         Counter const & c = it->second;
460
461         docstring ls = c.labelString(in_appendix);
462
463         callers.push_back(counter);
464         if (ls.empty()) {
465                 if (!c.master().empty())
466                         ls = flattenLabelString(c.master(), in_appendix, callers) 
467                                 + from_ascii(".");
468                 callers.pop_back();
469                 return ls + from_ascii("\\arabic{") + counter + "}";
470         }
471
472         while (true) {
473                 //lyxerr << "ls=" << to_utf8(ls) << endl;
474                 size_t const i = ls.find(from_ascii("\\the"), 0);
475                 if (i == docstring::npos)
476                         break;
477                 size_t const j = i + 4;
478                 size_t k = j;
479                 while (k < ls.size() && lowercase(ls[k]) >= 'a' 
480                        && lowercase(ls[k]) <= 'z')
481                         ++k;
482                 docstring const & newc = ls.substr(j, k - j);
483                 docstring const & repl = flattenLabelString(newc, in_appendix, callers);
484                 ls.replace(i, k - j + 4, repl);
485         }
486         callers.pop_back();
487
488         return ls;
489 }
490
491
492 docstring Counters::counterLabel(docstring const & format) const
493 {
494         docstring label = format;
495
496         // FIXME: Using regexps would be better, but we compile boost without
497         // wide regexps currently.
498         while (true) {
499                 //lyxerr << "label=" << to_utf8(label) << endl;
500                 size_t const i = label.find(from_ascii("\\the"), 0);
501                 if (i == docstring::npos)
502                         break;
503                 size_t const j = i + 4;
504                 size_t k = j;
505                 while (k < label.size() && lowercase(label[k]) >= 'a' 
506                        && lowercase(label[k]) <= 'z')
507                         ++k;
508                 docstring const & newc = label.substr(j, k - j);
509                 docstring const & repl = theCounter(newc);
510                 label.replace(i, k - j + 4, repl);
511         }
512         while (true) {
513                 //lyxerr << "label=" << to_utf8(label) << endl;
514
515                 size_t const i = label.find('\\', 0);
516                 if (i == docstring::npos)
517                         break;
518                 size_t const j = label.find('{', i + 1);
519                 if (j == docstring::npos)
520                         break;
521                 size_t const k = label.find('}', j + 1);
522                 if (k == docstring::npos)
523                         break;
524                 docstring const numbertype(label, i + 1, j - i - 1);
525                 docstring const counter(label, j + 1, k - j - 1);
526                 docstring const & rep = labelItem(counter, numbertype);
527                 label = docstring(label, 0, i) + rep
528                         + docstring(label, k + 1, docstring::npos);
529         }
530         //lyxerr << "DONE! label=" << to_utf8(label) << endl;
531         return label;
532 }
533
534
535 } // namespace lyx