SIONlib  1.7.7
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 ** Copyright (c) 2019 **
8 ** DataDirect Networks **
9 ** **
10 ** See the file COPYRIGHT in the package base directory for details **
11 ****************************************************************************/
18 #define _XOPEN_SOURCE 700
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "sion.h"
25 #include "sion_flags.h"
26 #include "sion_internal.h"
27 
28 
29 #define DFUNCTION "_sion_flags_init_entry"
30 void _sion_flags_init_entry(_sion_flags_entry* entry)
31 {
32  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
33 
34  entry->key = NULL;
35  entry->val = NULL;
36  entry->next = NULL;
37 
38  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
39 }
40 #undef DFUNCTION
41 
42 #define DFUNCTION "_sion_flags_create_entry"
50 {
51  _sion_flags_entry* new_entry = NULL;
52 
53  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
54 
55  new_entry = (_sion_flags_entry*)malloc(sizeof (_sion_flags_entry));
56  _sion_flags_init_entry(new_entry);
57 
58  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
59 
60  return new_entry;
61 }
62 #undef DFUNCTION
63 
64 #define DFUNCTION "_sion_flags_destroy_entry"
65 void _sion_flags_destroy_entry(_sion_flags_entry** entry)
66 {
67  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
68 
69  free((*entry)->key);
70  free((*entry)->val);
71  free(*entry);
72  *entry = NULL;
73 
74  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
75 }
76 #undef DFUNCTION
77 
78 #define DFUNCTION "_sion_flags_iter"
79 _sion_flags_entry* _sion_flags_iter(_sion_flags_store* store)
80 {
81  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
82  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
83 
84  return store->root;
85 }
86 #undef DFUNCTION
87 
88 #define DFUNCTION "_sion_flags_init_store"
89 void _sion_flags_init_store(_sion_flags_store* store)
90 {
91  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
92 
93  store->root = _sion_flags_create_entry();
94  store->current = store->root;
95 
96  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
97 }
98 #undef DFUNCTION
99 
100 #define DFUNCTION "_sion_flags_create_store"
108 {
109  _sion_flags_store* new_store = NULL;
110 
111  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
112 
113  new_store = (_sion_flags_store*)malloc(sizeof (_sion_flags_store));
114  _sion_flags_init_store(new_store);
115 
116  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
117 
118  return new_store;
119 }
120 #undef DFUNCTION
121 
122 #define DFUNCTION "_sion_flags_destroy_store"
123 void _sion_flags_destroy_store(_sion_flags_store** store)
124 {
125  _sion_flags_entry* entry = NULL;
126  _sion_flags_entry* next = NULL;
127 
128  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
129 
130  entry = _sion_flags_iter(*store);
131 
132  while (entry != NULL) {
133  next = entry->next;
134  _sion_flags_destroy_entry(&entry);
135  entry = next;
136  }
137 
138  free(*store);
139  *store = NULL;
140 
141  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
142 }
143 #undef DFUNCTION
144 
145 #define DFUNCTION "_sion_flags_add_entry"
157  const char* key,
158  const char* val)
159 {
160  _sion_flags_entry* new_entry = NULL;
161 
162  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
163 
164  /* include terminating null */
165  entry->key = (char*)malloc(strlen(key) + 1);
166  entry->val = (char*)malloc(strlen(val) + 1);
167  new_entry = _sion_flags_create_entry();
168  strcpy(entry->key, key);
169  strcpy(entry->val, val);
170  entry->next = new_entry;
171 
172  DPRINTFP((2, DFUNCTION, -1, "leave (entry->key = %s, entry->val = %s\n",
173  entry->key, entry->val));
174 
175  return new_entry;
176 }
177 #undef DFUNCTION
178 
179 #define DFUNCTION "_sion_flags_add"
180 void _sion_flags_add(_sion_flags_store* store,
181  const char* key,
182  const char* val)
183 {
184  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
185 
186  store->current = _sion_flags_add_entry(store->current, key, val);
187 
188  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
189 }
190 #undef DFUNCTION
191 
192 #define DFUNCTION "_sion_flags_get"
193 _sion_flags_entry* _sion_flags_get(_sion_flags_store* store, const char* key)
194 {
195  _sion_flags_entry* iter = NULL;
196 
197  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
198 
199  for (iter = _sion_flags_iter(store); iter->next != NULL; iter = iter->next) {
200  if (!strcmp(iter->key, key)) {
201  DPRINTFP((2, DFUNCTION, -1, "leave (key in store)\n"));
202  return iter;
203  }
204  }
205 
206  DPRINTFP((2, DFUNCTION, -1, "leave (key not in store)\n"));
207 
208  return NULL;
209 }
210 #undef DFUNCTION
211 
212 #define DFUNCTION "_sion_flags_update_mask"
213 sion_int64 _sion_flags_update_mask(_sion_flags_store* store)
214 {
215  _sion_flags_entry* iter = NULL;
216 
217  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
218 
219  store->mask = 0;
220  store->mask |= _SION_FMODE_ANSI;
221 
222  for (iter = _sion_flags_iter(store); iter->next != NULL; iter = iter->next) {
223  if ((!strcmp(iter->key, "w")) || (!strcmp(iter->key, "wb")) ||
224  (!strcmp(iter->key, "bw"))) {
225  store->mask |= _SION_FMODE_WRITE;
226  }
227  else if ((!strcmp(iter->key, "r")) || (!strcmp(iter->key, "rb")) ||
228  (!strcmp(iter->key, "br"))) {
229  store->mask |= _SION_FMODE_READ;
230  }
231  else if (!strcmp(iter->key, "buffered")) {
232  store->mask |= _SION_FMODE_BUFFERED;
233  }
234  else if (!strcmp(iter->key, "buddy")) {
235  store->mask |= _SION_FMODE_BUDDY;
236  }
237  else if (!strcmp(iter->key, "compress")) {
238  store->mask |= _SION_FMODE_COMPRESS;
239  }
240  else if (!strcmp(iter->key, "collective")) {
241  store->mask |= _SION_FMODE_COLLECTIVE;
242  }
243  else if ((!strcmp(iter->key, "collectivemerge")) ||
244  (!strcmp(iter->key, "cmerge"))) {
245  store->mask |= _SION_FMODE_COLLECTIVE_MERGE;
246  }
247  else if (!strcmp(iter->key, "keyval")) {
248  if ((!strcmp(iter->val, "default")) || (!strcmp(iter->val, "inline")) ||
249  (!strcmp(iter->val, ""))) {
250  store->mask |= _SION_FMODE_KEYVAL_INLINE;
251  }
252  else if (!strcmp(iter->key, "meta")) {
253  store->mask |= _SION_FMODE_KEYVAL_META;
254  }
255  else if (!strcmp(iter->key, "hash")) {
256  store->mask |= _SION_FMODE_KEYVAL_HASH;
257  }
258  else if (!strcmp(iter->key, "none")) {
259  store->mask |= _SION_FMODE_KEYVAL_NONE;
260  }
261  else if (!strcmp(iter->key, "unknown")) {
262  store->mask |= _SION_FMODE_KEYVAL_UNKNOWN;
263  }
264  }
265  else if (!strcmp(iter->key, "endianness")) {
266  store->mask |= _SION_FMODE_ENDIANNESS_SET;
267  if (!strcmp(iter->val, "big")) {
268  store->mask |= _SION_FMODE_ENDIANNESS_BIG;
269  }
270  }
271  else if (!strcmp(iter->key, "posix")) {
272  store->mask |= _SION_FMODE_POSIX;
273  store->mask ^= store->mask & _SION_FMODE_ANSI;
274 #if defined(_SION_SIONFWD)
275  store->mask ^= store->mask & _SION_FMODE_SIONFWD;
276 #endif
277 #ifdef _SION_IME_NATIVE
278  store->mask ^= store->mask & _SION_FMODE_IME_NATIVE;
279 #endif
280  }
281  else if (!strcmp(iter->key, "ansi")) {
282  store->mask |= _SION_FMODE_ANSI;
283  store->mask ^= store->mask & _SION_FMODE_POSIX;
284 #if defined(_SION_SIONFWD)
285  store->mask ^= store->mask & _SION_FMODE_SIONFWD;
286 #endif
287 #ifdef _SION_IME_NATIVE
288  store->mask ^= store->mask & _SION_FMODE_IME_NATIVE;
289 #endif
290  }
291 #if defined(_SION_SIONFWD)
292  else if (!strcmp(iter->key, "sionfwd")) {
293  store->mask |= _SION_FMODE_SIONFWD;
294  store->mask ^= store->mask & _SION_FMODE_ANSI;
295  store->mask ^= store->mask & _SION_FMODE_POSIX;
296 #ifdef _SION_IME_NATIVE
297  store->mask ^= store->mask & _SION_FMODE_IME_NATIVE;
298 #endif
299  }
300 #endif
301 #ifdef _SION_IME_NATIVE
302  else if (!strcmp(iter->key, "ime")) {
303  store->mask |= _SION_FMODE_IME_NATIVE;
304  store->mask ^= store->mask & _SION_FMODE_ANSI;
305  store->mask ^= store->mask & _SION_FMODE_POSIX;
306 #if defined(_SION_SIONFWD)
307  store->mask ^= store->mask & _SION_FMODE_SIONFWD;
308 #endif
309  }
310 #endif
311  }
312 
313  DPRINTFP((2, DFUNCTION, -1, "leave (mask = %llx)\n", store->mask));
314 
315  return store->mask;
316 }
317 #undef DFUNCTION
318 
319 #define DFUNCTION "_sion_parse_flags"
327 {
328  char* tmps = NULL;
329  char* key = NULL;
330  char* val = NULL;
331  char* pch = NULL;
332  char* saveptr = NULL;
333  const char* delim_tok = ",";
334  const char* delim_kv = "=";
335  int span = 0;
336  /* include terminating null */
337  const int len = strlen(flags) + 1;
338  _sion_flags_store* store = NULL;
339 
340  DPRINTFP((2, DFUNCTION, -1, "enter\n"));
341 
342  store = _sion_flags_create_store();
343 
344  tmps = (char*)malloc(len);
345  key = (char*)malloc(len);
346  val = (char*)malloc(len);
347  strcpy(tmps, flags);
348 
349  DPRINTF((32, DFUNCTION, -1, "tmps = '%s'\n", tmps));
350  DPRINTF((32, DFUNCTION, -1, "delim_tok = '%s'\n", delim_tok));
351  DPRINTF((32, DFUNCTION, -1, "delim_kv = '%s'\n", delim_kv));
352  pch = strtok_r(tmps, delim_tok, &saveptr);
353  while (pch != NULL) {
354  span = strcspn(pch, delim_kv);
355  strncpy(key, pch, span);
356  key[span] = '\0';
357  if (span < strlen(pch)) {
358  strcpy(val, &pch[span + 1]);
359  }
360  else {
361  val[0] = '\0';
362  }
363  DPRINTF((32, DFUNCTION, -1, "key = '%s' | val = '%s'\n", key, val));
364  _sion_flags_add(store, key, val);
365  pch = strtok_r(NULL, delim_tok, &saveptr);
366  }
367 
368  free(tmps);
369  free(key);
370  free(val);
371 
372  _sion_flags_update_mask(store);
373 
374  DPRINTFP((2, DFUNCTION, -1, "leave\n"));
375 
376  return store;
377 }
378 #undef DFUNCTION
_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:156
_sion_flags_store * _sion_flags_create_store(void)
Create a flags entry.
Definition: sion_flags.c:107
_sion_flags_store * _sion_parse_flags(const char *flags)
Parse flags and return a flags store with key value pairs.
Definition: sion_flags.c:326
_sion_flags_entry * _sion_flags_create_entry(void)
Create a flags entry.
Definition: sion_flags.c:49
Definition: sion_flags.h:32