Base: Add isNullOrEmpty string helper
This adds isNullOrEmpty string helper that cheks if string is... well null or empty. It is done to improve readability of the code and better express intent.
This commit is contained in:
@@ -457,19 +457,19 @@ void Application::renameDocument(const char *OldName, const char *NewName)
|
||||
Document* Application::newDocument(const char * Name, const char * UserName, DocumentCreateFlags CreateFlags)
|
||||
{
|
||||
auto getNameAndLabel = [this](const char * Name, const char * UserName) -> std::tuple<std::string, std::string> {
|
||||
bool defaultName = (!Name || Name[0] == '\0');
|
||||
bool isDefaultName = Base::Tools::isNullOrEmpty(Name);
|
||||
|
||||
// get a valid name anyway!
|
||||
if (defaultName) {
|
||||
if (isDefaultName) {
|
||||
Name = "Unnamed";
|
||||
}
|
||||
|
||||
std::string userName;
|
||||
if (UserName && UserName[0] != '\0') {
|
||||
if (!Base::Tools::isNullOrEmpty(UserName)) {
|
||||
userName = UserName;
|
||||
}
|
||||
else {
|
||||
userName = defaultName ? QObject::tr("Unnamed").toStdString() : Name;
|
||||
userName = isDefaultName ? QObject::tr("Unnamed").toStdString() : Name;
|
||||
|
||||
std::vector<std::string> names;
|
||||
names.reserve(DocMap.size());
|
||||
@@ -649,8 +649,8 @@ int Application::addPendingDocument(const char *FileName, const char *objName, b
|
||||
return 0;
|
||||
if(allowPartial && _allowPartial)
|
||||
return -1;
|
||||
assert(FileName && FileName[0]);
|
||||
assert(objName && objName[0]);
|
||||
assert(!Base::Tools::isNullOrEmpty(FileName));
|
||||
assert(!Base::Tools::isNullOrEmpty(objName));
|
||||
if(!_docReloadAttempts[FileName].emplace(objName).second)
|
||||
return -1;
|
||||
auto ret = _pendingDocMap.emplace(FileName,std::vector<std::string>());
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <App/Document.h>
|
||||
#include <Base/Exception.h>
|
||||
#include <Base/Placement.h>
|
||||
#include <Base/Tools.h>
|
||||
|
||||
#include "Datums.h"
|
||||
|
||||
@@ -359,7 +360,7 @@ bool LocalCoordinateSystem::LCSExtension::extensionGetSubObject(DocumentObject*&
|
||||
bool,
|
||||
int depth) const
|
||||
{
|
||||
if (!subname || subname[0] == '\0') {
|
||||
if (Base::Tools::isNullOrEmpty(subname)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1457,7 +1457,7 @@ std::vector<App::DocumentObject*> Document::readObjects(Base::XMLReader& reader)
|
||||
for (int j = 0; j < dcount; ++j) {
|
||||
reader.readElement(FC_ELEMENT_OBJECT_DEP);
|
||||
const char* name = reader.getAttribute(FC_ATTR_DEP_OBJ_NAME);
|
||||
if (name && name[0]) {
|
||||
if (!Base::Tools::isNullOrEmpty(name)) {
|
||||
info.deps.insert(name);
|
||||
}
|
||||
}
|
||||
@@ -3570,7 +3570,7 @@ DocumentObject* Document::addObject(const char* sType,
|
||||
}
|
||||
|
||||
// get Unique name
|
||||
const bool hasName = pObjectName && pObjectName[0] != '\0';
|
||||
const bool hasName = !Base::Tools::isNullOrEmpty(pObjectName);
|
||||
const string ObjectName = getUniqueObjectName(hasName ? pObjectName : type.getName());
|
||||
|
||||
d->activeObject = pcObject;
|
||||
@@ -3602,11 +3602,11 @@ DocumentObject* Document::addObject(const char* sType,
|
||||
|
||||
pcObject->setStatus(ObjectStatus::PartialObject, isPartial);
|
||||
|
||||
if (!viewType || viewType[0] == '\0') {
|
||||
if (Base::Tools::isNullOrEmpty(viewType)) {
|
||||
viewType = pcObject->getViewProviderNameOverride();
|
||||
}
|
||||
|
||||
if (viewType && viewType[0] != '\0') {
|
||||
if (!Base::Tools::isNullOrEmpty(viewType)) {
|
||||
pcObject->_pcViewProviderName = viewType;
|
||||
}
|
||||
|
||||
@@ -3746,7 +3746,7 @@ void Document::addObject(DocumentObject* pcObject, const char* pObjectName)
|
||||
|
||||
// get unique name
|
||||
string ObjectName;
|
||||
if (pObjectName && pObjectName[0] != '\0') {
|
||||
if (!Base::Tools::isNullOrEmpty(pObjectName)) {
|
||||
ObjectName = getUniqueObjectName(pObjectName);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <Base/Tools.h>
|
||||
|
||||
namespace App
|
||||
{
|
||||
@@ -65,7 +66,7 @@ constexpr std::array<TLicenseArr, countOfLicenses> licenseItems {{
|
||||
|
||||
int constexpr findLicense(const char* identifier)
|
||||
{
|
||||
if (!identifier || identifier[0] == '\0') {
|
||||
if (Base::Tools::isNullOrEmpty(identifier)) {
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i < countOfLicenses; i++) {
|
||||
|
||||
@@ -354,11 +354,11 @@ App::DocumentObjectExecReturn* LinkBaseExtension::extensionExecute()
|
||||
auto xlink = Base::freecad_dynamic_cast<PropertyXLink>(getLinkedObjectProperty());
|
||||
if (xlink) {
|
||||
const char* objname = xlink->getObjectName();
|
||||
if (objname && objname[0]) {
|
||||
if (!Base::Tools::isNullOrEmpty(objname)) {
|
||||
ss << "\nObject: " << objname;
|
||||
}
|
||||
const char* filename = xlink->getFilePath();
|
||||
if (filename && filename[0]) {
|
||||
if (!Base::Tools::isNullOrEmpty(filename)) {
|
||||
ss << "\nFile: " << filename;
|
||||
}
|
||||
}
|
||||
@@ -1756,7 +1756,7 @@ void LinkBaseExtension::parseSubName() const
|
||||
for (std::size_t i = 1; i < subs.size(); ++i) {
|
||||
auto& sub = subs[i];
|
||||
element = Data::findElementName(sub.c_str());
|
||||
if (element && element[0] && boost::starts_with(sub, mySubName)) {
|
||||
if (!Base::Tools::isNullOrEmpty(element) && boost::starts_with(sub, mySubName)) {
|
||||
mySubElements.emplace_back(element);
|
||||
}
|
||||
}
|
||||
@@ -2265,7 +2265,7 @@ void LinkBaseExtension::onExtendedDocumentRestored()
|
||||
std::set<std::string> subset(mySubElements.begin(), mySubElements.end());
|
||||
auto sub = xlink->getSubValues().front();
|
||||
auto element = Data::findElementName(sub.c_str());
|
||||
if (element && element[0]) {
|
||||
if (!Base::Tools::isNullOrEmpty(element)) {
|
||||
subset.insert(element);
|
||||
sub.resize(element - sub.c_str());
|
||||
}
|
||||
@@ -2374,7 +2374,7 @@ void LinkBaseExtension::setLink(int index,
|
||||
}
|
||||
|
||||
int idx = -1;
|
||||
if (getLinkModeValue() >= LinkModeAutoLink || (subname && subname[0])
|
||||
if (getLinkModeValue() >= LinkModeAutoLink || !Base::Tools::isNullOrEmpty(subname)
|
||||
|| !subElements.empty() || obj->getDocument() != parent->getDocument()
|
||||
|| (getElementListProperty()->find(obj->getNameInDocument(), &idx)
|
||||
&& idx != index)) {
|
||||
@@ -2433,7 +2433,7 @@ void LinkBaseExtension::setLink(int index,
|
||||
}
|
||||
|
||||
if (!xlink) {
|
||||
if (!subElements.empty() || (subname && subname[0])) {
|
||||
if (!subElements.empty() || !Base::Tools::isNullOrEmpty(subname)) {
|
||||
LINK_THROW(Base::RuntimeError, "SubName/SubElement link requires PropertyXLink");
|
||||
}
|
||||
linkProp->setValue(obj);
|
||||
@@ -2448,7 +2448,7 @@ void LinkBaseExtension::setLink(int index,
|
||||
subs.back() += s;
|
||||
}
|
||||
}
|
||||
else if (subname && subname[0]) {
|
||||
else if (!Base::Tools::isNullOrEmpty(subname)) {
|
||||
subs.emplace_back(subname);
|
||||
}
|
||||
xlink->setValue(obj, std::move(subs));
|
||||
|
||||
@@ -23,7 +23,9 @@
|
||||
#include "PreCompiled.h"
|
||||
|
||||
#include "Metadata.h"
|
||||
|
||||
#include <Base/FileInfo.h>
|
||||
#include <Base/Tools.h>
|
||||
|
||||
// inclusion of the generated files (generated out of MetadataPy.xml)
|
||||
#include "MetadataPy.h"
|
||||
@@ -166,7 +168,7 @@ void MetadataPy::setVersion(Py::String args)
|
||||
if (!PyArg_Parse(args.ptr(), "z", &name)) {
|
||||
throw Py::Exception();
|
||||
}
|
||||
if (name && name[0] != '\0') {
|
||||
if (!Base::Tools::isNullOrEmpty(name)) {
|
||||
getMetadataPtr()->setVersion(App::Meta::Version(std::string(name)));
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -71,7 +71,7 @@ bool Property::hasName() const
|
||||
|
||||
bool Property::isValidName(const char* name)
|
||||
{
|
||||
return name && name[0] != '\0';
|
||||
return !Base::Tools::isNullOrEmpty(name);
|
||||
}
|
||||
|
||||
std::string Property::getFullName() const
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <Base/Stream.h>
|
||||
#include <Base/Writer.h>
|
||||
#include <Base/Uuid.h>
|
||||
#include <Base/Tools.h>
|
||||
|
||||
#include "PropertyFile.h"
|
||||
#include "Document.h"
|
||||
@@ -110,7 +111,7 @@ std::string PropertyFileIncluded::getOriginalFileName() const
|
||||
|
||||
void PropertyFileIncluded::setValue(const char* sFile, const char* sName)
|
||||
{
|
||||
if (sFile && sFile[0] != '\0') {
|
||||
if (!Base::Tools::isNullOrEmpty(sFile)) {
|
||||
if (_cValue == sFile) {
|
||||
throw Base::FileSystemError("Not possible to set the same file!");
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <Base/Exception.h>
|
||||
#include <Base/Reader.h>
|
||||
#include <Base/Writer.h>
|
||||
#include <Base/Tools.h>
|
||||
|
||||
#include "PropertyLinks.h"
|
||||
#include "Application.h"
|
||||
@@ -3800,7 +3801,7 @@ void PropertyXLink::hasSetValue()
|
||||
void PropertyXLink::setSubName(const char* subname)
|
||||
{
|
||||
std::vector<std::string> subs;
|
||||
if (subname && subname[0]) {
|
||||
if (!Base::Tools::isNullOrEmpty(subname)) {
|
||||
subs.emplace_back(subname);
|
||||
}
|
||||
aboutToSetValue();
|
||||
@@ -3830,7 +3831,7 @@ void PropertyXLink::setValue(App::DocumentObject* lValue)
|
||||
void PropertyXLink::setValue(App::DocumentObject* lValue, const char* subname)
|
||||
{
|
||||
std::vector<std::string> subs;
|
||||
if (subname && subname[0]) {
|
||||
if (!Base::Tools::isNullOrEmpty(subname)) {
|
||||
subs.emplace_back(subname);
|
||||
}
|
||||
setValue(lValue, std::move(subs));
|
||||
@@ -4147,7 +4148,7 @@ void PropertyXLink::Save(Base::Writer& writer) const
|
||||
else {
|
||||
auto pDoc = owner->getDocument();
|
||||
const char* docPath = pDoc->getFileName();
|
||||
if (docPath && docPath[0]) {
|
||||
if (!Base::Tools::isNullOrEmpty(docPath)) {
|
||||
if (!filePath.empty()) {
|
||||
_path = DocInfo::getDocPath(filePath.c_str(), pDoc, false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user