From: sgunderson@bigfoot.com <> Date: Thu, 22 Sep 2011 20:42:28 +0000 (+0200) Subject: Start a PHP5-to-C++11 SWIG layer (!). X-Git-Url: https://git.sesse.net/?p=xml-template;a=commitdiff_plain;h=0ccbc5e506ee3ed6bcc77bc4d6cdbde9791e8ad5 Start a PHP5-to-C++11 SWIG layer (!). --- diff --git a/php5-swig/Makefile b/php5-swig/Makefile new file mode 100644 index 0000000..de44312 --- /dev/null +++ b/php5-swig/Makefile @@ -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 index 0000000..44cbd2f --- /dev/null +++ b/php5-swig/simple.php @@ -0,0 +1,11 @@ + '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 index 0000000..5a560ce --- /dev/null +++ b/php5-swig/xml-template.swig @@ -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 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); + diff --git a/tests/test.sh b/tests/test.sh index ba04ecd..cb118be 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -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