diff --git a/caca-php/examples/demo.php b/caca-php/examples/demo.php index c66da50..6583d1e 100644 --- a/caca-php/examples/demo.php +++ b/caca-php/examples/demo.php @@ -1,5 +1,5 @@ #!/usr/bin/php5 - diff --git a/caca-php/examples/dithering.php b/caca-php/examples/dithering.php index 97551a3..75b84d1 100755 --- a/caca-php/examples/dithering.php +++ b/caca-php/examples/dithering.php @@ -1,6 +1,10 @@ #!/usr/bin/php5 - $name) echo "* $name ($type)\n"; echo "\n"; -echo "Available export modules:\n"; +echo "Available import formats:\n"; +$list = caca_get_import_list(); +foreach($list as $format => $name) + echo "* $name ($format)\n"; +echo "\n"; + +echo "Available export formats:\n"; $list = caca_get_export_list(); -foreach($list as $type => $name) - echo "* $name ($type)\n"; +foreach($list as $format => $name) + echo "* $name ($format)\n"; echo "\n"; echo "Available caca fonts:\n"; diff --git a/caca-php/examples/example1.php b/caca-php/examples/example1.php index d62fb48..b1e7329 100755 --- a/caca-php/examples/example1.php +++ b/caca-php/examples/example1.php @@ -1,5 +1,5 @@ #!/usr/bin/php5 -trueColor) { - int line_size = img->sx * sizeof(int); - void *result = malloc(img->sy * line_size); - int j; - for (j = 0; j < img->sy; j++) - memcpy(result + (j * line_size), (const void *) img->tpixels[j], line_size); - return result; - } - return NULL; -} - + if (!img->trueColor) + return NULL; + int line_size = img->sx * sizeof(int); + void *result = malloc(img->sy * line_size); + int j; + for (j = 0; j < img->sy; j++) + memcpy(result + (j * line_size), (const void *) img->tpixels[j], line_size); + return result; +} //------- PHP Binding's specific functions ----------// @@ -396,12 +393,6 @@ PHP_FUNCTION(caca_create_canvas) { ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas); } -PHP_FUNCTION(caca_manage_canvas) { -} - -PHP_FUNCTION(caca_unmanage_canvas) { -} - PHP_FUNCTION(caca_set_canvas_size) { zval *_zval; long width, height = 0; @@ -1239,18 +1230,59 @@ PHP_FUNCTION(caca_render_canvas) { } PHP_FUNCTION(caca_canvas_set_figfont) { + zval *_zval; + char *font; + long font_len; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &font, &font_len) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + RETURN_SUCCESS(caca_canvas_set_figfont(canvas, font)); } PHP_FUNCTION(caca_put_figchar) { } PHP_FUNCTION(caca_flush_figlet) { + zval *_zval; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + RETURN_SUCCESS(caca_flush_figlet(canvas)); +} + +PHP_FUNCTION(caca_file_open) { + char *path, *mode; + long path_len, mode_len; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &mode, &mode_len) == FAILURE) { + RETURN_FALSE; + } + caca_file_t *file = caca_file_open(path, mode); + ZEND_REGISTER_RESOURCE(return_value, file, le_caca_file); } PHP_FUNCTION(caca_file_close) { + zval *_zval; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { + RETURN_FALSE; + } + caca_file_t *file; + ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file); + //TODO: check that file was not already closed + RETURN_SUCCESS(caca_file_close(file)); } PHP_FUNCTION(caca_file_tell) { + zval *_zval; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { + RETURN_FALSE; + } + caca_file_t *file; + ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file); + RETURN_LONG(caca_file_tell(file)); } PHP_FUNCTION(caca_file_read) { @@ -1263,25 +1295,47 @@ PHP_FUNCTION(caca_file_gets) { } PHP_FUNCTION(caca_file_eof) { + zval *_zval; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { + RETURN_FALSE; + } + caca_file_t *file; + ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file); + RETURN_BOOL(caca_file_eof(file) != 0); } PHP_FUNCTION(caca_import_string) { zval *_zval; - char *src, *type; - long src_len, type_len = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_len, &type, &type_len) == FAILURE) { + char *src, *format; + long src_len, format_len = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_len, &format, &format_len) == FAILURE) { RETURN_FALSE; } caca_canvas_t *canvas; ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); - RETURN_LONG(caca_import_memory(canvas, src, src_len, type)); + RETURN_LONG(caca_import_memory(canvas, src, src_len, format)); } PHP_FUNCTION(caca_import_file) { + zval *_zval; + char *filename, *format; + long filename_len, format_len = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &filename, &filename_len, &format, &format_len) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + + RETURN_LONG(caca_import_file(canvas, filename, format)); } PHP_FUNCTION(caca_get_import_list) { + char const * const *list = caca_get_import_list(); + int i; + array_init(return_value); + for(i = 0; list[i]; i += 2) + add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1); } PHP_FUNCTION(caca_export_string) { @@ -1401,7 +1455,7 @@ PHP_FUNCTION(caca_set_display_time) { PHP_FUNCTION(caca_get_display_time) { caca_display_t *display; FETCH_DISPLAY(display); - RETURN_LONG(caca_get_display_time(display)); //TODO: check return value + RETURN_LONG(caca_get_display_time(display)); } PHP_FUNCTION(caca_get_display_width) { diff --git a/caca-php/php_caca.h b/caca-php/php_caca.h index fedcf1f..2ccccbe 100644 --- a/caca-php/php_caca.h +++ b/caca-php/php_caca.h @@ -38,8 +38,6 @@ PHP_MINFO_FUNCTION(caca); PHP_FUNCTION(caca_create_event); PHP_FUNCTION(caca_create_canvas); -PHP_FUNCTION(caca_manage_canvas); -PHP_FUNCTION(caca_unmanage_canvas); PHP_FUNCTION(caca_set_canvas_size); PHP_FUNCTION(caca_get_canvas_width); PHP_FUNCTION(caca_get_canvas_height); @@ -133,6 +131,7 @@ PHP_FUNCTION(caca_render_canvas); PHP_FUNCTION(caca_canvas_set_figfont); PHP_FUNCTION(caca_put_figchar); PHP_FUNCTION(caca_flush_figlet); +PHP_FUNCTION(caca_file_open); PHP_FUNCTION(caca_file_close); PHP_FUNCTION(caca_file_tell); PHP_FUNCTION(caca_file_read);