SIONlib  1.6.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-2015 **
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, "compress")) {
229  store->mask |= _SION_FMODE_COMPRESS;
230  }
231  else if (!strcmp(iter->key, "collective")) {
232  store->mask |= _SION_FMODE_COLLECTIVE;
233  }
234  else if ((!strcmp(iter->key, "collectivemerge")) ||
235  (!strcmp(iter->key, "cmerge"))) {
236  store->mask |= _SION_FMODE_COLLECTIVE_MERGE;
237  }
238  else if (!strcmp(iter->key, "keyval")) {
239  if ((!strcmp(iter->val, "default")) || (!strcmp(iter->val, "inline")) ||
240  (!strcmp(iter->val, ""))) {
241  store->mask |= _SION_FMODE_KEYVAL_INLINE;
242  }
243  else if (!strcmp(iter->key, "meta")) {
244  store->mask |= _SION_FMODE_KEYVAL_META;
245  }
246  else if (!strcmp(iter->key, "hash")) {
247  store->mask |= _SION_FMODE_KEYVAL_HASH;
248  }
249  else if (!strcmp(iter->key, "none")) {
250  store->mask |= _SION_FMODE_KEYVAL_NONE;
251  }
252  else if (!strcmp(iter->key, "unknown")) {
253  store->mask |= _SION_FMODE_KEYVAL_UNKNOWN;
254  }
255  }
256  else if (!strcmp(iter->key, "endianness")) {
257  store->mask |= _SION_FMODE_ENDIANNESS_SET;
258  if (!strcmp(iter->val, "big")) {
259  store->mask |= _SION_FMODE_ENDIANNESS_BIG;
260  }
261  }
262  else if (!strcmp(iter->key, "posix")) {
263  store->mask |= _SION_FMODE_POSIX;
264  store->mask ^= store->mask & _SION_FMODE_ANSI;
265  }
266  else if (!strcmp(iter->key, "ansi")) {
267  store->mask |= _SION_FMODE_ANSI;
268  store->mask ^= store->mask & _SION_FMODE_POSIX;
269  }
270  }
271 
272  DPRINTFP((2, DFUNCTION, -1, "leave (mask = %llx)\n", store->mask));
273 
274  return store->mask;
275 }
276 #undef DFUNCTION
277 
278 #define DFUNCTION "_sion_parse_flags"
279 
286 {
287  char* tmps = NULL;
288  char* key = NULL;
289  char* val = NULL;
290  char* pch = NULL;
291  char* saveptr = NULL;
292  const char* delim_tok = ",";
293  const char* delim_kv = "=";
294  int span = 0;
295  /* include terminating null */
296  const int len = strlen(flags) + 1;
297  _sion_flags_store* store = NULL;
298 
299  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
300 
301  store = _sion_flags_create_store();
302 
303  tmps = (char*)malloc(len);
304  key = (char*)malloc(len);
305  val = (char*)malloc(len);
306  strcpy(tmps, flags);
307 
308  DPRINTF((32, DFUNCTION, -1, "tmps = '%s'\n", tmps));
309  DPRINTF((32, DFUNCTION, -1, "delim_tok = '%s'\n", delim_tok));
310  DPRINTF((32, DFUNCTION, -1, "delim_kv = '%s'\n", delim_kv));
311  pch = strtok_r(tmps, delim_tok, &saveptr);
312  while (pch != NULL) {
313  span = strcspn(pch, delim_kv);
314  strncpy(key, pch, span);
315  key[span] = '\0';
316  if (span < strlen(pch)) {
317  strcpy(val, &pch[span + 1]);
318  }
319  else {
320  val[0] = '\0';
321  }
322  DPRINTF((32, DFUNCTION, -1, "key = '%s' | val = '%s'\n", key, val));
323  _sion_flags_add(store, key, val);
324  pch = strtok_r(NULL, delim_tok, &saveptr);
325  }
326 
327  free(tmps);
328  free(key);
329  free(val);
330 
331  _sion_flags_update_mask(store);
332 
333  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
334 
335  return store;
336 }
337 #undef DFUNCTION
Definition: sion_flags.h:23
_sion_flags_store * _sion_parse_flags(const char *flags)
Parse flags and return a flags store with key value pairs.
Definition: sion_flags.c:285
_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