casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SDDBootStrap.h
Go to the documentation of this file.
00001 //# SDDBootStrap.h: this defines SDDBootStrap, which encapsulates the SDD bootstrap block.
00002 //# Copyright (C) 1995,1999,2001
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 //# $Id$
00028 
00029 #ifndef NRAO_SDDBOOTSTRAP_H
00030 #define NRAO_SDDBOOTSTRAP_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Utilities/CountedPtr.h>
00034 #include <casa/fstream.h>
00035 
00036 #include <casa/namespace.h>
00037 //# Forward Declarations 
00038 
00039 class SDDBlock;
00040 
00041 // <summary> 
00042 // Class encapsulates user-options for a bootstrap block from an SDD file
00043 // </summary>
00044 
00045 // <use visibility=export>
00046 
00047 // <reviewed reviewer="" date="" tests="" demos="">
00048 
00049 // <prerequisite>
00050 //   <li> SDDFile
00051 //   <li> SDDIndex
00052 //   <li> SDDData
00053 //   <li> The SDD data format
00054 // </prerequisite>
00055 //
00056 // <etymology>
00057 // SDD is the Single Dish Data format used by UniPOPS.  It is also the
00058 // on-line data format at the NRAO 12-m.  The bootstrap block is the first
00059 // 512 bytes of an SDD file.  It contains information that describes the
00060 // file and allows the rest of the file to be read correctly.  This class
00061 // encapsulates that data.
00062 // </etymology>
00063 //
00064 // <synopsis> 
00065 // This class encapsulates the SDD bootstrap block.  
00066 // It can be initialzed from a CountedPtr<fstream> object.  
00067 // In order to be written
00068 // to disk it must be attached to a CountedPtr<fstream> object.  
00069 // The class correctly positions the fstream to the
00070 // start of the file.  The information in the bootstrap is retrieved via
00071 // member functions.  Most of the information in the bootstrap can be
00072 // set.  The class does basic sanity checking on the information in the
00073 // bootstrap.  It is expected that this class will be used with other
00074 // SDD classes (most users will simply use SDDFile).
00075 // The hasChanged() function checks the internal copy of the bootstrap block with
00076 // the block on disk (if attached) and returns True if the file has changed
00077 // (i.e. whats on disk does NOT match this object) and False if it has
00078 // not changed.
00079 // </synopsis> 
00080 //
00081 // <example>
00082 // Create an SDDBootStrap from a file on disk.
00083 // Print some information, set some values, write it out to the same file.
00084 // <srcblock>
00085 //     fstream* file_ptr = new fstream("file_name", ios::nocreate);
00086 //     CountedPtr<fstream> file(file_ptr);
00087 //     SDDBootStrap bs(file);
00088 //     cout << "Number of index records : " << bs.nIndexRec() << endl;
00089 //     cout << "Number of data records : " << bs.nDataRec() << endl;
00090 //     cout << "Maximum index entry in use : " << bs.maxEntryUsed() << endl;
00091 //
00092 //     // set some new values
00093 //     bs.setNDataRec(bs.nDataRec()+5);
00094 //     if (!bs.setMaxEntryUsed(bs.maxEntryUsed()+1)) {
00095 //        cout << "unable to set max entry used" << endl;
00096 //        // do something appropriate here
00097 //     }
00098 //
00099 //     // write it out
00100 //     bs.write();
00101 // </srcblock>
00102 // </example>
00103 //
00104 // <motivation>
00105 // The SDD file format is used by UniPOPS and the on-line data
00106 // system at the NRAO 12-m.  It is necessary to read this type of
00107 // data directly into aips++.  Specifically, a table storage manager
00108 // can be written to make an SDD file look like a table.  Underneath that
00109 // storage manager are the SDD classes to encapsulate the data.
00110 // </motivation>
00111 //
00112 //
00113 // <todo asof="">
00114 // <li> I'm still not happy with the update mechanism
00115 // <li> Need to be more explicit about RO versus RW or is that info 
00116 // available directly from the fstream?
00117 // </todo>
00118 
00119 class SDDBootStrap
00120 {
00121 public: 
00122 
00123     enum TYPE_OF_SDD { DATA = 0,      // normal data file
00124                        RECORDS = 1 }; // individual records file
00125 
00126     enum SDD_VERSION { ORIGINAL = 0,  // used short integers, limited file size
00127                        CURRENT = 1 }; // uses long integers
00128 
00129 
00130     // Default constructor : empty CURRENT version
00131     // NOT attached to a file
00132     SDDBootStrap();
00133     // create from an fstream ptr in CountedPtr<fstream>
00134     SDDBootStrap(CountedPtr<fstream> streamPtr);
00135     // from an existing SDDBootStrap, copies all values
00136     SDDBootStrap(const SDDBootStrap& other);
00137 
00138     ~SDDBootStrap();
00139     
00140     // The assignment operator, does a full copy
00141     SDDBootStrap& operator=(const SDDBootStrap& other);
00142 
00143     // Attach
00144     void attach(CountedPtr<fstream> newStreamPtr);
00145     // write it out, must already be attached
00146     void write();
00147 
00148     // Get the CountedPtr<fstream> in use here
00149     CountedPtr<fstream>& theStream() { return theFile_p; }
00150 
00151     // These just return the values.
00152     uInt nIndexRec() const {return nIndexRec_p;}
00153     uInt nDataRec() const {return nDataRec_p;}
00154     uInt bytesPerRecord() const {return bytesPerRec_p;}
00155     uInt bytesPerEntry() const {return bytesPerEntry_p;}
00156     uInt maxEntryUsed() const {return maxEntryUsed_p;}
00157     uInt counter() const {return counter_p;}
00158     uInt type() const {return type_p;}
00159     uInt version() const {return version_p;}
00160 
00161     // Maximum index entries that can be used, based on
00162     // the values in the bootstrap
00163     uInt maxEntries() const 
00164     {return ((nIndexRec_p-1) * bytesPerRec_p / bytesPerEntry_p);}
00165 
00166     // the numebr of entries per record
00167     uInt entriesPerRecord() const
00168     {return bytesPerRec_p / bytesPerEntry_p;}
00169 
00170     // Set things that are allowed to be set
00171     void setNIndexRec(uInt newValue) {nIndexRec_p = newValue;}
00172     void setNDataRec(uInt newValue) {nDataRec_p = newValue;}
00173     void setBytesPerEntry(uInt newValue) {bytesPerEntry_p = newValue;}
00174     void setVersion(SDD_VERSION newValue) {version_p = newValue;}
00175     void setType(TYPE_OF_SDD newValue) {type_p = newValue;}
00176 
00177     // this requires a sanity check to ensure it is less than maxEntries
00178     Bool setMaxEntryUsed(uInt newValue);
00179 
00180     // This returns True if the bootstrap on disk differs from the
00181     // one here.  It does not change the bootstrap on disk.
00182     Bool hasChanged();
00183 
00184     // sync() re-reads the values from disk.  Any internal values
00185     // will be forgotten.
00186     void sync();
00187 
00188 private:
00189     // this enum describes where things are in the bootstrap block
00190     // for ORIGINAL version files, these are short (2-byte) unsigned integers
00191     // for CURRENT version files, these are long (4-byte) unsigned integers
00192     // all unused space is filled with zeros (both ORIGINAL and CURRECT).
00193     enum { N_INDEX_REC     = 0,   // Number of index records
00194            N_DATA_REC      = 1,   // Number of data records
00195            BYTES_PER_REC   = 2,   // Number of bytes/records, must be 512
00196            BYTES_PER_ENTRY = 3,   // Number of bytes/index-entry
00197            MAX_ENTRY_USED  = 4,   // Number (origin=1) of 
00198                                   // largest index entry in use
00199            COUNTER         = 5,   // incremented by 1 whenever the file
00200                                   // changes, wraps back to 0 at max
00201                                   // unsigned 4-byte integer
00202            TYPE            = 6,   // either DATA or RECORDS
00203            VERSION         = 7};  // either ORIGINAL or CURRENT
00204     uInt nIndexRec_p, nDataRec_p, bytesPerRec_p, bytesPerEntry_p,
00205         maxEntryUsed_p, counter_p, type_p, version_p;
00206 
00207     SDDBlock* bs_p;
00208     CountedPtr<fstream> theFile_p;
00209 };
00210 
00211 inline Bool SDDBootStrap::setMaxEntryUsed(uInt newValue) 
00212 {
00213     if (newValue <= maxEntries()) maxEntryUsed_p = newValue;
00214     return (newValue == maxEntryUsed_p);
00215 }
00216 
00217 
00218 #endif