Diff of /checkconfig.c [000000] .. [07a747]  Maximize  Restore

Switch to side-by-side view

--- a
+++ b/checkconfig.c
@@ -0,0 +1,138 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <libconfig.h>
+
+
+#define DEFCONFIGFILE "/etc/oggstreamer.cfg"
+     
+enum {
+T_BOOL,
+T_INT,
+T_STRING
+} typedef CONFIG_MODE;
+
+struct {
+	const char *string;
+	int integer;
+} typedef RETURN_VALUES;
+
+config_t cfg;
+
+
+int readconfig(CONFIG_MODE mode, RETURN_VALUES *rets, char *string, char *file)
+{
+	int ret=1;	
+	
+	config_init(&cfg);
+	
+	/* Read the file. If there is an error, report it and exit. */
+	if(! config_read_file(&cfg, file) )
+	{
+		fprintf(stderr, "%s:%d - %s\n", file,
+        	config_error_line(&cfg), config_error_text(&cfg));
+		config_destroy(&cfg);
+		return -1;
+	}
+
+	switch(mode)
+	{
+		case T_BOOL:
+			if(!config_lookup_bool(&cfg,string, &(rets->integer)) )
+				ret = -1;
+		break;
+		case T_INT:
+			if(!config_lookup_int(&cfg,string, &(rets->integer)) )
+				ret = -1;
+		break;
+		case T_STRING:
+			if(!config_lookup_string(&cfg,string, &(rets->string)) )
+				ret = -1;
+		break;
+	}
+
+	return ret;
+}
+
+void print_usage()
+{
+	fprintf(stderr,	
+	"checkconfig options name [configfile]\n" \
+	"\t-b BOOL entry\n" \
+	"\t-i INTEGER entry\n" \
+	"\t-s STRING entry\n" 
+	"\tname of the configstring\n" \
+	"\tconfigfile (default /etc/oggstreamer.cfg)\n");
+			
+}
+
+int main (int argc, char **argv)
+{
+	CONFIG_MODE mode;
+	RETURN_VALUES rets;
+	char *string,*file=NULL;
+
+	if( !strncmp(argv[1], "-b", 2 ) )
+	{
+		mode = T_BOOL;
+	}
+	else if ( !strncmp(argv[1], "-i", 2) )
+	{
+		mode = T_INT;
+	}
+	else if ( !strncmp(argv[1], "-s", 2) )
+	{
+		mode = T_STRING;
+	} 
+	else 
+	{
+		print_usage();
+		return -1;
+	}	
+
+	if (argc == 3)
+	{
+		string = argv[2];
+	} 
+	else if (argc == 4)
+	{
+		string = argv[2];
+		file = argv[3];
+	} 
+	else 
+	{
+		print_usage();
+		return -1;
+	}	
+
+	if(file == NULL) file = DEFCONFIGFILE;
+
+	if(readconfig(mode,&rets,string,file) == -1)
+	{
+		fprintf(stderr,"Error reading Configstring \"%s\" in File \"%s\"\n", string, file);
+		return -1;
+	} else {
+		switch(mode)
+		{
+			case T_BOOL:
+				if(rets.integer)
+					printf("TRUE");
+				else
+					printf("FALSE");
+			break;
+			case T_INT:
+				printf("%d",rets.integer);
+			break;
+			case T_STRING:
+				printf("%s",rets.string);
+			break;
+		}
+	}
+	
+
+	return 1;
+}
+
+