// Name.cxx #include "Name.h" #include #include #include using std::string; using std::wsetw; using std::setfill; using hes::Index; using hes::IndexVector; using hes::Name; #ifdef DEFECT_GCC_2_95 namespace std { typedef ios ios_base; } #endif //********************************************************************** // Static member functions. //********************************************************************** // Version. int Name::version() { return 1; } //********************************************************************** // Member functions. //********************************************************************** // Default constructor. Name::Name() : m_name("") { } //********************************************************************** // String constructor. Name::Name(string name) : m_name(name) { Index length = m_name.size(); // For now prohibit length > 127; // Later we can use the second byte to hold part of the length. if ( length<1 || length > 127 ) { m_name = ""; return; } // Insert the length and string in the index array. Index idx = length; unsigned int bytecount = 1; // range is (0,3) for ( unsigned int ich=0; ich>= 24; if ( length > 127 ) { return; } m_idx.push_back(*pidx); int bytecount = 1; for ( unsigned int ich=0; ich 4 ) { ++pidx; bytecount = 1; m_idx.push_back(*pidx); } Index idx = *pidx; idx <<= 8*(bytecount-1); idx >>= 24; m_name += char(idx); } ++pidx; if ( ! is_valid() ) { pidx = pidx0; } } //********************************************************************** // Validity check. bool Name::is_valid() const { if ( m_idx.size() ) { if ( (m_name.size()/4 + 1) == m_idx.size() ) { return true; } else { // Might want to add an assertion or exception here. return false; } } else { return false; } } //********************************************************************** // Return the key name. string Name::name() const { return m_name; } //********************************************************************** // Return the indices. const IndexVector& Name::indices() const { return m_idx; } //********************************************************************** // Free functions. //********************************************************************** // Ordering. bool operator<(const Name& lhs, const Name& rhs) { return lhs.name() < rhs.name(); } //********************************************************************** // Equality. bool operator==(const Name& lhs, const Name& rhs) { return lhs.name() == rhs.name(); } //********************************************************************** // Inequality. bool operator!=(const Name& lhs, const Name& rhs) { return ! (lhs == rhs); } //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const Name& rhs) { if ( ! rhs.is_valid() ) { lhs << "invalid name"; return lhs; } std::ios_base::fmtflags flags = lhs.flags(); char fillchar = lhs.fill(); lhs << rhs.name(); /* const IndexVector& idxs = rhs.indices(); lhs << "-"; for ( IndexVector::const_iterator pidx=idxs.begin(); pidx!=idxs.end(); ++pidx ) { if ( pidx != idxs.begin() ) { lhs << "-"; } lhs << hex << setw(8) << setfill('0') << *pidx; } */ lhs.flags(flags); lhs.fill(fillchar); return lhs; } //**********************************************************************