Revision 176b8de8

View differences:

include/grapes_config.h
1
#ifndef GRAPES_CONFIG_H
2
#define GRAPES_CONFIG_H
3
struct tag;
4

  
5
/*
6
 * earlier name-value pairs in the config string take precedence
7
 */
8

  
9
struct tag *grapes_config_parse(const char *cfg);
10
int grapes_config_value_int(const struct tag *cfg_values, const char *value, int *res);
11
int grapes_config_value_int_default(const struct tag *cfg_values, const char *value, int *res, int default_value);
12
int grapes_config_value_double(const struct tag *cfg_values, const char *value, double *res);
13
int grapes_config_value_double_default(const struct tag *cfg_values, const char *value, double *res, double default_value);
14
const char *grapes_config_value_str(const struct tag *cfg_values, const char *value);
15
const char *grapes_config_value_str_default(const struct tag *cfg_values, const char *value, const char *default_value);
16

  
17
#endif /* CONFIG_H */
src/ChunkBuffer/buffer.c
11 11

  
12 12
#include "chunk.h"
13 13
#include "chunkbuffer.h"
14
#include "config.h"
14
#include "grapes_config.h"
15 15

  
16 16
struct chunk_buffer {
17 17
  int size;
......
83 83
  }
84 84
  memset(cb, 0, sizeof(struct chunk_buffer));
85 85

  
86
  cfg_tags = config_parse(config);
86
  cfg_tags = grapes_config_parse(config);
87 87
  if (!cfg_tags) {
88 88
    free(cb);
89 89
    return NULL;
90 90
  }
91
  res = config_value_int(cfg_tags, "size", &cb->size);
91
  res = grapes_config_value_int(cfg_tags, "size", &cb->size);
92 92
  if (!res) {
93 93
    free(cb);
94 94
    free(cfg_tags);
src/ChunkIDSet/chunkids_ops.c
13 13
#include "chunkids_private.h"
14 14
#include "chunkids_iface.h"
15 15
#include "chunkidset.h"
16
#include "config.h"
16
#include "grapes_config.h"
17 17

  
18 18
#define DEFAULT_SIZE_INCREMENT 32
19 19

  
......
34 34
    return NULL;
35 35
  }
36 36
  p->n_elements = 0;
37
  cfg_tags = config_parse(config);
37
  cfg_tags = grapes_config_parse(config);
38 38
  if (!cfg_tags) {
39 39
    free(p);
40 40
    return NULL;
41 41
  }
42
  res = config_value_int(cfg_tags, "size", &p->size);
42
  res = grapes_config_value_int(cfg_tags, "size", &p->size);
43 43
  if (!res) {
44 44
    p->size = 0;
45 45
  }
......
54 54
  p->enc = &prio_encoding;
55 55
  p->ops = &list_ops;
56 56
  p->type = CIST_PRIORITY;
57
  type = config_value_str(cfg_tags, "type");
57
  type = grapes_config_value_str(cfg_tags, "type");
58 58
  if (type) {
59 59
    if (!memcmp(type, "priority", strlen(type) - 1)) {
60 60
      p->enc = &prio_encoding;
src/Chunkiser/input-stream-avf.c
12 12
#include "libav-compat.h"
13 13
#include "int_coding.h"
14 14
#include "payload.h"
15
#include "config.h"
15
#include "grapes_config.h"
16 16
#include "ffmpeg_compat.h"
17 17
#include "chunkiser_iface.h"
18 18

  
......
182 182
  desc->a_frames = 0;
183 183
  desc->a_data = NULL;
184 184
  desc->a_size = 0;
185
  cfg_tags = config_parse(config);
185
  cfg_tags = grapes_config_parse(config);
186 186
  if (cfg_tags) {
187 187
    const char *media;
188 188

  
189
    config_value_int(cfg_tags, "loop", &desc->loop);
190
    media = config_value_str(cfg_tags, "media");
189
    grapes_config_value_int(cfg_tags, "loop", &desc->loop);
190
    media = grapes_config_value_str(cfg_tags, "media");
191 191
    if (media) {
192 192
      if (!strcmp(media, "audio")) {
193 193
        audio_streams = 0;
......
200 200
        video_streams = 0;
201 201
      }
202 202
    }
203
    config_value_int(cfg_tags, "vframes", &desc->v_frames_max);
204
    config_value_int(cfg_tags, "aframes", &desc->a_frames_max);
203
    grapes_config_value_int(cfg_tags, "vframes", &desc->v_frames_max);
204
    grapes_config_value_int(cfg_tags, "aframes", &desc->a_frames_max);
205 205
  }
206 206
  free(cfg_tags);
207 207
  for (i = 0; i < desc->s->nb_streams; i++) {
src/Chunkiser/input-stream-dumb.c
15 15
#include <string.h>
16 16

  
17 17
#include "chunkiser_iface.h"
18
#include "config.h"
18
#include "grapes_config.h"
19 19

  
20 20
struct chunkiser_ctx {
21 21
  int loop;	//loop on input file infinitely
......
45 45

  
46 46
  *period = 0;
47 47
  res->chunk_size = DEFAULT_CHUNK_SIZE;
48
  cfg_tags = config_parse(config);
48
  cfg_tags = grapes_config_parse(config);
49 49
  if (cfg_tags) {
50 50
    const char *access_mode;
51 51

  
52
    config_value_int(cfg_tags, "loop", &res->loop);
53
    config_value_int(cfg_tags, "chunk_size", &res->chunk_size);
54
    access_mode = config_value_str(cfg_tags, "mode");
52
    grapes_config_value_int(cfg_tags, "loop", &res->loop);
53
    grapes_config_value_int(cfg_tags, "chunk_size", &res->chunk_size);
54
    access_mode = grapes_config_value_str(cfg_tags, "mode");
55 55
    if (access_mode && !strcmp(access_mode, "nonblock")) {
56 56
#ifndef _WIN32
57 57
      fcntl(res->fds[0], F_SETFL, O_NONBLOCK);
src/Chunkiser/input-stream-udp.c
21 21

  
22 22
#include "int_coding.h"
23 23
#include "payload.h"
24
#include "config.h"
24
#include "grapes_config.h"
25 25
#include "chunkiser_iface.h"
26 26

  
27 27
#define UDP_PORTS_NUM_MAX 10
......
90 90
  int i = 0;
91 91
  struct tag *cfg_tags;
92 92

  
93
  cfg_tags = config_parse(config);
93
  cfg_tags = grapes_config_parse(config);
94 94
  if (cfg_tags) {
95 95
    int j;
96 96

  
......
98 98
      char tag[8];
99 99

  
100 100
      sprintf(tag, "port%d", j);
101
      if (config_value_int(cfg_tags, tag, &res[i])) {
101
      if (grapes_config_value_int(cfg_tags, tag, &res[i])) {
102 102
        i++;
103 103
      }
104 104
    }
src/Chunkiser/input-stream.c
3 3
#include <string.h>
4 4

  
5 5
#include "chunk.h"
6
#include "config.h"
6
#include "grapes_config.h"
7 7
#include "chunkiser.h"
8 8
#include "chunkiser_iface.h"
9 9

  
......
32 32
#else
33 33
  res->in = &in_dumb;
34 34
#endif
35
  cfg_tags = config_parse(config);
35
  cfg_tags = grapes_config_parse(config);
36 36
  if (cfg_tags) {
37 37
    const char *type;
38 38

  
39
    type = config_value_str(cfg_tags, "chunkiser");
39
    type = grapes_config_value_str(cfg_tags, "chunkiser");
40 40
    if (type && !strcmp(type, "dummy")) {
41 41
      res->in = &in_dummy;
42 42
    }
src/Chunkiser/output-stream-avf.c
12 12
#include "libav-compat.h"
13 13
#include "int_coding.h"
14 14
#include "payload.h"
15
#include "config.h"
15
#include "grapes_config.h"
16 16
#include "ffmpeg_compat.h"
17 17
#include "dechunkiser_iface.h"
18 18

  
......
177 177
  } else {
178 178
    out->output_file = strdup("/dev/stdout");
179 179
  }
180
  cfg_tags = config_parse(config);
180
  cfg_tags = grapes_config_parse(config);
181 181
  if (cfg_tags) {
182 182
    const char *format;
183 183

  
184
    format = config_value_str(cfg_tags, "format");
184
    format = grapes_config_value_str(cfg_tags, "format");
185 185
    if (format) {
186 186
      out->output_format = strdup(format);
187 187
    }
188
    format = config_value_str(cfg_tags, "media");
188
    format = grapes_config_value_str(cfg_tags, "media");
189 189
    if (format) {
190 190
      if (!strcmp(format, "video")) {
191 191
        out->selected_streams = 0x01;
src/Chunkiser/output-stream-dummy.c
9 9
#include <stdio.h>
10 10
#include <string.h>
11 11

  
12
#include "config.h"
12
#include "grapes_config.h"
13 13
#include "dechunkiser_iface.h"
14 14

  
15 15
enum output_type {
......
44 44
  }
45 45
  res->last_id = -1;
46 46
  res->lost = 0;
47
  cfg_tags = config_parse(config);
47
  cfg_tags = grapes_config_parse(config);
48 48
  if (cfg_tags) {
49 49
    const char *pt;
50 50

  
51
    pt = config_value_str(cfg_tags, "type");
51
    pt = grapes_config_value_str(cfg_tags, "type");
52 52
    if (pt) {
53 53
      if (!strcmp(pt, "stats")) {
54 54
        res->type = stats;
src/Chunkiser/output-stream-play.c
16 16

  
17 17
#include "int_coding.h"
18 18
#include "payload.h"
19
#include "config.h"
19
#include "grapes_config.h"
20 20
#include "ffmpeg_compat.h"
21 21
#include "dechunkiser_iface.h"
22 22

  
......
715 715

  
716 716
  memset(out, 0, sizeof(struct dechunkiser_ctx));
717 717
  out->selected_streams = 0x01;
718
  cfg_tags = config_parse(config);
718
  cfg_tags = grapes_config_parse(config);
719 719
  if (cfg_tags) {
720 720
    const char *format;
721 721

  
722
    format = config_value_str(cfg_tags, "media");
722
    format = grapes_config_value_str(cfg_tags, "media");
723 723
    if (format) {
724 724
      if (!strcmp(format, "video")) {
725 725
        out->selected_streams = 0x01;
src/Chunkiser/output-stream-raw.c
15 15

  
16 16
#include "int_coding.h"
17 17
#include "payload.h"
18
#include "config.h"
18
#include "grapes_config.h"
19 19
#include "dechunkiser_iface.h"
20 20

  
21 21
enum pt {
......
52 52
      res->fd = 1;
53 53
    }
54 54
  }
55
  cfg_tags = config_parse(config);
55
  cfg_tags = grapes_config_parse(config);
56 56
  if (cfg_tags) {
57 57
    const char *pt;
58 58

  
59
    pt = config_value_str(cfg_tags, "payload");
59
    pt = grapes_config_value_str(cfg_tags, "payload");
60 60
    if (pt) {
61 61
      if (!strcmp(pt, "avf")) {
62 62
        res->payload_type = avf;
src/Chunkiser/output-stream-udp.c
19 19

  
20 20
#include "int_coding.h"
21 21
#include "payload.h"
22
#include "config.h"
22
#include "grapes_config.h"
23 23
#include "dechunkiser_iface.h"
24 24

  
25 25
#define UDP_PORTS_NUM_MAX 10
......
50 50
  struct tag *cfg_tags;
51 51

  
52 52
  sprintf(ip, "127.0.0.1");
53
  cfg_tags = config_parse(config);
53
  cfg_tags = grapes_config_parse(config);
54 54
  if (cfg_tags) {
55 55
    int j;
56 56
    const char *addr;
57 57

  
58
    addr = config_value_str(cfg_tags, "addr");
58
    addr = grapes_config_value_str(cfg_tags, "addr");
59 59
    if (addr) {
60 60
      sprintf(ip, "%s", addr);
61 61
    }
......
63 63
      char tag[8];
64 64

  
65 65
      sprintf(tag, "port%d", j);
66
      if (config_value_int(cfg_tags, tag, &ports[i])) {
66
      if (grapes_config_value_int(cfg_tags, tag, &ports[i])) {
67 67
        i++;
68 68
      }
69 69
    }
src/Chunkiser/output-stream.c
3 3
#include <string.h>
4 4

  
5 5
#include "chunk.h"
6
#include "config.h"
6
#include "grapes_config.h"
7 7
#include "chunkiser.h"
8 8
#include "dechunkiser_iface.h"
9 9

  
......
33 33
#else
34 34
  res->out = &out_raw;
35 35
#endif
36
  cfg_tags = config_parse(config);
36
  cfg_tags = grapes_config_parse(config);
37 37
  if (cfg_tags) {
38 38
    const char *type;
39 39

  
40
    type = config_value_str(cfg_tags, "dechunkiser");
40
    type = grapes_config_value_str(cfg_tags, "dechunkiser");
41 41
    if (type && !strcmp(type, "raw")) {
42 42
      res->out = &out_raw;
43 43
    } else if (type && !strcmp(type, "udp")) {
src/CloudSupport/cloud_helper.c
14 14

  
15 15
#include "../Utils/fifo_queue.h"
16 16

  
17
#include "config.h"
17
#include "grapes_config.h"
18 18

  
19 19
#define CLOUD_HELPER_INITAIL_INSTANCES 2
20 20

  
......
80 80
  struct tag *cfg_tags;
81 81
  const char *provider;
82 82

  
83
  cfg_tags = config_parse(config);
84
  provider = config_value_str(cfg_tags, "provider");
83
  cfg_tags = grapes_config_parse(config);
84
  provider = grapes_config_value_str(cfg_tags, "provider");
85 85

  
86 86
  if (!provider) return NULL;
87 87

  
src/CloudSupport/cloud_helper_delegate.c
10 10
#include <stdio.h>
11 11

  
12 12
#include "cloud_helper_iface.h"
13
#include "config.h"
13
#include "grapes_config.h"
14 14

  
15 15
struct delegate_iface {
16 16
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
......
50 50
  struct delegate_iface *delegate_impl;
51 51
  void *dlib;
52 52

  
53
  cfg_tags = config_parse(config);
54
  dlib_name = config_value_str(cfg_tags, "delegate_lib");
53
  cfg_tags = grapes_config_parse(config);
54
  dlib_name = grapes_config_value_str(cfg_tags, "delegate_lib");
55 55
  dlib = dlopen(dlib_name, RTLD_NOW);
56 56
  if (dlib == NULL) {
57 57
    printf("error: %s", dlerror());
src/CloudSupport/libs3_delegate_helper.c
26 26
#include "net_helper.h"
27 27
#include "cloud_helper_iface.h"
28 28
#include "request_handler.h"
29
#include "config.h"
29
#include "grapes_config.h"
30 30

  
31 31
#define CLOUD_NODE_ADDR "0.0.0.0"
32 32

  
......
481 481

  
482 482
  ctx = malloc(sizeof(struct libs3_cloud_context));
483 483
  memset(ctx, 0, sizeof(struct libs3_cloud_context));
484
  cfg_tags = config_parse(config);
484
  cfg_tags = grapes_config_parse(config);
485 485

  
486 486
  /* Parse fundametal parameters */
487
  arg = config_value_str(cfg_tags, "s3_access_key");
487
  arg = grapes_config_value_str(cfg_tags, "s3_access_key");
488 488
  if (!arg) {
489 489
    deallocate_context(ctx);
490 490
    fprintf(stderr,
......
494 494
  }
495 495
  ctx->s3_bucket_context.accessKeyId = strdup(arg);
496 496

  
497
  arg = config_value_str(cfg_tags, "s3_secret_key");
497
  arg = grapes_config_value_str(cfg_tags, "s3_secret_key");
498 498
  if (!arg) {
499 499
    deallocate_context(ctx);
500 500
    fprintf(stderr,
......
504 504
  }
505 505
  ctx->s3_bucket_context.secretAccessKey = strdup(arg);
506 506

  
507
  arg = config_value_str(cfg_tags, "s3_bucket_name");
507
  arg = grapes_config_value_str(cfg_tags, "s3_bucket_name");
508 508
  if (!arg) {
509 509
    deallocate_context(ctx);
510 510
    fprintf(stderr,
......
515 515
  ctx->s3_bucket_context.bucketName = strdup(arg);
516 516

  
517 517
  ctx->s3_bucket_context.protocol = S3ProtocolHTTPS;
518
  arg = config_value_str(cfg_tags, "s3_protocol");
518
  arg = grapes_config_value_str(cfg_tags, "s3_protocol");
519 519
  if (arg) {
520 520
    if (strcmp(arg, "https") == 0) {
521 521
      ctx->s3_bucket_context.protocol = S3ProtocolHTTPS;
......
529 529

  
530 530
  /* Parse optional parameters */
531 531
  ctx->blocking_put_request = 1;
532
  arg = config_value_str(cfg_tags, "s3_blocking_put");
532
  arg = grapes_config_value_str(cfg_tags, "s3_blocking_put");
533 533
  if (arg) {
534 534
    if (strcmp(arg, "1") == 0)
535 535
      ctx->blocking_put_request = 1;
src/CloudSupport/mysql_delegate_helper.c
25 25
#include "net_helper.h"
26 26
#include "request_handler.h"
27 27
#include "cloud_helper_iface.h"
28
#include "config.h"
28
#include "grapes_config.h"
29 29

  
30 30

  
31 31
#define CLOUD_NODE_ADDR "0.0.0.0"
......
95 95
static const char* parse_required_param(struct tag *cfg_tags, const char *name)
96 96
{
97 97
  const char *arg;
98
  arg = config_value_str(cfg_tags, name);
98
  arg = grapes_config_value_str(cfg_tags, name);
99 99
  if (!arg) {
100 100
    fprintf(stderr, "mysql_delegate_helper: missing required parameter " \
101 101
            "'%s'\n", name);
......
312 312

  
313 313
  ctx = malloc(sizeof(struct mysql_cloud_context));
314 314
  memset(ctx, 0, sizeof(struct mysql_cloud_context));
315
  cfg_tags = config_parse(config);
315
  cfg_tags = grapes_config_parse(config);
316 316

  
317 317
  /* Parse fundametal parameters */
318 318
  if (!(mysql_host=parse_required_param(cfg_tags, "mysql_host"))) {
src/Makefile
7 7
ifneq ($(ARCH),win32)
8 8
  SUBDIRS += CloudSupport
9 9
endif
10
COMMON_OBJS = config.o
10
COMMON_OBJS = grapes_config.o
11 11

  
12 12
OBJ_LSTS = $(addsuffix /objs.lst, $(SUBDIRS))
13 13

  
src/PeerSampler/cloudcast.c
24 24
#include "../Cache/topocache.h"
25 25
#include "../Cache/cloudcast_proto.h"
26 26
#include "../Cache/proto.h"
27
#include "config.h"
27
#include "grapes_config.h"
28 28
#include "grapes_msg_types.h"
29 29

  
30 30
#define DEFAULT_CACHE_SIZE 20
......
121 121
  if (!con) return NULL;
122 122
  con->local_node = myID;
123 123

  
124
  cfg_tags = config_parse(config);
125
  res = config_value_int(cfg_tags, "cache_size", &(con->cache_size));
124
  cfg_tags = grapes_config_parse(config);
125
  res = grapes_config_value_int(cfg_tags, "cache_size", &(con->cache_size));
126 126
  if (!res) {
127 127
    con->cache_size = DEFAULT_CACHE_SIZE;
128 128
  }
129
  res = config_value_int(cfg_tags, "sent_entries", &(con->sent_entries));
129
  res = grapes_config_value_int(cfg_tags, "sent_entries", &(con->sent_entries));
130 130
  if (!res) {
131 131
    con->sent_entries = DEFAULT_PARTIAL_VIEW_SIZE;
132 132
  }
133
  res = config_value_int(cfg_tags, "max_silence", &(con->max_silence));
133
  res = grapes_config_value_int(cfg_tags, "max_silence", &(con->max_silence));
134 134
  if (!res) {
135 135
    con->max_silence = 0;
136 136
  }
137 137

  
138
  res = config_value_double(cfg_tags, "cloud_respawn_prob", &(con->cloud_respawn_prob));
138
  res = grapes_config_value_double(cfg_tags, "cloud_respawn_prob", &(con->cloud_respawn_prob));
139 139
  if (!res) {
140 140
    con->max_silence = 0;
141 141
  }
src/PeerSampler/cyclon.c
17 17
#include "../Cache/topocache.h"
18 18
#include "../Cache/cyclon_proto.h"
19 19
#include "../Cache/proto.h"
20
#include "config.h"
20
#include "grapes_config.h"
21 21
#include "grapes_msg_types.h"
22 22

  
23 23
#define DEFAULT_CACHE_SIZE 10
......
88 88
  con = cyclon_context_init();
89 89
  if (!con) return NULL;
90 90

  
91
  cfg_tags = config_parse(config);
92
  res = config_value_int(cfg_tags, "cache_size", &(con->cache_size));
91
  cfg_tags = grapes_config_parse(config);
92
  res = grapes_config_value_int(cfg_tags, "cache_size", &(con->cache_size));
93 93
  if (!res) {
94 94
    con->cache_size = DEFAULT_CACHE_SIZE;
95 95
  }
96
  res = config_value_int(cfg_tags, "sent_entries", &(con->sent_entries));
96
  res = grapes_config_value_int(cfg_tags, "sent_entries", &(con->sent_entries));
97 97
  if (!res) {
98 98
    con->sent_entries = con->cache_size / 2;
99 99
  }
100
  res = config_value_int(cfg_tags, "period", &con->period);
100
  res = grapes_config_value_int(cfg_tags, "period", &con->period);
101 101
  if (!res) {
102 102
    con->period = DEFAULT_PERIOD;
103 103
  }
104
  res = config_value_int(cfg_tags, "bootstrap_period", &con->bootstrap_period);
104
  res = grapes_config_value_int(cfg_tags, "bootstrap_period", &con->bootstrap_period);
105 105
  if (!res) {
106 106
    con->bootstrap_period = DEFAULT_BOOTSTRAP_PERIOD;
107 107
  }
108
  res = config_value_int(cfg_tags, "bootstrap_cycles", &con->bootstrap_cycles);
108
  res = grapes_config_value_int(cfg_tags, "bootstrap_cycles", &con->bootstrap_cycles);
109 109
  if (!res) {
110 110
    con->bootstrap_cycles = DEFAULT_BOOTSTRAP_CYCLES;
111 111
  }
src/PeerSampler/ncast.c
18 18
#include "../Cache/topocache.h"
19 19
#include "../Cache/ncast_proto.h"
20 20
#include "../Cache/proto.h"
21
#include "config.h"
21
#include "grapes_config.h"
22 22
#include "grapes_msg_types.h"
23 23

  
24 24
#define DEFAULT_CACHE_SIZE 10
......
101 101
  context = ncast_context_init();
102 102
  if (!context) return NULL;
103 103

  
104
  cfg_tags = config_parse(config);
105
  res = config_value_int_default(cfg_tags, "cache_size", &context->cache_size, DEFAULT_CACHE_SIZE);
106
  res = config_value_int_default(cfg_tags, "max_timestamp", &max_timestamp, DEFAULT_MAX_TIMESTAMP);
107
  res = config_value_int_default(cfg_tags, "period", &context->period, DEFAULT_PERIOD);
108
  res = config_value_int_default(cfg_tags, "bootstrap_period", &context->bootstrap_period, DEFAULT_BOOTSTRAP_PERIOD);
109
  res = config_value_int_default(cfg_tags, "bootstrap_cycles", &context->bootstrap_cycles, DEFAULT_BOOTSTRAP_CYCLES);
110
  res = config_value_int_default(cfg_tags, "adaptive", &context->adaptive, plus_features);
111
  res = config_value_int_default(cfg_tags, "restart", &context->restart, plus_features);
112
  res = config_value_int_default(cfg_tags, "randomize", &context->randomize, plus_features);
113
  res = config_value_int_default(cfg_tags, "slowstart", &context->slowstart, plus_features);
104
  cfg_tags = grapes_config_parse(config);
105
  res = grapes_config_value_int_default(cfg_tags, "cache_size", &context->cache_size, DEFAULT_CACHE_SIZE);
106
  res = grapes_config_value_int_default(cfg_tags, "max_timestamp", &max_timestamp, DEFAULT_MAX_TIMESTAMP);
107
  res = grapes_config_value_int_default(cfg_tags, "period", &context->period, DEFAULT_PERIOD);
108
  res = grapes_config_value_int_default(cfg_tags, "bootstrap_period", &context->bootstrap_period, DEFAULT_BOOTSTRAP_PERIOD);
109
  res = grapes_config_value_int_default(cfg_tags, "bootstrap_cycles", &context->bootstrap_cycles, DEFAULT_BOOTSTRAP_CYCLES);
110
  res = grapes_config_value_int_default(cfg_tags, "adaptive", &context->adaptive, plus_features);
111
  res = grapes_config_value_int_default(cfg_tags, "restart", &context->restart, plus_features);
112
  res = grapes_config_value_int_default(cfg_tags, "randomize", &context->randomize, plus_features);
113
  res = grapes_config_value_int_default(cfg_tags, "slowstart", &context->slowstart, plus_features);
114 114
  free(cfg_tags);
115 115

  
116 116
  context->local_cache = cache_init(context->cache_size, metadata_size, max_timestamp);
src/PeerSampler/peersampler.c
6 6
#include "net_helper.h"
7 7
#include "peersampler.h"
8 8
#include "peersampler_iface.h"
9
#include "config.h"
9
#include "grapes_config.h"
10 10

  
11 11
extern struct peersampler_iface ncast;
12 12
extern struct peersampler_iface ncastplus;
......
32 32
  if (!tc) return NULL;
33 33

  
34 34
  tc->ps = &ncastplus;
35
  cfg_tags = config_parse(config);
36
  proto = config_value_str(cfg_tags, "protocol");
35
  cfg_tags = grapes_config_parse(config);
36
  proto = grapes_config_value_str(cfg_tags, "protocol");
37 37
  if (proto) {
38 38
    if (strcmp(proto, "newscast") == 0) {
39 39
      tc->ps = &ncast;
src/PeerSet/peerset_ops.c
15 15
#include "peerset.h"
16 16
#include "chunkidset.h"
17 17
#include "net_helper.h"
18
#include "config.h"
18
#include "grapes_config.h"
19 19

  
20 20
#define DEFAULT_SIZE_INCREMENT 32
21 21

  
......
69 69
    return NULL;
70 70
  }
71 71
  p->n_elements = 0;
72
  cfg_tags = config_parse(config);
72
  cfg_tags = grapes_config_parse(config);
73 73
  if (!cfg_tags) {
74 74
    free(p);
75 75
    return NULL;
76 76
  }
77
  res = config_value_int(cfg_tags, "size", &p->size);
77
  res = grapes_config_value_int(cfg_tags, "size", &p->size);
78 78
  if (!res) {
79 79
    p->size = 0;
80 80
  }
src/Tests/config_test.c
13 13
  struct tag *cfg_tags;
14 14
  int size=-1, len=-1, dummy=-1, res;
15 15
  
16
  cfg_tags = config_parse("size=10");
17
  res = config_value_int(cfg_tags, "size", &size);
16
  cfg_tags = grapes_config_parse("size=10");
17
  res = grapes_config_value_int(cfg_tags, "size", &size);
18 18
  printf("%d: Is %d = %d?\n", res, size, 10);
19 19
  free(cfg_tags);
20 20

  
21
  cfg_tags = config_parse("len=5,size=10");
22
  res = config_value_int(cfg_tags, "size", &size);
23
  res = config_value_int(cfg_tags, "len", &len);
21
  cfg_tags = grapes_config_parse("len=5,size=10");
22
  res = grapes_config_value_int(cfg_tags, "size", &size);
23
  res = grapes_config_value_int(cfg_tags, "len", &len);
24 24
  printf("%d: Is %d = %d?\n", res, size, 10);
25 25
  printf("%d: Is %d = %d?\n", res, len, 5);
26
  res = config_value_int(cfg_tags, "blah", &dummy);
26
  res = grapes_config_value_int(cfg_tags, "blah", &dummy);
27 27
  printf("%d: Is %d = ...?\n", res, dummy);
28 28
  free(cfg_tags);
29 29

  
src/TopologyManager/dumbTopman.c
14 14

  
15 15
#include "net_helper.h"
16 16
#include "../Cache/topocache.h"
17
#include "config.h"
17
#include "grapes_config.h"
18 18
#include "topman_iface.h"
19 19

  
20 20
#define DUMB_DEFAULT_MEM	20
......
56 56
	struct tag *cfg_tags;
57 57
	int res;
58 58

  
59
	cfg_tags = config_parse(config);
60
	res = config_value_int(cfg_tags, "cache_size", &cache_size);
59
	cfg_tags = grapes_config_parse(config);
60
	res = grapes_config_value_int(cfg_tags, "cache_size", &cache_size);
61 61
	if (!res) {
62 62
		cache_size = DUMB_DEFAULT_CSIZE;
63 63
	}
64
	res = config_value_int(cfg_tags, "memory", &memory);
64
	res = grapes_config_value_int(cfg_tags, "memory", &memory);
65 65
	if (!res) {
66 66
		memory = DUMB_DEFAULT_MEM;
67 67
	}
68
	res = config_value_int(cfg_tags, "period", &period);
68
	res = grapes_config_value_int(cfg_tags, "period", &period);
69 69
	if (!res) {
70 70
		period = DUMB_DEFAULT_PERIOD;
71 71
	}
src/TopologyManager/tman.c
16 16
#include "../Cache/blist_proto.h"
17 17
#include "../Cache/proto.h"
18 18
#include "grapes_msg_types.h"
19
#include "config.h"
19
#include "grapes_config.h"
20 20
#include "topman_iface.h"
21 21

  
22 22
#define TMAN_INIT_PEERS 10 // max # of neighbors in local cache (should be >= than the next)
......
70 70
	struct tag *cfg_tags;
71 71
	int res;
72 72

  
73
	cfg_tags = config_parse(config);
74
	res = config_value_int(cfg_tags, "cache_size", &init_cache_size);
73
	cfg_tags = grapes_config_parse(config);
74
	res = grapes_config_value_int(cfg_tags, "cache_size", &init_cache_size);
75 75
	if (!res) {
76 76
		init_cache_size = TMAN_INIT_PEERS;
77 77
	}
78 78
	cache_size = init_cache_size;
79
	res = config_value_int(cfg_tags, "max_preferred_peers", &max_preferred_peers);
79
	res = grapes_config_value_int(cfg_tags, "max_preferred_peers", &max_preferred_peers);
80 80
	if (!res) {
81 81
		max_preferred_peers = TMAN_MAX_PREFERRED_PEERS;
82 82
	}
83
	res = config_value_int(cfg_tags, "max_gossiping_peers", &max_gossiping_peers);
83
	res = grapes_config_value_int(cfg_tags, "max_gossiping_peers", &max_gossiping_peers);
84 84
	if (!res) {
85 85
		max_gossiping_peers = TMAN_MAX_GOSSIPING_PEERS;
86 86
	}
87
	res = config_value_int(cfg_tags, "period", &default_period);
87
	res = grapes_config_value_int(cfg_tags, "period", &default_period);
88 88
	if (!res) {
89 89
		default_period = TMAN_STD_PERIOD;
90 90
	}
src/config.c
1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

  
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdio.h>
10
#include "config.h"
11

  
12
#define NAME_SIZE 32
13
#define VAL_SIZE 64
14

  
15
struct tag {
16
  char name[NAME_SIZE];
17
  char value[VAL_SIZE];
18
};
19

  
20
#define MAX_TAGS 16
21

  
22
struct tag *config_parse(const char *cfg)
23
{
24
  struct tag *res;
25
  int i = 0;
26
  const char *p = cfg;
27

  
28
  res = malloc(sizeof(struct tag) * MAX_TAGS);
29
  if (res == NULL) {
30
    return res;
31
  }
32
  while (p && *p != 0) {
33
    char *p1 = strchr(p, '=');
34
    
35
    memset(res[i].name, 0, NAME_SIZE);
36
    memset(res[i].value, 0, VAL_SIZE);
37
    if (p1) {
38
      if (i % MAX_TAGS == 0) {
39
        res = realloc(res, sizeof(struct tag) * (i + MAX_TAGS));
40
      }
41
      if (p1 - p > NAME_SIZE - 1) {
42
        fprintf(stderr, "Error, config name too long:%s\n", p);
43
        free(res);
44
        return NULL;
45
      }
46
      memcpy(res[i].name, p, p1 - p);
47
      p = strchr(p1, ',');
48
      if (p == NULL) {
49
        p = p1 + strlen(p1);
50
      }
51
      if (p - p1 > VAL_SIZE - 1) {
52
        fprintf(stderr, "Error, config value too long:%s\n", p1);
53
        free(res);
54
        return NULL;
55
      }
56
      memcpy(res[i++].value, p1 + 1, p - p1 - 1);
57
      if (*p) p++;
58
    } else {
59
      p = NULL;
60
    }
61
  }
62
  memset(res[i].name, 0, NAME_SIZE);
63
  memset(res[i].value, 0, VAL_SIZE);
64

  
65
  res = realloc(res, sizeof(struct tag) * (i + 1));
66

  
67
  return res;
68
}
69

  
70
const char *config_value_str(const struct tag *cfg_values, const char *value)
71
{
72
  int i, done;
73

  
74
  i = 0; done = 0;
75
  while (!done) {
76
    if (!strcmp(cfg_values[i].name, value)) {
77
      return cfg_values[i].value;
78
    }
79
    if (cfg_values[i].name[0] == 0) {
80
      done = 1;
81
    } else {
82
      i++;
83
    }
84
  }
85

  
86
  return NULL;
87
}
88

  
89
int config_value_int(const struct tag *cfg_values, const char *value, int *res)
90
{
91
  const char *str_res;
92

  
93
  str_res = config_value_str(cfg_values, value);
94
  if (str_res == NULL) {
95
    return 0;
96
  }
97

  
98
  *res = atoi(str_res);
99

  
100
  return 1;
101
}
102

  
103
int config_value_double(const struct tag *cfg_values, const char *value, double *res)
104
{
105
  const char *str_res;
106

  
107
  str_res = config_value_str(cfg_values, value);
108
  if (str_res == NULL) {
109
    return 0;
110
  }
111

  
112
  *res = strtod(str_res, NULL);
113

  
114
  return 1;
115
}
116

  
117
const char *config_value_str_default(const struct tag *cfg_values, const char *value, const char *default_value)
118
{
119
  const char *res;
120

  
121
  res = config_value_str(cfg_values, value);
122
  return res ? res : default_value;
123
}
124

  
125
int config_value_int_default(const struct tag *cfg_values, const char *value, int *res, int default_value)
126
{
127
  int r;
128

  
129
  r = config_value_int(cfg_values, value, res);
130
  if (!r) {
131
    *res = default_value;
132
  }
133
  return r;
134
}
135

  
136
int config_value_double_default(const struct tag *cfg_values, const char *value, double *res, double default_value)
137
{
138
  int r;
139

  
140
  r = config_value_double(cfg_values, value, res);
141
  if (!r) {
142
    *res = default_value;
143
  }
144
  return r;
145
}
src/config.h
1
#ifndef CONFIG_H
2
#define CONFIG_H
3
struct tag;
4

  
5
/*
6
 * earlier name-value pairs in the config string take precedence
7
 */
8

  
9
struct tag *config_parse(const char *cfg);
10
int config_value_int(const struct tag *cfg_values, const char *value, int *res);
11
int config_value_int_default(const struct tag *cfg_values, const char *value, int *res, int default_value);
12
int config_value_double(const struct tag *cfg_values, const char *value, double *res);
13
int config_value_double_default(const struct tag *cfg_values, const char *value, double *res, double default_value);
14
const char *config_value_str(const struct tag *cfg_values, const char *value);
15
const char *config_value_str_default(const struct tag *cfg_values, const char *value, const char *default_value);
16

  
17
#endif /* CONFIG_H */
src/grapes_config.c
1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

  
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdio.h>
10
#include "grapes_config.h"
11

  
12
#define NAME_SIZE 32
13
#define VAL_SIZE 64
14

  
15
struct tag {
16
  char name[NAME_SIZE];
17
  char value[VAL_SIZE];
18
};
19

  
20
#define MAX_TAGS 16
21

  
22
struct tag *grapes_config_parse(const char *cfg)
23
{
24
  struct tag *res;
25
  int i = 0;
26
  const char *p = cfg;
27

  
28
  res = malloc(sizeof(struct tag) * MAX_TAGS);
29
  if (res == NULL) {
30
    return res;
31
  }
32
  while (p && *p != 0) {
33
    char *p1 = strchr(p, '=');
34
    
35
    memset(res[i].name, 0, NAME_SIZE);
36
    memset(res[i].value, 0, VAL_SIZE);
37
    if (p1) {
38
      if (i % MAX_TAGS == 0) {
39
        res = realloc(res, sizeof(struct tag) * (i + MAX_TAGS));
40
      }
41
      if (p1 - p > NAME_SIZE - 1) {
42
        fprintf(stderr, "Error, config name too long:%s\n", p);
43
        free(res);
44
        return NULL;
45
      }
46
      memcpy(res[i].name, p, p1 - p);
47
      p = strchr(p1, ',');
48
      if (p == NULL) {
49
        p = p1 + strlen(p1);
50
      }
51
      if (p - p1 > VAL_SIZE - 1) {
52
        fprintf(stderr, "Error, config value too long:%s\n", p1);
53
        free(res);
54
        return NULL;
55
      }
56
      memcpy(res[i++].value, p1 + 1, p - p1 - 1);
57
      if (*p) p++;
58
    } else {
59
      p = NULL;
60
    }
61
  }
62
  memset(res[i].name, 0, NAME_SIZE);
63
  memset(res[i].value, 0, VAL_SIZE);
64

  
65
  res = realloc(res, sizeof(struct tag) * (i + 1));
66

  
67
  return res;
68
}
69

  
70
const char *grapes_config_value_str(const struct tag *cfg_values, const char *value)
71
{
72
  int i, done;
73

  
74
  i = 0; done = 0;
75
  while (!done) {
76
    if (!strcmp(cfg_values[i].name, value)) {
77
      return cfg_values[i].value;
78
    }
79
    if (cfg_values[i].name[0] == 0) {
80
      done = 1;
81
    } else {
82
      i++;
83
    }
84
  }
85

  
86
  return NULL;
87
}
88

  
89
int grapes_config_value_int(const struct tag *cfg_values, const char *value, int *res)
90
{
91
  const char *str_res;
92

  
93
  str_res = grapes_config_value_str(cfg_values, value);
94
  if (str_res == NULL) {
95
    return 0;
96
  }
97

  
98
  *res = atoi(str_res);
99

  
100
  return 1;
101
}
102

  
103
int grapes_config_value_double(const struct tag *cfg_values, const char *value, double *res)
104
{
105
  const char *str_res;
106

  
107
  str_res = grapes_config_value_str(cfg_values, value);
108
  if (str_res == NULL) {
109
    return 0;
110
  }
111

  
112
  *res = strtod(str_res, NULL);
113

  
114
  return 1;
115
}
116

  
117
const char *grapes_config_value_str_default(const struct tag *cfg_values, const char *value, const char *default_value)
118
{
119
  const char *res;
120

  
121
  res = grapes_config_value_str(cfg_values, value);
122
  return res ? res : default_value;
123
}
124

  
125
int grapes_config_value_int_default(const struct tag *cfg_values, const char *value, int *res, int default_value)
126
{
127
  int r;
128

  
129
  r = grapes_config_value_int(cfg_values, value, res);
130
  if (!r) {
131
    *res = default_value;
132
  }
133
  return r;
134
}
135

  
136
int grapes_config_value_double_default(const struct tag *cfg_values, const char *value, double *res, double default_value)
137
{
138
  int r;
139

  
140
  r = grapes_config_value_double(cfg_values, value, res);
141
  if (!r) {
142
    *res = default_value;
143
  }
144
  return r;
145
}

Also available in: Unified diff