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