~ecs/ini

d92632237be03e87f9cc3ff76918eeb085382384 — Eyal Sawady 10 months ago c51b334
frontend.c: handle '-' file
1 files changed, 22 insertions(+), 9 deletions(-)

M frontend.c
M frontend.c => frontend.c +22 -9
@@ 126,24 126,30 @@ static int value_handler(void *user, const char *section,
 */
int main(int argc, char **argv)
{
	FILE *file;
	if (argc >= 2) {
		if (!strcmp(argv[1], "-")) {
			file = stdin;
		} else {
			file = fopen(argv[1], "r");
		}
	}
	switch (argc) {
	case 2:
	{
		struct array sections = {0, 8, NULL};
		sections.sections = malloc(8 * sizeof(char *));

		if (ini_parse(argv[1], section_list_handler, &sections) < 0) {
		if (ini_parse_file(file, section_list_handler, &sections) < 0) {
			deinit_array(sections);
			printf("Cant load '%s'\n", argv[1]);
			return 2;
			goto fail;
		}
		deinit_array(sections);
		break;
	}
	case 3:
		if (ini_parse(argv[1], key_list_handler, argv[2]) < 0) {
			printf("Cant load '%s'\n", argv[1]);
			return 2;
		if (ini_parse_file(file, key_list_handler, argv[2]) < 0) {
			goto fail;
		}
		break;
	case 4:


@@ 152,15 158,22 @@ int main(int argc, char **argv)
		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;
		if (ini_parse_file(file, value_handler, &ini_value) < 0) {
			goto fail;
		}
		break;
	}
	default:
		printf("Usage: %s <file> [<section> [<key>]]\n", argv[0]);
		if (argc >= 2) {
			fclose(file);
		}
		return 1;
	}
	fclose(file);
	return 0;
fail:
	fclose(file);
	printf("Can't load '%s'\n", argv[1]);
	return 2;
}