SIONlib  1.7.4
Scalable I/O library for parallel access to task-local files
sion_flags.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2019 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
15 #define _XOPEN_SOURCE 700
16 
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 #include "sion.h"
22 #include "sion_flags.h"
23 #include "sion_internal.h"
24 
25 
26 #define DFUNCTION "_sion_flags_init_entry"
27 void _sion_flags_init_entry(_sion_flags_entry* entry)
28 {
29  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
30 
31  entry->key = NULL;
32  entry->val = NULL;
33  entry->next = NULL;
34 
35  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
36 }
37 #undef DFUNCTION
38 
39 #define DFUNCTION "_sion_flags_create_entry"
40 
47 {
48  _sion_flags_entry* new_entry = NULL;
49 
50  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
51 
52  new_entry = (_sion_flags_entry*)malloc(sizeof (_sion_flags_entry));
53  _sion_flags_init_entry(new_entry);
54 
55  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
56 
57  return new_entry;
58 }
59 #undef DFUNCTION
60 
61 #define DFUNCTION "_sion_flags_destroy_entry"
62 void _sion_flags_destroy_entry(_sion_flags_entry** entry)
63 {
64  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
65 
66  free((*entry)->key);
67  free((*entry)->val);
68  free(*entry);
69  *entry = NULL;
70 
71  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
72 }
73 #undef DFUNCTION
74 
75 #define DFUNCTION "_sion_flags_iter"
76 _sion_flags_entry* _sion_flags_iter(_sion_flags_store* store)
77 {
78  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
79  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
80 
81  return store->root;
82 }
83 #undef DFUNCTION
84 
85 #define DFUNCTION "_sion_flags_init_store"
86 void _sion_flags_init_store(_sion_flags_store* store)
87 {
88  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
89 
90  store->root = _sion_flags_create_entry();
91  store->current = store->root;
92 
93  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
94 }
95 #undef DFUNCTION
96 
97 #define DFUNCTION "_sion_flags_create_store"
98 
105 {
106  _sion_flags_store* new_store = NULL;
107 
108  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
109 
110  new_store = (_sion_flags_store*)malloc(sizeof (_sion_flags_store));
111  _sion_flags_init_store(new_store);
112 
113  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
114 
115  return new_store;
116 }
117 #undef DFUNCTION
118 
119 #define DFUNCTION "_sion_flags_destroy_store"
120 void _sion_flags_destroy_store(_sion_flags_store** store)
121 {
122  _sion_flags_entry* entry = NULL;
123  _sion_flags_entry* next = NULL;
124 
125  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
126 
127  entry = _sion_flags_iter(*store);
128 
129  while (entry != NULL) {
130  next = entry->next;
131  _sion_flags_destroy_entry(&entry);
132  entry = next;
133  }
134 
135  free(*store);
136  *store = NULL;
137 
138  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
139 }
140 #undef DFUNCTION
141 
142 #define DFUNCTION "_sion_flags_add_entry"
143 
154  const char* key,
155  const char* val)
156 {
157  _sion_flags_entry* new_entry = NULL;
158 
159  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
160 
161  /* include terminating null */
162  entry->key = (char*)malloc(strlen(key) + 1);
163  entry->val = (char*)malloc(strlen(val) + 1);
164  new_entry = _sion_flags_create_entry();
165  strcpy(entry->key, key);
166  strcpy(entry->val, val);
167  entry->next = new_entry;
168 
169  DPRINTFP((2, DFUNCTION, -1, "leave (entry->key = %s, entry->val = %s\n",
170  entry->key, entry->val));
171 
172  return new_entry;
173 }
174 #undef DFUNCTION
175 
176 #define DFUNCTION "_sion_flags_add"
177 void _sion_flags_add(_sion_flags_store* store,
178  const char* key,
179  const char* val)
180 {
181  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
182 
183  store->current = _sion_flags_add_entry(store->current, key, val);
184 
185  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
186 }
187 #undef DFUNCTION
188 
189 #define DFUNCTION "_sion_flags_get"
190 _sion_flags_entry* _sion_flags_get(_sion_flags_store* store, const char* key)
191 {
192  _sion_flags_entry* iter = NULL;
193 
194  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
195 
196  for (iter = _sion_flags_iter(store); iter->next != NULL; iter = iter->next) {
197  if (!strcmp(iter->key, key)) {
198  DPRINTFP((2, DFUNCTION, -1, "leave (key in store)\n"));
199  return iter;
200  }
201  }
202 
203  DPRINTFP((2, DFUNCTION, -1, "leave (key not in store)\n"));
204 
205  return NULL;
206 }
207 #undef DFUNCTION
208 
209 #define DFUNCTION "_sion_flags_update_mask"
210 sion_int64 _sion_flags_update_mask(_sion_flags_store* store)
211 {
212  _sion_flags_entry* iter = NULL;
213 
214  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
215 
216  store->mask = 0;
217  store->mask |= _SION_FMODE_ANSI;
218 
219  for (iter = _sion_flags_iter(store); iter->next != NULL; iter = iter->next) {
220  if ((!strcmp(iter->key, "w")) || (!strcmp(iter->key, "wb")) ||
221  (!strcmp(iter->key, "bw"))) {
222  store->mask |= _SION_FMODE_WRITE;
223  }
224  else if ((!strcmp(iter->key, "r")) || (!strcmp(iter->key, "rb")) ||
225  (!strcmp(iter->key, "br"))) {
226  store->mask |= _SION_FMODE_READ;
227  }
228  else if (!strcmp(iter->key, "buffered")) {
229  store->mask |= _SION_FMODE_BUFFERED;
230  }
231  else if (!strcmp(iter->key, "buddy")) {
232  store->mask |= _SION_FMODE_BUDDY;
233  }
234  else if (!strcmp(iter->key, "compress")) {
235  store->mask |= _SION_FMODE_COMPRESS;
236  }
237  else if (!strcmp(iter->key, "collective")) {
238  store->mask |= _SION_FMODE_COLLECTIVE;
239  }
240  else if ((!strcmp(iter->key, "collectivemerge")) ||
241  (!strcmp(iter->key, "cmerge"))) {
242  store->mask |= _SION_FMODE_COLLECTIVE_MERGE;
243  }
244  else if (!strcmp(iter->key, "keyval")) {
245  if ((!strcmp(iter->val, "default")) || (!strcmp(iter->val, "inline")) ||
246  (!strcmp(iter->val, ""))) {
247  store->mask |= _SION_FMODE_KEYVAL_INLINE;
248  }
249  else if (!strcmp(iter->key, "meta")) {
250  store->mask |= _SION_FMODE_KEYVAL_META;
251  }
252  else if (!strcmp(iter->key, "hash")) {
253  store->mask |= _SION_FMODE_KEYVAL_HASH;
254  }
255  else if (!strcmp(iter->key, "none")) {
256  store->mask |= _SION_FMODE_KEYVAL_NONE;
257  }
258  else if (!strcmp(iter->key, "unknown")) {
259  store->mask |= _SION_FMODE_KEYVAL_UNKNOWN;
260  }
261  }
262  else if (!strcmp(iter->key, "endianness")) {
263  store->mask |= _SION_FMODE_ENDIANNESS_SET;
264  if (!strcmp(iter->val, "big")) {
265  store->mask |= _SION_FMODE_ENDIANNESS_BIG;
266  }
267  }
268  else if (!strcmp(iter->key, "posix")) {
269  store->mask |= _SION_FMODE_POSIX;
270  store->mask ^= store->mask & _SION_FMODE_ANSI;
271  }
272  else if (!strcmp(iter->key, "ansi")) {
273  store->mask |= _SION_FMODE_ANSI;
274  store->mask ^= store->mask & _SION_FMODE_POSIX;
275  }
276  }
277 
278  DPRINTFP((2, DFUNCTION, -1, "leave (mask = %llx)\n", store->mask));
279 
280  return store->mask;
281 }
282 #undef DFUNCTION
283 
284 #define DFUNCTION "_sion_parse_flags"
285 
292 {
293  char* tmps = NULL;
294  char* key = NULL;
295  char* val = NULL;
296  char* pch = NULL;
297  char* saveptr = NULL;
298  const char* delim_tok = ",";
299  const char* delim_kv = "=";
300  int span = 0;
301  /* include terminating null */
302  const int len = strlen(flags) + 1;
303  _sion_flags_store* store = NULL;
304 
305  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
306 
307  store = _sion_flags_create_store();
308 
309  tmps = (char*)malloc(len);
310  key = (char*)malloc(len);
311  val = (char*)malloc(len);
312  strcpy(tmps, flags);
313 
314  DPRINTF((32, DFUNCTION, -1, "tmps = '%s'\n", tmps));
315  DPRINTF((32, DFUNCTION, -1, "delim_tok = '%s'\n", delim_tok));
316  DPRINTF((32, DFUNCTION, -1, "delim_kv = '%s'\n", delim_kv));
317  pch = strtok_r(tmps, delim_tok, &saveptr);
318  while (pch != NULL) {
319  span = strcspn(pch, delim_kv);
320  strncpy(key, pch, span);
321  key[span] = '\0';
322  if (span < strlen(pch)) {
323  strcpy(val, &pch[span + 1]);
324  }
325  else {
326  val[0] = '\0';
327  }
328  DPRINTF((32, DFUNCTION, -1, "key = '%s' | val = '%s'\n", key, val));
329  _sion_flags_add(store, key, val);
330  pch = strtok_r(NULL, delim_tok, &saveptr);
331  }
332 
333  free(tmps);
334  free(key);
335  free(val);
336 
337  _sion_flags_update_mask(store);
338 
339  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
340 
341  return store;
342 }
343 #undef DFUNCTION
Definition: sion_flags.h:31
_sion_flags_entry * _sion_flags_create_entry(void)
Create a flags entry.
Definition: sion_flags.c:46
_sion_flags_store * _sion_parse_flags(const char *flags)
Parse flags and return a flags store with key value pairs.
Definition: sion_flags.c:291
_sion_flags_entry * _sion_flags_add_entry(_sion_flags_entry *entry, const char *key, const char *val)
Create a flags entry.
Definition: sion_flags.c:153
_sion_flags_store * _sion_flags_create_store(void)
Create a flags entry.
Definition: sion_flags.c:104