// FilePhysical.cxx #include "FilePhysical.h" #include #include "hesbase/Name.h" #ifdef DEFECT_GCC_2_95 namespace std { typedef ios ios_base; } #endif using std::ios_base; using std::fstream; using hes::Name; using hes::File; using hes::FilePhysical; //********************************************************************** // Local functions. //********************************************************************** namespace { // Return if a file exists. bool file_exists(Name name) { string command = "ls " + name.name() + " >/dev/null 2>&1"; int stat = system(command.c_str()); return stat == 0; } } //********************************************************************** // Member functions. //********************************************************************** // Constructor. FilePhysical::FilePhysical(bool read_unlocked) : m_read_unlocked(read_unlocked), m_open_status(CLOSED), m_pfstrm(0) { } //********************************************************************** // Destructor. FilePhysical::~FilePhysical() { delete m_pfstrm; } //********************************************************************** // Return the file stream. std::fstream& FilePhysical::file_stream() { assert( m_pfstrm != 0 ); return *m_pfstrm; } //********************************************************************** // Return the file stream. std::fstream& FilePhysical::file_stream() const { assert( m_pfstrm != 0 ); return *m_pfstrm; } //********************************************************************** // Open or close the file. int FilePhysical::open(OpenStatus stat) { // If the request is to close the file, then do so by deleting // the file stream. if ( stat == CLOSED ) { // Notify subclass that the file is about to be closed. end_file(); // Subclass should no longer be writing an event. assert ( ! is_writing() ); delete m_pfstrm; m_pfstrm = 0; m_open_status = CLOSED; return 0; } // If there is a request to open a file that is already open, then // close it first. if ( m_pfstrm != 0 ) { int stat = open(CLOSED); assert( stat == 0 ); } assert( m_pfstrm == 0 ); // There must be a physical name defined. if ( ! physical_name().is_valid() ) { return 1; } if ( stat == READONLY ) { if ( !m_read_unlocked && !is_locked() ) { return 2; } if ( ! file_exists(physical_name()) ) { return 3; } m_pfstrm = new fstream(physical_name().name().c_str(), ios_base::in); } else { if ( is_locked() ) { return 4; } if ( stat==NEW && file_exists(physical_name()) ) { return 5; } if ( stat==APPEND && !file_exists(physical_name()) ) { return 6; } m_pfstrm = new fstream(physical_name().name().c_str(), ios_base::in|ios_base::out); } if ( m_pfstrm == 0 ) { return 7; } if ( ! m_pfstrm->is_open() ) { delete m_pfstrm; return 8; } m_open_status = stat; // Notify subclass that the file has been opened. begin_file(); return 0; } //********************************************************************** // Return the open status. File::OpenStatus FilePhysical::open_status() const { return m_open_status; } //********************************************************************** // Can event data be read? bool FilePhysical::can_be_read() const { return is_open() && ( is_locked() || m_read_unlocked ); } //********************************************************************** // Can event data be written? bool FilePhysical::can_be_written() const { return is_open() && m_open_status!=READONLY && !is_locked(); } //**********************************************************************