My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
DirfileWriter.h
Go to the documentation of this file.
1 #ifndef GCP_UTIL_DIRFILEWRITER_H
2 #define GCP_UTIL_DIRFILEWRITER_H
3 
11 #include "DirfileWriter.h"
12 
13 #include <list>
14 #include <string>
15 #include <vector>
16 
17 #include <unistd.h>
18 
19 namespace gcp {
20  namespace util {
21 
23  public:
24 
25  DirfileException(std::string message) {
26  message_ = message;
27  }
28 
29  std::string message_;
30  };
31 
32  class DirfileWriter {
33  public:
34 
35  // ArchiverWriterDirfil will maintain an array of objects of the
36  // following type, one per archived register.
37 
38  struct Register {
39 
40  enum Type {
41  NONE = 0x0,
42  UCHAR = 0x1,
43  CHAR = 0x2,
44  BOOL = 0x4,
45  USHORT = 0x8,
46  SHORT = 0x10,
47  UINT = 0x20,
48  INT = 0x40,
49  ULONG = 0x80,
50  LONG = 0x100,
51  FLOAT = 0x200,
52  DOUBLE = 0x400,
53  COMPLEX = 0x800
54  };
55 
56  // The name of the file in which this register will be written
57 
58  std::string name_;
59 
60  // A pointer to the beginning of the memory for this register
61  // value
62 
63  void* ptr_;
64 
65  // The number of bytes in this register block
66 
67  unsigned nByte_;
68  unsigned nBytePerEl_;
69 
70  // The number of elements in this register block
71 
72  unsigned nEl_;
73 
74  // A file descriptor associated with the currently open file
75  // corresponding to this register
76 
77  int fd_;
78 
79  // The number of elements we will buffer before writing
80 
81  unsigned nBuffer_;
82 
83  // The current sample
84 
85  unsigned iBuffer_;
86 
87  // The type of this register
88 
89  Type type_;
90 
91  // A format string for this register
92 
93  std::string format_;
94 
95  // A buffer for data
96 
97  std::vector<unsigned char> bytes_;
98 
99  // Constructors
100 
101  Register(std::string name,
102  unsigned char* base,
103  unsigned startEl,
104  unsigned nEl,
105  Type type,
106  bool writeIndex,
107  unsigned nBuffer);
108 
109  Register(const Register& reg);
110 
111  virtual ~Register();
112 
113  // Open a file for this register in the specified directory
114 
115  int open(char* dir);
116 
117  // Close the file associated with this register
118 
119  void close();
120 
121  // Flush the file associated with this register
122 
123  void flush();
124 
125  // Write the current value of this register
126 
127  int write();
128 
129  // A format string for this register
130 
131  std::string format();
132 
133  // Return the size, in bytes, of the requested type
134 
135  unsigned sizeOf(Type type);
136 
137  };
138 
152  DirfileWriter(unsigned nBuffer=1, bool writeIndex=true);
153 
154  void initialize(unsigned nBuffer, bool writeIndex);
155 
156  // Add an arbitrary type of register to the list of registers
157  // maintained by this object
158 
159  void addRegister(std::string name, unsigned char* base, Register::Type,
160  unsigned startEl=0, unsigned nEl=1);
161 
162  // Add a floating-point register to the list of registers this
163  // object will maintain
164 
165  void addFloatRegister(std::string name, unsigned char* base, unsigned startEl=0, unsigned nEl=1);
166 
170  DirfileWriter(const DirfileWriter& objToBeCopied);
171 
175  DirfileWriter(DirfileWriter& objToBeCopied);
176 
180  void operator=(const DirfileWriter& objToBeAssigned);
181 
185  void operator=(DirfileWriter& objToBeAssigned);
186 
190  virtual ~DirfileWriter();
191 
192  // Overloaded methods of the base class that will be called by
193  // the Archiver parent
194 
195  int openArcfile(std::string dir);
196  void closeArcfile();
197  void flushArcfile();
198  int writeIntegration();
199  bool isOpen();
200 
201  protected:
202 
203  // This is the number of samples we will buffer before writing to disk
204 
205  unsigned nBuffer_;
206 
207  unsigned maxlen_;
208 
209  bool writeIndex_;
210 
211  // True if archive files are currently open
212 
213  bool isOpen_;
214 
215  // The list of registers
216 
217  std::list<Register> registers_;
218 
219  // The base directory in which we are working
220 
221  std::string dirname_;
222 
223  // Insert a block of register elements into the list
224 
225  void insertReg(std::string name,
226  unsigned char* basePtr,
227  unsigned startEl,
228  unsigned nEl,
229  Register::Type type);
230 
231  // Create the directory for the current set of archive files
232 
233  int createDir(char* dir);
234 
235  // Write the format file needed for things like KST
236 
237  int outputFormatFile(char* dir);
238 
239  }; // End class DirfileWriter
240 
241  } // End namespace util
242 } // End namespace gcp
243 
244 
245 #endif // End #ifndef GCP_UTIL_DIRFILEWRITER_H
int createDir(char *dir)
Definition: DirfileWriter.cc:157
std::string format()
Definition: DirfileWriter.cc:349
void addFloatRegister(std::string name, unsigned char *base, unsigned startEl=0, unsigned nEl=1)
Definition: DirfileWriter.cc:266
Definition: DirfileWriter.h:32
int open(char *dir)
Definition: DirfileWriter.cc:407
int write()
Definition: DirfileWriter.cc:454
int outputFormatFile(char *dir)
Definition: DirfileWriter.cc:213
Definition: DirfileWriter.h:22
void initialize(unsigned nBuffer, bool writeIndex)
Definition: DirfileWriter.cc:59
void flush()
Definition: DirfileWriter.cc:441
Definition: DirfileWriter.h:38
void insertReg(std::string name, unsigned char *basePtr, unsigned startEl, unsigned nEl, Register::Type type)
Definition: DirfileWriter.cc:236
bool isOpen()
Definition: DirfileWriter.cc:205
DirfileWriter(unsigned nBuffer=1, bool writeIndex=true)
Definition: DirfileWriter.cc:51
void flushArcfile()
Definition: DirfileWriter.cc:177
Register(std::string name, unsigned char *base, unsigned startEl, unsigned nEl, Type type, bool writeIndex, unsigned nBuffer)
Definition: DirfileWriter.cc:279
virtual ~Register()
Definition: DirfileWriter.cc:397
void addRegister(std::string name, unsigned char *base, Register::Type, unsigned startEl=0, unsigned nEl=1)
Definition: DirfileWriter.cc:255
unsigned sizeOf(Type type)
Definition: DirfileWriter.cc:527
void operator=(const DirfileWriter &objToBeAssigned)
Definition: DirfileWriter.cc:95
void close()
Definition: DirfileWriter.cc:430
void closeArcfile()
Definition: DirfileWriter.cc:165
int writeIntegration()
Definition: DirfileWriter.cc:187
int openArcfile(std::string dir)
Definition: DirfileWriter.cc:112
virtual ~DirfileWriter()
Definition: DirfileWriter.cc:70