Ignore:
Timestamp:
Oct 28, 2008, 6:59:41 PM (15 years ago)
Author:
nico
Message:
  • Add php binding for caca_draw_polyline and caca_draw_thin_polyline
  • Add a sample program examples/polyline.php
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca-php/php_caca.c

    r3129 r3131  
    376376//------- PHP Binding's specific functions ----------//
    377377
     378//TODO: register new resources in caca_get_event and remove caca_create_event
    378379PHP_FUNCTION(caca_create_event) {
    379380        caca_event_t *event;
     
    425426        caca_canvas_t *canvas;
    426427        FETCH_CANVAS(canvas);
    427         RETURN_STRING((char *) caca_get_canvas_chars(canvas), 1); //TODO: check that return \0 terminated string
     428        RETURN_STRING((char *) caca_get_canvas_chars(canvas), 1);
     429        //TODO: check that return \0 terminated string
    428430}
    429431
     
    431433        caca_canvas_t *canvas;
    432434        FETCH_CANVAS(canvas);
    433         RETURN_STRING((char *) caca_get_canvas_attrs(canvas), 1); //TODO: check that return \0 terminated string
     435        RETURN_STRING((char *) caca_get_canvas_attrs(canvas), 1);
     436        //TODO: check that return \0 terminated string
    434437}
    435438
     
    732735
    733736PHP_FUNCTION(caca_draw_polyline) {
     737        zval *zval_res, *zval_arr;
     738        char *c;
     739        long c_len;
     740
     741        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ras", &zval_res, &zval_arr, &c, &c_len) == FAILURE) {
     742                RETURN_FALSE;
     743        }
     744
     745        if (c_len != 1) {
     746                RETURN_FALSE;
     747        }
     748
     749        caca_canvas_t *canvas;
     750        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
     751
     752        HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
     753        int lenmax = zend_hash_num_elements(arr_hash);
     754        int *tbl_x, *tbl_y;
     755        int tbl_count = 0;
     756        tbl_x = malloc(sizeof(int) * lenmax);
     757        tbl_y = malloc(sizeof(int) * lenmax);
     758       
     759        HashPosition pos;
     760        zval **pt, **x, **y;
     761        for (
     762                zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
     763                zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
     764                zend_hash_move_forward_ex(arr_hash, &pos)
     765        ) {
     766                if (
     767                        Z_TYPE_P(*pt) == IS_ARRAY
     768                        && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
     769                        && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
     770                ) {
     771                        convert_to_long_ex(x);
     772                        convert_to_long_ex(y);
     773                        tbl_x[tbl_count] = Z_LVAL_PP(x);
     774                        tbl_y[tbl_count] = Z_LVAL_PP(y);
     775                        tbl_count++;
     776                }
     777        }
     778        int res = caca_draw_polyline(canvas, tbl_x, tbl_y, tbl_count - 1, c[0]);
     779        free(tbl_x);
     780        free(tbl_y);
     781        RETURN_SUCCESS(res);
    734782}
    735783
     
    746794
    747795PHP_FUNCTION(caca_draw_thin_polyline) {
     796        zval *zval_res, *zval_arr;
     797        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &zval_arr) == FAILURE) {
     798                RETURN_FALSE;
     799        }
     800
     801        caca_canvas_t *canvas;
     802        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
     803
     804        HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
     805        int lenmax = zend_hash_num_elements(arr_hash);
     806        int *tbl_x, *tbl_y;
     807        int tbl_count = 0;
     808        tbl_x = malloc(sizeof(int) * lenmax);
     809        tbl_y = malloc(sizeof(int) * lenmax);
     810       
     811        HashPosition pos;
     812        zval **pt, **x, **y;
     813        for (
     814                zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
     815                zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
     816                zend_hash_move_forward_ex(arr_hash, &pos)
     817        ) {
     818                if (
     819                        Z_TYPE_P(*pt) == IS_ARRAY
     820                        && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
     821                        && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
     822                ) {
     823                        convert_to_long_ex(x);
     824                        convert_to_long_ex(y);
     825                        tbl_x[tbl_count] = Z_LVAL_PP(x);
     826                        tbl_y[tbl_count] = Z_LVAL_PP(y);
     827                        tbl_count++;
     828                }
     829        }
     830        int res = caca_draw_thin_polyline(canvas, tbl_x, tbl_y, tbl_count - 1);
     831        free(tbl_x);
     832        free(tbl_y);
     833        RETURN_SUCCESS(res);
    748834}
    749835
     
    12821368        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
    12831369
    1284         //TODO: get a string instead of a long
    1285         caca_put_figchar(canvas, c);
     1370        RETURN_SUCCESS(caca_put_figchar(canvas, c));
    12861371}
    12871372
Note: See TracChangeset for help on using the changeset viewer.