From: Maxim Mamontov Date: Mon, 8 Aug 2011 18:28:47 +0000 (+0300) Subject: Suffix construction and prefix compare methods added to the OID X-Git-Tag: 2.408-alpha~45 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/559d32bc2789dc69a7c19598a31f485c6caaff11 Suffix construction and prefix compare methods added to the OID --- diff --git a/projects/stargazer/plugins/other/smux/types.cpp b/projects/stargazer/plugins/other/smux/types.cpp index f8eb5209..14117ce5 100644 --- a/projects/stargazer/plugins/other/smux/types.cpp +++ b/projects/stargazer/plugins/other/smux/types.cpp @@ -1,12 +1,12 @@ #include #include +#include #include #include "types.h" -bool StringToArcs(const char * str, size_t length, std::vector & arcs) +bool ParseArcs(const char * str, size_t length, unsigned * a, size_t * pos) { -unsigned a[1024]; if (length == 0) return false; const char * left = str; @@ -24,9 +24,31 @@ while ((left - str) < length) return false; left = pos + 1; } +*pos = arcPos; +return true; +} -std::vector newArcs(a, a + arcPos); -arcs.swap(newArcs); +bool StringToArcs(const char * str, size_t length, std::vector & arcs) +{ +unsigned a[1024]; +size_t pos = 0; + +if (!ParseArcs(str, length, a, &pos)) + return false; + +arcs.assign(a, a + pos); +return true; +} + +bool AppendToArcs(const char * str, size_t length, std::vector & arcs) +{ +unsigned a[1024]; +size_t pos = 0; + +if (!ParseArcs(str, length, a, &pos)) + return false; + +std::copy(&a[0], &a[pos], std::back_inserter(arcs)); return true; } @@ -74,6 +96,78 @@ OID::~OID() { } +bool OID::addSuffix(const char * suffix, size_t length) +{ +if (!AppendToArcs(suffix, length, arcs)) + return false; +return true; +} + +bool OID::addSuffix(const std::string & suffix) +{ +if (!AppendToArcs(suffix.c_str(), suffix.length(), arcs)) + return false; +return true; +} + +bool OID::addSuffix(const unsigned * suffix, size_t length) +{ +std::copy(suffix, suffix + length, std::back_inserter(arcs)); +return true; +} + +bool OID::addSuffix(const std::vector & suffix) +{ +std::copy(suffix.begin(), suffix.end(), std::back_inserter(arcs)); +return true; +} + +bool OID::addSuffix(unsigned a, unsigned b) +{ +arcs.push_back(a); +arcs.push_back(b); +return true; +} + +OID OID::copyWithSuffix(const char * suffix, size_t length) const +{ +OID oid(*this); +if (!oid.addSuffix(suffix, length)) + throw std::runtime_error("Invalid suffix"); +return oid; +} + +OID OID::copyWithSuffix(const std::string & suffix) const +{ +OID oid(*this); +if (!oid.addSuffix(suffix)) + throw std::runtime_error("Invalid suffix"); +return oid; +} + +OID OID::copyWithSuffix(const unsigned * suffix, size_t length) const +{ +OID oid(*this); +if (!oid.addSuffix(suffix, length)) + throw std::runtime_error("Invalid suffix"); +return oid; +} + +OID OID::copyWithSuffix(const std::vector & suffix) const +{ +OID oid(*this); +if (!oid.addSuffix(suffix)) + throw std::runtime_error("Invalid suffix"); +return oid; +} + +OID OID::copyWithSuffix(unsigned a, unsigned b) const +{ +OID oid(*this); +oid.addSuffix(a, b); +return oid; +} + std::string OID::ToString() const { std::stringstream stream; @@ -105,12 +199,36 @@ return true; bool OID::operator<(const OID & rvalue) const { -for (size_t i = 0; i < std::min(arcs.size(), rvalue.arcs.size()); ++i) - if (arcs[i] > rvalue.arcs[i]) - return false; -if (rvalue.arcs.size() < arcs.size()) +size_t i = 0; +size_t min = std::min(arcs.size(), rvalue.arcs.size()); +while (i < min && + arcs[i] == rvalue.arcs[i]) + ++i; +if (i == min) + { + if (rvalue.arcs.size() > arcs.size()) + return true; return false; -return true; + } + +if (arcs[i] < rvalue.arcs[i]) + return true; + +return false; +} + +bool OID::PrefixLess(const OID & rvalue) const +{ +size_t i = 0; +size_t min = std::min(arcs.size(), rvalue.arcs.size()); +while (i < min && + arcs[i] == rvalue.arcs[i]) + ++i; +if (i == min) + return false; +if (arcs[i] < rvalue.arcs[i]) + return true; +return false; } std::ostream & operator<<(std::ostream & stream, const OID & oid) diff --git a/projects/stargazer/plugins/other/smux/types.h b/projects/stargazer/plugins/other/smux/types.h index 974d56bf..a121bf7a 100644 --- a/projects/stargazer/plugins/other/smux/types.h +++ b/projects/stargazer/plugins/other/smux/types.h @@ -17,6 +17,18 @@ class OID { OID(const OID & rvalue); ~OID(); + bool addSuffix(const char * suffix, size_t length); + bool addSuffix(const std::string & suffix); + bool addSuffix(const unsigned * suffix, size_t length); + bool addSuffix(const std::vector & suffix); + bool addSuffix(unsigned a, unsigned b); + + OID copyWithSuffix(const char * suffix, size_t length) const; + OID copyWithSuffix(const std::string & suffix) const; + OID copyWithSuffix(const unsigned * suffix, size_t length) const; + OID copyWithSuffix(const std::vector & suffix) const; + OID copyWithSuffix(unsigned a, unsigned b) const; + std::string ToString() const; const std::vector & ToVector() const { return arcs; } void ToOID(OBJECT_IDENTIFIER_t * oid) const; @@ -28,10 +40,18 @@ class OID { bool operator>(const OID & rvalue) const { return !operator==(rvalue) && !operator<(rvalue); } + bool PrefixLess(const OID & rvalue) const; + friend std::ostream & operator<<(std::ostream & stream, const OID & oid); private: std::vector arcs; }; +inline +bool PrefixLess(const OID & a, const OID & b) +{ +return a.PrefixLess(b); +} + #endif