File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / miniupnpc / src / testupnpreplyparse.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:21:37 2023 UTC (9 months, 1 week ago) by misho
Branches: miniupnpc, MAIN
CVS tags: v2_2_5p0, HEAD
Version 2.2.5p0

    1: /* $Id: testupnpreplyparse.c,v 1.1.1.1 2023/09/27 11:21:37 misho Exp $ */
    2: /* MiniUPnP project
    3:  * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
    4:  * (c) 2006-2017 Thomas Bernard
    5:  * This software is subject to the conditions detailed
    6:  * in the LICENCE file provided within the distribution */
    7: #include <stdio.h>
    8: #include <string.h>
    9: #include <stdlib.h>
   10: #include "upnpreplyparse.h"
   11: 
   12: int
   13: test_parsing(const char * buf, int len, FILE * f)
   14: {
   15: 	char line[1024];
   16: 	struct NameValueParserData pdata;
   17: 	int ok = 1;
   18: 	ParseNameValue(buf, len, &pdata);
   19: 	/* check result */
   20: 	if(f != NULL)
   21: 	{
   22: 		while(fgets(line, sizeof(line), f))
   23: 		{
   24: 			char * value;
   25: 			char * equal;
   26: 			char * parsedvalue;
   27: 			int l;
   28: 			l = strlen(line);
   29: 			while((l > 0) && ((line[l-1] == '\r') || (line[l-1] == '\n')))
   30: 				line[--l] = '\0';
   31: 			/* skip empty lines */
   32: 			if(l == 0)
   33: 				continue;
   34: 			equal = strchr(line, '=');
   35: 			if(equal == NULL)
   36: 			{
   37: 				fprintf(stderr, "Warning, line does not contain '=' : %s\n", line);
   38: 				continue;
   39: 			}
   40: 			*equal = '\0';
   41: 			value = equal + 1;
   42: 			parsedvalue = GetValueFromNameValueList(&pdata, line);
   43: 			if((parsedvalue == NULL) || (strcmp(parsedvalue, value) != 0))
   44: 			{
   45: 				fprintf(stderr, "Element <%s> : expecting value '%s', got '%s'\n",
   46: 				        line, value, parsedvalue ? parsedvalue : "<null string>");
   47: 				ok = 0;
   48: 			}
   49: 		}
   50: 	}
   51: 	ClearNameValueList(&pdata);
   52: 	return ok;
   53: }
   54: 
   55: int main(int argc, char * * argv)
   56: {
   57: 	FILE * f;
   58: 	char * buffer;
   59: 	long l;
   60: 	int ok;
   61: 
   62: 	if(argc<2)
   63: 	{
   64: 		fprintf(stderr, "Usage: %s file.xml [file.namevalues]\n", argv[0]);
   65: 		return 1;
   66: 	}
   67: 	f = fopen(argv[1], "r");
   68: 	if(!f)
   69: 	{
   70: 		fprintf(stderr, "Error : can not open file %s\n", argv[1]);
   71: 		return 2;
   72: 	}
   73: 	if(fseek(f, 0, SEEK_END) < 0) {
   74: 		perror("fseek");
   75: 		return 1;
   76: 	}
   77: 	l = (int)ftell(f);
   78: 	if(l < 0) {
   79: 		perror("ftell");
   80: 		return 1;
   81: 	}
   82: 	if(fseek(f, 0, SEEK_SET) < 0) {
   83: 		perror("fseek");
   84: 		return 1;
   85: 	}
   86: 	buffer = malloc(l + 1);
   87: 	if(buffer == NULL) {
   88: 		fprintf(stderr, "Error: failed to allocate %ld bytes\n", l+1);
   89: 		return 1;
   90: 	}
   91: 	l = fread(buffer, 1, l, f);
   92: 	fclose(f);
   93: 	f = NULL;
   94: 	buffer[l] = '\0';
   95: 	if(argc > 2)
   96: 	{
   97: 		f = fopen(argv[2], "r");
   98: 		if(!f)
   99: 		{
  100: 			fprintf(stderr, "Error : can not open file %s\n", argv[2]);
  101: 			return 2;
  102: 		}
  103: 	}
  104: #ifdef DEBUG
  105: 	DisplayNameValueList(buffer, l);
  106: #endif
  107: 	ok = test_parsing(buffer, l, f);
  108: 	if(f)
  109: 	{
  110: 		fclose(f);
  111: 	}
  112: 	free(buffer);
  113: 	return ok ? 0 : 3;
  114: }
  115: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>