[mw-devel] MW3 r973 - branches/newipc3/src

arthur at sucs.org arthur at sucs.org
Sat Oct 6 17:49:23 BST 2007


Author: arthur
Date: 2007-10-06 17:49:22 +0100 (Sat, 06 Oct 2007)
New Revision: 973

Added:
   branches/newipc3/src/xml.c
   branches/newipc3/src/xml.h
Modified:
   branches/newipc3/src/Makefile
Log:
import the simple xml api from marvn


Modified: branches/newipc3/src/Makefile
===================================================================
--- branches/newipc3/src/Makefile	2007-09-27 19:21:48 UTC (rev 972)
+++ branches/newipc3/src/Makefile	2007-10-06 16:49:22 UTC (rev 973)
@@ -14,8 +14,9 @@
  
 # cflags for standard 'cc' compiler
 CFLAGS+= -Wall -pedantic -fpie -std=gnu99 -D_GNU_SOURCE
+CFLAGS+= $(shell xml2-config --cflags)
 LDFLAGS+= -pie
-LDLIBS+= -lreadline -lhistory -ltermcap -lcrypt -ljs -lsqlite3 -lcurl
+LDLIBS+= -lreadline -lhistory -ltermcap -lcrypt -ljs -lsqlite3 -lcurl -lxml2
 
 # info strings, do not edit.
 DEFS:= -DBUILD_DATE=\"$(shell date +%Y%m%d)\"
@@ -81,7 +82,7 @@
 topten.o sort.o tidyup.o gags.o script_inst.o script.o\
 incoming.o command.o chattable.o alias.o frl.o hash.o vars.o expand.o\
 mud.o mudtable.o files.o completion.o sentinel.o iconv.o gagtable.o \
-js.o sqlite.o ipc.o session.o socket.o
+js.o sqlite.o ipc.o session.o socket.o xml.o
 	$(CC) $(LDFLAGS) $(LDLIBS) -o $@ $^
 
 setup_homepath:

Added: branches/newipc3/src/xml.c
===================================================================
--- branches/newipc3/src/xml.c	                        (rev 0)
+++ branches/newipc3/src/xml.c	2007-10-06 16:49:22 UTC (rev 973)
@@ -0,0 +1,147 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <libxml/tree.h>
+#include <libxml/parser.h>
+
+#include "session.h"
+#include "xml.h"
+
+extern int debug;
+
+/* produce a string version of an int */
+const char * int2str( int i )
+{
+	static char text[40];
+	snprintf(text, 40, "%d", i);
+	return text;
+}
+
+xmlDocPtr xml_create( char * type, int ref )
+{
+	xmlDocPtr new;
+	xmlNodePtr root;
+
+	new = xmlNewDoc((unsigned char *)"1.0");
+	new->children = xmlNewDocNode(new, NULL, (unsigned char *)type, NULL);
+	root = xmlDocGetRootElement(new);
+	xmlNewProp(root, (unsigned char *)"id", (unsigned char *)int2str(ref) );
+	return new;
+}
+
+char * xml_tostring( xmlDocPtr doc )
+{
+	xmlChar *text;
+	int size;
+	xmlDocDumpMemory(doc, &text, &size);
+//	printf("Printing XML doc of %d bytes.\n", size);
+//	printf("%s\n", text);
+	return (char *)text;
+}
+
+xmlDocPtr xml_stringto( const char *text )
+{
+	xmlDocPtr doc;
+	doc=xmlParseMemory(text, strlen(text));
+	return doc;
+}
+
+void xml_destroy( xmlDocPtr doc )
+{
+	if (doc == NULL) return;
+	xmlFreeDoc( doc );
+}
+
+void xml_set(xmlDocPtr doc, const char * name, const char * value )
+{
+	xmlNodePtr root, cur;
+	int count=0;
+
+	if (doc == NULL) return;
+	root = xmlDocGetRootElement(doc);
+	if (root==NULL) return;
+
+	/* remove old occurances */
+	for (cur = root->children; cur != NULL; cur = cur->next) {
+		if (xmlStrcasecmp(cur->name, (unsigned char *)name) == 0) {
+			xmlNodeSetContent(cur, (unsigned char *)value);
+			count++;
+		}
+	}
+	if (count == 0) {
+		if (debug) printf("New node added.\n");
+		xmlNewTextChild(root, NULL, (unsigned char *)name, (unsigned char *)value);
+	} else {
+		if (debug) printf("%d Nodes(s) replaced.\n", count);
+	}
+}
+
+char * xml_get(xmlDocPtr doc, const char * name)
+{
+	xmlNodePtr root, cur;
+	if (doc == NULL) return NULL;
+	root = xmlDocGetRootElement(doc);
+	if (root==NULL) return NULL;
+
+	for (cur = root->children; cur != NULL; cur = cur->next) {
+		if (xmlStrcasecmp(cur->name, (unsigned char *)name) == 0) {
+			return (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+		}
+	}
+	return NULL;
+}
+
+const char * xml_msgtype(xmlDocPtr doc)
+{
+	xmlNodePtr root;
+	if (doc == NULL) return NULL;
+	root = xmlDocGetRootElement(doc);
+	if (root==NULL) return NULL;
+
+	return (char *)root->name;
+}
+
+int xml_msgid(xmlDocPtr doc)
+{
+	xmlNodePtr root;
+	char *value;
+
+	if (doc == NULL) return -1;
+	root = xmlDocGetRootElement(doc);
+	if (root==NULL) return -1;
+
+	value = (char *)xmlGetProp(root, (unsigned char *)"id");
+	if (value == NULL) return -1;
+	return atoi(value);
+}
+
+#ifdef DEBUG_XML
+int main(int argc, char **argv)
+{
+	xmlDocPtr doc;
+	char *name;
+
+	doc = xml_create("message", 42);
+	xml_set(doc, "Name", "fred");
+	xml_set(doc, "Sex", "probably");
+	xml_tostring(doc);
+	xml_set(doc, "name", "smith");
+	xml_tostring(doc);
+
+	name = xml_get(doc, "Name");
+	printf("My name is '%s'\n", name);
+	free(name);
+
+	xml_destroy(doc);
+
+	return 0;
+}
+#endif

Added: branches/newipc3/src/xml.h
===================================================================
--- branches/newipc3/src/xml.h	                        (rev 0)
+++ branches/newipc3/src/xml.h	2007-10-06 16:49:22 UTC (rev 973)
@@ -0,0 +1,14 @@
+#include <libxml/tree.h>
+#include <libxml/parser.h>
+
+/* xml.c */
+const char *int2str(int i);
+xmlDocPtr xml_create(char *type, int ref);
+char *xml_tostring(xmlDocPtr doc);
+xmlDocPtr xml_stringto(const char *text);
+void xml_destroy(xmlDocPtr doc);
+void xml_set(xmlDocPtr doc, const char *name, const char *value);
+char *xml_get(xmlDocPtr doc, const char *name);
+const char * xml_msgtype(xmlDocPtr doc);
+int xml_msgid(xmlDocPtr doc);
+





More information about the mw-devel mailing list