~ecs/ini

1ea3a76c98c5bb7832a911da81c453937a0b7da8 — Eyal Sawady 1 year, 20 days ago bb99dce
Use ANSI C
1 files changed, 27 insertions(+), 15 deletions(-)

M frontend.c
M frontend.c => frontend.c +27 -15
@@ 31,7 31,8 @@ struct array {

static void deinit_array(struct array arr)
{
	for (int i = 0; i < arr.size; i++) {
	int i;
	for (i = 0; i < arr.size; i++) {
		free(arr.sections[i]);
	}
	free(arr.sections);


@@ 40,12 41,11 @@ static void deinit_array(struct array arr)
static int section_list_handler(void *user, const char *section,
		const char *key, const char *value)
{
	(void)key, (void)value; /* unused */
	struct array *sections = (struct array *)user;

	bool found_match = false;

	for (int i = 0; i < sections->size; i++) {
	int i;
	for (i = 0; i < sections->size; i++) {
		if (strcmp(sections->sections[i], section) == 0) {
			found_match = true;
			break;


@@ 55,16 55,23 @@ static int section_list_handler(void *user, const char *section,
	if (!found_match) {
		printf("%s\n", section);
		if (sections->size + 1 > sections->capacity) {
			size_t oldbcap =
				(size_t)sections->capacity * sizeof(char *);
			sections->capacity *= 2;
			sections->sections = realloc(sections->sections,
					sections->capacity * sizeof(char *));
			sections->sections =
				realloc(sections->sections, oldbcap * 2);
		}
		{
			size_t section_len = strlen(section);
			sections->sections[sections->size] =
				malloc(section_len + 1);
			strncpy(sections->sections[sections->size], section,
					section_len + 1);
			sections->size++;
		}
		size_t section_len = strlen(section);
		sections->sections[sections->size] = malloc(section_len + 1);
		strncpy(sections->sections[sections->size], section,
				section_len + 1);
		sections->size++;
	}

	(void)key, (void)value; /* unused */
	return 1;
}



@@ 76,7 83,6 @@ struct key_array {
static int key_list_handler(void *user, const char *section,
		const char *key, const char *value)
{
	(void)value; /* unused */
	char *section_to_match = (char *)user;

	if (strcmp(section_to_match, section) != 0) {


@@ 84,6 90,8 @@ static int key_list_handler(void *user, const char *section,
	}

	printf("%s\n", key);

	(void)value; /* unused */
	return 1;
}



@@ 120,7 128,7 @@ int main(int argc, char **argv)
{
	switch (argc) {
	case 2:
		/* this space intentionally left blank */;
	{
		struct array sections = {0, 8, NULL};
		sections.sections = malloc(8 * sizeof(char *));



@@ 131,6 139,7 @@ int main(int argc, char **argv)
		}
		deinit_array(sections);
		break;
	}
	case 3:
		if (ini_parse(argv[1], key_list_handler, argv[2]) < 0) {
			printf("Cant load '%s'\n", argv[1]);


@@ 138,14 147,17 @@ int main(int argc, char **argv)
		}
		break;
	case 4:
		/* this space intentionally left blank */;
		struct ini_value ini_value = {argv[2], argv[3]};
	{
		struct ini_value ini_value;
		ini_value.section = argv[2];
		ini_value.key = argv[3];

		if (ini_parse(argv[1], value_handler, &ini_value) < 0) {
			printf("Cant load '%s'\n", argv[1]);
			return 2;
		}
		break;
	}
	default:
		printf("Usage: %s <file> [<section> [<key>]]\n", argv[0]);
		return 1;