diff --git a/src/Makefile b/src/Makefile index 695302a..9539da2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -27,6 +27,7 @@ INC += nturi.h INC += ntndarrayAttribute.h LIBSRCS += ntutils.cpp +LIBSRCS += ntid.cpp LIBSRCS += ntfield.cpp LIBSRCS += ntscalar.cpp LIBSRCS += ntscalarArray.cpp diff --git a/src/nt/ntid.cpp b/src/nt/ntid.cpp new file mode 100644 index 0000000..9be2c10 --- /dev/null +++ b/src/nt/ntid.cpp @@ -0,0 +1,186 @@ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS JavaIOC is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ + +#include +#include + +namespace epics { + +namespace nt { + + const static std::string BAD_NAME = "?"; + + NTID::NTID(const std::string & id) + : fullName(id), + qualifiedName(BAD_NAME), + namespaceStr(BAD_NAME), + name(BAD_NAME), + version(BAD_NAME), + nsSepIndex(std::string::npos), + versionSepIndex(std::string::npos), + nsQualified(false), + hasVersion(false), + endMajorIndex(0), + majorVersionStr(BAD_NAME), + majorVersionParsed(false), + hasMajor(false), + majorVersion(0), + + endMinorIndex(0), + minorVersionStr(BAD_NAME), + minorVersionParsed(false), + hasMinor(false), + minorVersion(0) + { + nsSepIndex = id.find('/'); + nsQualified = nsSepIndex != std::string::npos; + size_t startIndex = nsQualified ? nsSepIndex+1 : 0; + versionSepIndex = id.find(':', startIndex); + hasVersion = versionSepIndex != std::string::npos; + } + + std::string NTID::getFullName() { return fullName; } + + std::string NTID::getQualifiedName() + { + if (qualifiedName == BAD_NAME) + { + qualifiedName = hasVersion ? + fullName.substr(0, versionSepIndex) : fullName; + } + return qualifiedName; + } + + + std::string NTID::getNamespace() + { + if (namespaceStr == BAD_NAME) + { + namespaceStr = nsQualified ? + fullName.substr(0, nsSepIndex) : ""; + } + return namespaceStr; + } + + std::string NTID::getName() + { + if (name == BAD_NAME) + { + if (hasVersion) + { + size_t startIndex = nsQualified ? nsSepIndex+1 : 0; + name = fullName.substr(startIndex, versionSepIndex); + } + else if (nsQualified) + { + name = fullName.substr(nsSepIndex+1); + } + else + { + name = fullName; + } + } + return name; + } + + + std::string NTID::getVersion() + { + if (version == BAD_NAME) + { + version = (hasVersion) ? fullName.substr(versionSepIndex+1) : ""; + } + return version; + } + + + std::string NTID::getMajorVersionString() + { + if (majorVersionStr == BAD_NAME) + { + if (hasVersion) + { + endMajorIndex = fullName.find('.', versionSepIndex+1); + majorVersionStr = (endMajorIndex != std::string::npos) + ? fullName.substr(versionSepIndex+1, endMajorIndex-(versionSepIndex+1)) : + fullName.substr(versionSepIndex+1); + } + else + majorVersionStr = ""; + } + return majorVersionStr; + } + + + bool NTID::hasMajorVersion() + { + if (hasVersion && !majorVersionParsed) + { + try { + using pvData::detail::parseToPOD; + parseToPOD(getMajorVersionString(), &majorVersion); + hasMajor = true; + } catch (...) {} + majorVersionParsed = true; + } + return hasMajor; + } + + + int NTID::getMajorVersion() + { + // call hasMajorVersion() to calculate values + hasMajorVersion(); + return majorVersion; + } + + + std::string NTID::getMinorVersionString() + { + // call hasMinorVersion() to calculate start of minor + getMajorVersionString(); + if (minorVersionStr == BAD_NAME) + { + if (hasVersion && endMajorIndex != std::string::npos) + { + endMinorIndex = fullName.find('.', endMajorIndex+1); + minorVersionStr = (endMinorIndex != std::string::npos) + ? fullName.substr(endMajorIndex+1, endMinorIndex-(endMajorIndex+1)) : + fullName.substr(endMajorIndex+1); + } + else + minorVersionStr = ""; + } + return minorVersionStr; + } + + + bool NTID::hasMinorVersion() + { + if (hasVersion && !minorVersionParsed) + { + try { + using pvData::detail::parseToPOD; + parseToPOD(getMinorVersionString(), &minorVersion); + hasMinor = true; + } catch (...) {} + minorVersionParsed = true; + } + return hasMinor; + } + + + int NTID::getMinorVersion() + { + // call hasMinorVersion() to calculate values + hasMinorVersion(); + return minorVersion; + } + + +}} + + diff --git a/src/nt/ntid.h b/src/nt/ntid.h index f17aa25..662ec21 100644 --- a/src/nt/ntid.h +++ b/src/nt/ntid.h @@ -6,6 +6,7 @@ #ifndef NTID_H #define NTID_H +#include namespace epics { @@ -27,14 +28,14 @@ public: * @param id The the id to be parsed. * @return NTNDArray instance on success, null otherwise. */ - NTID(String id); + NTID(const std::string &id); /** * Get the full name of the id, i.e. the original ID * * For example above returns "epics:nt/NTNDArray:1.2" * @return the full name */ - String getFullName(); + std::string getFullName(); /** * Get the fully qualified name including namespaces, but excluding version numbers @@ -42,7 +43,7 @@ public: * For example above return "epics:nt/NTNDArray" * @return the fully qualified name */ - String getQualifiedName(); + std::string getQualifiedName(); /** * Get the namespace @@ -50,7 +51,7 @@ public: * For example above return "epics:nt" * @return the namespace */ - String getNamespace(); + std::string getNamespace(); /** * Get the unqualified name, without namespace or version @@ -58,7 +59,7 @@ public: * For example above return "NTNDArray" * @return the unqualified name */ - String getName(); + std::string getName(); /** * Get the unqualified name, without namespace or version @@ -66,7 +67,7 @@ public: * For example above return "NTNDArray" * @return the unqualified name */ - String getVersion(); + std::string getVersion(); /** * Get the Major version as a string @@ -74,14 +75,14 @@ public: * For example above return "1" * @return the Major string */ - String getMajorVersionString(); + std::string getMajorVersionString(); /** * Does the ID contain a major version and is it a number * * @return true if it contains a major version number */ - boolean hasMajorVersion(); + bool hasMajorVersion(); /** * Get the Major version as an integer @@ -97,14 +98,14 @@ public: * For example above return "1" * @return the Major string */ - String getMinorVersionString(); + std::string getMinorVersionString(); /** * Does the ID contain a minor version and is it a number * * @return true if it contains a minor version number */ - boolean hasMinorVersion(); + bool hasMinorVersion(); /** * Get the Minor version as an integer @@ -112,7 +113,32 @@ public: * For example above return 1 * @return the Minor string */ - int getMinorVersion() + int getMinorVersion(); + +private: + std::string fullName; + std::string qualifiedName; + std::string namespaceStr; + std::string name; + std::string version; + + size_t nsSepIndex; + size_t versionSepIndex; + bool nsQualified; + bool hasVersion; + + size_t endMajorIndex; + std::string majorVersionStr; + bool majorVersionParsed; + bool hasMajor; + int majorVersion; + + size_t endMinorIndex; + std::string minorVersionStr; + bool minorVersionParsed; + bool hasMinor; + int minorVersion; + }; }}