InsetListingsParams.h/cpp: Allow , in parameter string which is common in caption

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18279 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Bo Peng 2007-05-12 15:07:18 +00:00
parent 4d0552dd80
commit 59252c1f8f
2 changed files with 47 additions and 7 deletions

View File

@ -27,6 +27,7 @@ using std::ostream;
using std::string; using std::string;
using std::exception; using std::exception;
using lyx::support::trim; using lyx::support::trim;
using lyx::support::isStrInt;
namespace lyx namespace lyx
{ {
@ -314,7 +315,7 @@ void parValidator::validate(std::string const & par) const
return; return;
} }
case INTEGER: { case INTEGER: {
if (par.empty() && !info->onoff) { if (!isStrInt(par)) {
if (info->hint != "") if (info->hint != "")
throw invalidParam(info->hint); throw invalidParam(info->hint);
else else
@ -435,6 +436,11 @@ void InsetListingsParams::addParam(string const & key, string const & value)
return; return;
// exception may be thown. // exception may be thown.
parValidator(key.c_str()).validate(value); parValidator(key.c_str()).validate(value);
// duplicate parameters!
if (find(keys_.begin(), keys_.end(), key) != keys_.end())
throw invalidParam("Parameter " + key + " has already been defined");
else
keys_.push_back(key);
if (!params_.empty()) if (!params_.empty())
params_ += ','; params_ += ',';
if (value.empty()) if (value.empty())
@ -453,22 +459,35 @@ void InsetListingsParams::addParam(string const & key, string const & value)
} }
void InsetListingsParams::setParams(string const & par) void InsetListingsParams::addParams(string const & par)
{ {
string key; string key;
string value; string value;
bool isValue = false; bool isValue = false;
params_.clear(); int braces = 0;
for (size_t i = 0; i < par.size(); ++i) { for (size_t i = 0; i < par.size(); ++i) {
// end of par // end of par
if (par[i] == '\n' || par[i] == ',') { if (par[i] == '\n') {
addParam(trim(key), trim(value)); addParam(trim(key), trim(value));
key = string(); key = string();
value = string(); value = string();
isValue = false; isValue = false;
} else if (par[i] == '=') continue;
} else if (par[i] == ',' && braces == 0) {
addParam(trim(key), trim(value));
key = string();
value = string();
isValue = false;
continue;
} else if (par[i] == '=' && braces == 0) {
isValue = true; isValue = true;
else if (isValue) continue;
} else if (par[i] == '{' && par[i - 1] == '=')
braces ++;
else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ','))
braces --;
if (isValue)
value += par[i]; value += par[i];
else else
key += par[i]; key += par[i];
@ -478,6 +497,14 @@ void InsetListingsParams::setParams(string const & par)
} }
void InsetListingsParams::setParams(string const & par)
{
params_.clear();
keys_.clear();
addParams(par);
}
string InsetListingsParams::encodedString() const string InsetListingsParams::encodedString() const
{ {
// Encode string! // Encode string!
@ -499,12 +526,19 @@ string InsetListingsParams::separatedParams(bool keepComma) const
// , might be used as regular parameter option so // , might be used as regular parameter option so
// the prcess might be more complicated than what I am doing here // the prcess might be more complicated than what I am doing here
string opt; string opt;
int braces = 0;
for (size_t i = 0; i < params_.size(); ++i) for (size_t i = 0; i < params_.size(); ++i)
if (params_[i] == ',') { if (params_[i] == ',' && braces == 0) {
if (keepComma) if (keepComma)
opt += ",\n"; opt += ",\n";
else else
opt += "\n"; opt += "\n";
} else if (params_[i] == '{' && params_[i - 1] == '=') {
braces ++;
opt += params_[i];
} else if (params_[i] == '}' && (i == params_.size() -1 || params_[i + 1] == ',')) {
braces --;
opt += params_[i];
} else } else
opt += params_[i]; opt += params_[i];
return opt; return opt;

View File

@ -39,6 +39,9 @@ public:
/// add key=value to params_ /// add key=value to params_
void addParam(std::string const & key, std::string const & value); void addParam(std::string const & key, std::string const & value);
/// add a few parameters
void addParams(std::string const & par);
/// set params_ with par, throw an exception if par is valid /// set params_ with par, throw an exception if par is valid
void setParams(std::string const & par); void setParams(std::string const & par);
@ -74,6 +77,9 @@ private:
/// that can be passed to listing packages. /// that can be passed to listing packages.
std::string params_; std::string params_;
/// keys defined in params_
std::vector<std::string> keys_;
/// collapsable status /// collapsable status
InsetCollapsable::CollapseStatus status_; InsetCollapsable::CollapseStatus status_;
}; };