This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
example.cpp
131 lines (108 loc) · 3.25 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// PHP EMBED example program
// Copyright (c) 2007 Andrew Bosworth, Facebook, inc
// Modified by Dmitry Zenovich <dzenovich@gmail.com>
// All rights reserved
#include "php_stl.h"
// prototype
void print_php_array(php_array &a, int depth = 0);
// define our own output functions with this exact signature
void print_null(const char *str) {}
void print_mine(const char *str){
printf("%s", str);
}
int main(int argc, char **argv){
// Instantiate the object, set type_warnings to true
php_stl p(true);
// Load a php file
if(SUCCESS != p.load("example.php")){
printf("load failed\n");
return 1;
}
// redefine the output functions to do what we want
p.set_message_function(print_null);
p.set_output_function(print_mine);
// an example of calling into a function with C arguments
long tre = 3;
char *tres = p.call_c_string("trivial_func", "sld", "answer: ", tre, 4.5);
if(tres){
printf("%s\n", tres);
free(tres);
}
printf("\n");
// this test function returns a string, but we call_long so it converts it
// to a long value. Since I have instantiated this php object with the
// type_warnings parameter to true, this will output an error. If you do NOT
// want an error message, just omit the argument or set it to false. You can
// redirect this error output by using set_error_function
long hit = p.call_long("test");
printf("long value: %ld\n", hit);
printf("\n");
// redirect error output to null
p.set_error_function(print_null);
// convert a php string array to a vector of longs
vector<long> blah = p.call_long_vector("blah");
for(vector<long>::iterator it = blah.begin(); it != blah.end(); it++){
printf("%ld\n", *it);
}
printf("\n");
// get a nested array back from php
php_array *a = p.call_php_array("foo_complex_array");
// use our function below with iterators and type checking to print it in C
print_php_array(*a);
printf("\n");
// now try printing it with PHP, should use our output function from above
p.call_void("print_r", "a", a);
delete a;
}
void print_php_array(php_array &a, int depth){
php_iterator it(a);
for(int i = 0; i < depth; i++)
printf(" ");
printf("%d elements\n", it.size());
while(!it.done()){
for(int i = 0; i < depth; i++)
printf(" ");
char *str;
switch(it.get_key_type()){
case IS_LONG:
printf("long %ld => ", it.get_key_long());
break;
case IS_STRING:
printf("string %s => ", str = it.get_key_c_string());
free(str);
break;
default:
printf("??? %s => ", str = it.get_key_c_string());
free(str);
break;
}
switch(it.get_data_type()){
case IS_LONG:
printf("long %ld\n", it.get_data_long());
break;
case IS_STRING:
printf("string %s\n", str = it.get_data_c_string());
free(str);
break;
case IS_DOUBLE:
printf("double %f\n", it.get_data_double());
break;
case IS_BOOL:
printf("bool %s\n", str = it.get_data_c_string());
free(str);
break;
case IS_ARRAY:
printf("Array\n");
{
php_array t = it.get_data_array();
print_php_array(t, depth + 1);
}
break;
default:
printf("??? %s\n", str = it.get_data_c_string());
free(str);
break;
}
it++;
}
}