nidas v1.2.3
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#ifndef NIDAS_UTIL_FILESET_H
28#define NIDAS_UTIL_FILESET_H
29
30#include "IOException.h"
31#include "UTime.h"
32
33#include <list>
34#include <set>
35#include <string>
36#include <locale>
37#include <ctime>
38#include <limits.h>
39#include <cstdio>
40// #include <limits>
41#include <sys/types.h>
42#include <sys/uio.h>
43
44namespace nidas { namespace util {
45
52class FileSet {
53public:
54
58 FileSet();
59
63 FileSet(const FileSet& x);
64
68 FileSet& operator=(const FileSet& x);
69
73 virtual FileSet* clone() const;
74
79 virtual ~FileSet();
80
86 virtual void setDir(const std::string& val);
87
88 virtual const std::string& getDir() { return _dir; }
89
90 int getFd() const { return _fd; }
91
92 bool isNewFile() const { return _newFile; }
93
101 virtual void setFileName(const std::string& val);
102
103 virtual const std::string& getFileName() { return _filename; }
104
108 virtual const std::string& getPath() { return _fullpath; }
109
110 virtual void addFileName(const std::string& val) { _fileset.push_back(val); }
111
115 void setFileLengthSecs(int val)
116 {
117 // LLONG_MAX is 292471 years in microsconds, so we
118 // won't have a Y2K-type issue for a while...
119 if (val <= 0) _fileLength = LONG_LONG_MAX;
120 else _fileLength = (long long) val * USECS_PER_SEC;
121 }
122
124 {
125 if (_fileLength == LONG_LONG_MAX) return 0;
126 return (int)(_fileLength / USECS_PER_SEC);
127 }
128
138 virtual UTime createFile(UTime tfile, bool exact);
139
140 void setStartTime(const UTime& val) { _startTime = val; }
141
142 UTime getStartTime() const { return _startTime; }
143
144 void setEndTime(const UTime& val) { _endTime = val; }
145
146 UTime getEndTime() const { return _endTime; }
147
151 const std::string& getCurrentName() const { return _currname; }
152
160 virtual void
161 closeFile();
162
170 virtual void
171 openFileForWriting(const std::string& filename);
172
178 void openNextFile();
179
185 virtual size_t read(void* buf, size_t count);
186
192 virtual size_t write(const void* buf, size_t count);
193
197 virtual size_t write(const struct iovec* iov, int iovcnt);
198
199 static const char pathSeparator;
200
204 static void createDirectory(const std::string& name, mode_t mode);
205
211 static std::string getDirPortion(const std::string& path);
212
218 static std::string getFilePortion(const std::string& path);
219
225 static std::string makePath(const std::string& dir,const std::string& file);
226
236 void checkPathFormat(const UTime& t1, const UTime& t2);
237
241 std::list<std::string> matchFiles(const UTime& t1, const UTime& t2);
242
246 long long getFileSize() const;
247
252 int getLastErrno() const
253 {
254 return _lastErrno;
255 }
256
261 void
262 setKeepOpening(bool keepopening)
263 {
264 _keepopening = keepopening;
265 }
266
267 bool
269 {
270 return _keepopening;
271 }
272
273protected:
274
275 std::string formatName(const UTime& t1);
276
277 static void replaceChars(std::string& in,const std::string& pat,
278 const std::string& rep);
279
280 const std::time_put<char>& _timeputter;
281
283
287 mutable int _lastErrno;
288
289 int _fd;
290
292
293private:
294
295 void initialize();
296
297 std::string _dir;
298
299 std::string _filename;
300
301 std::string _currname;
302
303 std::string _fullpath;
304
305
307
309
310 std::list<std::string> _fileset;
311
312 std::list<std::string>::iterator _fileiter;
313
315
319 long long _fileLength;
320
321};
322
323}} // namespace nidas namespace util
324
325#endif
A description of a set of output files, consisting of a directory name and a file name format contain...
Definition FileSet.h:52
virtual size_t read(void *buf, size_t count)
Read from current file.
Definition FileSet.cc:290
bool _keepopening
Definition FileSet.h:291
void initialize()
Definition FileSet.cc:326
void setFileLengthSecs(int val)
Set/get the file length in seconds.
Definition FileSet.h:115
bool _initialized
Definition FileSet.h:314
std::list< std::string > matchFiles(const UTime &t1, const UTime &t2)
Definition FileSet.cc:488
FileSet()
constructor
Definition FileSet.cc:52
std::string _filename
Definition FileSet.h:299
long long getFileSize() const
Definition FileSet.cc:155
void setEndTime(const UTime &val)
Definition FileSet.h:144
virtual ~FileSet()
Destructor.
Definition FileSet.cc:101
long long _fileLength
File length, in microseconds.
Definition FileSet.h:319
bool isNewFile() const
Definition FileSet.h:92
virtual void closeFile()
Closes any file currently open.
Definition FileSet.cc:122
void checkPathFormat(const UTime &t1, const UTime &t2)
Check that any date or time descriptors, e.g.
Definition FileSet.cc:444
static const char pathSeparator
Definition FileSet.h:199
virtual void setDir(const std::string &val)
Set directory portion of file path.
Definition FileSet.cc:109
const std::time_put< char > & _timeputter
Definition FileSet.h:280
int getFileLengthSecs() const
Definition FileSet.h:123
virtual const std::string & getDir()
Definition FileSet.h:88
bool _newFile
Definition FileSet.h:282
virtual void setFileName(const std::string &val)
Set file name portion of file path.
Definition FileSet.cc:115
void openNextFile()
Open the next file to be read.
Definition FileSet.cc:378
virtual size_t write(const void *buf, size_t count)
Write to current file.
Definition FileSet.cc:305
std::list< std::string >::iterator _fileiter
Definition FileSet.h:312
int _lastErrno
This value can get set by getFileSize() which is a const method.
Definition FileSet.h:287
std::string _dir
Definition FileSet.h:297
virtual const std::string & getPath()
Get the full path, the concatenation of getDir() and getFileName().
Definition FileSet.h:108
const std::string & getCurrentName() const
Get name of current file.
Definition FileSet.h:151
int getFd() const
Definition FileSet.h:90
int getLastErrno() const
Get last error value.
Definition FileSet.h:252
std::list< std::string > _fileset
Definition FileSet.h:310
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:262
std::string _currname
Definition FileSet.h:301
int _fd
Definition FileSet.h:289
static std::string getDirPortion(const std::string &path)
Utility function to return the directory portion of a file path.
Definition FileSet.cc:416
UTime getEndTime() const
Definition FileSet.h:146
static void createDirectory(const std::string &name, mode_t mode)
Definition FileSet.cc:169
std::string _fullpath
Definition FileSet.h:303
virtual UTime createFile(UTime tfile, bool exact)
Create a new file, with a name formed from a time.
Definition FileSet.cc:223
UTime _endTime
Definition FileSet.h:308
virtual FileSet * clone() const
Virtual constructor.
Definition FileSet.cc:96
virtual void openFileForWriting(const std::string &filename)
Open a new file for writing.
Definition FileSet.cc:210
bool keepOpening()
Definition FileSet.h:268
UTime getStartTime() const
Definition FileSet.h:142
static void replaceChars(std::string &in, const std::string &pat, const std::string &rep)
Definition FileSet.cc:628
std::string formatName(const UTime &t1)
Definition FileSet.cc:439
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
static std::string getFilePortion(const std::string &path)
Utility function to return the file portion of a file path.
Definition FileSet.cc:425
virtual void addFileName(const std::string &val)
Definition FileSet.h:110
FileSet & operator=(const FileSet &x)
Assignment operator.
Definition FileSet.cc:75
void setStartTime(const UTime &val)
Definition FileSet.h:140
UTime _startTime
Definition FileSet.h:306
virtual const std::string & getFileName()
Definition FileSet.h:103
A class for parsing, formatting and doing operations on time, based on Unix time conventions,...
Definition UTime.h:95
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
#define USECS_PER_SEC
Definition ublox.cc:59