configuration.c 6.34 KB
Newer Older
1
2
#include <stdio.h>
#include <stdlib.h>
3
#include <string.h>
4
5
#include <assert.h>
#include <errno.h>
6
7
#include "codes/configuration.h"

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * 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;

    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);

40
41
42
#ifdef __APPLE__
    f = fopen(filepath, "r");
#else
43
    f = fmemopen(txtdata, txtsize, "rb");
44
#endif
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    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);

    return rc;
}

int configuration_get_value(ConfigHandle *handle,
                            const char *section_name,
                            const char *key_name,
                            char *value,
                            size_t len)
{
    SectionHandle section_handle;
    int           rc;

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

    rc = cf_getKey(*handle, section_handle, key_name, value, len);
    assert(rc);

    (void) cf_closeSection(*handle, section_handle);

    return rc;
}

int configuration_get_value_int (ConfigHandle *handle,
                                 const char *section_name,
                                 const char *key_name,
                                 int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
                                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,
                                  unsigned int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
                                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,
                                     long int *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
                                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,
                                    double *value)
{
    char valuestr[256];
    int rc = 1;
    int r;

    r = configuration_get_value(handle,
                                section_name,
                                key_name,
                                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)
181
182
183
184
185
186
187
188
189
190
{
    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];

191
    memset (lpgroups, 0, sizeof(*lpgroups));
192

193
194
    cf_openSection(*handle, ROOT_SECTION, section_name, &sh);
    cf_listSection(*handle, sh, se, &se_count); 
195
196
197

    for (i = 0; i < se_count; i++)
    {
198
        //printf("section: %s type: %d\n", se[i].name, se[i].type);
199
200
201
        if (se[i].type == SE_SECTION)
        {
            subse_count = 10;
202
203
204
            cf_openSection(*handle, sh, se[i].name, &subsh);
            cf_listSection(*handle, subsh, subse, &subse_count);
            strncpy(lpgroups->lpgroups[i].name, se[i].name,
205
                    CONFIGURATION_MAX_NAME);
206
207
            lpgroups->lpgroups[i].repetitions = 1;
            lpgroups->lpgroups_count++;
208
209
210
211
            for (j = 0, lpt = 0; j < subse_count; j++)
            {
                if (subse[j].type == SE_KEY)
                {
212
213
                   cf_getKey(*handle, subsh, subse[j].name, data, sizeof(data));
                   //printf("key: %s value: %s\n", subse[j].name, data);
214
215
                   if (strcmp("repetitions", subse[j].name) == 0)
                   {
216
217
                       lpgroups->lpgroups[i].repetitions = atoi(data);
		       //printf("\n Repetitions: %ld ", lpgroups->lpgroups[i].repetitions);
218
219
220
221
                   }
                   else
                   {
                       // assume these are lptypes and counts
222
                       strncpy(lpgroups->lpgroups[i].lptypes[lpt].name,
223
                               subse[j].name,
224
225
226
                               sizeof(lpgroups->lpgroups[i].lptypes[lpt].name));
                       lpgroups->lpgroups[i].lptypes[lpt].count = atoi(data);
                       lpgroups->lpgroups[i].lptypes_count++;
227
228
229
230
                       lpt++;
                   }
                }
            }
231
            cf_closeSection(*handle, subsh);
232
233
234
        }
    }

235
    cf_closeSection(*handle, sh);
236
237
238
    
    return 0;
}