LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lXDR.hh
Go to the documentation of this file.
1 //
3 // Header file for a light-weight XDR class
4 // This class does not support the full XDR protocol, and
5 // neither does it work for all platforms. It was mainly
6 // written, in combination with lStdHep, to provide a faster
7 // alternative to the more cumdersome methods using mcfio in
8 // CLHEP.
9 //
10 // W.G.J. Langeveld, 24 May 2002
11 //
12 // Release notes:
13 // - Version 1.0 (23-Oct-2003)
14 //
16 #ifndef LXDR__HH
17 #define LXDR__HH
18 
19 #include <stdio.h>
20 
21 namespace UTIL{
22 
24 //
25 // The main lXDR class.
26 //
28 class lXDR {
29 private:
30 //
31 // The current version/revision is:
32 //
33  enum { MAJOR = 1, MINOR = 0, DAY = 23, MONTH = 10, YEAR = 2003 };
34 // ========================================================
35 public:
36  static int getMajor(void) { return(MAJOR); };
37  static int getMinor(void) { return(MINOR); };
38  static const char *getText(void) {
39  static char buff[80];
40  sprintf(buff, "lXDR version %d.%d (%02d.%02d.%d) by W.G.J. Langeveld, SLAC",
41  MAJOR, MINOR, DAY, MONTH, YEAR);
42  return(buff);
43  };
44 public:
45 //
46 // Constructors, destructor
47 // ------------------------
48 // Constructor opens file, destructor closes file. Once opened for
49 // reading, the file cannot be written to, and v.v.
50 //
51  lXDR(const char *filename = 0, bool open_for_write = false);
52 private: // Prevent copying
53  lXDR(const lXDR &);
54 public:
55  virtual ~lXDR();
56 //
57 // Change the file being read/written. If another file is currently
58 // being read or written, it is first closed. The new file position
59 // is the start of the file.
60 //
61  void setFileName(const char *filename, bool open_for_write = false);
62  const char *getFileName(void) const { return(_fileName); };
63 //
64 // Prevent assignment:
65 //
66 private:
67  lXDR &operator=(const lXDR &);
68 public:
69 //
70 // Check for errors in the last operation.
71 //
72  long getError(void) const { return(_error); };
73 //
74 // Read data.
75 // ----------
76 // The following routines read single longs floats or doubles.
77 // Check getError() for succes or failure.
78 //
79  long readLong(void);
80  double readFloat(void); // Note that this returns a double!!
81  double readDouble(void);
82 //
83 // The following routines read the length of an array of char, long or double
84 // from the file, allocate a suitably large array, and read the data from the
85 // file. Character strings are null terminated.
86 // Check getError() for succes or failure.
87 //
88  const char *readString(long &length);
89  long *readLongArray(long &length);
90  double *readFloatArray(long &length); // Note that this returns an array of doubles!!
91  double *readDoubleArray(long &length);
92 //
93 // Write data
94 // ----------
95 // The following routines write single longs or doubles.
96 // They return getError().
97 //
98  long writeLong(long data);
99  long writeDouble(double data);
100 //
101 // The following routines write the length of an array of char, long or double
102 // to the file, then write the data itself.
103 // The functions return getError().
104 //
105  long writeString(const char *data);
106  long writeString(const char *data, long length);
107  long writeLongArray(const long *data, long length);
108  long writeDoubleArray(const double *data, long length);
109 
110  void setError(long error) { _error = error; return; };
111 //
112 // Set or get (with no arguments) file position.
113 //
114  long filePosition(long pos = -1);
115 
116 private:
117  char *_fileName;
118  FILE *_fp;
119  long _error{0};
120  bool _openForWrite{false};
121 
122  bool _hasNetworkOrder{false};
123  double ntohd(double d) const;
124  double htond(double d) const { return(ntohd(d)); };
125 
126  long checkRead(long *);
127  long checkRead(float *);
128  long checkRead(double *);
129  long checkWrite(long *);
130  long checkWrite(double *);
131 };
132 
133 #define LXDR_SUCCESS 0
134 #define LXDR_OPENFAILURE 1
135 #define LXDR_READONLY 2
136 #define LXDR_WRITEONLY 3
137 #define LXDR_NOFILE 4
138 #define LXDR_READERROR 5
139 #define LXDR_WRITEERROR 6
140 #define LXDR_SEEKERROR 7
141 
142 }
143 #endif
long filePosition(long pos=-1)
Definition: lXDR.cc:338
long readLong(void)
Definition: lXDR.cc:152
bool _openForWrite
Definition: lXDR.hh:120
long _error
Definition: lXDR.hh:119
long checkRead(long *)
Definition: lXDR.cc:110
double readFloat(void)
Definition: lXDR.cc:166
void setFileName(const char *filename, bool open_for_write=false)
Definition: lXDR.cc:49
static int getMajor(void)
Definition: lXDR.hh:36
double ntohd(double d) const
Definition: lXDR.cc:87
long writeDoubleArray(const double *data, long length)
Definition: lXDR.cc:323
virtual ~lXDR()
Definition: lXDR.cc:28
lXDR(const char *filename=0, bool open_for_write=false)
Definition: lXDR.cc:41
long writeLong(long data)
Definition: lXDR.cc:284
void setError(long error)
Definition: lXDR.hh:110
lXDR & operator=(const lXDR &)
FILE * _fp
Definition: lXDR.hh:118
long * readLongArray(long &length)
Definition: lXDR.cc:188
const char * getFileName(void) const
Definition: lXDR.hh:62
static const char * getText(void)
Definition: lXDR.hh:38
double * readDoubleArray(long &length)
Definition: lXDR.cc:220
long writeDouble(double data)
Definition: lXDR.cc:289
static int getMinor(void)
Definition: lXDR.hh:37
double htond(double d) const
Definition: lXDR.hh:124
long checkWrite(long *)
Definition: lXDR.cc:261
bool _hasNetworkOrder
Definition: lXDR.hh:122
long writeString(const char *data)
Definition: lXDR.cc:294
double readDouble(void)
Definition: lXDR.cc:159
double * readFloatArray(long &length)
Definition: lXDR.cc:234
long getError(void) const
Definition: lXDR.hh:72
long writeLongArray(const long *data, long length)
Definition: lXDR.cc:308
const char * readString(long &length)
Definition: lXDR.cc:173
char * _fileName
Definition: lXDR.hh:117