Commit 63a0aff0 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Clean up changes to fix absolute/relative path adjustments.

parent 087b6d38
......@@ -14,9 +14,3 @@ XdmfReader::~XdmfReader()
{
std::cout << "Deleted XdmfReader " << this << std::endl;
}
boost::shared_ptr<XdmfItem> XdmfReader::read(const std::string & fileName) const
{
return XdmfCoreReader::read(fileName);
}
......@@ -17,8 +17,6 @@ public:
XdmfNewMacro(XdmfReader);
virtual ~XdmfReader();
boost::shared_ptr<XdmfItem> read(const std::string & fileName) const;
protected:
XdmfReader();
......
......@@ -580,19 +580,21 @@ void XdmfArray::populateItem(const std::map<std::string, std::string> & itemProp
size_t colonLocation = contentVal.find(":");
if(colonLocation != std::string::npos)
{
size_t fileDir = contentVal.substr(0, colonLocation).find_last_of("/\\");
if(fileDir == std::string::npos)
std::string hdf5Path = contentVal.substr(0, colonLocation);
std::string dataSetPath = contentVal.substr(colonLocation + 1, contentVal.size() - colonLocation - 1);
if(hdf5Path.compare(XdmfObject::getRealPath(hdf5Path)) != 0)
{
std::stringstream newContentVal;
newContentVal << xmlDir->second << "/" << contentVal;
contentVal = newContentVal.str();
// Dealing with a relative path for hdf5 location
std::stringstream newHDF5Path;
newHDF5Path << xmlDir->second << hdf5Path;
hdf5Path = newHDF5Path.str();
}
mHDF5Controller = XdmfHDF5Controller::New(hdf5Path, dataSetPath, sizeVal, arrayType);
}
else
{
assert(false);
}
mHDF5Controller = XdmfHDF5Controller::New(contentVal, sizeVal, arrayType);
}
else if(format->second.compare("XML") == 0)
{
......
......@@ -117,15 +117,11 @@ XdmfCoreReader::~XdmfCoreReader()
boost::shared_ptr<XdmfItem> XdmfCoreReader::read(const std::string & filePath) const
{
std::string xmlDir = "";
size_t index = filePath.find_last_of("/\\");
std::string xmlDir = XdmfObject::getRealPath(filePath);
size_t index = xmlDir.find_last_of("/\\");
if(index != std::string::npos)
{
xmlDir = filePath.substr(0, index);
}
else
{
xmlDir = XdmfObject::getCWD();
xmlDir = xmlDir.substr(0, index + 1);
}
const xmlDocPtr document = xmlReadFile(filePath.c_str(), NULL, 0);
......
......@@ -23,10 +23,10 @@ public:
/**
* Read an Xdmf file from disk into memory.
*
* @param fileName the path on disk to the Xdmf file to read in.
* @param filePath the path on disk to the Xdmf file to read in.
* @return an XdmfItem at the root of the Xdmf tree.
*/
virtual boost::shared_ptr<XdmfItem> read(const std::string & fileName) const;
virtual boost::shared_ptr<XdmfItem> read(const std::string & filePath) const;
protected:
......
......@@ -7,44 +7,21 @@
#include "XdmfArrayType.hpp"
#include "XdmfHDF5Controller.hpp"
XdmfHDF5Controller::XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type) :
mSize(size),
mType(type)
XdmfHDF5Controller::XdmfHDF5Controller(const std::string & hdf5FilePath, const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type) :
mDataSetPath(dataSetPath),
mSize(size),
mType(type)
{
size_t colonLocation = dataSetPath.find(":");
if(colonLocation != std::string::npos)
{
mFilePath = dataSetPath.substr(0, colonLocation);
if(colonLocation + 1 != mFilePath.size())
{
mDataSetName = dataSetPath.substr(colonLocation + 1, dataSetPath.size());
}
else
{
assert(false);
}
mFilePath = XdmfObject::getRealPath(mFilePath);
}
else
{
assert(false);
}
mFilePath = XdmfObject::getRealPath(hdf5FilePath);
}
XdmfHDF5Controller::~XdmfHDF5Controller()
{
}
std::string XdmfHDF5Controller::getDataSetName() const
{
return mDataSetName;
}
std::string XdmfHDF5Controller::getDataSetPath() const
{
std::stringstream toReturn;
toReturn << mFilePath << ":" << mDataSetName;
return toReturn.str();
return mDataSetPath;
}
std::string XdmfHDF5Controller::getFilePath() const
......@@ -65,7 +42,7 @@ boost::shared_ptr<const XdmfArrayType> XdmfHDF5Controller::getType() const
void XdmfHDF5Controller::read(XdmfArray * const array)
{
hid_t hdf5Handle = H5Fopen(mFilePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
hid_t dataset = H5Dopen(hdf5Handle, mDataSetName.c_str(), H5P_DEFAULT);
hid_t dataset = H5Dopen(hdf5Handle, mDataSetPath.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset);
hssize_t numVals = H5Sget_simple_extent_npoints(dataspace);
hid_t datatype = H5Dget_type(dataset);
......
......@@ -24,23 +24,16 @@ public:
/**
* Create a new controller for an hdf5 data set on disk.
*/
static boost::shared_ptr<XdmfHDF5Controller> New(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type)
static boost::shared_ptr<XdmfHDF5Controller> New(const std::string & hdf5FilePath, const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type)
{
boost::shared_ptr<XdmfHDF5Controller> p(new XdmfHDF5Controller(dataSetPath, size, type));
boost::shared_ptr<XdmfHDF5Controller> p(new XdmfHDF5Controller(hdf5FilePath, dataSetPath, size, type));
return p;
}
/**
* Get the name of the data set owned by this controller. For "/home/output.h5:/foo/data" this is "/foo/data"
* Get the path of the data set within the hdf5 file owned by this controller. For "/home/output.h5:/foo/data" this is "/foo/data"
*
* @return a std::string containing the name of the data set.
*/
std::string getDataSetName() const;
/**
* Get the absolute path to the hdf5 data set on disk owned by this controller. For "/home/output.h5:/foo/data" this is "/home/output.h5:/foo/data"
*
* @return a std::string to the hdf5 data set location on disk.
* @return a std::string containing the path of the data set.
*/
std::string getDataSetPath() const;
......@@ -74,14 +67,14 @@ public:
protected:
XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type);
XdmfHDF5Controller(const std::string & hdf5FilePath, const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type);
private:
XdmfHDF5Controller(const XdmfHDF5Controller & hdf5Controller); // Not implemented.
void operator=(const XdmfHDF5Controller & hdf5Controller); // Not implemented.
std::string mDataSetName;
std::string mDataSetPath;
std::string mFilePath;
unsigned int mSize;
boost::shared_ptr<const XdmfArrayType> mType;
......
......@@ -136,17 +136,17 @@ void XdmfHDF5Writer::visit(XdmfArray & array, const boost::shared_ptr<XdmfBaseVi
if(datatype != -1)
{
std::string hdf5FilePath = mImpl->mFilePath;
std::stringstream dataSetName;
std::stringstream dataSetPath;
if((mImpl->mMode == Overwrite || mImpl->mMode == Append) && array.mHDF5Controller)
{
// Write to the previous dataset
dataSetName << array.mHDF5Controller->getDataSetName();
dataSetPath << array.mHDF5Controller->getDataSetPath();
hdf5FilePath = array.mHDF5Controller->getFilePath();
}
else
{
dataSetName << "Data" << mImpl->mDataSetId;
dataSetPath << "Data" << mImpl->mDataSetId;
}
// Open a hdf5 dataset and write to it on disk.
......@@ -168,7 +168,7 @@ void XdmfHDF5Writer::visit(XdmfArray & array, const boost::shared_ptr<XdmfBaseVi
{
hdf5Handle = H5Fcreate(hdf5FilePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
}
hid_t dataset = H5Dopen(hdf5Handle, dataSetName.str().c_str(), H5P_DEFAULT);
hid_t dataset = H5Dopen(hdf5Handle, dataSetPath.str().c_str(), H5P_DEFAULT);
hid_t dataspace = H5S_ALL;
hid_t memspace = H5S_ALL;
......@@ -180,7 +180,7 @@ void XdmfHDF5Writer::visit(XdmfArray & array, const boost::shared_ptr<XdmfBaseVi
hid_t property = H5Pcreate(H5P_DATASET_CREATE);
hsize_t chunkSize = 1024;
status = H5Pset_chunk(property, 1, &chunkSize);
dataset = H5Dcreate(hdf5Handle, dataSetName.str().c_str(), datatype, memspace, H5P_DEFAULT, property, H5P_DEFAULT);
dataset = H5Dcreate(hdf5Handle, dataSetPath.str().c_str(), datatype, memspace, H5P_DEFAULT, property, H5P_DEFAULT);
status = H5Pclose(property);
}
else
......@@ -223,14 +223,10 @@ void XdmfHDF5Writer::visit(XdmfArray & array, const boost::shared_ptr<XdmfBaseVi
// Restore previous error handler
H5Eset_auto2(0, old_func, old_client_data);
std::stringstream writtenDataSet;
writtenDataSet << hdf5FilePath << ":" << dataSetName.str();
// Attach a new controller to the array if needed.
if(mImpl->mMode == Default || !array.mHDF5Controller)
{
boost::shared_ptr<XdmfHDF5Controller> newDataSetController = XdmfHDF5Controller::New(writtenDataSet.str(),
array.getSize(), array.getType());
boost::shared_ptr<XdmfHDF5Controller> newDataSetController = XdmfHDF5Controller::New(hdf5FilePath, dataSetPath.str(), array.getSize(), array.getType());
array.setHDF5Controller(newDataSetController);
mImpl->mDataSetId++;
}
......
......@@ -15,13 +15,6 @@ XdmfObject::~XdmfObject()
{
}
std::string XdmfObject::getCWD()
{
char buffer[256];
getcwd(buffer, 256);
return buffer;
}
std::string XdmfObject::getRealPath(const std::string & path)
{
char realPath[PATH_MAX];
......
......@@ -23,13 +23,6 @@ public:
virtual ~XdmfObject();
/**
* Gets the current working directory Xdmf is running in. This is useful for reading and writing files containing relative paths to hdf5 datasets.
*
* @return the current working directory.
*/
static std::string getCWD();
/**
* Converts a filesystem path to an absolute real path (absolute path with no symlinks)
*
......
......@@ -138,34 +138,19 @@ void XdmfWriter::visit(XdmfArray & array, const boost::shared_ptr<XdmfBaseVisito
if(array.getHDF5Controller() || array.getSize() > mImpl->mLightDataLimit)
{
mImpl->mHDF5Writer->visit(array, mImpl->mHDF5Writer);
std::string contentVal = array.getHDF5Controller()->getDataSetPath();
if(size_t colonLocation = contentVal.find(":") != std::string::npos)
std::string hdf5Path = array.getHDF5Controller()->getFilePath();
size_t index = hdf5Path.find_last_of("/\\");
if(index != std::string::npos)
{
if(size_t fileDir = contentVal.substr(0, colonLocation).find_last_of("/\\") != std::string::npos)
{
// Absolute Path
std::string cwd = XdmfObject::getCWD();
if(size_t relPathBegin = contentVal.find(cwd) != std::string::npos)
{
// Substitute Relative Path
xmlTextValues << contentVal.substr(cwd.size() + 1, contentVal.size() - cwd.size());
}
else
{
// Write Absolute Path
xmlTextValues << contentVal;
}
}
else
std::string hdf5Dir = hdf5Path.substr(0, index + 1);
if(mImpl->mXMLFilePath.find(hdf5Dir) == 0)
{
// Relative Path
xmlTextValues << contentVal;
hdf5Path = hdf5Path.substr(hdf5Dir.size(), hdf5Path.size() - hdf5Dir.size());
}
}
else
{
assert(false);
}
xmlTextValues << hdf5Path << ":" << array.getHDF5Controller()->getDataSetPath();
}
else
{
......
......@@ -4,16 +4,9 @@
int main(int argc, char* argv[])
{
boost::shared_ptr<XdmfHDF5Controller> controller = XdmfHDF5Controller::New("output.h5:/foo/data1", 10, XdmfArrayType::Int8());
assert(controller->getDataSetName().compare("/foo/data1") == 0);
std::string realPath = XdmfObject::getRealPath("output.h5");
std::stringstream realDataSetPath;
realDataSetPath << realPath << ":" << "/foo/data1";
assert(controller->getDataSetPath().compare(realDataSetPath.str()) == 0);
assert(controller->getFilePath().compare(realPath) == 0);
boost::shared_ptr<XdmfHDF5Controller> controller = XdmfHDF5Controller::New("output.h5", "/foo/data1", 10, XdmfArrayType::Int8());
assert(controller->getDataSetPath().compare("/foo/data1") == 0);
assert(controller->getFilePath().compare(XdmfObject::getRealPath("output.h5")) == 0);
assert(controller->getSize() == 10);
assert(controller->getType() == XdmfArrayType::Int8());
return 0;
......
......@@ -9,7 +9,6 @@ int main(int argc, char* argv[])
std::string realPath = XdmfObject::getRealPath("output.xmf");
std::cout << realPath << " " << writer->getFilePath() << std::endl;
assert(writer->getFilePath().compare(realPath) == 0);
writer->setLightDataLimit(10);
assert(writer->getLightDataLimit() == 10);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment