Skip to content

Commit 0bfda8c

Browse files
committed
Make extension compile and work with PHP 7
1 parent 8c45446 commit 0bfda8c

File tree

1 file changed

+111
-7
lines changed

1 file changed

+111
-7
lines changed

geospatial.c

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,60 @@ PHP_MINFO_FUNCTION(geospatial)
186186
}
187187
/* }}} */
188188

189+
/* {{{ Version compat helpers */
190+
#if PHP_VERSION_ID >= 70000
191+
# define ADD_STRING(zv, name, val) add_assoc_string_ex(zv, name, sizeof(name)-1, val);
192+
# define GEOSPAT_MAKE_STD_ZVAL(zv) zv = ecalloc(sizeof(zval), 1);
193+
#else
194+
# define ADD_STRING(zv, name, val) add_assoc_string_ex(zv, name, sizeof(name), val, 1);
195+
# define GEOSPAT_MAKE_STD_ZVAL(zv) MAKE_STD_ZVAL(zv)
196+
#endif
197+
/* }}} */
198+
189199
/* {{{ Helpers */
190200
void retval_point_from_coordinates(zval *return_value, double lon, double lat)
191201
{
192202
zval *coordinates;
193203

194204
array_init(return_value);
195-
MAKE_STD_ZVAL(coordinates);
205+
GEOSPAT_MAKE_STD_ZVAL(coordinates);
196206
array_init(coordinates);
197-
add_assoc_string_ex(return_value, "type", sizeof("type"), "Point", 1);
207+
ADD_STRING(return_value, "type", "Point");
198208
add_next_index_double(coordinates, lon);
199209
add_next_index_double(coordinates, lat);
210+
#if PHP_VERSION_ID >= 70000
211+
add_assoc_zval_ex(return_value, "coordinates", sizeof("coordinates") - 1, coordinates);
212+
efree(coordinates);
213+
#else
200214
add_assoc_zval_ex(return_value, "coordinates", sizeof("coordinates"), coordinates);
215+
#endif
201216
}
202217

203218
static int parse_point_pair(zval *coordinates, double *lon, double *lat)
204219
{
205220
HashTable *coords;
221+
#if PHP_VERSION_ID >= 70000
222+
zval *z_lon, *z_lat;
223+
#else
206224
zval **z_lon, **z_lat;
225+
#endif
207226

208227
coords = HASH_OF(coordinates);
209228
if (coords->nNumOfElements != 2) {
210229
return 0;
211230
}
231+
#if PHP_VERSION_ID >= 70000
232+
if ((z_lon = zend_hash_index_find(coords, 0)) == NULL) {
233+
return 0;
234+
}
235+
if ((z_lat = zend_hash_index_find(coords, 1)) == NULL) {
236+
return 0;
237+
}
238+
convert_to_double_ex(z_lon);
239+
convert_to_double_ex(z_lat);
240+
*lon = Z_DVAL_P(z_lon);
241+
*lat = Z_DVAL_P(z_lat);
242+
#else
212243
if (zend_hash_index_find(coords, 0, (void**) &z_lon) != SUCCESS) {
213244
return 0;
214245
}
@@ -219,11 +250,29 @@ static int parse_point_pair(zval *coordinates, double *lon, double *lat)
219250
convert_to_double_ex(z_lat);
220251
*lon = Z_DVAL_PP(z_lon);
221252
*lat = Z_DVAL_PP(z_lat);
253+
#endif
222254
return 1;
223255
}
224256

225257
int geojson_point_to_lon_lat(zval *point, double *lon, double *lat)
226258
{
259+
#if PHP_VERSION_ID >= 70000
260+
zval *type, *coordinates;
261+
262+
if ((type = zend_hash_str_find(HASH_OF(point), "type", sizeof("type") - 1)) == NULL) {
263+
return 0;
264+
}
265+
if (Z_TYPE_P(type) != IS_STRING || strcmp(Z_STRVAL_P(type), "Point") != 0) {
266+
return 0;
267+
}
268+
if ((coordinates = zend_hash_str_find(HASH_OF(point), "coordinates", sizeof("coordinates") - 1)) == NULL) {
269+
return 0;
270+
}
271+
if (Z_TYPE_P(coordinates) != IS_ARRAY) {
272+
return 0;
273+
}
274+
return parse_point_pair(coordinates, lon, lat);
275+
#else
227276
zval **type, **coordinates;
228277

229278
if (zend_hash_find(HASH_OF(point), "type", sizeof("type"), (void**) &type) != SUCCESS) {
@@ -238,8 +287,8 @@ int geojson_point_to_lon_lat(zval *point, double *lon, double *lat)
238287
if (Z_TYPE_PP(coordinates) != IS_ARRAY) {
239288
return 0;
240289
}
241-
242290
return parse_point_pair(*coordinates, lon, lat);
291+
#endif
243292
}
244293

245294
/* }}} */
@@ -410,7 +459,11 @@ PHP_FUNCTION(dms_to_decimal)
410459
double degrees, minutes, sign;
411460
double seconds, decimal;
412461
char *direction = "";
462+
#if PHP_VERSION_ID >= 70000
463+
size_t direction_len;
464+
#else
413465
int direction_len;
466+
#endif
414467

415468
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd|s", &degrees, &minutes, &seconds, &direction, &direction_len) == FAILURE) {
416469
return;
@@ -436,7 +489,11 @@ PHP_FUNCTION(decimal_to_dms)
436489
int degrees, minutes;
437490
char *direction;
438491
char *coordinate;
492+
#if PHP_VERSION_ID >= 70000
493+
size_t coordinate_len;
494+
#else
439495
int coordinate_len;
496+
#endif
440497

441498
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ds", &decimal, &coordinate, &coordinate_len) == FAILURE) {
442499
return;
@@ -456,7 +513,7 @@ PHP_FUNCTION(decimal_to_dms)
456513
add_assoc_long(return_value, "degrees", degrees);
457514
add_assoc_long(return_value, "minutes", minutes);
458515
add_assoc_double(return_value, "seconds", seconds);
459-
add_assoc_string(return_value, "direction", direction, 1);
516+
ADD_STRING(return_value, "direction", direction);
460517
}
461518
/* }}} */
462519

@@ -690,17 +747,28 @@ geo_array *geo_hashtable_to_array(zval *array)
690747
geo_array *tmp;
691748
int element_count;
692749
HashPosition pos;
750+
#if PHP_VERSION_ID >= 70000
751+
zval *entry;
752+
#else
693753
zval **entry;
754+
#endif
694755
double lon, lat;
695756
int i = 0;
696757

697758
element_count = zend_hash_num_elements(Z_ARRVAL_P(array));
698759
tmp = geo_array_ctor(element_count);
699760

761+
#if PHP_VERSION_ID >= 70000
762+
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(array), entry) {
763+
764+
if (!parse_point_pair(entry, &lon, &lat)) {
765+
#else
700766
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);
701767
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **)&entry, &pos) == SUCCESS) {
702768

703769
if (!parse_point_pair(*entry, &lon, &lat)) {
770+
#endif
771+
704772
goto failure;
705773
}
706774

@@ -710,7 +778,11 @@ geo_array *geo_hashtable_to_array(zval *array)
710778

711779
zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos);
712780
i++;
781+
#if PHP_VERSION_ID >= 70000
782+
} ZEND_HASH_FOREACH_END();
783+
#else
713784
}
785+
#endif
714786

715787
return tmp;
716788

@@ -722,6 +794,28 @@ geo_array *geo_hashtable_to_array(zval *array)
722794
int geojson_linestring_to_array(zval *line, geo_array **array)
723795
{
724796
geo_array *tmp;
797+
#if PHP_VERSION_ID >= 70000
798+
zval *type, *coordinates;
799+
800+
if (Z_TYPE_P(line) != IS_ARRAY) {
801+
return 0;
802+
}
803+
804+
if ((type = zend_hash_str_find(HASH_OF(line), "type", sizeof("type") - 1)) == NULL) {
805+
return 0;
806+
}
807+
if (Z_TYPE_P(type) != IS_STRING || strcmp(Z_STRVAL_P(type), "Linestring") != 0) {
808+
return 0;
809+
}
810+
if ((coordinates = zend_hash_str_find(HASH_OF(line), "coordinates", sizeof("coordinates") - 1)) == NULL) {
811+
return 0;
812+
}
813+
if (Z_TYPE_P(coordinates) != IS_ARRAY) {
814+
return 0;
815+
}
816+
817+
tmp = geo_hashtable_to_array(coordinates);
818+
#else
725819
zval **type, **coordinates;
726820

727821
if (Z_TYPE_P(line) != IS_ARRAY) {
@@ -742,6 +836,7 @@ int geojson_linestring_to_array(zval *line, geo_array **array)
742836
}
743837

744838
tmp = geo_hashtable_to_array(*coordinates);
839+
#endif
745840
if (tmp && array) {
746841
*array = tmp;
747842
return 1;
@@ -828,11 +923,14 @@ PHP_FUNCTION(rdp_simplify)
828923
rdp_simplify(points, epsilon, 0, points->count - 1);
829924
for (i = 0; i < points->count; i++) {
830925
if (points->status[i]) {
831-
MAKE_STD_ZVAL(pair);
926+
GEOSPAT_MAKE_STD_ZVAL(pair);
832927
array_init(pair);
833928
add_next_index_double(pair, points->x[i]);
834929
add_next_index_double(pair, points->y[i]);
835930
add_next_index_zval(return_value, pair);
931+
#if PHP_VERSION_ID >= 70000
932+
efree(pair);
933+
#endif
836934
}
837935
}
838936

@@ -899,11 +997,14 @@ PHP_FUNCTION(interpolate_linestring)
899997

900998
for (i = 0; i < new_array->count; i++) {
901999
if (new_array->status[i]) {
902-
MAKE_STD_ZVAL(pair);
1000+
GEOSPAT_MAKE_STD_ZVAL(pair);
9031001
array_init(pair);
9041002
add_next_index_double(pair, new_array->x[i]);
9051003
add_next_index_double(pair, new_array->y[i]);
9061004
add_next_index_zval(return_value, pair);
1005+
#if PHP_VERSION_ID >= 70000
1006+
efree(pair);
1007+
#endif
9071008
}
9081009
}
9091010

@@ -940,11 +1041,14 @@ PHP_FUNCTION(interpolate_polygon)
9401041

9411042
for (i = 0; i < points->count; i++) {
9421043
if (points->status[i]) {
943-
MAKE_STD_ZVAL(pair);
1044+
GEOSPAT_MAKE_STD_ZVAL(pair);
9441045
array_init(pair);
9451046
add_next_index_double(pair, points->x[i]);
9461047
add_next_index_double(pair, points->y[i]);
9471048
add_next_index_zval(return_value, pair);
1049+
#if PHP_VERSION_ID >= 70000
1050+
efree(pair);
1051+
#endif
9481052
}
9491053
}
9501054

0 commit comments

Comments
 (0)