// FileId.cxx #include "FileId.h" #include #include #include "FileType.h" using std::ostream; using std::ios_base; using std::endl; using std::setw; using std::hex; using hes::Index; using hes::FileType; using hes::FileId; #ifdef DEFECT_GCC_2_95 namespace std { typedef ios ios_base; } #endif namespace { // Bit masks. long LOMASK = 0xFF000000L; long HIMASK = 0x00FFFFFFL; } //********************************************************************** // Static member functions. //********************************************************************** // Return the version. int FileId::version() { return 1; } //********************************************************************** // Member functions. //********************************************************************** // Default constructor. FileId::FileId() : m_hi(0), m_lo(0) { } //********************************************************************** // Constructor from type, hi and lo. FileId::FileId(FileType type, Index hi, Index lo) : m_hi(0), m_lo(0) { bool bad = false; // Check the lo index. if ( lo == 0 ) { bad = true; } // Check the high index. if ( (hi & LOMASK) != 0 ) { bad = true; } // Check the filetype. if ( ! type.is_valid() ) { bad = true; } if ( ! bad ) { m_lo = lo; Index ityp = type.index(); ityp<<=24; m_hi = hi + ityp; } } //********************************************************************** // Constructor from type_hi and lo. FileId::FileId(Index type_hi, Index lo) : m_hi(type_hi), m_lo(lo) { if ( lo==0 || type_hi==0 || !file_type().is_valid() ) { m_hi = 0; m_lo = 0; } } //********************************************************************** // Validity check. bool FileId::is_valid() const { return m_hi > 0 && m_lo > 0; } //********************************************************************** // Return the file type. FileType FileId::file_type() const { Index ityp = m_hi; ityp >>= 24; return FileType(ityp); } //********************************************************************** // Return the hi index. Index FileId::hi_index() const { if ( ! is_valid() ) return 0; Index hi = m_hi; hi &= 0x00FFFFFF; return hi; } //********************************************************************** // Return the lo index. Index FileId::lo_index() const { if ( ! is_valid() ) return 0; return m_lo; } //********************************************************************** // Return the type_hi index. Index FileId::type_hi_index() const { if ( ! is_valid() ) return 0; return m_hi; } //********************************************************************** // Free functions. //********************************************************************** // Ordering. bool operator<(const FileId& lhs, const FileId& rhs) { if ( lhs.type_hi_index() == rhs.type_hi_index() ) { return lhs.lo_index() < rhs.lo_index(); } else { return lhs.type_hi_index() < rhs.type_hi_index(); } } //********************************************************************** // Equality. bool operator==(const FileId& lhs, const FileId& rhs) { if ( lhs.is_valid() ) { if ( rhs.is_valid() ) { return lhs.type_hi_index()==rhs.type_hi_index() && lhs.lo_index()==rhs.lo_index(); } else { return false; } } else { return rhs.is_valid(); } } //********************************************************************** bool operator!=(const FileId& lhs, const FileId& rhs) { return ! ( lhs == rhs ); } //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const FileId& rhs) { std::ios_base::fmtflags flags = lhs.flags(); if ( rhs.is_valid() ) { lhs << hex << setfill('0') << setw(2) << rhs.file_type().index() << "-" << setw(6) << rhs.hi_index() << "-" << setw(8) << rhs.lo_index() << setfill(' '); } else { lhs << "invalid file id"; } lhs.flags(flags); return lhs; } //**********************************************************************