aboutsummaryrefslogtreecommitdiffstats
path: root/lib/librte_cfgfile/rte_cfgfile.c
blob: 614269634109a6263902a3c3f339c6cd4cde4aaf (plain)
1
2
3
4
5
6
7
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
40
41
42
43
44
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <rte_string_fns.h>
#include <rte_common.h>

#include "rte_cfgfile.h"

struct rte_cfgfile_section {
	char name[CFG_NAME_LEN];
	int num_entries;
	int allocated_entries;
	struct rte_cfgfile_entry *entries;
};

struct rte_cfgfile {
	int flags;
	int num_sections;
	int allocated_sections;
	struct rte_cfgfile_section *sections;
};

/** when we resize a file structure, how many extra entries
 * for new sections do we add in */
#define CFG_ALLOC_SECTION_BATCH 8
/** when we resize a section structure, how many extra entries
 * for new entries do we add in */
#define CFG_ALLOC_ENTRY_BATCH 16

/**
 * Default cfgfile load parameters.
 */
static const struct rte_cfgfile_parameters default_cfgfile_params = {
	.comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
};

/**
 * Defines the list of acceptable comment characters supported by this
 * library.
 */
static const char valid_comment_chars[] = {
	'!',
	'#',
	'%',
	';',
	'@'
};

static unsigned
_strip(char *str, unsigned len)
{
	int newlen = len;
	if (len == 0)
		return 0;

	if (isspace(str[len-1])) {
		/* strip trailing whitespace */
		while (newlen > 0 && isspace(str[newlen - 1]))
			str[--newlen] = '\0';
	}

	if (isspace(str[0])) {
		/* strip leading whitespace */
		int i, start = 1;
		while (isspace(str[start]) && start < newlen)
			start++
			; /* do nothing */
		newlen -= start;
		for (i = 0; i < newlen; i++)
			str[i] = str[i+start];
		str[i] = '\0';
	}
	return newlen;
}

static struct rte_cfgfile_section *
_get_section(struct rte_cfgfile *cfg, const char *sectionname)
{
	int i;

	for (i = 0; i < cfg->num_sections; i++) {
		if (strncmp(cfg->sections[i].name, sectionname,
				sizeof(cfg->sections[0].name)) == 0)
			return &cfg->sections[i];
	}
	return NULL;
}

static int
_add_entry(struct rte_cfgfile_section *section, const char *entryname,
		const char *entryvalue)
{
	/* resize entry structure if we don't have room for more entries */
	if (section->num_entries == section->allocated_entries) {
		struct rte_cfgfile_entry *n_entries = realloc(
				section->entries,
				sizeof(struct rte_cfgfile_entry) *
				((section->allocated_entries) +
						CFG_ALLOC_ENTRY_BATCH));

		if (n_entries == NULL)
			return -ENOMEM;

		section->entries = n_entries;
		section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
	}
	/* fill up entry fields with key name and value */
	struct rte_cfgfile_entry *curr_entry =
					&section->entries[section->num_entries];

	snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
	snprintf(curr_entry->value,
				sizeof(curr_entry->value), "%s", entryvalue);
	section->num_entries++;

	return 0;
}

static int
rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
{
	unsigned int valid_comment;
	unsigned int i;

	if (!params) {
		printf("Error - missing cfgfile parameters\n");
		return -EINVAL;
	}

	valid_comment = 0;
	for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
		if (params->comment_character == valid_comment_chars[i]) {
			valid_comment = 1;
			break;
		}
	}

	if (valid_comment == 0)	{
		printf("Error - invalid comment characters %c\n",
		       params->comment_character);
		return -ENOTSUP;
	}

	return 0;
}

struct rte_cfgfile *
rte_cfgfile_load(const char *filename, int flags)
{
	return rte_cfgfile_load_with_params(filename, flags,
					    &default_cfgfile_params);
}

struct rte_cfgfile *
rte_cfgfile_load_with_params(const char *filename, int flags,
			     const struct rte_cfgfile_parameters *params)
{
	char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
	int lineno = 0;
	struct rte_cfgfile *cfg = NULL;

	if (rte_cfgfile_check_params(params))
		return NULL;

	FILE *f = fopen(filename, "r");
	if (f == NULL)
		return NULL;

	cfg = rte_cfgfile_create(flags);

	while (fgets(buffer, sizeof(buffer), f) != NULL) {
		char *pos = NULL;
		size_t len = strnlen(buffer, sizeof(buffer));
		lineno++;
		if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
			printf("Error line %d - no \\n found on string. "
					"Check if line too long\n", lineno);
			goto error1;
		}
		/* skip parsing if comment character found */
		pos = memchr(buffer, params->comment_character, len);
		if (pos != NULL && (*(pos-1) != '\\')) {
			*pos = '\0';
			len = pos -  buffer;
		}

		len = _strip(buffer, len);
		/* skip lines without useful content */
		if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
			continue;

		if (buffer[0] == '[') {
			/* section heading line */
			char *end = memchr(buffer, ']', len);
			if (end == NULL) {
				printf("Error line %d - no terminating ']'"
					"character found\n", lineno);
				goto error1;
			}
			*end = '\0';
			_strip(&buffer[1], end - &buffer[1]);

			rte_cfgfile_add_section(cfg, &buffer[1]);
		} else {
			/* key and value line */
			char *split[2] = {NULL};

			split[0] = buffer;
			split[1] = memchr(buffer, '=', len);
			if (split[1] == NULL) {
				printf("Error line %d - no '='"
					"character found\n", lineno);
				goto error1;
			}
			*split[1] = '\0';
			split[1]++;

			_strip(split[0], strlen(split[0]));
			_strip(split[1], strlen(split[1]));
			char *end = memchr(split[1], '\\', strlen(split[1]));

			size_t split_len = strlen(split[1]) + 1;
			while (end != NULL) {
				if (*(end+1) == params->comment_character) {
					*end = '\0';
					strlcat(split[1], end+1, split_len);
				} else
					end++;
				end = memchr(end, '\\', strlen(end));
			}

			if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
					(*split[1] == '\0')) {
				printf("Error at line %d - cannot use empty "
							"values\n", lineno);
				goto error1;
			}

			if (cfg->num_sections == 0)
				goto error1;

			_add_entry(&cfg->sections[cfg->num_sections - 1],
					split[0], split[1]);
		}
	}
	fclose(f);
	return cfg;
error1:
	rte_cfgfile_close(cfg);
	fclose(f);
	return NULL;
}

struct rte_cfgfile *
rte_cfgfile_create(int flags)
{
	int i;
	struct rte_cfgfile *cfg = NULL;

	cfg = malloc(sizeof(*cfg));

	if (cfg == NULL)
		return NULL;

	cfg->flags = flags;
	cfg->num_sections = 0;

	/* allocate first batch of sections and entries */
	cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
			CFG_ALLOC_SECTION_BATCH);

	if (cfg->sections == NULL)
		goto error1;

	cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;

	for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
		cfg->sections[i].entries = malloc(sizeof(
			struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);

		if (cfg->sections[i].entries == NULL)
			goto error1;

		cfg->sections[i].num_entries = 0;
		cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
	}

	if (flags & CFG_FLAG_GLOBAL_SECTION)
		rte_cfgfile_add_section(cfg, "GLOBAL");

	return cfg;
error1:
	if (cfg->sections != NULL) {
		for (i = 0; i < cfg->allocated_sections; i++) {
			if (cfg->sections[i].entries != NULL) {
				free(cfg->sections[i].entries);
				cfg->sections[i].entries = NULL;
			}
		}
		free(cfg->sections);
		cfg->sections = NULL;
	}
	free(cfg);
	return NULL;
}

int
rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
{
	int i;

	if (cfg == NULL)
		return -EINVAL;

	if (sectionname == NULL)
		return -EINVAL;

	/* resize overall struct if we don't have room for more	sections */
	if (cfg->num_sections == cfg->allocated_sections) {

		struct rte_cfgfile_section *n_sections =
				realloc(cfg->sections,
				sizeof(struct rte_cfgfile_section) *
				((cfg->allocated_sections) +
				CFG_ALLOC_SECTION_BATCH));

		if (n_sections == NULL)
			return -ENOMEM;

		for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
			n_sections[i + cfg->allocated_sections].num_entries = 0;
			n_sections[i +
				 cfg->allocated_sections].allocated_entries = 0;
			n_sections[i + cfg->allocated_sections].entries = NULL;
		}
		cfg->sections = n_sections;
		cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
	}

	snprintf(cfg->sections[cfg->num_sections].name,
			sizeof(cfg->sections[0].name), "%s", sectionname);
	cfg->sections[cfg->num_sections].num_entries = 0;
	cfg->num_sections++;

	return 0;
}

int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
		const char *sectionname, const char *entryname,
		const char *entryvalue)
{
	int ret;

	if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
			|| (entryvalue == NULL))
		return -EINVAL;

	if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
		return -EEXIST;

	/* search for section pointer by sectionname */
	struct rte_cfgfile_section *curr_section = _get_section(cfg,
								sectionname);
	if (curr_section == NULL)
		return -EINVAL;

	ret = _add_entry(curr_section, entryname, entryvalue);

	return ret;
}

int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
		const char *entryname, const char *entryvalue)
{
	int i;

	if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
		return -EINVAL;

	/* search for section pointer by sectionname */
	struct rte_cfgfile_section *curr_section = _get_section(cfg,
								sectionname);
	if (curr_section == NULL)
		return -EINVAL;

	if (entryvalue == NULL)
		entryvalue = "";

	for (i = 0; i < curr_section->num_entries; i++)
		if (!strcmp(curr_section->entries[i].name, entryname)) {
			snprintf(curr_section->entries[i].value,
					sizeof(curr_section->entries[i].value),
							"%s", entryvalue);
			return 0;
		}
	printf("Error - entry name doesn't exist\n");
	return -EINVAL;
}

int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
{
	int i, j;

	if ((cfg == NULL) || (filename == NULL))
		return -EINVAL;

	FILE *f = fopen(filename, "w");

	if (f == NULL)
		return -EINVAL;

	for (i = 0; i < cfg->num_sections; i++) {
		fprintf(f, "[%s]\n", cfg->sections[i].name);

		for (j = 0; j < cfg->sections[i].num_entries; j++) {
			fprintf(f, "%s=%s\n",
					cfg->sections[i].entries[j].name,
					cfg->sections[i].entries[j].value);
		}
	}
	return fclose(f);
}

int rte_cfgfile_close(struct rte_cfgfile *cfg)
{
	int i;

	if (cfg == NULL)
		return -1;

	if (cfg->sections != NULL) {
		for (i = 0; i < cfg->allocated_sections; i++) {
			if (cfg->sections[i].entries != NULL) {
				free(cfg->sections[i].entries);
				cfg->sections[i].entries = NULL;
			}
		}
		free(cfg->sections);
		cfg->sections = NULL;
	}
	free(cfg);
	cfg = NULL;

	return 0;
}

int
rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
size_t length)
{
	int i;
	int num_sections = 0;
	for (i = 0; i < cfg->num_sections; i++) {
		if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
			num_sections++;
	}
	return num_sections;
}

int
rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
	int max_sections)
{
	int i;

	for (i = 0; i < cfg->num_sections && i < max_sections; i++)
		snprintf(sections[i], CFG_NAME_LEN, "%s",
		cfg->sections[i].name);

	return i;
}

int
rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
{
	return _get_section(cfg, sectionname) != NULL;
}

int
rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
	const char *sectionname)
{
	const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
	if (s == NULL)
		return -1;
	return s->num_entries;
}

int
rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
	char *sectionname, int index)
{
	if (index < 0 || index >= cfg->num_sections)
		return -1;

	const struct rte_cfgfile_section *sect = &(cfg->sections[index]);

	snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
	return sect->num_entries;
}
int
rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
		struct rte_cfgfile_entry *entries, int max_entries)
{
	int i;
	const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
	if (sect == NULL)
		return -1;
	for (i = 0; i < max_entries && i < sect->num_entries; i++)
		entries[i] = sect->entries[i];
	return i;
}

int
rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
		char *sectionname,
		struct rte_cfgfile_entry *entries, int max_entries)
{
	int i;
	const struct rte_cfgfile_section *sect;

	if (index < 0 || index >= cfg->num_sections)
		return -1;
	sect = &cfg->sections[index];
	snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
	for (i = 0; i < max_entries && i < sect->num_entries; i++)
		entries[i] = sect->entries[i];
	return i;
}

const char *
rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
		const char *entryname)
{
	int i;
	const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
	if (sect == NULL)
		return NULL;
	for (i = 0; i < sect->num_entries; i++)
		if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
									== 0)
			return sect->entries[i].value;
	return NULL;
}

int
rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
		const char *entryname)
{
	return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
}