Explorar o código

learning to cope with expat to populate namespace

Leon Urbas %!s(int64=11) %!d(string=hai) anos
pai
achega
932aa42822
Modificáronse 3 ficheiros con 86 adicións e 1 borrados
  1. 5 1
      README.md
  2. 6 0
      tool/Makefile.am
  3. 75 0
      tool/xml2ns0.c

+ 5 - 1
README.md

@@ -10,7 +10,11 @@ An open-source communication stack implementation of OPC UA (OPC Unified Archite
 ### Ubuntu
 ##### Getting gcc toolchain:
 ```bash
-sudo apt-get install build-essential subversion git autoconf libtool texinfo python-lxml
+sudo apt-get install build-essential subversion git autoconf libtool texinfo python-lxml 
+```
+##### Getting toolchain for tools:
+```bash
+sudo apt-get install gperf libexpat
 ```
 ##### Getting and installing *check* as unit test framework (http://check.sourceforge.net/):
 ```bash

+ 6 - 0
tool/Makefile.am

@@ -3,6 +3,12 @@ INCLUDE_DIR = $(top_builddir)/include
 AUTO_NAME = opcua
 NS0_NAME = ua_namespace_0
 
+bin_PROGRAMS = xml2ns0 
+xml2ns0_SOURCES = xml2ns0.c
+xml2ns0_CFLAGS = -I$(INCLUDE_DIR)
+xml2ns0_LDADD = $(GLOBAL_AM_LDADD)
+xml2ns0_LDFLAGS = -lexpat
+
 all-local: $(AUTO_NAME).cgen $(AUTO_NAME).hgen $(NS0_NAME).cgen $(NS0_NAME).hgen
 
 $(AUTO_NAME).cgen $(AUTO_NAME).hgen: Opc.Ua.Types.bsd generate_builtin.py

+ 75 - 0
tool/xml2ns0.c

@@ -0,0 +1,75 @@
+/*
+ * xml2ns0.c
+ *
+ *  Created on: 21.04.2014
+ *      Author: mrt
+ */
+
+#include <expat.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct parent {
+	int depth;
+	void* obj[20];
+} parent_t;
+
+void startElement(void * data, const char *el, const char **attr) {
+  parent_t* p = (parent_t*) data;
+  int i, j;
+
+
+  if (p->depth==0) { printf("new %s --\n", el); }
+  p->obj[p->depth] = el;
+  for (i = 0; attr[i]; i += 2) {
+	  for (j = 0; j < p->depth; j++) {
+		  printf("%s.",p->obj[j]);
+	  }
+	  printf("%s='%s'\n", attr[i], attr[i + 1]);
+  }
+  p->depth++;
+}  /* End of start handler */
+
+void handleText(void * data, const char *s, int len) {
+  parent_t* p = (parent_t*) data;
+  int j, i;
+
+  if (len > 0) {
+	  // process only strings that are not entirely built of whitespaces
+	  for (i=0; i<len;i++) {
+		  if (! isspace(s[i])) {
+			  for (j = 0; j < p->depth; j++) {
+				  printf("%s.",p->obj[j]);
+			  }
+			  printf("%s={%d,'%.*s'}\n", "Value", len, len, s);
+			  break;
+		  }
+	  }
+  }
+}  /* End of text handler */
+
+void endElement(void *data, const char *el) {
+	parent_t* p = (parent_t*) data;
+	p->depth--;
+}  /* End of end handler */
+
+int main()
+{
+  char buf[1024];
+  int len;   /* len is the number of bytes in the current bufferful of data */
+  int done;
+  parent_t p;
+  p.depth = 0;
+
+  XML_Parser parser = XML_ParserCreate(NULL);
+  XML_SetUserData(parser, &p);
+  XML_SetElementHandler(parser, startElement, endElement);
+  XML_SetCharacterDataHandler(parser, handleText);
+  while ((len = read(0,buf,1024)) > 0) {
+    if (!XML_Parse(parser, buf, len, (len<1024))) {
+      return 1;
+    }
+  }
+  XML_ParserFree(parser);
+  return 0;
+}