Blame Xml/xml_sample/xml_sample.c

Packit 857059
/* BEGIN_ICS_COPYRIGHT7 ****************************************
Packit 857059
Packit 857059
Copyright (c) 2015, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
     documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
** END_ICS_COPYRIGHT7   ****************************************/
Packit 857059
Packit 857059
/* [ICS VERSION STRING: unknown] */
Packit 857059
Packit 857059
#include <stdio.h>
Packit 857059
#include <stdlib.h>
Packit 857059
#include <stdarg.h>
Packit 857059
#include <unistd.h>
Packit 857059
#include <fnmatch.h>
Packit 857059
#include <ctype.h>
Packit 857059
#define _GNU_SOURCE
Packit 857059
Packit 857059
#include <ixml.h>
Packit 857059
Packit 857059
/* example of simple non-predefined parser */
Packit 857059
/* for an example of a predefined parser, see opareport/topology.c */
Packit 857059
Packit 857059
static void *FieldXmlParserStart(IXmlParserState_t *state, void *parent, const char **attr);
Packit 857059
static void FieldXmlParserEnd(IXmlParserState_t *state, const IXML_FIELD *field, void *object, void *parent, XML_Char *content, unsigned len, boolean valid);
Packit 857059
Packit 857059
static IXML_FIELD Fields[] = {
Packit 857059
	{ tag:"*", format:'w', subfields:Fields, start_func:FieldXmlParserStart, end_func:FieldXmlParserEnd }, // wildcard to traverse xml tree, keep all whitespace
Packit 857059
	{ NULL }
Packit 857059
};
Packit 857059
Packit 857059
static IXML_FIELD StrField = 
Packit 857059
	{ tag:"*", format:'s', start_func:FieldXmlParserStart, end_func:FieldXmlParserEnd }; // keep all whitespace
Packit 857059
Packit 857059
static IXML_FIELD OtherField = 
Packit 857059
	{ tag:"*", format:'k', start_func:FieldXmlParserStart, end_func:FieldXmlParserEnd }; // trim leading/trailing whitespace
Packit 857059
Packit 857059
Packit 857059
static void *FieldXmlParserStart(IXmlParserState_t *state, void *parent, const char **attr)
Packit 857059
{
Packit 857059
	int i;
Packit 857059
	const char *tag=IXmlParserGetCurrentTag(state);
Packit 857059
	const char *parent_tag=IXmlParserGetParentTag(state);	// can be NULL at top level tag
Packit 857059
	const char *full_tag = IXmlParserGetCurrentFullTag(state);
Packit 857059
Packit 857059
	if (!tag) {
Packit 857059
		fprintf(stderr, "tag pointer is null.\n");
Packit 857059
		return NULL;
Packit 857059
	}
Packit 857059
Packit 857059
	// process attributes as needed
Packit 857059
	printf("Start %s in %s", full_tag?full_tag:"NONE", parent_tag?parent_tag:"NONE");
Packit 857059
	for (i = 0; attr[i]; i += 2) {
Packit 857059
		printf(" %s='%s'", attr[i], attr[i+1]);
Packit 857059
	}
Packit 857059
	printf("\n");
Packit 857059
	if (strcmp("NodeGUID", tag) == 0) {
Packit 857059
		// example of changing parsing format
Packit 857059
		// here is a tag we want lead/trail whitespace trimmed for
Packit 857059
		IXmlParserSetField(state, &OtherField);
Packit 857059
	} else if (strcmp("NodeDesc", tag) == 0) {
Packit 857059
		// example of changing parsing format
Packit 857059
		// here is a tag we want to keep lead/trail whitespace
Packit 857059
		IXmlParserSetField(state, &StrField);
Packit 857059
	} else {
Packit 857059
		// all other tags processed via Fields which uses 'w' format
Packit 857059
		// and hence will keep lead/trail whitespace or permit a container
Packit 857059
	}
Packit 857059
	return NULL;	// pointer returned here will be passed as object to ParserEnd function below
Packit 857059
}
Packit 857059
Packit 857059
static void FieldXmlParserEnd(IXmlParserState_t *state, const IXML_FIELD *field, void *object, void *parent, XML_Char *content, unsigned len, boolean valid)
Packit 857059
{
Packit 857059
	const char *full_tag = IXmlParserGetCurrentFullTag(state);
Packit 857059
	if (! valid) {
Packit 857059
		// syntax error during tag (or its children tags), cleanup
Packit 857059
		printf("Cleanup %s", full_tag?full_tag:"<nil>");
Packit 857059
	} else {
Packit 857059
		// depending on tag and information from Start (object), process content
Packit 857059
		printf("End %s: '%s'\n", full_tag?full_tag:"<nil>", content?content:"<nil>");
Packit 857059
	}
Packit 857059
}
Packit 857059
Packit 857059
FSTATUS Xml2ParseInputFile(const char *input_file)
Packit 857059
{
Packit 857059
	if (strcmp(input_file, "-") == 0) {
Packit 857059
		printf("Parsing stdin...\n");
Packit 857059
		if (FSUCCESS != IXmlParseFile(stdin, "stdin", IXML_PARSER_FLAG_NONE, Fields, NULL, NULL, NULL, NULL, NULL, NULL)) {
Packit 857059
			return FERROR;
Packit 857059
		}
Packit 857059
	} else {
Packit 857059
		printf("Parsing %s...\n", input_file);
Packit 857059
		if (FSUCCESS != IXmlParseInputFile(input_file, IXML_PARSER_FLAG_NONE, Fields, NULL, NULL, NULL, NULL, NULL, NULL)) {
Packit 857059
			return FERROR;
Packit 857059
		}
Packit 857059
	}
Packit 857059
	return FSUCCESS;
Packit 857059
}
Packit 857059
Packit 857059
void Usage(void)
Packit 857059
{
Packit 857059
	fprintf(stderr, "Usage: xml_sample input_file\n");
Packit 857059
	exit(2);
Packit 857059
}
Packit 857059
Packit 857059
int main(int argc, char **argv)
Packit 857059
{
Packit 857059
	char *filename;
Packit 857059
	if (argc != 2)
Packit 857059
		Usage();
Packit 857059
	filename = argv[1];
Packit 857059
	if (!filename) {
Packit 857059
		fprintf(stderr, "Error: null input filename\n");
Packit 857059
		exit(2);
Packit 857059
	}
Packit 857059
	if (FSUCCESS == Xml2ParseInputFile(filename))
Packit 857059
		exit(0);
Packit 857059
	else
Packit 857059
		exit(1);
Packit 857059
}