mirror of
				https://github.com/aportelli/LatAnalyze.git
				synced 2025-11-04 00:04:31 +00:00 
			
		
		
		
	Hdf5File: Added some code to sanitize group names
Currently you can pass in relative paths with slashes in, and this upsets HDF5's createGroup function.
This commit is contained in:
		@@ -63,7 +63,7 @@ void Hdf5File::save(const DMat &m, const string &name)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    DataSpace dataSpace(2, dim), attrSpace(1, &attrDim);
 | 
					    DataSpace dataSpace(2, dim), attrSpace(1, &attrDim);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    group = h5File_->createGroup(name.c_str());
 | 
					    group = h5File_->createGroup(name.c_str() + nameOffset(name));
 | 
				
			||||||
    attr  = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace);
 | 
					    attr  = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace);
 | 
				
			||||||
    attr.write(PredType::NATIVE_SHORT, &dMatType);
 | 
					    attr.write(PredType::NATIVE_SHORT, &dMatType);
 | 
				
			||||||
    dataset = group.createDataSet("data", PredType::NATIVE_DOUBLE, dataSpace);
 | 
					    dataset = group.createDataSet("data", PredType::NATIVE_DOUBLE, dataSpace);
 | 
				
			||||||
@@ -82,7 +82,7 @@ void Hdf5File::save(const DMatSample &sample, const string &name)
 | 
				
			|||||||
    const long int nSample = sample.size();
 | 
					    const long int nSample = sample.size();
 | 
				
			||||||
    string         datasetName;
 | 
					    string         datasetName;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    group = h5File_->createGroup(name.c_str());
 | 
					    group = h5File_->createGroup(name.c_str() + nameOffset(name));
 | 
				
			||||||
    attr  = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace);
 | 
					    attr  = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace);
 | 
				
			||||||
    attr.write(PredType::NATIVE_SHORT, &dMatSampleType);
 | 
					    attr.write(PredType::NATIVE_SHORT, &dMatSampleType);
 | 
				
			||||||
    attr  = group.createAttribute("nSample", PredType::NATIVE_LONG, attrSpace);
 | 
					    attr  = group.createAttribute("nSample", PredType::NATIVE_LONG, attrSpace);
 | 
				
			||||||
@@ -105,7 +105,7 @@ void Hdf5File::save(const RandGenState &state, const string &name)
 | 
				
			|||||||
    hsize_t   attrDim = 1;
 | 
					    hsize_t   attrDim = 1;
 | 
				
			||||||
    DataSpace dataSpace(1, &dim), attrSpace(1, &attrDim);
 | 
					    DataSpace dataSpace(1, &dim), attrSpace(1, &attrDim);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    group = h5File_->createGroup(name.c_str());
 | 
					    group = h5File_->createGroup(name.c_str() + nameOffset(name));
 | 
				
			||||||
    attr  = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace);
 | 
					    attr  = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace);
 | 
				
			||||||
    attr.write(PredType::NATIVE_SHORT, &rgStateType);
 | 
					    attr.write(PredType::NATIVE_SHORT, &rgStateType);
 | 
				
			||||||
    dataset = group.createDataSet("data", PredType::NATIVE_DOUBLE, dataSpace);
 | 
					    dataset = group.createDataSet("data", PredType::NATIVE_DOUBLE, dataSpace);
 | 
				
			||||||
@@ -314,3 +314,18 @@ string Hdf5File::load(const string &name)
 | 
				
			|||||||
        return "";
 | 
					        return "";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					size_t Hdf5File::nameOffset(const string& name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    size_t ret = 0;
 | 
				
			||||||
 | 
					    string badChars = "/";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (auto c : badChars) {
 | 
				
			||||||
 | 
					        size_t pos = name.rfind(c);
 | 
				
			||||||
 | 
					        if (pos != string::npos and pos > ret) {
 | 
				
			||||||
 | 
					            ret = pos;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return ret;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -62,6 +62,8 @@ private:
 | 
				
			|||||||
                   void load(DMat &m, const H5NS::DataSet &d);
 | 
					                   void load(DMat &m, const H5NS::DataSet &d);
 | 
				
			||||||
                   void load(DMatSample &s, const H5NS::DataSet &d);
 | 
					                   void load(DMatSample &s, const H5NS::DataSet &d);
 | 
				
			||||||
                   void load(RandGenState &state, const H5NS::DataSet &d);
 | 
					                   void load(RandGenState &state, const H5NS::DataSet &d);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    static size_t nameOffset(const std::string& name);
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    // file name
 | 
					    // file name
 | 
				
			||||||
    std::unique_ptr<H5NS::H5File> h5File_{nullptr};
 | 
					    std::unique_ptr<H5NS::H5File> h5File_{nullptr};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user