www.pudn.com > edgedetect_canny.rar > config_file.c, change:2006-09-20,size:5423b


#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#include <malloc.h> 
#include <string.h> 
#include <assert.h> 
 
#include "global.h" 
 
 
extern InputParameters ConfigInput; 
extern Mapping Map[]; 
extern char InputFileName[50]; 
extern char OutputFileName[50]; 
extern FILE *bmpInput,*bmpOutput; 
 
void ParseContent (char *buf, int bufsize); 
char *GetConfigFileContent (char *Filename); 
static int ParameterNameToMapIndex (char *s); 
static void PatchInp (); 
 
 
 
void Configure (int ac, char *av[]) 
{ 
	char *content; 
	int CLcount, ContentLen, NumberParams; 
 
	memset (&ConfigInput, 0, sizeof (InputParameters)); 
 
	// Process default config file 
	// Parse the command line 
 
	CLcount = 1; 
	while (CLcount < ac) 
	{ 
		if (0 == strncmp (av[CLcount], "-f", 2))  // A file parameter? 
		{ 
			content = GetConfigFileContent (av[CLcount+1]); 
			printf ("Parsing Configfile %s", av[CLcount+1]); 
			ParseContent (content, strlen (content)); 
			printf ("\n"); 
			free (content); 
			CLcount += 2; 
		}  
		else 
		{ 
			 
			printf ("Error in command line, support -f *.cfg."); 
			exit(0); 
			 
		} 
	} 
	printf ("\n"); 
} 
 
void ParseContent (char *buf, int bufsize) 
{ 
	char *items[100];//max item to parse 
	int MapIdx; 
	int item     = 0; 
	int InString = 0; 
	int InItem   = 0; 
	char *p      = buf; 
	char *bufend = &buf[bufsize]; 
	float fltContent; 
	int i; 
 
	// Stage one: Generate an argc/argv-type list in items[], without comments and whitespace. 
	// This is context insensitive and could be done most easily with lex(1). 
 
	while (p < bufend) 
	{ 
		switch (*p) 
		{ 
		case 13: 
			p++; 
			break; 
		case '#':                 // Found comment 
			*p = '\0';              // Replace '#' with '\0' in case of comment immediately following integer or string 
			while (*p != '\n' && p < bufend)  // Skip till EOL or EOF, whichever comes first 
				p++; 
			InString = 0; 
			InItem = 0; 
			break; 
		case '\n': 
			InItem = 0; 
			InString = 0; 
			*p++='\0'; 
			break; 
		case ' ': 
		case '\t':              // Skip whitespace, leave state unchanged 
			if (InString) 
				p++; 
			else 
			{                     // Terminate non-strings once whitespace is found 
				*p++ = '\0'; 
				InItem = 0; 
			} 
			break; 
		case '"':               // Begin/End of String 
			*p++ = '\0'; 
			if (!InString) 
			{ 
				items[item++] = p; 
				InItem = ~InItem; 
			} 
			else 
				InItem = 0; 
			InString = ~InString; // Toggle 
			break; 
		default: 
			if (!InItem) 
			{ 
				items[item++] = p; 
				InItem = ~InItem; 
			} 
			p++; 
		} 
	} 
 
	item--; 
 
	for (i=0; i<item; i+= 3) 
	{ 
		if (0 > (MapIdx = ParameterNameToMapIndex (items[i]))) 
		{ 
			printf (" Parsing error in config file: Parameter Name '%s' not recognized.", items[i]); 
			exit(0); 
		} 
		if (strcmp ("=", items[i+1])) 
		{ 
			printf (" Parsing error in config file: '=' expected as the second token in each line."); 
			exit(0); 
		} 
 
		// Now interprete the Value, context sensitive... 
		switch (Map[MapIdx].Type) 
		{ 
		case 0:           // Numerical 
			if (1 != sscanf (items[i+2], "%f", &fltContent)) 
			{ 
				printf (" Parsing error: Expected numerical value for Parameter of %s, found '%s'.", items[i], items[i+2]); 
				exit(0); 
			} 
			* (float *) (Map[MapIdx].Place) = fltContent; 
			printf ("."); 
			break; 
		case 1: 
			strcpy ((char *) Map[MapIdx].Place, items [i+2]); 
			printf ("."); 
			break; 
		default: 
			assert ("Unknown value type in the map definition of configfile.h"); 
		} 
	} 
 
	PatchInp(); 
} 
 
 
 
char *GetConfigFileContent (char *Filename) 
{ 
	unsigned FileSize; 
	FILE *f; 
	char *buf; 
 
	if (NULL == (f = fopen (Filename, "r"))) 
	{ 
		printf ("Cannot open configuration file %s.\n", Filename); 
		exit(0); 
	} 
 
	if (0 != fseek (f, 0, SEEK_END)) 
	{ 
		printf ("Cannot fseek in configuration file %s.\n", Filename); 
		exit(0); 
	} 
 
	FileSize = ftell (f); 
 
	if (FileSize < 0 || FileSize > 60000) 
	{ 
		printf ("Unreasonable Filesize %d reported by ftell for configuration file %s.\n", FileSize, Filename); 
		exit(0); 
	} 
 
	if (0 != fseek (f, 0, SEEK_SET)) 
	{ 
		printf ("Cannot fseek in configuration file %s.\n", Filename); 
		exit(0); 
	} 
 
	if ((buf = (char*)malloc (FileSize + 1))==NULL) 
		printf("Could not allocate memory: GetConfigFileContent buf.\n"); 
 
	// Note that ftell() gives us the file size as the file system sees it.  The actual file size, 
	// as reported by fread() below will be often smaller due to CR/LF to CR conversion and/or 
	// control characters after the dos EOF marker in the file. 
 
	FileSize = fread (buf, 1, FileSize, f); 
	buf[FileSize] = '\0'; 
 
	fclose (f); 
 
	return buf; 
} 
 
static int ParameterNameToMapIndex (char *s) 
{ 
	int i = 0; 
 
	while (Map[i].TokenName != NULL) 
	{ 
		if (0==strcmp (Map[i].TokenName, s)) 
			return i; 
		else 
			i++; 
	} 
 
	return -1; 
}; 
 
static void PatchInp () 
{ 
	// Open Files 
	if (strlen (ConfigInput.InputFile) > 0 && (bmpInput=fopen(ConfigInput.InputFile,"rb"))==NULL) 
	{ 
		printf("Input file %s does not exist",ConfigInput.InputFile);     
	} 
	else 
		strcpy(InputFileName, ConfigInput.InputFile); 
 
 
	if (strlen (ConfigInput.OutputFile) > 0 && (bmpOutput=fopen(ConfigInput.OutputFile,"wb"))==NULL) 
	{ 
		printf("Input file %s does not exist",ConfigInput.OutputFile);     
	} 
	else 
		strcpy(OutputFileName, ConfigInput.OutputFile); 
}