X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fcounters.C;h=78dceb03a7ebe88f28dcf9803bee31b927b4f3be;hb=3ef684e752bb5afdbfdea51d4c3df4afe1461916;hp=c6d5745ad1a62bab49ea9d1211d64fdd970374f3;hpb=1abac590e8cb0d99634087f1560047b8fbfc3941;p=lyx.git diff --git a/src/counters.C b/src/counters.C index c6d5745ad1..78dceb03a7 100644 --- a/src/counters.C +++ b/src/counters.C @@ -1,26 +1,33 @@ -/* This file is part of - * ====================================================== +/** + * \file counters.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. * - * LyX, The Document Processor + * \author Lars Gullik Bjønnes + * \author Martin Vermeer + * \author André Pönitz * - * Copyright 1995 Matthias Ettrich - * Copyright 1995-2001 The LyX Team. - * - * - * ====================================================== */ - -#ifdef __GNUG__ -#pragma implementation -#endif + * Full author contact details are available in file CREDITS. + */ #include #include "counters.h" #include "debug.h" + #include "support/lstrings.h" +#include "support/convert.h" + +#include + +#include + + +namespace lyx { using std::endl; -using std::vector; +using std::ostringstream; +using std::string; Counter::Counter() @@ -58,139 +65,108 @@ void Counter::reset() value_ = 0; } -string Counter::master() const -{ - return master_; -} -void Counter::setMaster(string const & m) +docstring Counter::master() const { - master_ = m; + return master_; } -Counters::Counters() +void Counter::setMaster(docstring const & m) { - // Ehh, should this take a textclass arg? - - // Sectioning counters: - newCounter("part"); - newCounter("chapter"); - newCounter("section", "chapter"); - newCounter("subsection", "section"); - newCounter("subsubsection", "subsection"); - newCounter("paragraph", "subsubsection"); - newCounter("subparagraph", "paragraph"); - - sects.push_back("chapter"); - sects.push_back("section"); - sects.push_back("subsection"); - sects.push_back("subsubsection"); - sects.push_back("paragraph"); - sects.push_back("subparagraph"); - - // Enumeration counters: - newCounter("enumi"); - newCounter("enumii", "enumi"); - newCounter("enumiii", "enumii"); - newCounter("enumiv", "enumiii"); - - enums.push_back("enumi"); - enums.push_back("enumii"); - enums.push_back("enumiii"); - enums.push_back("enumiv"); - - // Biblio: - newCounter("bibitem"); - - // Float counters: - newCounter("Figure"); - newCounter("Table"); + master_ = m; } -void Counters::newCounter(string const & newc) +void Counters::newCounter(docstring const & newc) { // First check if newc already exist - CounterList::iterator cit = counterList.find(newc); + CounterList::iterator const cit = counterList.find(newc); // if already exist give warning and return if (cit != counterList.end()) { - lyxerr << "The new counter already exists." << endl; + lyxerr << "New counter already exists: " + << to_utf8(newc) + << endl; return; } counterList[newc]; - cit = counterList.find(newc); - cit->second.setMaster(""); } -void Counters::newCounter(string const & newc, string const & masterc) +void Counters::newCounter(docstring const & newc, + docstring const & masterc) { // First check if newc already exists - CounterList::iterator cit = counterList.find(newc); + CounterList::iterator const cit = counterList.find(newc); // if already existant give warning and return if (cit != counterList.end()) { - lyxerr << "The new counter already exists." << endl; + lyxerr << "New counter already exists: " + << to_utf8(newc) + << endl; return; } // then check if masterc exists - CounterList::iterator it = counterList.find(masterc); + CounterList::iterator const it = counterList.find(masterc); // if not give warning and return if (it == counterList.end()) { - lyxerr << "The master counter does not exist." << endl; + lyxerr << "Master counter does not exist: " + << to_utf8(masterc) + << endl; return; } - counterList[newc]; - cit = counterList.find(newc); - cit->second.setMaster(masterc); + counterList[newc].setMaster(masterc); } -void Counters::set(string const & ctr, int val) +void Counters::set(docstring const & ctr, int const val) { - CounterList::iterator it = counterList.find(ctr); + CounterList::iterator const it = counterList.find(ctr); if (it == counterList.end()) { - lyxerr << "set: Counter does not exist: " << ctr << endl; + lyxerr << "set: Counter does not exist: " + << to_utf8(ctr) << endl; return; } it->second.set(val); } -void Counters::addto(string const & ctr, int val) +void Counters::addto(docstring const & ctr, int const val) { - CounterList::iterator it = counterList.find(ctr); + CounterList::iterator const it = counterList.find(ctr); if (it == counterList.end()) { - lyxerr << "addto: Counter does not exist: " << ctr << endl; + lyxerr << "addto: Counter does not exist: " + << to_utf8(ctr) << endl; return; } it->second.addto(val); } -int Counters::value(string const & ctr) const +int Counters::value(docstring const & ctr) const { - CounterList::const_iterator cit = counterList.find(ctr); + CounterList::const_iterator const cit = counterList.find(ctr); if (cit == counterList.end()) { - lyxerr << "value: Counter does not exist: " << ctr << endl; + lyxerr << "value: Counter does not exist: " + << to_utf8(ctr) << endl; return 0; } return cit->second.value(); } -void Counters::step(string const & ctr) +void Counters::step(docstring const & ctr) { CounterList::iterator it = counterList.find(ctr); if (it == counterList.end()) { - lyxerr << "step: Counter does not exist: " << ctr << endl; + lyxerr << "step: Counter does not exist: " + << to_utf8(ctr) << endl; return; } it->second.step(); it = counterList.begin(); - CounterList::iterator end = counterList.end(); + CounterList::iterator const end = counterList.end(); for (; it != end; ++it) { if (it->second.master() == ctr) { it->second.reset(); @@ -198,17 +174,31 @@ void Counters::step(string const & ctr) } } -void Counters::reset(string const & match) + +void Counters::reset() { + CounterList::iterator it = counterList.begin(); + CounterList::iterator const end = counterList.end(); + for (; it != end; ++it) { + it->second.reset(); + } +} + + +void Counters::reset(docstring const & match) +{ + BOOST_ASSERT(!match.empty()); + CounterList::iterator it = counterList.begin(); CounterList::iterator end = counterList.end(); for (; it != end; ++it) { - if (it->first.find(match) != string::npos || match == "") + if (it->first.find(match) != string::npos) it->second.reset(); } } -void Counters::copy(Counters & from, Counters & to, string const & match) + +void Counters::copy(Counters & from, Counters & to, docstring const & match) { CounterList::iterator it = counterList.begin(); CounterList::iterator end = counterList.end(); @@ -222,139 +212,124 @@ void Counters::copy(Counters & from, Counters & to, string const & match) namespace { -inline -char loweralphaCounter(int n) +char loweralphaCounter(int const n) { if (n < 1 || n > 26) return '?'; - else - return 'a' + n - 1; + return 'a' + n - 1; } -inline -char alphaCounter(int n) + +char alphaCounter(int const n) { if (n < 1 || n > 26) return '?'; - else - return 'A' + n - 1; + return 'A' + n - 1; } -inline -char hebrewCounter(int n) + +char hebrewCounter(int const n) { static const char hebrew[22] = { 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ë', 'ì', 'î', 'ð', 'ñ', 'ò', 'ô', 'ö', '÷', 'ø', 'ù', 'ú' }; + if (n < 1 || n > 22) return '?'; - else - return hebrew[n-1]; + return hebrew[n - 1]; } -inline -string const romanCounter(int n) + +docstring const lowerromanCounter(int const n) { - static char const * roman[20] = { + static char const * const roman[20] = { "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii", "xiii", "xiv", "xv", "xvi", "xvii", "xviii", "xix", "xx" }; + if (n < 1 || n > 20) - return "??"; - else - return roman[n-1]; + return from_ascii("??"); + return from_ascii(roman[n - 1]); +} + + +docstring const romanCounter(int const n) +{ + static char const * const roman[20] = { + "I", "II", "III", "IV", "V", + "VI", "VII", "VIII", "IX", "X", + "XI", "XII", "XIII", "XIV", "XV", + "XVI", "XVII", "XVIII", "XIX", "XX" + }; + + if (n < 1 || n > 20) + return from_ascii("??"); + return from_ascii(roman[n - 1]); } } // namespace anon -string Counters::labelItem(string const & ctr, - string const & numbertype, - string const & langtype, - bool first) + +docstring Counters::labelItem(docstring const & ctr, + docstring const & numbertype) { - ostringstream s, o; - CounterList::iterator it = counterList.find(ctr); - if (it == counterList.end()) { - lyxerr << "Counter does not exist." << endl; - return ""; + if (counterList.find(ctr) == counterList.end()) { + lyxerr << "Counter " + << to_utf8(ctr) + << " does not exist." << endl; + return docstring(); } - string mstr = it->second.master(); - if (!first) { - s << "." << value(ctr); - } else { - if (numbertype == "sectioning" || numbertype == "appendix") { - if (numbertype == "appendix") { - if (langtype == "hebrew") { - o << hebrewCounter(value(ctr)); - } else { - o << alphaCounter(value(ctr)); - } - } else o << value(ctr); - } - s << o.str(); - } - return s.str(); + + if (numbertype == "hebrew") + return docstring(1, hebrewCounter(value(ctr))); + + if (numbertype == "alph") + return docstring(1, loweralphaCounter(value(ctr))); + + if (numbertype == "Alph") + return docstring(1, alphaCounter(value(ctr))); + + if (numbertype == "roman") + return lowerromanCounter(value(ctr)); + + if (numbertype == "Roman") + return romanCounter(value(ctr)); + + return convert(value(ctr)); } -string Counters::numberLabel(string const & ctr, - string const & numbertype, - string const & langtype, - int head) + +docstring Counters::counterLabel(docstring const & format) { - ostringstream s, o; - if (numbertype == "sectioning" || numbertype == "appendix") { - if (ctr == "chapter" && head == 0) { - s << labelItem("chapter", numbertype, langtype, true); - } else if (ctr == "section" && head <= 1) { - s << numberLabel("chapter", numbertype, langtype, head) - << labelItem("section", numbertype, langtype, head == 1); - } else if (ctr == "subsection" && head <= 2) { - s << numberLabel("section", numbertype, langtype, head) - << labelItem("subsection", numbertype, langtype, head == 2); - } else if (ctr == "subsubsection" && head <= 3) { - s << numberLabel("subsection", numbertype, langtype, head) - << labelItem("subsubsection", numbertype, langtype, head == 3); - } else if (ctr == "paragraph" && head <= 4) { - s << numberLabel("subsubsection", numbertype, langtype, head) - << labelItem("paragraph", numbertype, langtype, head == 4); - } else if (ctr == "subparagraph" && head <= 5) { - s << numberLabel("paragraph", numbertype, langtype, head) - << labelItem("subparagraph", numbertype, langtype, head == 5); - } else if (ctr == "Figure" || ctr == "Table") { - // figure, table, ... - lyxerr << "Counter:" << ctr << endl; - s << numberLabel("chapter", numbertype, langtype, head) - << labelItem(ctr, numbertype, langtype, head == 1); - } + docstring label = format; + while (true) { +#ifdef WITH_WARNINGS +#warning Using boost::regex or boost::spirit would make this code a lot simpler... (Lgb) +#endif - } else if (numbertype == "enumeration") { - ostringstream ei, eii, eiii, eiv; - //string ei, eiii, eiv; - //char eii; - if (langtype == "hebrew") { - ei << '.' << value("enumi"); - eii << '(' << hebrewCounter(value("enumii")) << ')'; - eiii << '.' << romanCounter(value("enumiii")); - eiv << '.' << alphaCounter(value("enumiv")); - } else { - ei << value("enumi") << '.'; - eii << '(' << loweralphaCounter(value("enumii")) << ')'; - eiii << romanCounter(value("enumiii")) << '.'; - eiv << alphaCounter(value("enumiv")) << '.'; - } - if (ctr == "enumii") { - s << eii.str(); - } else if (ctr == "enumi") { - s << ei.str(); - } else if (ctr == "enumiii") { - s << eiii.str(); - } else if (ctr == "enumiv") { - s << eiv.str(); - } + size_t const i = label.find('\\', 0); + if (i == docstring::npos) + break; + size_t const j = label.find('{', i + 1); + if (j == docstring::npos) + break; + size_t const k = label.find('}', j + 1); + if (k == string::npos) + break; + docstring const numbertype(label, i + 1, j - i - 1); + docstring const counter(label, j + 1, k - j - 1); + docstring const rep = labelItem(counter, numbertype); + label = docstring(label, 0, i) + rep + docstring(label, k + 1, string::npos); + //lyxerr << " : " << " (" << counter << "," + // << numbertype << ") -> " << label << endl; } - return s.str(); + //lyxerr << "counterLabel: " << format << " -> " << label << endl; + return label; } + + +} // namespace lyx