]> git.sesse.net Git - xml-template/commitdiff
Start a PHP5-to-C++11 SWIG layer (!).
authorsgunderson@bigfoot.com <>
Thu, 22 Sep 2011 20:42:28 +0000 (22:42 +0200)
committersgunderson@bigfoot.com <>
Thu, 22 Sep 2011 20:42:28 +0000 (22:42 +0200)
php5-swig/Makefile [new file with mode: 0644]
php5-swig/simple.php [new file with mode: 0644]
php5-swig/xml-template.swig [new file with mode: 0644]
tests/test.sh

diff --git a/php5-swig/Makefile b/php5-swig/Makefile
new file mode 100644 (file)
index 0000000..de44312
--- /dev/null
@@ -0,0 +1,15 @@
+CPPFLAGS=$(shell php-config --includes)
+CXXFLAGS=-std=gnu++0x $(shell xml2-config --cflags) -fPIC -g
+LDFLAGS=-shared
+LIBS=../c++11/xml-template.o
+
+XML_Template_SWIG.so: xml-template_wrap.o
+       $(CXX) $(LDFLAGS) -o $@ $< $(LIBS)
+
+xml-template_wrap.cpp XML_Template_SWIG.php php_XML_Template_SWIG.h : xml-template.swig
+       swig -c++ -php $<
+
+clean:
+       $(RM) XML_Template_SWIG.so xml-template_wrap.o
+       $(RM) xml-template_wrap.cpp XML_Template_SWIG.php php_XML_Template_SWIG.h 
+
diff --git a/php5-swig/simple.php b/php5-swig/simple.php
new file mode 100644 (file)
index 0000000..44cbd2f
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+dl("XML_Template_SWIG.so");
+$doc = process_file('../xml/simple.xml', array( 
+       'title' => 'A very basic example',
+       '#hello' => 'Hello world!'
+), true);
+output_to_fd_and_free($doc, 1);
+
+?>
+
diff --git a/php5-swig/xml-template.swig b/php5-swig/xml-template.swig
new file mode 100644 (file)
index 0000000..5a560ce
--- /dev/null
@@ -0,0 +1,73 @@
+%module XML_Template_SWIG
+
+%{
+
+#include "../c++11/xml-template.h"
+               
+bool is_associative_array(HashTable *ht)
+{
+       if (ht->nNumOfElements == 0) {
+               return true;
+       }
+       for (unsigned i = 0; i < ht->nNumOfElements; ++i) {
+               char buf[32];
+               sprintf(buf, "%u", i);
+               if (!zend_hash_exists(ht, buf, strlen(buf))) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+Directive* convert_php_objects_to_directive(zval *obj)
+{
+       switch (Z_TYPE_P(obj)) {
+       case IS_ARRAY: {
+               HashTable *ht = Z_ARRVAL_P(obj);
+               if (is_associative_array(ht)) {
+                       std::unordered_map<std::string, Directive *> my_map;
+                       for (zend_hash_internal_pointer_reset(ht); zend_hash_has_more_elements(ht) == SUCCESS; zend_hash_move_forward(ht)) {
+                               char *str_key;
+                               ulong num_key;
+                               zend_hash_get_current_key(ht, &str_key, &num_key, 0);
+
+                               std::string key;
+                               if (zend_hash_get_current_key_type(ht) == HASH_KEY_IS_STRING) {
+                                       key = str_key;
+                               } else {
+                                       char buf[32];
+                                       sprintf(buf, "%lu", num_key);
+                                       key = buf;
+                               }
+
+                               zval **data;
+                               zend_hash_get_current_data(ht, (void **)&data);
+                               my_map.insert(make_pair(key, convert_php_objects_to_directive(*data)));
+                       }
+                       return new Substitute(my_map);
+               } else {
+                       printf("ARRAY\n");
+               }
+               break;
+       }
+       case IS_STRING: {
+               char *str = Z_STRVAL_P(obj);
+               return new Replace(str);
+       }
+       default:
+               printf("WARNING: Unknown type %d!\n", Z_TYPE_P(obj));
+               break;
+       }
+
+       return NULL;
+}
+
+%}
+
+%typemap(in) Directive* {
+       $1 = convert_php_objects_to_directive(*$input);
+}
+
+xmlDocPtr process_file(const char *input_filename, Directive *root_directive, bool clean);
+void output_to_fd_and_free(xmlDocPtr doc, int fd);
+
index ba04ecd51f276e39b84ffb0372c0c7b94b57a5fc..cb118beb4e3288f1cbea0f6ef4d2ae51e7e6329f 100755 (executable)
@@ -24,6 +24,9 @@ for L in $LANGUAGES; do
                if [ "$L" = "php5" ]; then
                        php5-cgi -q ../php5/$T.php > $TEMPFILE
                fi
+               if [ "$L" = "php5-swig" ]; then
+                       ( cd ../c++11 && make -s && cd ../php5-swig && make -s && sudo cp XML_Template_SWIG.so /usr/lib/php5/20090626+lfs ) && php5-cgi -q ../php5-swig/$T.php > $TEMPFILE
+               fi
                if [ "$L" = "python" ]; then
                        python ../python/$T.py > $TEMPFILE
                fi