diff --git a/src/Base/Tools.cpp b/src/Base/Tools.cpp index c25c57fe86..29d2f96ed2 100644 --- a/src/Base/Tools.cpp +++ b/src/Base/Tools.cpp @@ -23,124 +23,124 @@ #include "PreCompiled.h" #ifndef _PreComp_ -# include -# include -# include +# include +# include +# include #endif - -# include + +# include #include "PyExport.h" #include "Tools.h" namespace Base { -struct string_comp : public std::binary_function -{ - // s1 and s2 must be numbers represented as string - bool operator()(const std::string& s1, const std::string& s2) - { - if (s1.size() < s2.size()) - return true; - else if (s1.size() > s2.size()) - return false; - else - return s1 < s2; - } - static std::string increment(const std::string& s) - { - std::string n = s; - int addcarry=1; - for (std::string::reverse_iterator it = n.rbegin(); it != n.rend(); ++it) { - if (addcarry == 0) - break; - int d = *it - 48; - d = d + addcarry; - *it = ((d%10) + 48); - addcarry = d / 10; - } - if (addcarry > 0) { - std::string b; - b.resize(1); - b[0] = addcarry + 48; - n = b + n; - } - - return n; - } -}; -} - -std::string Base::Tools::getUniqueName(const std::string& name, const std::vector& names, int d) -{ - // find highest suffix - std::string num_suffix; - for (std::vector::const_iterator it = names.begin(); it != names.end(); ++it) { - if (it->substr(0, name.length()) == name) { // same prefix - std::string suffix(it->substr(name.length())); - if (suffix.size() > 0) { - std::string::size_type pos = suffix.find_first_not_of("0123456789"); - if (pos==std::string::npos) - num_suffix = std::max(num_suffix, suffix, Base::string_comp()); - } - } - } - - std::stringstream str; - str << name; - if (d > 0) { - str.fill('0'); - str.width(d); - } - str << Base::string_comp::increment(num_suffix); - return str.str(); -} - -std::string Base::Tools::addNumber(const std::string& name, unsigned int num, int d) -{ - std::stringstream str; - str << name; - if (d > 0) { - str.fill('0'); - str.width(d); - } - str << num; - return str.str(); -} - -std::string Base::Tools::getIdentifier(const std::string& name) -{ - // check for first character whether it's a digit - std::string CleanName = name; - if (!CleanName.empty() && CleanName[0] >= 48 && CleanName[0] <= 57) - CleanName[0] = '_'; - // strip illegal chars - for (std::string::iterator it = CleanName.begin(); it != CleanName.end(); ++it) { - if (!((*it>=48 && *it<=57) || // number - (*it>=65 && *it<=90) || // uppercase letter - (*it>=97 && *it<=122))) // lowercase letter - *it = '_'; // it's neither number nor letter - } - - return CleanName; -} - -std::wstring Base::Tools::widen(const std::string& str) -{ - std::wostringstream wstm; - const std::ctype& ctfacet = std::use_facet< std::ctype >(wstm.getloc()); - for (size_t i=0; i& ctfacet = std::use_facet< std::ctype >(stm.getloc()); - for (size_t i=0; i +{ + // s1 and s2 must be numbers represented as string + bool operator()(const std::string& s1, const std::string& s2) + { + if (s1.size() < s2.size()) + return true; + else if (s1.size() > s2.size()) + return false; + else + return s1 < s2; + } + static std::string increment(const std::string& s) + { + std::string n = s; + int addcarry=1; + for (std::string::reverse_iterator it = n.rbegin(); it != n.rend(); ++it) { + if (addcarry == 0) + break; + int d = *it - 48; + d = d + addcarry; + *it = ((d%10) + 48); + addcarry = d / 10; + } + if (addcarry > 0) { + std::string b; + b.resize(1); + b[0] = addcarry + 48; + n = b + n; + } + + return n; + } +}; +} + +std::string Base::Tools::getUniqueName(const std::string& name, const std::vector& names, int d) +{ + // find highest suffix + std::string num_suffix; + for (std::vector::const_iterator it = names.begin(); it != names.end(); ++it) { + if (it->substr(0, name.length()) == name) { // same prefix + std::string suffix(it->substr(name.length())); + if (suffix.size() > 0) { + std::string::size_type pos = suffix.find_first_not_of("0123456789"); + if (pos==std::string::npos) + num_suffix = std::max(num_suffix, suffix, Base::string_comp()); + } + } + } + + std::stringstream str; + str << name; + if (d > 0) { + str.fill('0'); + str.width(d); + } + str << Base::string_comp::increment(num_suffix); + return str.str(); +} + +std::string Base::Tools::addNumber(const std::string& name, unsigned int num, int d) +{ + std::stringstream str; + str << name; + if (d > 0) { + str.fill('0'); + str.width(d); + } + str << num; + return str.str(); +} + +std::string Base::Tools::getIdentifier(const std::string& name) +{ + // check for first character whether it's a digit + std::string CleanName = name; + if (!CleanName.empty() && CleanName[0] >= 48 && CleanName[0] <= 57) + CleanName[0] = '_'; + // strip illegal chars + for (std::string::iterator it = CleanName.begin(); it != CleanName.end(); ++it) { + if (!((*it>=48 && *it<=57) || // number + (*it>=65 && *it<=90) || // uppercase letter + (*it>=97 && *it<=122))) // lowercase letter + *it = '_'; // it's neither number nor letter + } + + return CleanName; +} + +std::wstring Base::Tools::widen(const std::string& str) +{ + std::wostringstream wstm; + const std::ctype& ctfacet = std::use_facet< std::ctype >(wstm.getloc()); + for (size_t i=0; i& ctfacet = std::use_facet< std::ctype >(stm.getloc()); + for (size_t i=0; it.start(); -} - -int StopWatch::restart() -{ - return d->t.restart(); -} - -int StopWatch::elapsed() -{ - return d->t.elapsed(); -} - -std::string StopWatch::toString(int ms) const -{ - int total = ms; - int msec = total % 1000; - total = total / 1000; - int secs = total % 60; - total = total / 60; - int mins = total % 60; - int hour = total / 60; - std::stringstream str; - str << "Needed time: "; - if (hour > 0) - str << hour << "h " << mins << "m " << secs << "s"; - else if (mins > 0) - str << mins << "m " << secs << "s"; - else if (secs > 0) - str << secs << "s"; - else - str << msec << "ms"; - return str.str(); -} - + +std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s) +{ + std::string string; + PyObject* unicode = PyUnicode_DecodeUnicodeEscape(s.c_str(), s.size(), "strict"); + if (!unicode) + return string; + + if (PyUnicode_Check(unicode)) { + PyObject* value = PyUnicode_AsUTF8String(unicode); + string = PyString_AsString(value); + Py_DECREF(value); + } + else if (PyString_Check(unicode)) { + string = PyString_AsString(unicode); + } + + Py_DECREF(unicode); + return string; +} + +// ---------------------------------------------------------------------------- + +using namespace Base; + +struct StopWatch::Private +{ + QTime t; +}; + +StopWatch::StopWatch() : d(new Private) +{ +} + +StopWatch::~StopWatch() +{ + delete d; +} + +void StopWatch::start() +{ + d->t.start(); +} + +int StopWatch::restart() +{ + return d->t.restart(); +} + +int StopWatch::elapsed() +{ + return d->t.elapsed(); +} + +std::string StopWatch::toString(int ms) const +{ + int total = ms; + int msec = total % 1000; + total = total / 1000; + int secs = total % 60; + total = total / 60; + int mins = total % 60; + int hour = total / 60; + std::stringstream str; + str << "Needed time: "; + if (hour > 0) + str << hour << "h " << mins << "m " << secs << "s"; + else if (mins > 0) + str << mins << "m " << secs << "s"; + else if (secs > 0) + str << secs << "s"; + else + str << msec << "ms"; + return str.str(); +} + diff --git a/src/Base/Tools.h b/src/Base/Tools.h index 3b563c8690..5025b092c4 100644 --- a/src/Base/Tools.h +++ b/src/Base/Tools.h @@ -156,6 +156,7 @@ struct BaseExport Tools static std::wstring widen(const std::string& str); static std::string narrow(const std::wstring& str); static std::string escapedUnicodeFromUtf8(const char *s); + static std::string escapedUnicodeToUtf8(const std::string& s); /** * @brief toStdString Convert a QString into a UTF-8 encoded std::string.