read_ini.c 7.16 KB
Newer Older
1
2
3
4
5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "read_ini.h"
#include "ini.h"
6
#include "../malleability/MAM.h"
7

iker_martin's avatar
iker_martin committed
8

9
ext_functions_t *user_functions;
10
void get_numbers_from_string(const char *input, size_t *res_len, int **res);
iker_martin's avatar
iker_martin committed
11

12
13
14
15
16
17
18
/*
 * Funcion utilizada para leer el fichero de configuracion
 * y guardarlo en una estructura para utilizarlo en el futuro.
 *
 * Primero lee la seccion "general" y a continuacion cada una
 * de las secciones "resize%d".
 */
19
20
static int handler(void* user, const char* section, const char* name,
                   const char* value) {
21
    int ret_value=1;
22
23
    int *aux;
    size_t aux_len;
24
25
    configuration* pconfig = (configuration*)user;

26
    if(pconfig->actual_group >= pconfig->n_groups && pconfig->actual_stage >= pconfig->n_stages) {
27
28
29
      return 1; // There is no more work to perform
    }

30
    char *resize_name = malloc(10 * sizeof(char));
31
    snprintf(resize_name, 10, "resize%zu", pconfig->actual_group);
32

33
    char *stage_name = malloc(10 * sizeof(char));
34
    snprintf(stage_name, 10, "stage%zu", pconfig->actual_stage);
35

36
    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
37
    #define LAST(iter, total) iter < total
38
    if (MATCH("general", "Total_Resizes")) {
39
40
        pconfig->n_resizes = strtoul(value, NULL, 10);
        pconfig->n_groups = pconfig->n_resizes+1;
41
42
        user_functions->resizes_f(pconfig);
    } else if (MATCH("general", "Total_Stages")) {
43
        pconfig->n_stages = strtoul(value, NULL, 10);
44
        user_functions->stages_f(pconfig); 
45
46
    } else if (MATCH("general", "Granularity")) {
        pconfig->granularity = atoi(value);
47
    } else if (MATCH("general", "SDR")) { // TODO Refactor a nombre manual
48
        pconfig->sdr = strtoul(value, NULL, 10);
49
    } else if (MATCH("general", "ADR")) { // TODO Refactor a nombre manual
50
        pconfig->adr = strtoul(value, NULL, 10);
51
52
    } else if (MATCH("general", "Rigid")) {
        pconfig->rigid_times = atoi(value);
53
54
    } else if (MATCH("general", "Capture_Method")) {
        pconfig->capture_method = atoi(value);
55

56
    // Iter stage
57
    } else if (MATCH(stage_name, "Stage_Type") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
58
        pconfig->stages[pconfig->actual_stage].pt = atoi(value);
59
60
    } else if (MATCH(stage_name, "Stage_Time_Capped") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
        pconfig->stages[pconfig->actual_stage].t_capped = atoi(value);
61
    } else if (MATCH(stage_name, "Stage_Bytes") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
62
        pconfig->stages[pconfig->actual_stage].bytes = atoi(value);
63
64
    } else if (MATCH(stage_name, "Stage_Identifier") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
        pconfig->stages[pconfig->actual_stage].id = atoi(value);
65
    } else if (MATCH(stage_name, "Stage_Time") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
66
67
        pconfig->stages[pconfig->actual_stage].t_stage = (float) atof(value);
        pconfig->actual_stage = pconfig->actual_stage+1; // Ultimo elemento del grupo
68
69

    // Resize stage
70
71
72
73
74
75
76
    } else if (MATCH(resize_name, "Iters") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].iters = atoi(value);
    } else if (MATCH(resize_name, "Procs") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].procs = atoi(value);
    } else if (MATCH(resize_name, "FactorS") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].factor =(float) atof(value);
    } else if (MATCH(resize_name, "Dist") && LAST(pconfig->actual_group, pconfig->n_groups)) {
77
78
79
80
	int aux_value = MALL_DIST_COMPACT;
        if (strcmp(value, "spread") == 0) {
          aux_value = MALL_DIST_SPREAD;
  	}
81
        pconfig->groups[pconfig->actual_group].phy_dist = aux_value;
82
83
84
    } else if (MATCH(resize_name, "Redistribution_Method") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].rm = atoi(value);
    } else if (MATCH(resize_name, "Redistribution_Strategy") && LAST(pconfig->actual_group, pconfig->n_groups)) {
85
86
87
        get_numbers_from_string(value, &aux_len, &aux);
        pconfig->groups[pconfig->actual_group].rs = aux;
        pconfig->groups[pconfig->actual_group].rs_len = aux_len;
88
89
90
    } else if (MATCH(resize_name, "Spawn_Method") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].sm = atoi(value);
    } else if (MATCH(resize_name, "Spawn_Strategy") && LAST(pconfig->actual_group, pconfig->n_groups)) {
91
92
93
        get_numbers_from_string(value, &aux_len, &aux);
        pconfig->groups[pconfig->actual_group].ss = aux;
        pconfig->groups[pconfig->actual_group].ss_len = aux_len;
94
        pconfig->actual_group = pconfig->actual_group+1; // Ultimo elemento de la estructura
95

96
    // Unkown case
97
    } else {
98
        ret_value = 0;  /* unknown section or name, error */
99
100
101
    }
 
    free(resize_name);
102
    free(stage_name);
103
    return ret_value;
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
/**
 * @brief Extracts numbers from a comma-separated string and stores them in an array.
 *
 * This function takes a string containing a sequence of numbers separated by commas,
 * converts each number to an integer, and stores them in a dynamically allocated array.
 *
 * @param input The input string containing comma-separated numbers.
 * @param res_len Pointer to an integer that will hold the length of the resulting array.
 *            Note: Null can be passed if the caller does not need it.
 * @param res Pointer to an integer array where the extracted numbers will be stored.
 *            Note: The memory for this array is dynamically allocated and should be freed by the caller.
 */
void get_numbers_from_string(const char *input, size_t *res_len, int **res) {
  char *aux, *token;
  int num;
  size_t len, malloc_len;
  len = 0;
  malloc_len = 10;
  *res = (int *) malloc(malloc_len * sizeof(int));
  aux = (char *) malloc((strlen(input)+1) * sizeof(char));
  strcpy(aux, input);

  token = strtok(aux, ",");
  while (token != NULL) {
    num = atoi(token);

    if(len == malloc_len) {
      malloc_len += 10;
      *res = (int *) realloc(*res, malloc_len * sizeof(int));
    }
    (*res)[len] = num;
    len++;

    token = strtok(NULL, ",");
    }

  if(res_len != NULL) *res_len = len;
  if(len != malloc_len) {
    *res = (int *) realloc(*res, len * sizeof(int));
  }

  free(aux);
}

150
151
152
153
154
155
156
/*
 * Crea y devuelve una estructura de configuracion a traves
 * de un nombre de fichero dado.
 *
 * La memoria de la estructura se reserva en la funcion y es conveniente
 * liberarla con la funcion "free_config()"
 */
157
configuration *read_ini_file(char *file_name, ext_functions_t init_functions) {
158
159
    configuration *config = NULL;

160
    config = malloc(sizeof(configuration));
161
162
163
164
    if(config == NULL) {
        printf("Error when reserving configuration structure\n");
	return NULL;
    }
165
166
    config->capture_method = 0;
    config->rigid_times = 0;
167
168
    config->n_resizes = 0;
    config->n_groups = 1;
169
    config->n_stages = 1;
170
    config->actual_group=0;
171
    config->actual_stage=0;
172

173
174
    user_functions = &init_functions;

175
176
177
178
179
180
    if(ini_parse(file_name, handler, config) < 0) { // Obtener configuracion
        printf("Can't load '%s'\n", file_name);
        return NULL;
    }
    return config;
}