32 #include "splash/core/DCGroup.hpp" 33 #include "splash/basetypes/basetypes.hpp" 34 #include <splash/basetypes/generateCollectionType.hpp> 42 std::string DCGroup::getExceptionString(std::string msg, std::string name)
44 return (std::string(
"Exception for DCGroup [") + name + std::string(
"] ") +
58 H5Handle DCGroup::create(H5Handle base, std::string path)
61 bool mustCreate =
false;
62 H5Handle currentHandle = base;
63 char c_path[path.size() + 1];
64 strcpy(c_path, path.c_str());
66 char *token = strtok(c_path,
"/");
69 if (mustCreate || !H5Lexists(currentHandle, token, H5P_DEFAULT))
71 H5Handle newHandle = H5Gcreate(currentHandle, token, H5P_LINK_CREATE_DEFAULT,
72 H5P_GROUP_CREATE_DEFAULT, H5P_GROUP_ACCESS_DEFAULT);
74 throw DCException(getExceptionString(
"Failed to create group", path));
76 currentHandle = newHandle;
80 currentHandle = H5Gopen(currentHandle, token, H5P_DEFAULT);
81 if (currentHandle < 0)
84 throw DCException(getExceptionString(
"Failed to create group", path));
86 currentHandle = H5Gcreate(currentHandle, token, H5P_LINK_CREATE_DEFAULT,
87 H5P_GROUP_CREATE_DEFAULT, H5P_GROUP_ACCESS_DEFAULT);
88 if (currentHandle < 0)
89 throw DCException(getExceptionString(
"Failed to create group", path));
95 handles.push_back(currentHandle);
96 token = strtok(NULL,
"/");
102 H5Handle DCGroup::open(H5Handle base, std::string path)
106 const bool is_basepath(path == std::string(
"/"));
108 if (checkExistence && !H5Lexists(base, path.c_str(), H5P_DEFAULT) &&
110 throw DCException(getExceptionString(
"Failed to open group", path));
112 newHandle = H5Gopen(base, path.c_str(), H5P_DEFAULT);
115 throw DCException(getExceptionString(
"Failed to open group", path));
117 handles.push_back(newHandle);
121 H5Handle DCGroup::openCreate(H5Handle base, std::string path)
124 if (checkExistence && exists(base, path))
125 return open(base, path);
127 return create(base, path);
130 void DCGroup::close()
133 for (HandlesList::const_reverse_iterator iter = handles.rbegin();
134 iter != handles.rend(); ++iter)
136 if (H5Gclose(*iter) < 0)
137 throw DCException(getExceptionString(
"Failed to close group",
""));
143 bool DCGroup::exists(H5Handle base, std::string path)
145 return (H5Lexists(base, path.c_str(), H5P_DEFAULT) == H5_TRUE);
148 void DCGroup::remove(H5Handle base, std::string path)
151 if (H5Ldelete(base, path.c_str(), H5P_LINK_ACCESS_DEFAULT) < 0)
152 throw DCException(getExceptionString(
"failed to remove group", path));
155 H5Handle DCGroup::getHandle()
157 if (handles.size() > 0)
158 return *(handles.rbegin());
160 return INVALID_HANDLE;
163 void DCGroup::getEntriesInternal(H5Handle base,
const std::string baseGroup,
164 std::string baseName, VisitObjCBType *param)
167 H5G_info_t group_info;
168 H5Gget_info(base, &group_info);
170 for (
size_t i = 0; i < group_info.nlinks; ++i)
172 std::string currentBaseName = baseName;
173 std::string currentEntryName =
"";
176 H5Oget_info_by_idx(base,
".", H5_INDEX_NAME, H5_ITER_INC, i, &obj_info, H5P_DEFAULT);
180 ssize_t len_name = H5Lget_name_by_idx(base,
".", H5_INDEX_NAME,
181 H5_ITER_INC, i, NULL, 0, H5P_LINK_ACCESS_DEFAULT);
182 char *link_name_c =
new char[len_name + 1];
183 H5Lget_name_by_idx(base,
".", H5_INDEX_NAME,
184 H5_ITER_INC, i, link_name_c, len_name + 1, H5P_LINK_ACCESS_DEFAULT);
186 currentEntryName = std::string(link_name_c);
187 if (obj_info.type == H5O_TYPE_GROUP)
189 currentEntryName += std::string(
"/");
191 currentBaseName += currentEntryName;
193 delete[] link_name_c;
196 if (obj_info.type == H5O_TYPE_GROUP)
198 hid_t group_id = H5Oopen_by_idx(base,
".", H5_INDEX_NAME, H5_ITER_INC, i, H5P_DEFAULT);
199 getEntriesInternal(group_id, baseGroup, currentBaseName, param);
203 if (obj_info.type == H5O_TYPE_DATASET)
207 param->entries[param->count].name = currentBaseName;
209 hid_t dataset_id = H5Oopen_by_idx(base,
".", H5_INDEX_NAME, H5_ITER_INC, i, H5P_DEFAULT);
210 hid_t datatype_id = H5Dget_type(dataset_id);
212 H5Dclose(datatype_id);
213 H5Oclose(dataset_id);
CollectionType * generateCollectionType(hid_t datatype_id)