configuration.c 9.8 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1
2
3
4
5
6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7
8
#include <stdio.h>
#include <stdlib.h>
9
#include <string.h>
10
11
#include <assert.h>
#include <errno.h>
12
#include <libgen.h>
13
14
#include "codes/configuration.h"

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Global to hold configuration in memory
 */
ConfigHandle config;

int configuration_load (const char *filepath,
                        MPI_Comm comm,
                        ConfigHandle *handle)
{
    MPI_File   fh;
    MPI_Status status;
    MPI_Offset txtsize;
    FILE      *f;
    char      *txtdata;
    char      *error;
    int        rc;
31
    char      *tmp_path;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

    rc = MPI_File_open(comm, (char*)filepath, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
    assert(rc == MPI_SUCCESS);

    rc = MPI_File_get_size(fh, &txtsize);
    assert(rc == MPI_SUCCESS);

    txtdata = malloc(txtsize);
    assert(txtdata); 

    rc = MPI_File_read_all(fh, txtdata, txtsize, MPI_BYTE, &status);
    assert(rc == MPI_SUCCESS);

    rc = MPI_File_close(&fh);
    assert(rc == MPI_SUCCESS);

48
49
50
#ifdef __APPLE__
    f = fopen(filepath, "r");
#else
51
    f = fmemopen(txtdata, txtsize, "rb");
52
#endif
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    assert(f);

    *handle = txtfile_openStream(f, &error);
    if (error)
    {
        fprintf(stderr, "config error: %s\n", error);
        free(error);
        rc = 1;
    }
    else
    {
        rc = 0;
    }

    fclose(f);

69
70
71
72
    /* NOTE: posix version overwrites argument :(. */
    tmp_path = strdup(filepath);
    (*handle)->config_dir = strdup(dirname(tmp_path));
    free(tmp_path);
73

74
75
76
77
78
79
    return rc;
}

int configuration_get_value(ConfigHandle *handle,
                            const char *section_name,
                            const char *key_name,
80
                            const char *annotation,
81
82
83
84
85
                            char *value,
                            size_t len)
{
    SectionHandle section_handle;
    int           rc;
86
87
88
89
90
91
92
93
94
95
96
97
98
    // reading directly from the config, so need to inject the annotation
    // directly into the search string
    char key_name_tmp[CONFIGURATION_MAX_NAME];
    char *key_name_full;
    if (annotation==NULL){
        // sorry const type... we promise we won't change you
        key_name_full = (char*) key_name;
    }
    else{
        assert(snprintf(key_name_tmp, CONFIGURATION_MAX_NAME, "%s@%s",
                    key_name, annotation) < CONFIGURATION_MAX_NAME);
        key_name_full = key_name_tmp;
    }
99
100
101
102

    rc = cf_openSection(*handle, ROOT_SECTION, section_name, &section_handle);
    assert(rc == 1);

103
    rc = cf_getKey(*handle, section_handle, key_name_full, value, len);
104
105
106
107
108
109
110
    assert(rc);

    (void) cf_closeSection(*handle, section_handle);

    return rc;
}

111
112
113
114
int configuration_get_value_relpath(
        ConfigHandle *handle,
        const char * section_name,
        const char * key_name,
115
        const char *annotation,
116
117
118
119
        char *value,
        size_t length){
    char *tmp = malloc(length);

120
121
    configuration_get_value(handle, section_name, key_name, annotation, tmp,
            length);
122
123
124
125
126
127
128
129

    /* concat the configuration value with the directory */
    int w = snprintf(value, length, "%s/%s", (*handle)->config_dir, tmp);

    free(tmp);
    return w;
}

130
131
132
int configuration_get_multivalue(ConfigHandle *handle,
                                 const char *section_name,
                                 const char *key_name,
133
                                 const char *annotation,
134
135
136
137
138
                                 char ***values,
                                 size_t *len)
{
    SectionHandle section_handle;
    int           rc;
139
140
141
142
143
144
145
146
147
148
149
150
151
    // reading directly from the config, so need to inject the annotation
    // directly into the search string
    char key_name_tmp[CONFIGURATION_MAX_NAME];
    char *key_name_full;
    if (annotation==NULL){
        // sorry const type... we promise we won't change you
        key_name_full = (char*) key_name;
    }
    else{
        assert(snprintf(key_name_tmp, CONFIGURATION_MAX_NAME, "%s@%s",
                    key_name, annotation) < CONFIGURATION_MAX_NAME);
        key_name_full = key_name_tmp;
    }
152
153
154
155

    rc = cf_openSection(*handle, ROOT_SECTION, section_name, &section_handle);
    assert(rc == 1);

156
    rc = cf_getMultiKey(*handle, section_handle, key_name_full, values, len);
157
158
159
160
161
162
163
    assert(rc);

    (void) cf_closeSection(*handle, section_handle);

    return rc;
}

164
165
166
int configuration_get_value_int (ConfigHandle *handle,
                                 const char *section_name,
                                 const char *key_name,
167
                                 const char *annotation,
168
169
170
171
172
173
174
175
176
                                 int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
177
                                annotation,
178
179
180
181
182
183
184
185
186
187
188
189
190
191
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        *value = atoi(valuestr);
        rc = 0;
    }

    return rc;
}

int configuration_get_value_uint (ConfigHandle *handle,
                                  const char *section_name,
                                  const char *key_name,
192
                                  const char *annotation,
193
194
195
196
197
198
199
200
201
                                  unsigned int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
202
                                annotation,
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        *value = (unsigned int) atoi(valuestr);
        rc = 0;
    }

    return rc;
}

int configuration_get_value_longint (ConfigHandle *handle,
                                     const char *section_name,
                                     const char *key_name,
217
                                     const char *annotation,
218
219
220
221
222
223
224
225
226
                                     long int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
227
                                annotation,
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        errno = 0;
        *value = strtol(valuestr, NULL, 10);
        rc = errno;
    }

    return rc;
}

int configuration_get_value_double (ConfigHandle *handle,
                                    const char *section_name,
                                    const char *key_name,
243
                                    const char *annotation,
244
245
246
247
248
249
250
251
252
                                    double *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
253
                                annotation,
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
                                valuestr,
                                sizeof(valuestr));
    if (r > 0)
    {
        errno = 0;
        *value = strtod(valuestr, NULL);
        rc = errno;
    }

    return rc;
}

int configuration_get_lpgroups (ConfigHandle *handle,
                                const char *section_name,
                                config_lpgroups_t *lpgroups)
269
270
271
272
273
274
275
276
277
278
{
    SectionHandle sh;
    SectionHandle subsh;
    SectionEntry se[10];
    SectionEntry subse[10];
    size_t se_count = 10;
    size_t subse_count = 10;
    int i, j, lpt;
    char data[256];

279
    memset (lpgroups, 0, sizeof(*lpgroups));
280

281
282
    cf_openSection(*handle, ROOT_SECTION, section_name, &sh);
    cf_listSection(*handle, sh, se, &se_count); 
283
284
285

    for (i = 0; i < se_count; i++)
    {
286
        //printf("section: %s type: %d\n", se[i].name, se[i].type);
287
288
289
        if (se[i].type == SE_SECTION)
        {
            subse_count = 10;
290
291
292
            cf_openSection(*handle, sh, se[i].name, &subsh);
            cf_listSection(*handle, subsh, subse, &subse_count);
            strncpy(lpgroups->lpgroups[i].name, se[i].name,
293
                    CONFIGURATION_MAX_NAME);
294
295
            lpgroups->lpgroups[i].repetitions = 1;
            lpgroups->lpgroups_count++;
296
297
298
299
            for (j = 0, lpt = 0; j < subse_count; j++)
            {
                if (subse[j].type == SE_KEY)
                {
300
301
                   cf_getKey(*handle, subsh, subse[j].name, data, sizeof(data));
                   //printf("key: %s value: %s\n", subse[j].name, data);
302
303
                   if (strcmp("repetitions", subse[j].name) == 0)
                   {
304
305
                       lpgroups->lpgroups[i].repetitions = atoi(data);
		       //printf("\n Repetitions: %ld ", lpgroups->lpgroups[i].repetitions);
306
307
308
                   }
                   else
                   {
309
310
311
                       size_t s = sizeof(lpgroups->lpgroups[i].lptypes[lpt].name);
                       char *nm   = lpgroups->lpgroups[i].lptypes[lpt].name;
                       char *anno = lpgroups->lpgroups[i].lptypes[lpt].anno;
312
                       // assume these are lptypes and counts
313
314
315
316
317
318
319
320
321
322
323
                       strncpy(nm, subse[j].name, s-1);
                       lpgroups->lpgroups[i].lptypes[lpt].name[s-1] = '\0';

                       char *c = strchr(nm, '@');
                       if (c) {
                           strcpy(anno, c+1);
                           *c = '\0';
                       }
                       else {
                           anno[0] = '\0';
                       }
324
325
                       lpgroups->lpgroups[i].lptypes[lpt].count = atoi(data);
                       lpgroups->lpgroups[i].lptypes_count++;
326
327
328
329
                       lpt++;
                   }
                }
            }
330
            cf_closeSection(*handle, subsh);
331
332
333
        }
    }

334
    cf_closeSection(*handle, sh);
335
336
337
    
    return 0;
}
Philip Carns's avatar
Philip Carns committed
338
339
340
341
342
343
344
345
346

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */