casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Casarc.h
Go to the documentation of this file.
00001 //# Casarc.h: Class to read the casa general resource files
00002 //# Copyright (C) 2009
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 
00027 #ifndef CASA_CASARC_H
00028 #define CASA_CASARC_H
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <string>
00032 #include <list>
00033 #include <map>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037     class Casarc {
00038 
00039         friend class CasarcCleanup;
00040 
00041         public:
00042 
00043             typedef std::map<std::string,std::string>::const_iterator iterator;
00044 
00045             // return default casarc file, e.g. ~/.casarc
00046             static Casarc &instance( );
00047 
00048             // set and clear the default casarc path (to something other than ~/.casarc)
00049             static void setDefaultPath( const std::string &path );
00050             static void clearDefaultPath( );
00051 
00052             // return a casarc object attached to a particular file
00053             static Casarc &instance( const std::string &path );
00054 
00055             // return the list of rcfiles that have been loaded
00056             static const std::list<Casarc*> & list( );
00057 
00058             // adds the keyword->value mapping
00059             void put( const std::string &keyword, const std::string &value );
00060 
00061             // retrieves the mapping for keyword (or "" if the mapping is not defined)
00062             std::string get( const std::string &keyword );
00063 
00064             // retrieves the mapping for keyword in value (and returns true if the
00065             // mapping is defined or false otherwise)
00066             bool get( const std::string &keyword, std::string &value );
00067 
00068             size_t size( ) const;
00069 
00070             // path to the file that this Casarc mirrors...
00071             const std::string &path( ) const { return filename; }
00072 
00073             iterator begin( );
00074             iterator end( );
00075 
00076         private:
00077 
00078             class meta_entry_ {
00079             public:
00080                 meta_entry_( off_t keys_, int keyl_, off_t vals_, int vall_, off_t times_, int timel_ ) : 
00081                                         key_offset_(keys_), key_len_(keyl_), 
00082                                         val_offset_(vals_), val_len_(vall_),
00083                                         time_offset_(times_), time_len_(timel_) { }
00084 
00085                 off_t key_offset( ) const { return key_offset_; }
00086                 int key_length( ) const { return key_len_; }
00087 
00088                 off_t value_offset( ) const { return val_offset_; }
00089                 int value_length( ) const { return val_len_; }
00090 
00091                 off_t time_offset( ) const { return time_offset_; }
00092                 int time_length( ) const { return time_len_; }
00093 
00094             private:
00095                 off_t key_offset_;
00096                 int key_len_;
00097                 off_t val_offset_;
00098                 int val_len_;
00099                 off_t time_offset_;
00100                 int time_len_;
00101             };
00102 
00103             char *mapped_file;
00104             off_t mapped_file_size;
00105 
00106             std::list<int> stale_fds;
00107             void close( int );
00108 
00109             std::list<pid_t> have_lock;
00110 
00111             enum lock_mode { READ, READ_WRITE, WRITE, APPEND };
00112             // returns a file descriptor or -1 on error...
00113             int lock( lock_mode mode );
00114             void unlock( int fd );
00115 
00116             void sync( );
00117             double current_modification_time( struct stat &buf );
00118 
00119             std::string filename;
00120             double timestamp;
00121 
00122             std::map<std::string,std::string> rcmap;
00123             // this is broken off separate from the rcmap to allow the
00124             // rcmap to be used for iteration by users of this class...
00125             std::map<std::string,meta_entry_> rcmetamap;
00126 
00127             void read_file( );
00128 
00129             ino_t inode;
00130 
00131             // singleton Casarcs, (inode->casarc)
00132             static std::map<ino_t,Casarc*> *rcfiles;
00133             static std::map<std::string,Casarc*> *filenames;
00134             static std::list<Casarc*> *rclist;
00135             static std::string *default_path;
00136             // path is the path to the .casarc (or .aipsrc file)
00137             Casarc( const std::string &path );
00138             static bool initialized;
00139             static void startup( );
00140             static void shutdown( );
00141             ~Casarc( ) { }
00142     };
00143 
00144     static class CasarcCleanup {
00145         public:
00146             CasarcCleanup( ) { creation_count += 1; }
00147             ~CasarcCleanup( ) { if ( --creation_count == 0 ) { Casarc::shutdown( ); } }
00148         private:
00149             static unsigned int creation_count;
00150     } local_cleanup_object;
00151   
00152 } //# NAMESPACE CASA - END
00153 
00154 #endif