Skip to content

Commit

Permalink
allow choice of overwriting file or not
Browse files Browse the repository at this point in the history
  • Loading branch information
carltimmer committed Dec 9, 2024
1 parent ed4c2f5 commit 22f4229
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/libsrc++/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,15 @@ namespace evio {
* the dictionary and/or first event will be placed in its
* own record and written as the user header.
* @param userLen length of valid data (bytes) in userHdr (starting at off).
* @param overwrite if true, overwrite existing file.
* @throws EvioException filename arg is null, if constructor specified writing to a buffer,
* if open() was already called without being followed by reset(),
* if file cannot be found, if IO error writing to file,
* or if filename is empty.
* if IO error writing to file,
* if filename is empty,
* or if overwrite is false and file exists.
*/
void Writer::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen) {
void Writer::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen, bool overwrite) {

if (opened) {
throw EvioException("currently open, call reset() first");
Expand Down Expand Up @@ -503,7 +506,14 @@ namespace evio {

// Write this to file
fileName = filename;
// TODO: what flags??? instead of "rw"
if (!overwrite) {
// Check if the file exists using std::ifstream
std::ifstream fileCheck(filename);
if (fileCheck.good()) {
throw EvioException("file already exists: " + filename);
}
}

outFile.open(filename, std::ios::binary);
if (outFile.fail()) {
throw EvioException("error opening file " + filename);
Expand Down
2 changes: 1 addition & 1 deletion src/libsrc++/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace evio {
void addTrailerWithIndex(bool addTrailingIndex);

void open(const std::string & filename);
void open(const std::string & filename, uint8_t* userHdr, uint32_t len);
void open(const std::string & filename, uint8_t* userHdr, uint32_t len, bool overwrite = true);
void open(std::shared_ptr<ByteBuffer> buf, uint8_t* userHdr, uint32_t len);

static std::shared_ptr<ByteBuffer> createRecord(const std::string & dictionary,
Expand Down
18 changes: 14 additions & 4 deletions src/libsrc++/WriterMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,14 @@ namespace evio {
* the dictionary and/or first event will be placed in its
* own record and written as the user header.
* @param userLen length of userHdr in bytes.
* @param overwrite if true, overwrite existing file.
* @throws EvioException if filename arg is bad,
* or if open() was already called without being followed by reset().
* @throws IOException if file cannot be found or IO error writing to file
* if open() was already called without being followed by reset(),
* if IO error writing to file,
* if filename is empty,
* if overwrite is false and file exists.
*/
void WriterMT::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen) {
void WriterMT::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen, bool overwrite) {

if (opened) {
throw EvioException("currently open, call reset() first");
Expand Down Expand Up @@ -307,7 +310,14 @@ namespace evio {

// Write this to file
fileName = filename;
// TODO: what flags??? instead of "rw"
if (!overwrite) {
// Check if the file exists using std::ifstream
std::ifstream fileCheck(filename);
if (fileCheck.good()) {
throw EvioException("file already exists: " + filename);
}
}

outFile.open(filename, std::ios::binary);
outFile.write(reinterpret_cast<const char*>(fileHeaderBuffer->array()), fileHeaderBuffer->remaining());
if (outFile.fail()) {
Expand Down
2 changes: 1 addition & 1 deletion src/libsrc++/WriterMT.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ namespace evio {
void addTrailerWithIndex(bool addTrailingIndex);

void open(const std::string & filename);
void open(const std::string & filename, uint8_t* userHdr, uint32_t userLen);
void open(const std::string & filename, uint8_t* userHdr, uint32_t userLen, bool overwrite = true);

std::shared_ptr<ByteBuffer> createHeader(uint8_t* userHdr, uint32_t userLen);
std::shared_ptr<ByteBuffer> createHeader(ByteBuffer & userHdr);
Expand Down

0 comments on commit 22f4229

Please sign in to comment.