includeaddkeyword.c revision 77e4d358c68aeabdae9ddeba84e7f511e730a764
1<![CDATA[
2#include <stdio.h>
3#include <string.h>
4#include <stdlib.h>
5#include <libxml/xmlmemory.h>
6#include <libxml/parser.h>
7
8void
9parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
10
11	xmlNewTextChild (cur, NULL, "keyword", keyword);
12    return;
13}
14
15xmlDocPtr
16parseDoc(char *docname, char *keyword) {
17
18	xmlDocPtr doc;
19	xmlNodePtr cur;
20
21	doc = xmlParseFile(docname);
22
23	if (doc == NULL ) {
24		fprintf(stderr,"Document not parsed successfully. \n");
25		xmlFreeDoc(doc);
26		return (NULL);
27	}
28
29	cur = xmlDocGetRootElement(doc);
30
31	if (cur == NULL) {
32		fprintf(stderr,"empty document\n");
33		xmlFreeDoc(doc);
34		return (NULL);
35	}
36
37	if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
38		fprintf(stderr,"document of the wrong type, root node != story");
39		xmlFreeDoc(doc);
40		return (NULL);
41	}
42
43	cur = cur->xmlChildrenNode;
44	while (cur != NULL) {
45		if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
46			parseStory (doc, cur, keyword);
47		}
48
49	cur = cur->next;
50	}
51	return(doc);
52}
53
54int
55main(int argc, char **argv) {
56
57	char *docname;
58	char *keyword;
59	xmlDocPtr doc;
60
61	if (argc <= 2) {
62		printf("Usage: %s docname, keyword\n", argv[0]);
63		return(0);
64	}
65
66	docname = argv[1];
67	keyword = argv[2];
68	doc = parseDoc (docname, keyword);
69	if (doc != NULL) {
70		xmlSaveFormatFile (docname, doc, 0);
71		xmlFreeDoc(doc);
72	}
73
74	return (1);
75}
76]]>
77