/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * COPYING NOTES
 *
 * test.c -- minimal test for parsing library
 * 
 * Copyright (C) 2003 Roberto A. Foglietta <robang@libero.it>
 * Copyright (C) 2003 GEA-Automotive <fogliettar@gea-automotive.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * REVISION NOTES:
 * released 22-05-2003 by Roberto A. Foglietta
 */

#include <stdio.h>

/*
 * This example needs the iofile library sources that it's free software too.
 * You could download from the same site of this file or you could ask to the 
 * author of this file (see below). I/O file library sources was not included
 * in this project as assurance you will use the latest bugs-free version! ;-)
 */
#include "iofile.h"
#include "parser.h"

#define HELP_STRING "\n\n\tusage: test [nomefile]\n\n"


int main(int argc, char **argv)
{
	long lun;
	char *pt;
	unsigned char *text;
	
	/* check the command line argument */
	if(argc != 2) {
		printf(HELP_STRING);
		return 1;
	}
	text = read_file_to_buffer((const char *)argv[1], "r", &lun);
	
	pt = (char *)text;
	while (pt != NULL) {
		/* RAF 2003-05-22:
		 * I'm imaging to have some quite similar sections of my .ini file
		 * but this condition is only one of the many possible examples.
		 * In this case using vector and matrix is the most convenient way.
		 */
		static int s = 0;
		union alltypes *val;
		const char     *name[4]    = { "name",       "type",             "1point",           "2point"};	
		const char     *cast[4][3] = { {"%s", NULL}, {"%s", "%d", NULL}, {"%d", "%d", NULL}, {"%d", "%d", NULL} };
		/* !ATTENTION!   
		 *	                         any 'cast' vector of strings have to end with a NULL pointer like below^
		 *	                         the absence of that NULL terminator pointer will crash the apps! :-(
		 * !ATTENTION! */

		int i;
		char *sc;
		
		pt = grab_section_from_text ((const char *)pt, NULL);
		sc = dup_n_separate_section ((const char *)pt);
		if(sc == NULL) break;
			
		printf("\n I'm parsing the %d section of %s file\n", ++s, argv[1]);	
		for(i = 0; i < 4; i++) {
			printf(" + '%s' parameter has following values:\n", name[i]);
			val = get_mult_param_value_from_text ((const char *)sc, name[i], cast[i]);
			if(val == NULL)  {
				printf(" + -> ERROR: returned NULL pointer\n");
			} else {
				int j = 0; 
				while (val[j].s != NULL && cast[i][j] != NULL) {
					printf(" + - %d value, type '%s' = '", j+1, cast[i][j]);
					printf(cast[i][j], val[j]);
					printf("'\n");
					j++;
				}
			}
		}		
		if(sc) free (sc);
		if(val) free(val);
	}
	
	if(text) free(text);
	return 0;
}