Skip to content

Commit

Permalink
add initial file structure creation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Feb 28, 2024
1 parent 4f5aece commit dfaea2a
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 253 deletions.
27 changes: 14 additions & 13 deletions src/BaseIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ class BaseIO
virtual std::string getFileName() = 0;

/** Opens the file for writing */
virtual int open(std::string fileName) = 0;
virtual int open() = 0;

/** Opens the file for writing */
virtual int open(bool newfile) = 0;

/** Closes the file */
virtual void close() = 0;
Expand All @@ -85,23 +88,26 @@ class BaseIO

// /** Sets a string attribute at a given location in the file */
virtual int setAttribute(const std::string& data,
std::string path,
std::string name) = 0;
std::string path,
std::string name) = 0;

/** Sets a std::string array attribute at a given location in the file */
virtual int setAttribute(const std::vector<std::string>& data,
std::string path,
std::string name) = 0;
std::string path,
std::string name) = 0;

/** Sets a std::string array attribute at a given location in the file */
virtual int setAttribute(const std::vector<const char*>& data,
std::string path,
std::string name,
size_t maxSize) = 0;
std::string path,
std::string name,
size_t maxSize) = 0;

/** Creates a new group */
virtual int createGroup(std::string path) = 0;

/** Creates a non-modifiable dataset with a string value */
virtual void createStringDataSet(std::string path, std::string value) = 0;

// ------------------------------------------------------------
// OTHER METHODS
// ------------------------------------------------------------
Expand All @@ -116,16 +122,11 @@ class BaseIO
const std::string filename;

protected:
/** Creates the basic file structure upon opening */
virtual int createFileStructure() = 0; // TODO - not sure if this needs to be
// here or ust in the NWB side

/** Creates a new group (ignores if it exists) */
virtual int createGroupIfDoesNotExist(std::string path) = 0;

bool readyToOpen;
bool opened;
bool newfile;
};

/**
Expand Down
76 changes: 33 additions & 43 deletions src/HDF5IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <iostream>
#include <memory>
#include <vector>
#include <H5Cpp.h>

#include "HDF5IO.hpp"

#include <H5Cpp.h>

using namespace H5;
using namespace AQNWBIO;
Expand All @@ -15,6 +15,11 @@ using namespace AQNWBIO;

HDF5IO::HDF5IO() {}

HDF5IO::HDF5IO(std::string fileName)
: filename(fileName)
{
}

HDF5IO::~HDF5IO()
{
close();
Expand All @@ -25,27 +30,18 @@ std::string HDF5IO::getFileName()
return filename;
}

int HDF5IO::open(std::string fileName)
int HDF5IO::open()
{
filename = fileName;

if (!readyToOpen)
return -1;

if (std::filesystem::exists(getFileName()))
newfile = false;
else
newfile = true;

if (opened)
return -1;

return open(newfile);
if (std::filesystem::exists(getFileName())) {
return open(false);
} else {
return open(true);
}
}

int HDF5IO::open(bool newfile)
{
int accFlags, ret = 0;
int accFlags = 0;

if (opened)
return -1;
Expand All @@ -56,21 +52,12 @@ int HDF5IO::open(bool newfile)
accFlags = H5F_ACC_TRUNC;
else
accFlags = H5F_ACC_RDWR;

file = std::make_unique<H5::H5File>(
getFileName(), accFlags, FileCreatPropList::DEFAULT, props);
opened = true;

if (newfile) {
ret = createFileStructure();
}

if (ret) {
file = nullptr;
opened = false;
std::cerr << "Error creating file structure" << std::endl;
}

return ret;
return 0;
}

void HDF5IO::close()
Expand All @@ -79,11 +66,6 @@ void HDF5IO::close()
opened = false;
}

int HDF5IO::createFileStructure() // TODO - move this to NWB class
{
return 0;
}

int HDF5IO::setAttribute(BaseDataType type,
const void* data,
std::string path,
Expand Down Expand Up @@ -132,8 +114,8 @@ int HDF5IO::setAttribute(BaseDataType type,
}

int HDF5IO::setAttribute(const std::string& data,
std::string path,
std::string name)
std::string path,
std::string name)
{
std::vector<const char*> dataPtrs;
dataPtrs.push_back(data.c_str());
Expand All @@ -142,8 +124,8 @@ int HDF5IO::setAttribute(const std::string& data,
}

int HDF5IO::setAttribute(const std::vector<std::string>& data,
std::string path,
std::string name)
std::string path,
std::string name)
{
std::vector<const char*> dataPtrs;
size_t maxLength = 0;
Expand All @@ -157,9 +139,9 @@ int HDF5IO::setAttribute(const std::vector<std::string>& data,
}

int HDF5IO::setAttribute(const std::vector<const char*>& data,
std::string path,
std::string name,
size_t maxSize)
std::string path,
std::string name,
size_t maxSize)
{
H5Object* loc;
Group gloc;
Expand Down Expand Up @@ -251,20 +233,28 @@ HDF5RecordingData* HDF5IO::getDataSet(std::string path)
data = std::make_unique<H5::DataSet>(file->openDataSet(path));
return new HDF5RecordingData(data.release());
} catch (DataSetIException error) {
// std::cout << "DataSetIException" << std::endl;
error.printErrorStack();
return nullptr;
} catch (FileIException error) {
// std::cout << "FileIException" << std::endl;
error.printErrorStack();
return nullptr;
} catch (DataSpaceIException error) {
// std::cout << "DataSpaceIException" << std::endl;
error.printErrorStack();
return nullptr;
}
}

void HDF5IO::createStringDataSet(std::string path, std::string value)
{
std::unique_ptr<H5::DataSet> dataset;
DataType H5type = getH5Type(BaseDataType::STR(value.length()));
DataSpace dSpace(H5S_SCALAR);

dataset =
std::make_unique<H5::DataSet>(file->createDataSet(path, H5type, dSpace));
dataset->write(value.c_str(), H5type);
}

HDF5RecordingData* HDF5IO::createDataSet(BaseDataType type,
int sizeX,
int chunkX,
Expand Down
31 changes: 17 additions & 14 deletions src/HDF5IO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ class HDF5IO : public BaseIO
/** Constructor */
HDF5IO();

/** Constructor */
HDF5IO(std::string fileName);

/** Destructor */
~HDF5IO();

/** Returns the full path to the HDF5 file */
std::string getFileName() override;

/** Opens the file for writing */
int open(std::string fileName) override;
/** Opens existing file or creates new file for writing */
int open() override;

/** Opens existing file or creates new file for writing */
int open(bool newfile) override;

/** Closes the file */
void close() override;
Expand Down Expand Up @@ -69,12 +75,12 @@ class HDF5IO : public BaseIO
/** Creates a new group (throws an exception if it exists) */
int createGroup(std::string path) override;

/** Creates the basic file structure upon opening */
int createFileStructure() override;

// /** Returns a pointer to a dataset at a given path*/
HDF5RecordingData* getDataSet(std::string path);

/** Creates a non-modifiable dataset with a string value */
void createStringDataSet(std::string path, std::string value) override;

/** aliases for createDataSet */
HDF5RecordingData* createDataSet(BaseDataType type,
int sizeX,
Expand All @@ -96,21 +102,12 @@ class HDF5IO : public BaseIO
int chunkY,
std::string path);

inline int showError(const char* error)
{
std::cerr << error << std::endl;
return -1;
}

protected:
std::string filename;

/** Creates a new group (ignores if it exists) */
int createGroupIfDoesNotExist(std::string path) override;

/** Opens existing file or creates new file */
int open(bool newfile);

private:
std::unique_ptr<H5::H5File> file;

Expand Down Expand Up @@ -150,3 +147,9 @@ class HDF5RecordingData : public BaseRecordingData
private:
std::unique_ptr<H5::DataSet> dSet;
};

inline int showError(const char* error)
{
std::cerr << error << std::endl;
return -1;
}
Loading

0 comments on commit dfaea2a

Please sign in to comment.