FiberBundleHDF5  FiberHDF5 Documentation, Revision 2026
High-Performance Fiber Bundle Data Model for Scientific Visualization
Loading...
Searching...
No Matches
F5contenttype.c
Go to the documentation of this file.
1/*
2 *
3 * $Id: F5contenttype.c,v 1.9 2007/10/04 16:37:50 werner Exp $
4 *
5 */
6
7#include "F5private.h"
8#include "F5C.h"
9#include "F5X.h"
10#include <assert.h>
11#include <string.h>
12#include <stdlib.h>
13#include <ctype.h>
14
15/* Author: SteffenProhaska */
16int F5Cset (F5Path* fpath, const char* fieldname, const char* content_type) {
17 int ok = 1;
18 const int len = strlen (content_type);
19 hid_t attr_type = -1;
20 hid_t attr_space = -1;
21 hid_t attr_id = -1;
22 hid_t attr_size = 0;
23 hid_t FieldTable = -1;
24 hid_t FieldInfo = -1;
25
27 ok = 0;
28 }
29
30 if (ok && ((attr_type = H5Tcopy (H5T_C_S1)) < 0)) {
31 ok = 0;
32 }
33
34 attr_size = len + 1;
35
36 if (ok && (H5Tset_size (attr_type, (size_t)attr_size) < 0 )) {
37 ok = 0;
38 }
39
40 if (ok && (H5Tset_strpad (attr_type, H5T_STR_NULLTERM) < 0 )) {
41 ok = 0;
42 }
43
44 if (ok && (attr_space = H5Screate (H5S_SCALAR)) < 0) {
45 ok = 0;
46 }
47
48 /* Creating Field content_type information per Grid */
49 if (ok && (FieldTable = F5Gappend (fpath->Grid_hid, FIBER_FIELDS_TAG)) < 0)
50 {
51 ok = 0;
52 }
53
54 if (ok && (FieldInfo = F5Gappend (FieldTable, fieldname)) < 0)
55 {
56 ok = 0;
57 }
58
59 if (ok) {
60 enum { attr_name_bufsize = 20 };
61 char attr_name[attr_name_bufsize];
62 const int attnum = H5Aget_num_attrs (FieldInfo);
63
64 assert (attr_name_bufsize > (strlen (FIBER_CONTENT_TYPE_TAG) + 1 + 3));
65
66 sprintf (attr_name, "%s%03d", FIBER_CONTENT_TYPE_TAG, attnum);
67 if ((attr_id = H5Acreate2(FieldInfo, attr_name, attr_type, attr_space, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
68 ok = 0;
69 }
70 }
71
72 if (ok && H5Awrite (attr_id, attr_type, content_type) < 0) {
73 ok = 0;
74 }
75
76 if (attr_id >= 0) {
77 H5Aclose (attr_id);
78 }
79 if (FieldInfo >= 0) {
80 H5Gclose(FieldInfo);
81 }
82 if (FieldTable >= 0) {
83 H5Gclose(FieldTable);
84 }
85 if (attr_space >= 0) {
86 H5Sclose (attr_space);
87 }
88 if (attr_type >= 0) {
89 H5Tclose (attr_type);
90 }
91
92 if (!ok) {
93 return -1;
94 }
95 return 1;
96}
97
98/* Author: SteffenProhaska
99
100 todo:
101 - separate into a function which only assembles all objects
102 - ignore objects starting with '_' (to be discussed)
103 - use the new function here
104 todo:
105 - separate DArray stuff
106 */
107int F5Cget_fields (F5Path* fpath, size_t* count, char*** names) {
108 int ok = 1;
109 char* buffer = 0;
110 hid_t FieldTable = -1;
111 hsize_t num_objs = 0;
112 int bufsize = 0;
113 int nextidx = 0;
114 int i;
115
116 if (ok && (FieldTable = F5Gappend (fpath->Grid_hid, FIBER_FIELDS_TAG)) < 0)
117 {
118 ok = 0;
119 }
120 if (ok && (H5Gget_num_objs (FieldTable, &num_objs)) < 0)
121 {
122 ok = 0;
123 }
124 /* allocate a buffer which will take all the strings and the array of pointers to them */
125 assert (sizeof (int) <= sizeof (void*));
126
127 bufsize = num_objs * (sizeof (void*) + 20);
128 nextidx = num_objs * sizeof(void*);
129 if (ok && num_objs > 0 && (buffer = (char*)malloc (bufsize)) == 0) {
130 ok = 0;
131 }
132 for (i = 0; i < num_objs; i++) {
133 const int len = H5Gget_objname_by_idx (FieldTable, i, 0, 0);
134 /* realloc until we have enough space */
135 while (nextidx + len + 1 >= bufsize) {
136 void* newbuf = 0;
137 bufsize = (int)((float)bufsize * 1.2);
138 newbuf = realloc (buffer, bufsize);
139 if (!newbuf) {
140 ok = 0;
141 break;
142 }
143 buffer = (char*)newbuf;
144 }
145 /* todo understand the len stuff perhaps + 1 is not needed */
146 if (H5Gget_objname_by_idx (FieldTable, i, buffer + nextidx, len + 1) <= 0) {
147 ok = 0;
148 break;
149 };
150 ((int*)buffer)[i] = nextidx;
151 nextidx += len + 1;
152 }
153 /* now fill in the array of pointers to the strings */
154 if (ok) {
155 for (i = num_objs - 1; i >= 0; i--) {
156 const int index = ((int*)buffer)[i];
157 ((char**)buffer)[i] = buffer + index;
158 }
159 }
160 if (FieldTable >= 0) {
161 H5Gclose(FieldTable);
162 }
163 if (!ok) {
164 free (buffer);
165 return -1;
166 }
167 *count = num_objs;
168 *names = (char**)buffer;
169 return 1;
170}
171
172/* Author: SteffenProhaska
173
174 Todo:
175 - separate into a function which gets all string attributes
176 - check attribute type
177 - use this new function here
178 */
179int F5Cget_content_types (F5Path* fpath, const char* fieldname, size_t* count, char*** names) {
180 int ok = 1;
181 char* buffer = 0;
182 hid_t FieldTable = -1;
183 hid_t FieldInfo = -1;
184 hid_t attr_id = -1;
185 hid_t type_id = -1;
186 hsize_t num_attrs = 0;
187 int bufsize = 0;
188 int nextidx = 0;
189 int i;
190
191 if (ok && (FieldTable = F5Gappend (fpath->Grid_hid, FIBER_FIELDS_TAG)) < 0)
192 {
193 ok = 0;
194 }
195 if (ok && (FieldInfo = F5Gappend (FieldTable, fieldname)) < 0)
196 {
197 ok = 0;
198 }
199
200 if (ok) {
201 num_attrs = H5Aget_num_attrs (FieldInfo);
202 }
203 /* allocate a buffer which will take all the strings and the array of pointers to them */
204 assert (sizeof (int) <= sizeof (void*));
205
206 bufsize = num_attrs * (sizeof (void*) + 50);
207 nextidx = num_attrs * sizeof(void*);
208 if (ok && num_attrs > 0 && (buffer = (char*)malloc (bufsize)) == 0) {
209 ok = 0;
210 }
211 for (i = 0; i < num_attrs; i++) {
212 if ((attr_id = H5Aopen_idx (FieldInfo, i)) < 0) {
213 ok = 0;
214 break;
215 };
216 if ((type_id = H5Aget_type (attr_id)) < 0) {
217 ok = 0;
218 break;
219 }
220 {
221 size_t size = H5Tget_size (type_id);
222
223 /* realloc until we have enough space */
224 while (nextidx + size >= bufsize) {
225 void* newbuf = 0;
226 bufsize = (int)((float)bufsize * 1.2);
227 newbuf = realloc (buffer, bufsize);
228 if (!newbuf) {
229 ok = 0;
230 break;
231 }
232 buffer = (char*)newbuf;
233 }
234 if (H5Aread (attr_id, type_id, buffer + nextidx) < 0) {
235 ok = 0;
236 break;
237 };
238 ((int*)buffer)[i] = nextidx;
239 nextidx += size;
240 }
241 H5Tclose (type_id);
242 type_id = -1;
243 H5Aclose (attr_id);
244 attr_id = -1;
245 }
246 /* now fill in the array of pointers to the strings */
247 if (ok) {
248 for (i = num_attrs - 1; i >= 0; i--) {
249 const int index = ((int*)buffer)[i];
250 ((char**)buffer)[i] = buffer + index;
251 }
252 }
253 if (type_id >= 0) {
254 H5Tclose (type_id);
255 }
256 if (attr_id >= 0) {
257 H5Aclose (attr_id);
258 }
259 if (FieldTable >= 0) {
260 H5Gclose(FieldTable);
261 }
262 if (FieldInfo >= 0) {
263 H5Gclose(FieldInfo);
264 }
265 if (!ok) {
266 free (buffer);
267 return -1;
268 }
269 *count = num_attrs;
270 *names = (char**)buffer;
271 return 1;
272}
273
274int F5Cparse_version_string (const char* version_string, char* str, size_t size
275 , int* major, int* minor, int* patch, const char** remainder) {
276 const char* run = version_string;
277 int stridx = 0;
278 int matches = 0;
279 int numbers[3];
280 numbers[0] = 0;
281 numbers[1] = 0;
282 numbers[2] = 0;
283
284 while (*run && *run != '-' && *run != '/') {
285 stridx++;
286 run++;
287 }
288 if (*run != '-') {
289 return -1;
290 }
291 run++;
292
293 while (matches < 3) {
294 if (!isdigit (*run)) {
295 return -1;
296 }
297 numbers[matches] = atoi (run);
298 matches++;
299 while (isdigit (*run)) {
300 run++;
301 }
302 if (*run == '/' || *run == '\0') {
303 /* set version numbers */
304 *major = numbers[0];
305 *minor = numbers[1];
306 *patch = numbers[2];
307 /* copy name */
308 {
309 size_t num = stridx;
310 if (num > size - 1) {
311 num = size - 1;
312 }
313 if (num > 0) {
314 memcpy (str, version_string, num);
315 str[num] = '\0';
316 } else if (size > 0) {
317 str[0] = '\0';
318 }
319 }
320 if (remainder) {
321 if (*run == '/') {
322 *remainder = run + 1;
323 } else {
324 *remainder = run;
325 }
326 }
327 return 1;
328 }
329 if (*run != '.') {
330 return -1;
331 }
332 run++;
333 }
334 return -1;
335}
336
337/* A list of strings a valid F5 url might start with.
338 * Without the F5-MAJOR.MINOR.PATCH part
339 *
340 * You should only add to this list and never remove
341 * a string. Otherwise it would break backward
342 * compatibility.
343 *
344 */
345static const char* valid_f5_urls[] = {
346 "http://www.fiberbundle.net/"
347 , 0
348};
349
350int F5Cget_version (const char* content_type, int* major, int* minor, int* patch, const char** remainder) {
351 const char** url_starts;
352 for (url_starts = valid_f5_urls; *url_starts; url_starts++) {
353 const char* start = *url_starts;
354 const int len = strlen (start);
355
356 if (strncmp (content_type, start, len) == 0) {
357 int maj, min, pat;
358 char buf[20];
359 int err;
360 const char* rem;
361 err = F5Cparse_version_string (content_type + len, buf, 20, &maj, &min, &pat, &rem);
362 if (err > 0 && strcmp ("F5", buf) == 0) {
363 *major = maj;
364 *minor = min;
365 *patch = pat;
366 if (remainder) {
367 *remainder = rem;
368 }
369 return 1;
370 }
371 }
372 }
373 return -1;
374}
375
376int F5Cget_subspec (const char* content_type, char* str, size_t size
377 , int* major, int* minor, int* patch
378 , const char** remainder) {
379 int maj, min, pat;
380 int err;
381 const char* rem;
382 err = F5Cget_version (content_type, &maj, &min, &pat, &rem);
383 if (err >= 0) {
384 err = F5Cparse_version_string (rem, str, size, major, minor, patch, remainder);
385 }
386 return err;
387}
return ok
Definition F5P.c:95
H5Tclose(type_id)
free(name)
int F5Cget_subspec(const char *content_type, char *str, size_t size, int *major, int *minor, int *patch, const char **remainder)
int F5Cget_content_types(F5Path *fpath, const char *fieldname, size_t *count, char ***names)
int F5Cset(F5Path *fpath, const char *fieldname, const char *content_type)
int F5Cget_version(const char *content_type, int *major, int *minor, int *patch, const char **remainder)
int F5Cparse_version_string(const char *version_string, char *str, size_t size, int *major, int *minor, int *patch, const char **remainder)
int F5Cget_fields(F5Path *fpath, size_t *count, char ***names)
#define FIBER_FIELDS_TAG
Definition F5defs.h:27
#define FIBER_CONTENT_TYPE_TAG
Definition F5defs.h:28
#define FIBER_CONTENT_TYPE_MAXTAGLEN
Definition F5defs.h:29
#define H5Gclose(x)
Definition F5X.h:144
hid_t F5Gappend(hid_t loc_id, const char *name)
Definition F5X.c:59
hid_t Grid_hid
Definition F5Path.h:53