]> git.sesse.net Git - xml-template/blob - php5-swig/xml-template.swig
5a560cee6883bf4bce903964fe653ee2755a71c5
[xml-template] / php5-swig / xml-template.swig
1 %module XML_Template_SWIG
2
3 %{
4
5 #include "../c++11/xml-template.h"
6                 
7 bool is_associative_array(HashTable *ht)
8 {
9         if (ht->nNumOfElements == 0) {
10                 return true;
11         }
12         for (unsigned i = 0; i < ht->nNumOfElements; ++i) {
13                 char buf[32];
14                 sprintf(buf, "%u", i);
15                 if (!zend_hash_exists(ht, buf, strlen(buf))) {
16                         return true;
17                 }
18         }
19         return false;
20 }
21
22 Directive* convert_php_objects_to_directive(zval *obj)
23 {
24         switch (Z_TYPE_P(obj)) {
25         case IS_ARRAY: {
26                 HashTable *ht = Z_ARRVAL_P(obj);
27                 if (is_associative_array(ht)) {
28                         std::unordered_map<std::string, Directive *> my_map;
29                         for (zend_hash_internal_pointer_reset(ht); zend_hash_has_more_elements(ht) == SUCCESS; zend_hash_move_forward(ht)) {
30                                 char *str_key;
31                                 ulong num_key;
32                                 zend_hash_get_current_key(ht, &str_key, &num_key, 0);
33
34                                 std::string key;
35                                 if (zend_hash_get_current_key_type(ht) == HASH_KEY_IS_STRING) {
36                                         key = str_key;
37                                 } else {
38                                         char buf[32];
39                                         sprintf(buf, "%lu", num_key);
40                                         key = buf;
41                                 }
42
43                                 zval **data;
44                                 zend_hash_get_current_data(ht, (void **)&data);
45                                 my_map.insert(make_pair(key, convert_php_objects_to_directive(*data)));
46                         }
47                         return new Substitute(my_map);
48                 } else {
49                         printf("ARRAY\n");
50                 }
51                 break;
52         }
53         case IS_STRING: {
54                 char *str = Z_STRVAL_P(obj);
55                 return new Replace(str);
56         }
57         default:
58                 printf("WARNING: Unknown type %d!\n", Z_TYPE_P(obj));
59                 break;
60         }
61
62         return NULL;
63 }
64
65 %}
66
67 %typemap(in) Directive* {
68         $1 = convert_php_objects_to_directive(*$input);
69 }
70
71 xmlDocPtr process_file(const char *input_filename, Directive *root_directive, bool clean);
72 void output_to_fd_and_free(xmlDocPtr doc, int fd);
73