Casarc.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CASA_CASARC_H
00029 #define CASA_CASARC_H
00030
00031 #include <casacore/casa/aips.h>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <string>
00035 #include <list>
00036 #include <map>
00037
00038 namespace casacore {
00039
00040 class Casarc {
00041
00042 friend class CasarcCleanup;
00043
00044 public:
00045
00046 typedef std::map<std::string,std::string>::const_iterator iterator;
00047
00048
00049 static Casarc &instance( );
00050
00051
00052 static void setDefaultPath( const std::string &path );
00053 static void clearDefaultPath( );
00054
00055
00056 static Casarc &instance( const std::string &path );
00057
00058
00059 static const std::list<Casarc*> & list( );
00060
00061
00062 void put( const std::string &keyword, const std::string &value );
00063
00064
00065 std::string get( const std::string &keyword );
00066
00067
00068
00069 bool get( const std::string &keyword, std::string &value );
00070
00071 size_t size( ) const;
00072
00073
00074 const std::string &path( ) const { return filename; }
00075
00076 iterator begin( );
00077 iterator end( );
00078
00079 private:
00080
00081 class meta_entry_ {
00082 public:
00083 meta_entry_( off_t keys_, int keyl_, off_t vals_, int vall_, off_t times_, int timel_ ) :
00084 key_offset_(keys_), key_len_(keyl_),
00085 val_offset_(vals_), val_len_(vall_),
00086 time_offset_(times_), time_len_(timel_) { }
00087
00088 off_t key_offset( ) const { return key_offset_; }
00089 int key_length( ) const { return key_len_; }
00090
00091 off_t value_offset( ) const { return val_offset_; }
00092 int value_length( ) const { return val_len_; }
00093
00094 off_t time_offset( ) const { return time_offset_; }
00095 int time_length( ) const { return time_len_; }
00096
00097 private:
00098 off_t key_offset_;
00099 int key_len_;
00100 off_t val_offset_;
00101 int val_len_;
00102 off_t time_offset_;
00103 int time_len_;
00104 };
00105
00106 char *mapped_file;
00107 off_t mapped_file_size;
00108
00109 std::list<int> stale_fds;
00110 void close( int );
00111
00112 std::list<pid_t> have_lock;
00113
00114 enum lock_mode { READ, READ_WRITE, WRITE, APPEND };
00115
00116 int lock( lock_mode mode );
00117 void unlock( int fd );
00118
00119 void sync( );
00120 double current_modification_time( struct stat &buf );
00121
00122 std::string filename;
00123 double timestamp;
00124
00125 std::map<std::string,std::string> rcmap;
00126
00127
00128 std::map<std::string,meta_entry_> rcmetamap;
00129
00130 void read_file( );
00131
00132 ino_t inode;
00133
00134
00135 static std::map<ino_t,Casarc*> *rcfiles;
00136 static std::map<std::string,Casarc*> *filenames;
00137 static std::list<Casarc*> *rclist;
00138 static std::string *default_path;
00139
00140 Casarc( const std::string &path );
00141 static bool initialized;
00142 static void startup( );
00143 static void shutdown( );
00144 ~Casarc( ) { }
00145 };
00146
00147 static class CasarcCleanup {
00148 public:
00149 CasarcCleanup( ) { creation_count += 1; }
00150 ~CasarcCleanup( ) { if ( --creation_count == 0 ) { Casarc::shutdown( ); } }
00151 private:
00152 static unsigned int creation_count;
00153 } local_cleanup_object;
00154
00155 }
00156
00157 #endif