Base: fix handling of path separators in parameter group names

This commit is contained in:
wmayer
2022-03-29 14:54:03 +02:00
parent 7c24e54704
commit b47029998e
3 changed files with 96 additions and 64 deletions

View File

@@ -312,33 +312,34 @@ void ParameterGrp::insert(const char* FileName)
Base::Reference<ParameterGrp> ParameterGrp::GetGroup(const char* Name)
{
std::string cName = Name;
if (cName.empty())
throw Base::ValueError("Empty group name");
// Remove all leading slashes
std::string::size_type beg = cName.find_first_not_of('/');
if (beg > 0) {
cName.erase(0, beg);
}
// Remove all trailing slashes
std::string::size_type end = cName.find_last_not_of('/');
if (end+1 < cName.size()) {
cName.erase(end+1);
}
std::string::size_type pos = cName.find('/');
// is there a path separator ?
if (pos == std::string::npos) {
return _GetGroup(Name);
}
else if (pos == cName.size()) {
// ending slash! cut it away
cName.erase(pos);
return _GetGroup(cName.c_str());
}
else if (pos == 0) {
// a leading slash is not handled (root unknown)
//throw FCException("ParameterGrp::GetGroup() leading slash not allowed");
// remove leading slash
cName.erase(0,1);
// subsequent call
return GetGroup(cName.c_str());
}
else {
// path, split the first path
std::string cTemp;
// getting the first part
cTemp.assign(cName,0,pos);
cTemp.assign(cName, 0, pos);
// removing the first part from the original
cName.erase(0,pos+1);
cName.erase(0, pos+1);
//subsequent call
return _GetGroup(cTemp.c_str())->GetGroup(cName.c_str());
}

View File

@@ -98,6 +98,7 @@ public:
Py::Object repr();
Py::Object getGroup(const Py::Tuple&);
Py::Object getGroupName(const Py::Tuple&);
Py::Object getGroups(const Py::Tuple&);
Py::Object remGroup(const Py::Tuple&);
Py::Object hasGroup(const Py::Tuple&);
@@ -159,6 +160,7 @@ void ParameterGrpPy::init_type()
behaviors().readyType();
add_varargs_method("GetGroup",&ParameterGrpPy::getGroup,"GetGroup(str)");
add_varargs_method("GetGroupName",&ParameterGrpPy::getGroupName,"GetGroupName()");
add_varargs_method("GetGroups",&ParameterGrpPy::getGroups,"GetGroups()");
add_varargs_method("RemGroup",&ParameterGrpPy::remGroup,"RemGroup(str)");
add_varargs_method("HasGroup",&ParameterGrpPy::hasGroup,"HasGroup(str)");
@@ -260,17 +262,33 @@ Py::Object ParameterGrpPy::getGroup(const Py::Tuple& args)
if (!PyArg_ParseTuple(args.ptr(), "s", &pstr))
throw Py::Exception();
try {
// get the Handle of the wanted group
Base::Reference<ParameterGrp> handle = _cParamGrp->GetGroup(pstr);
if (handle.isValid()) {
// create a python wrapper class
ParameterGrpPy *pcParamGrp = new ParameterGrpPy(handle);
// increment the ref count
return Py::asObject(pcParamGrp);
}
else {
throw Py::RuntimeError("GetGroup failed");
}
}
catch (const Base::Exception& e) {
e.setPyException();
throw Py::Exception();
}
}
Py::Object ParameterGrpPy::getGroupName(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), ""))
throw Py::Exception();
// get the Handle of the wanted group
Base::Reference<ParameterGrp> handle = _cParamGrp->GetGroup(pstr);
if (handle.isValid()) {
// create a python wrapper class
ParameterGrpPy *pcParamGrp = new ParameterGrpPy(handle);
// increment the ref count
return Py::asObject(pcParamGrp);
}
else {
throw Py::RuntimeError("GetGroup failed");
}
std::string name = _cParamGrp->GetGroupName();
return Py::String(name);
}
Py::Object ParameterGrpPy::getGroups(const Py::Tuple& args)