lXDR.hh

Go to the documentation of this file.
00001 
00002 //
00003 // Header file for a light-weight XDR class
00004 // This class does not support the full XDR protocol, and
00005 // neither does it work for all platforms. It was mainly 
00006 // written, in combination with lStdHep, to provide a faster
00007 // alternative to the more cumdersome methods using mcfio in
00008 // CLHEP.
00009 //
00010 // W.G.J. Langeveld, 24 May 2002
00011 //
00012 // Release notes:
00013 // - Version 1.0 (23-Oct-2003)
00014 //
00016 #ifndef LXDR__HH
00017 #define LXDR__HH
00018 
00019 #include <stdio.h>
00020 
00022 //
00023 // The main lXDR class.
00024 //
00026 
00031 class lXDR {
00032 private:
00033 //
00034 // The current version/revision is:
00035 //
00036    enum { MAJOR = 1, MINOR = 0, DAY = 23, MONTH = 10, YEAR = 2003 };
00037 //       ========================================================
00038 public:
00039    static int         getMajor(void) { return(MAJOR); };
00040    static int         getMinor(void) { return(MINOR); };
00041    static const char *getText(void)  {
00042       static char buff[80];
00043       sprintf(buff, "lXDR version %d.%d (%02d.%02d.%d) by W.G.J. Langeveld, SLAC",
00044               MAJOR, MINOR, DAY, MONTH, YEAR);
00045       return(buff);
00046    };
00047 public:
00048 //
00049 // Constructors, destructor
00050 // ------------------------
00051 // Constructor opens file, destructor closes file. Once opened for
00052 // reading, the file cannot be written to, and v.v.
00053 //
00054    lXDR(const char *filename = 0, bool open_for_write = false);
00055 private: // Prevent copying
00056    lXDR(const lXDR &);
00057 public:
00058    virtual ~lXDR();
00059 //
00060 // Change the file being read/written. If another file is currently
00061 // being read or written, it is first closed. The new file position
00062 // is the start of the file.
00063 //
00064    void        setFileName(const char *filename, bool open_for_write = false);
00065    const char *getFileName(void) const { return(_fileName); };
00066 //
00067 // Prevent assignment:
00068 //
00069 private:
00070    lXDR       &operator=(const lXDR &);
00071 public:
00072 //
00073 // Check for errors in the last operation.
00074 //
00075    long        getError(void) const { return(_error); };
00076 //
00077 // Read data.
00078 // ----------
00079 // The following routines read single longs floats or doubles.
00080 // Check getError() for succes or failure.
00081 //
00082    long        readLong(void);
00083    double      readFloat(void);  // Note that this returns a double!!
00084    double      readDouble(void);
00085 //
00086 // The following routines read the length of an array of char, long or double
00087 // from the file, allocate a suitably large array, and read the data from the
00088 // file. Character strings are null terminated.
00089 // Check getError() for succes or failure.
00090 //
00091    const char *readString(long &length);
00092    long       *readLongArray(long &length);
00093    double     *readFloatArray(long &length); // Note that this returns an array of doubles!!
00094    double     *readDoubleArray(long &length);
00095 //
00096 // Write data
00097 // ----------
00098 // The following routines write single longs or doubles.
00099 // They return getError().
00100 //
00101    long        writeLong(long data);
00102    long        writeDouble(double data);
00103 //
00104 // The following routines write the length of an array of char, long or double
00105 // to the file, then write the data itself.
00106 // The functions return getError().
00107 //
00108    long        writeString(const char *data);
00109    long        writeString(const char *data, long length);
00110    long        writeLongArray(const long *data, long length);
00111    long        writeDoubleArray(const double *data, long length);
00112 
00113    void        setError(long error) { _error = error; return; };
00114 //
00115 // Set or get (with no arguments) file position.
00116 //
00117    long        filePosition(long pos = -1);
00118 
00119 private:
00120    char      *_fileName;
00121    FILE      *_fp;
00122    long       _error;
00123    bool       _openForWrite;
00124 
00125    bool       _hasNetworkOrder;
00126    double     ntohd(double d) const;
00127    double     htond(double d) const { return(ntohd(d)); };
00128 
00129    long       checkRead(long *);
00130    long       checkRead(float *);
00131    long       checkRead(double *);
00132    long       checkWrite(long *);
00133    long       checkWrite(double *);
00134 };
00135 
00136 #define LXDR_SUCCESS         0
00137 #define LXDR_OPENFAILURE     1
00138 #define LXDR_READONLY        2
00139 #define LXDR_WRITEONLY       3
00140 #define LXDR_NOFILE          4
00141 #define LXDR_READERROR       5
00142 #define LXDR_WRITEERROR      6
00143 #define LXDR_SEEKERROR       7
00144  
00145 
00146 #endif
00147 

Generated on Thu Nov 15 15:24:16 2007 for Simulator for the Linear Collider by  doxygen 1.5.4