2003-08-22 09:44:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file counters.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-22 09:44:59 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Martin Vermeer
|
2003-09-12 17:13:22 +00:00
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
2000-07-24 13:53:19 +00:00
|
|
|
|
*
|
2003-08-22 09:44:59 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
2000-07-24 13:53:19 +00:00
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "counters.h"
|
|
|
|
|
#include "debug.h"
|
2002-09-06 14:48:01 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2003-09-12 17:13:22 +00:00
|
|
|
|
#include "support/std_sstream.h"
|
|
|
|
|
#include "support/tostr.h"
|
2003-09-09 17:25:35 +00:00
|
|
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
|
|
|
|
using std::endl;
|
2003-09-05 18:02:24 +00:00
|
|
|
|
using std::ostringstream;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Counter::Counter()
|
|
|
|
|
{
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::set(int v)
|
|
|
|
|
{
|
|
|
|
|
value_ = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::addto(int v)
|
|
|
|
|
{
|
|
|
|
|
value_ += v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Counter::value() const
|
|
|
|
|
{
|
|
|
|
|
return value_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::step()
|
|
|
|
|
{
|
|
|
|
|
++value_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::reset()
|
|
|
|
|
{
|
|
|
|
|
value_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
string Counter::master() const
|
|
|
|
|
{
|
|
|
|
|
return master_;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counter::setMaster(string const & m)
|
|
|
|
|
{
|
|
|
|
|
master_ = m;
|
|
|
|
|
}
|
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
|
|
|
|
void Counters::newCounter(string const & newc)
|
|
|
|
|
{
|
|
|
|
|
// First check if newc already exist
|
2000-09-26 13:54:57 +00:00
|
|
|
|
CounterList::iterator cit = counterList.find(newc);
|
2002-08-06 22:40:59 +00:00
|
|
|
|
// if already exist give warning and return
|
2000-07-17 18:27:53 +00:00
|
|
|
|
if (cit != counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "The new counter already exists." << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
counterList[newc];
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counters::newCounter(string const & newc, string const & masterc)
|
2000-07-17 18:27:53 +00:00
|
|
|
|
{
|
2002-08-06 22:40:59 +00:00
|
|
|
|
// First check if newc already exists
|
2000-09-26 13:54:57 +00:00
|
|
|
|
CounterList::iterator cit = counterList.find(newc);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
// if already existant give warning and return
|
|
|
|
|
if (cit != counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "The new counter already exists." << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
// then check if masterc exists
|
|
|
|
|
CounterList::iterator it = counterList.find(masterc);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
// if not give warning and return
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "The master counter does not exist." << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
counterList[newc].setMaster(masterc);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
void Counters::set(string const & ctr, int val)
|
2000-07-17 18:27:53 +00:00
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.find(ctr);
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "set: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
it->second.set(val);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::addto(string const & ctr, int val)
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.find(ctr);
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "addto: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
it->second.addto(val);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
int Counters::value(string const & ctr) const
|
2000-07-17 18:27:53 +00:00
|
|
|
|
{
|
|
|
|
|
CounterList::const_iterator cit = counterList.find(ctr);
|
|
|
|
|
if (cit == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "value: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
return cit->second.value();
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::step(string const & ctr)
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.find(ctr);
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "step: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
|
|
|
|
|
it->second.step();
|
|
|
|
|
it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
if (it->second.master() == ctr) {
|
|
|
|
|
it->second.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 20:34:20 +00:00
|
|
|
|
|
|
|
|
|
void Counters::reset()
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
it->second.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counters::reset(string const & match)
|
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(!match.empty());
|
2002-08-11 20:34:20 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
CounterList::iterator it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
2002-08-11 20:34:20 +00:00
|
|
|
|
if (it->first.find(match) != string::npos)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
it->second.reset();
|
2002-08-09 12:14:54 +00:00
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counters::copy(Counters & from, Counters & to, string const & match)
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
if (it->first.find(match) != string::npos || match == "") {
|
|
|
|
|
to.set(it->first, from.value(it->first));
|
2002-08-09 12:14:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
char loweralphaCounter(int n)
|
|
|
|
|
{
|
2003-09-15 11:00:00 +00:00
|
|
|
|
if (n < 1 || n > 26)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
return '?';
|
2003-09-12 17:13:22 +00:00
|
|
|
|
return 'a' + n - 1;
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
char alphaCounter(int n)
|
|
|
|
|
{
|
|
|
|
|
if (n < 1 || n > 26)
|
|
|
|
|
return '?';
|
2003-09-12 17:13:22 +00:00
|
|
|
|
return 'A' + n - 1;
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
char hebrewCounter(int n)
|
|
|
|
|
{
|
|
|
|
|
static const char hebrew[22] = {
|
|
|
|
|
'<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>',
|
|
|
|
|
'<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>',
|
|
|
|
|
'<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>'
|
|
|
|
|
};
|
2003-09-12 17:13:22 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
if (n < 1 || n > 22)
|
|
|
|
|
return '?';
|
2003-09-12 17:13:22 +00:00
|
|
|
|
return hebrew[n - 1];
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
|
string const lowerromanCounter(int n)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
{
|
|
|
|
|
static char const * roman[20] = {
|
|
|
|
|
"i", "ii", "iii", "iv", "v",
|
|
|
|
|
"vi", "vii", "viii", "ix", "x",
|
|
|
|
|
"xi", "xii", "xiii", "xiv", "xv",
|
|
|
|
|
"xvi", "xvii", "xviii", "xix", "xx"
|
|
|
|
|
};
|
2003-09-12 17:13:22 +00:00
|
|
|
|
|
|
|
|
|
if (n < 1 || n > 20)
|
|
|
|
|
return "??";
|
|
|
|
|
return roman[n - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string const romanCounter(int n)
|
|
|
|
|
{
|
|
|
|
|
static char const * roman[20] = {
|
|
|
|
|
"I", "II", "III", "IV", "V",
|
|
|
|
|
"VI", "VII", "VIII", "IX", "X",
|
|
|
|
|
"XI", "XII", "XIII", "XIV", "XV",
|
|
|
|
|
"XVI", "XVII", "XVIII", "XIX", "XX"
|
|
|
|
|
};
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
if (n < 1 || n > 20)
|
|
|
|
|
return "??";
|
2003-09-12 17:13:22 +00:00
|
|
|
|
return roman[n - 1];
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
|
string Counters::labelItem(string const & ctr, string const & numbertype)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
{
|
2003-09-12 17:13:22 +00:00
|
|
|
|
if (counterList.find(ctr) == counterList.end()) {
|
|
|
|
|
lyxerr << "Counter " << ctr << " does not exist." << endl;
|
2002-11-27 10:30:28 +00:00
|
|
|
|
return string();
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
2002-09-05 12:58:10 +00:00
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
|
if (numbertype == "hebrew")
|
|
|
|
|
return string(1, hebrewCounter(value(ctr)));
|
|
|
|
|
|
|
|
|
|
if (numbertype == "alph")
|
|
|
|
|
return string(1, loweralphaCounter(value(ctr)));
|
|
|
|
|
|
|
|
|
|
if (numbertype == "Alph")
|
|
|
|
|
return string(1, alphaCounter(value(ctr)));
|
|
|
|
|
|
|
|
|
|
if (numbertype == "roman")
|
|
|
|
|
return lowerromanCounter(value(ctr));
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
|
if (numbertype == "Roman")
|
|
|
|
|
return romanCounter(value(ctr));
|
|
|
|
|
|
|
|
|
|
return tostr(value(ctr));
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
|
string Counters::counterLabel(string const & format)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
{
|
2003-09-12 17:13:22 +00:00
|
|
|
|
string label = format;
|
|
|
|
|
while (true) {
|
2003-09-15 11:00:00 +00:00
|
|
|
|
#warning Using boost::regex would make this code a lot simpler... (Lgb)
|
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
|
size_t const i = label.find('\\', 0);
|
|
|
|
|
if (i == string::npos)
|
|
|
|
|
break;
|
|
|
|
|
size_t const j = label.find('{', i + 1);
|
|
|
|
|
if (j == string::npos)
|
|
|
|
|
break;
|
|
|
|
|
size_t const k = label.find('}', j + 1);
|
|
|
|
|
if (k == string::npos)
|
|
|
|
|
break;
|
|
|
|
|
string const numbertype(label, i + 1, j - i - 1);
|
|
|
|
|
string const counter(label, j + 1, k - j - 1);
|
|
|
|
|
string const rep = labelItem(counter, numbertype);
|
|
|
|
|
label = string(label, 0, i) + rep + string(label, k + 1, string::npos);
|
|
|
|
|
//lyxerr << " : " << " (" << counter << ","
|
|
|
|
|
// << numbertype << ") -> " << label << endl;
|
|
|
|
|
}
|
|
|
|
|
//lyxerr << "counterLabel: " << format << " -> " << label << endl;
|
|
|
|
|
return label;
|
|
|
|
|
}
|
2002-08-09 12:14:54 +00:00
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
|
|
|
|
|
|
string Counters::enumLabel(string const & ctr, string const & langtype)
|
|
|
|
|
{
|
|
|
|
|
ostringstream os;
|
|
|
|
|
|
|
|
|
|
if (langtype == "hebrew") {
|
|
|
|
|
if (ctr == "enumi")
|
|
|
|
|
os << '.' << value("enumi");
|
|
|
|
|
else if (ctr == "enumii")
|
|
|
|
|
os << '(' << hebrewCounter(value("enumii")) << ')';
|
|
|
|
|
else if (ctr == "enumiii")
|
|
|
|
|
os << '.' << lowerromanCounter(value("enumiii"));
|
|
|
|
|
else if (ctr == "enumiv")
|
|
|
|
|
os << '.' << alphaCounter(value("enumiv"));
|
|
|
|
|
} else {
|
|
|
|
|
if (ctr == "enumi")
|
|
|
|
|
os << value("enumi") << '.';
|
|
|
|
|
else if (ctr == "enumii")
|
|
|
|
|
os << '(' << loweralphaCounter(value("enumii")) << ')';
|
|
|
|
|
else if (ctr == "enumiii")
|
|
|
|
|
os << lowerromanCounter(value("enumiii")) << '.';
|
|
|
|
|
else if (ctr == "enumiv")
|
|
|
|
|
os << alphaCounter(value("enumiv")) << '.';
|
2002-08-09 12:14:54 +00:00
|
|
|
|
}
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2003-09-15 11:00:00 +00:00
|
|
|
|
return os.str();
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|