~ecs/ini

f8b7c3c62f29459b56d24ffd9889b9538aa74988 — Eyal Sawady 1 year, 4 months ago
Initial commit
7 files changed, 211 insertions(+), 0 deletions(-)

A .gitmodules
A LICENSE
A LICENSE.inih
A Makefile
A README.md
A frontend.c
A inih
A  => .gitmodules +3 -0
@@ 1,3 @@
[submodule "inih"]
	path = inih
	url = https://github.com/benhoyt/inih

A  => LICENSE +19 -0
@@ 1,19 @@
Copyright (c) 2019 Eyal Sawady

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => LICENSE.inih +27 -0
@@ 1,27 @@

The "inih" library is distributed under the New BSD license:

Copyright (c) 2009, Ben Hoyt
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Ben Hoyt nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY BEN HOYT ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN HOYT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

A  => Makefile +11 -0
@@ 1,11 @@
CC = musl-gcc
CFLAGS = -static -Wall -Wextra -pedantic -O2

ini: inih-target frontend.c
	$(CC) $(CFLAGS) frontend.c ini.o -o ini

inih-target: inih/ini.c
	$(CC) -Iinih $(CFLAGS) -c inih/ini.c -o ini.o

clean:
	rm -f ini.o ini

A  => README.md +11 -0
@@ 1,11 @@
# Simple CLI INI parser

That's it.

## Status

Feature-complete, may have bugs, and lacks documentation.

# License

MIT

A  => frontend.c +139 -0
@@ 1,139 @@
/* Copyright (c) 2019 Eyal Sawady
 *
 * Frontend for Ben Hoyt's inih library.
 * inih is licensed under the 3-clause BSD license, and this frontend
 * is licensed under the MIT license. See LICENSE and LICENSE.inih for
 * details.
 *
 * Interface:
 * 	0 opts:  Help message
 * 	1 opt:   List all sections in file
 * 	2 opts:  List all keys in a section
 * 	3 opts:  Value of key in section
 * 	>3 opts: Help message
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inih/ini.h"

/* Invariants:
 *   size <= capacity
 *   sections is an array of *unique* strings.
 */
struct array {
	int size;
	int capacity;
	char **sections;
};

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++) {
		if (strcmp(sections->sections[i], section) == 0) {
			found_match = true;
			break;
		}
	}

	if (!found_match) {
		printf("%s\n", section);
		if (sections->size + 1 > sections->capacity) {
			sections->capacity *= 2;
			sections->sections = realloc(sections->sections,
					sections->capacity * sizeof(char *));
		}
		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++;
	}
	return 1;
}

struct key_array {
	char *section;
	struct array *keys;
};

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) {
		return 1;
	}

	printf("%s\n", key);
	return 1;
}

struct ini_value {
	char *section;
	char *key;
};

static int value_handler(void *user, const char *section,
		const char *key, const char *value)
{
	struct ini_value *ini_value = (struct ini_value *)user;

	if (strcmp(ini_value->section, section) != 0) {
		return 1;
	}
	if (strcmp(ini_value->key, key) != 0) {
		return 1;
	}

	printf("%s\n", value);
	return 1;
}

/* Exit status:
 *   0: Success
 *   1: Improper usage
 *   2: Could not load INI file
 *
 * In order to avoid complexity, if there's no match, the exit status
 * will be 0 and nothing will be printed.
 */
int main(int argc, char **argv)
{
	if (argc == 1 || argc > 4) {
		printf("Usage: %s <file> [<section> [<key>]]\n", argv[0]);
		return 1;
	} else if (argc == 2) {
		struct array sections = {0, 8, NULL};
		sections.sections = malloc(8 * sizeof(char *));

		if (ini_parse(argv[1], section_list_handler, &sections) < 0) {
			printf("Cant load '%s'\n", argv[1]);
			return 2;
		}
	} else if (argc == 3) {
		if (ini_parse(argv[1], key_list_handler, argv[2]) < 0) {
			printf("Cant load '%s'\n", argv[1]);
			return 2;
		}

	} else {
		struct ini_value ini_value = {argv[2], argv[3]};

		if (ini_parse(argv[1], value_handler, &ini_value) < 0) {
			printf("Cant load '%s'\n", argv[1]);
			return 2;
		}
	}
	return 0;
}

A  => inih +1 -0
@@ 1,1 @@
Subproject commit 185923c7f3620b3eb58cef01e343189c676a0954