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 //# $Id$
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 { //# NAMESPACE CASACORE - BEGIN
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             // return default casarc file, e.g. ~/.casarc
00049             static Casarc &instance( );
00050 
00051             // set and clear the default casarc path (to something other than ~/.casarc)
00052             static void setDefaultPath( const std::string &path );
00053             static void clearDefaultPath( );
00054 
00055             // return a casarc object attached to a particular file
00056             static Casarc &instance( const std::string &path );
00057 
00058             // return the list of rcfiles that have been loaded
00059             static const std::list<Casarc*> & list( );
00060 
00061             // adds the keyword->value mapping
00062             void put( const std::string &keyword, const std::string &value );
00063 
00064             // retrieves the mapping for keyword (or "" if the mapping is not defined)
00065             std::string get( const std::string &keyword );
00066 
00067             // retrieves the mapping for keyword in value (and returns true if the
00068             // mapping is defined or false otherwise)
00069             bool get( const std::string &keyword, std::string &value );
00070 
00071             size_t size( ) const;
00072 
00073             // path to the file that this Casarc mirrors...
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             // returns a file descriptor or -1 on error...
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             // this is broken off separate from the rcmap to allow the
00127             // rcmap to be used for iteration by users of this class...
00128             std::map<std::string,meta_entry_> rcmetamap;
00129 
00130             void read_file( );
00131 
00132             ino_t inode;
00133 
00134             // singleton Casarcs, (inode->casarc)
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             // path is the path to the .casarc (or .aipsrc file)
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 } //# NAMESPACE CASACORE - END
00156 
00157 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1