nidas  v1.2-1520
FileSet.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 /*
27  ********************************************************************
28  Copyright by the National Center for Atmospheric Research
29 
30  $LastChangedDate$
31 
32  $LastChangedRevision$
33 
34  $LastChangedBy$
35 
36  $HeadURL$
37  ********************************************************************
38 
39 */
40 
41 #ifndef NIDAS_UTIL_FILESET_H
42 #define NIDAS_UTIL_FILESET_H
43 
44 #include "IOException.h"
45 #include "UTime.h"
46 
47 #include <list>
48 #include <set>
49 #include <string>
50 #include <locale>
51 #include <ctime>
52 #include <limits.h>
53 #include <cstdio>
54 // #include <limits>
55 #include <sys/types.h>
56 #include <sys/uio.h>
57 
58 namespace nidas { namespace util {
59 
66 class FileSet {
67 public:
68 
72  FileSet();
73 
77  FileSet(const FileSet& x);
78 
82  FileSet& operator=(const FileSet& x);
83 
87  virtual FileSet* clone() const;
88 
93  virtual ~FileSet();
94 
100  virtual void setDir(const std::string& val);
101 
102  virtual const std::string& getDir() { return _dir; }
103 
104  int getFd() const { return _fd; }
105 
106  bool isNewFile() const { return _newFile; }
107 
115  virtual void setFileName(const std::string& val);
116 
117  virtual const std::string& getFileName() { return _filename; }
118 
122  virtual const std::string& getPath() { return _fullpath; }
123 
124  virtual void addFileName(const std::string& val) { _fileset.push_back(val); }
125 
129  void setFileLengthSecs(int val)
130  {
131  // LLONG_MAX is 292471 years in microsconds, so we
132  // won't have a Y2K-type issue for a while...
133  if (val <= 0) _fileLength = LONG_LONG_MAX;
134  else _fileLength = (long long) val * USECS_PER_SEC;
135  }
136 
137  int getFileLengthSecs() const
138  {
139  if (_fileLength == LONG_LONG_MAX) return 0;
140  return (int)(_fileLength / USECS_PER_SEC);
141  }
142 
150  virtual UTime createFile(UTime tfile,bool exact) throw(IOException);
151 
152  void setStartTime(const UTime& val) { _startTime = val; }
153 
154  UTime getStartTime() const { return _startTime; }
155 
156  void setEndTime(const UTime& val) { _endTime = val; }
157 
158  UTime getEndTime() const { return _endTime; }
159 
163  const std::string& getCurrentName() const { return _currname; }
164 
170  virtual void
171  closeFile() throw(IOException);
172 
178  virtual void
179  openFileForWriting(const std::string& filename) throw(IOException);
180 
184  void openNextFile() throw(IOException);
185 
189  virtual size_t read(void* buf, size_t count) throw(IOException);
190 
194  virtual size_t write(const void* buf, size_t count) throw(IOException);
195 
196  virtual size_t write(const struct iovec* iov, int iovcnt) throw(IOException);
197 
198  static const char pathSeparator;
199 
200  static void createDirectory(const std::string& name,mode_t mode);
201 
207  static std::string getDirPortion(const std::string& path);
208 
214  static std::string getFilePortion(const std::string& path);
215 
221  static std::string makePath(const std::string& dir,const std::string& file);
222 
223 #if !defined(NIDAS_EMBEDDED)
224 
231  void checkPathFormat(const UTime& t1, const UTime& t2) throw(IOException);
232 #endif
233 
234  std::list<std::string> matchFiles(const UTime& t1, const UTime& t2)
235  throw(IOException);
236 
237  long long getFileSize() const throw(IOException);
238 
243  int getLastErrno() const
244  {
245  return _lastErrno;
246  }
247 
252  void
253  setKeepOpening(bool keepopening)
254  {
255  _keepopening = keepopening;
256  }
257 
258  bool
260  {
261  return _keepopening;
262  }
263 
264 protected:
265 
266  std::string formatName(const UTime& t1);
267 
268  static void replaceChars(std::string& in,const std::string& pat,
269  const std::string& rep);
270 
271  const std::time_put<char>& _timeputter;
272 
273  bool _newFile;
274 
278  mutable int _lastErrno;
279 
280  int _fd;
281 
283 
284 private:
285 
286  void initialize();
287 
288  std::string _dir;
289 
290  std::string _filename;
291 
292  std::string _currname;
293 
294  std::string _fullpath;
295 
296 
298 
300 
301  std::list<std::string> _fileset;
302 
303  std::list<std::string>::iterator _fileiter;
304 
306 
310  long long _fileLength;
311 
312 };
313 
314 }} // namespace nidas namespace util
315 
316 #endif
const std::time_put< char > & _timeputter
Definition: FileSet.h:271
void setStartTime(const UTime &val)
Definition: FileSet.h:152
long long _fileLength
File length, in microseconds.
Definition: FileSet.h:310
static std::string getDirPortion(const std::string &path)
Utility function to return the directory portion of a file path.
Definition: FileSet.cc:416
virtual UTime createFile(UTime tfile, bool exact)
Create a new file, with a name formed from a time.
Definition: FileSet.cc:223
virtual void setDir(const std::string &val)
Set directory portion of file path.
Definition: FileSet.cc:109
static std::string makePath(const std::string &dir, const std::string &file)
Utility function to create a full path name from a directory and file portion.
Definition: FileSet.cc:433
virtual const std::string & getFileName()
Definition: FileSet.h:117
A class for parsing, formatting and doing operations on time, based on Unix time conventions, where leap seconds are ignored, so that there are always 60 seconds in a minute, 3600 seconds in an hour and 86400 seconds in a day.
Definition: UTime.h:76
static void createDirectory(const std::string &name, mode_t mode)
Definition: FileSet.cc:169
virtual void openFileForWriting(const std::string &filename)
Open a new file for writing.
Definition: FileSet.cc:210
FileSet & operator=(const FileSet &x)
Assignment operator.
Definition: FileSet.cc:75
int _fd
Definition: FileSet.h:280
A description of a set of output files, consisting of a directory name and a file name format contain...
Definition: FileSet.h:66
virtual FileSet * clone() const
Virtual constructor.
Definition: FileSet.cc:96
void setFileLengthSecs(int val)
Set/get the file length in seconds.
Definition: FileSet.h:129
virtual void addFileName(const std::string &val)
Definition: FileSet.h:124
static void replaceChars(std::string &in, const std::string &pat, const std::string &rep)
Definition: FileSet.cc:632
void setEndTime(const UTime &val)
Definition: FileSet.h:156
std::list< std::string > matchFiles(const UTime &t1, const UTime &t2)
Definition: FileSet.cc:490
virtual const std::string & getPath()
Get the full path, the concatenation of getDir() and getFileName().
Definition: FileSet.h:122
virtual ~FileSet()
Destructor.
Definition: FileSet.cc:101
int getFileLengthSecs() const
Definition: FileSet.h:137
virtual size_t read(void *buf, size_t count)
Read from current file.
Definition: FileSet.cc:290
std::string formatName(const UTime &t1)
Definition: FileSet.cc:439
virtual const std::string & getDir()
Definition: FileSet.h:102
std::string _fullpath
Definition: FileSet.h:294
virtual size_t write(const void *buf, size_t count)
Write to current file.
Definition: FileSet.cc:305
static std::string getFilePortion(const std::string &path)
Utility function to return the file portion of a file path.
Definition: FileSet.cc:425
std::list< std::string > _fileset
Definition: FileSet.h:301
int _lastErrno
This value can get set by getFileSize() which is a const method.
Definition: FileSet.h:278
void initialize()
Definition: FileSet.cc:326
void checkPathFormat(const UTime &t1, const UTime &t2)
Check that any date or time descriptors, e.g.
Definition: FileSet.cc:445
std::list< std::string >::iterator _fileiter
Definition: FileSet.h:303
UTime getStartTime() const
Definition: FileSet.h:154
void setKeepOpening(bool keepopening)
Set whether the FileSet should keep going to the next file when an error happens opening a file...
Definition: FileSet.h:253
virtual void setFileName(const std::string &val)
Set file name portion of file path.
Definition: FileSet.cc:115
long long getFileSize() const
Definition: FileSet.cc:155
bool _initialized
Definition: FileSet.h:305
UTime _startTime
Definition: FileSet.h:297
bool isNewFile() const
Definition: FileSet.h:106
std::string _dir
Definition: FileSet.h:288
static const char pathSeparator
Definition: FileSet.h:198
Definition: IOException.h:37
bool keepOpening()
Definition: FileSet.h:259
FileSet()
constructor
Definition: FileSet.cc:52
UTime getEndTime() const
Definition: FileSet.h:158
std::string _filename
Definition: FileSet.h:290
void openNextFile()
Open the next file to be read.
Definition: FileSet.cc:378
#define USECS_PER_SEC
Definition: ublox.cc:59
std::string _currname
Definition: FileSet.h:292
int getLastErrno() const
Get last error value.
Definition: FileSet.h:243
bool _keepopening
Definition: FileSet.h:282
int getFd() const
Definition: FileSet.h:104
virtual void closeFile()
Closes any file currently open.
Definition: FileSet.cc:122
const std::string & getCurrentName() const
Get name of current file.
Definition: FileSet.h:163
UTime _endTime
Definition: FileSet.h:299
bool _newFile
Definition: FileSet.h:273