cvs: php4 /ext/printer php_printer.h printer.c
| From: | Daniel Beulshausen | Date: | Fri, 06 Jul 2001 16:21:21 +0000 |
| Subject: | cvs: php4 /ext/printer php_printer.h printer.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-6113@lists.php.net to get a copy of this message | ||
dbeu Fri Jul 6 12:21:21 2001 EDT
Modified files:
/php4/ext/printer php_printer.h printer.c
Log:
fix win9x crash bug + improve resource freeing
Index: php4/ext/printer/php_printer.h diff -u php4/ext/printer/php_printer.h:1.8 php4/ext/printer/php_printer.h:1.9 --- php4/ext/printer/php_printer.h:1.8 Thu Apr 5 08:36:00 2001 +++ php4/ext/printer/php_printer.h Fri Jul 6 12:21:19 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_printer.h,v 1.8 2001/04/05 12:36:00 dbeu Exp $ */ +/* $Id: php_printer.h,v 1.9 2001/07/06 16:21:19 dbeu Exp $ */ #ifndef PHP_PRINTER_H #define PHP_PRINTER_H @@ -67,7 +67,6 @@ HANDLE handle; LPTSTR name; LPDEVMODE device; - DOC_INFO_1 info1; DOCINFO info; HDC dc; } printer; Index: php4/ext/printer/printer.c diff -u php4/ext/printer/printer.c:1.13 php4/ext/printer/printer.c:1.14 --- php4/ext/printer/printer.c:1.13 Wed Jun 6 09:05:48 2001 +++ php4/ext/printer/printer.c Fri Jul 6 12:21:20 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: printer.c,v 1.13 2001/06/06 13:05:48 rasmus Exp $ */ +/* $Id: printer.c,v 1.14 2001/07/06 16:21:20 dbeu Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -48,7 +48,8 @@ COLORREF hex_to_rgb(char * hex); char *rgb_to_hex(COLORREF rgb); -static void destroy_ressources(zend_rsrc_list_entry *resource); +static void printer_close(zend_rsrc_list_entry *resource); +static void object_close(zend_rsrc_list_entry *resource); char *get_default_printer(void); function_entry printer_functions[] = { @@ -109,7 +110,7 @@ php_info_print_table_row(2, "Printer Support", "enabled"); php_info_print_table_row(2, "Default printing device", PRINTERG(default_printer) ? PRINTERG(default_printer) : "<b>not detected</b>"); php_info_print_table_row(2, "Module state", "working"); - php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.13 2001/06/06 13:05:48 rasmus Exp $"); + php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.14 2001/07/06 16:21:20 dbeu Exp $"); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); } @@ -118,15 +119,17 @@ { PRINTERLS_FETCH(); - if(new_value != NULL && new_value != "") { - efree(PRINTERG(default_printer)); + if(new_value != "") { + if(PRINTERG(default_printer)) { + efree(PRINTERG(default_printer)); + } PRINTERG(default_printer) = estrdup(new_value); } return SUCCESS; } PHP_INI_BEGIN() - STD_PHP_INI_ENTRY("printer.default_printer", NULL, PHP_INI_ALL, OnUpdatePrinter, default_printer, zend_printer_globals, printer_globals) + STD_PHP_INI_ENTRY("printer.default_printer", "", PHP_INI_ALL, OnUpdatePrinter, default_printer, zend_printer_globals, printer_globals) PHP_INI_END() #define COPIES 0 @@ -153,14 +156,21 @@ printer_globals->default_printer = get_default_printer(); } +static void php_printer_shutdown(zend_printer_globals *printer_globals) { + if(printer_globals->default_printer) { + efree(printer_globals->default_printer); + } +} + PHP_MINIT_FUNCTION(printer) { - ZEND_INIT_MODULE_GLOBALS(printer, php_printer_init, NULL); + ZEND_INIT_MODULE_GLOBALS(printer, php_printer_init, php_printer_shutdown); REGISTER_INI_ENTRIES(); - le_printer = zend_register_list_destructors_ex(destroy_ressources, NULL, "printer", module_number); - le_pen = zend_register_list_destructors_ex(NULL, NULL, "printer pen", module_number); - le_font = zend_register_list_destructors_ex(NULL, NULL, "printer font", module_number); - le_brush = zend_register_list_destructors_ex(NULL, NULL, "printer brush", module_number); + + le_printer = zend_register_list_destructors_ex(printer_close, NULL, "printer", module_number); + le_pen = zend_register_list_destructors_ex(object_close, NULL, "printer pen", module_number); + le_font = zend_register_list_destructors_ex(object_close, NULL, "printer font", module_number); + le_brush = zend_register_list_destructors_ex(object_close, NULL, "printer brush", module_number); REGP_CONSTANT("PRINTER_COPIES", COPIES); REGP_CONSTANT("PRINTER_MODE", MODE); @@ -243,7 +253,7 @@ -/* {{{ proto mixed printer_open(string printername) +/* {{{ proto mixed printer_open([string printername]) Return a handle to the printer or false if connection failed */ PHP_FUNCTION(printer_open) { @@ -286,7 +296,7 @@ /* }}} */ -/* {{{ proto bool printer_close(resource connection) +/* {{{ proto void printer_close(resource connection) Close the printer connection */ PHP_FUNCTION(printer_close) { @@ -299,14 +309,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); - if( ClosePrinter(resource->handle) != 0 ) { - efree(resource->device); - RETURN_TRUE; - } - else { - php_error(E_WARNING,"the printer connection to [%s] couldn't be closed", resource->name); - RETURN_FALSE; - } + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -317,6 +320,7 @@ { pval **arg1, **arg2; printer *resource; + DOC_INFO_1 docinfo; int sd, sp = 0, recieved; if ( zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE ) { @@ -327,11 +331,11 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); convert_to_string_ex(arg2); - resource->info1.pDocName = (PSTR)resource->info.lpszDocName; - resource->info1.pDatatype = (PSTR)resource->info.lpszDatatype; - resource->info1.pOutputFile = NULL; + docinfo.pDocName = (LPTSTR)resource->info.lpszDocName; + docinfo.pDatatype = (LPTSTR)resource->info.lpszDatatype; + docinfo.pOutputFile = NULL; - sd = StartDocPrinter(resource->handle, 1, (LPSTR)&resource->info1); + sd = StartDocPrinter(resource->handle, 1, (LPSTR)&docinfo); sp = StartPagePrinter(resource->handle); if( sd && sp ) { @@ -341,7 +345,7 @@ RETURN_TRUE; } else { - php_error(E_WARNING,"couldn't allocate the printerjob" ); + php_error(E_WARNING,"couldn't allocate the printerjob [%d]", GetLastError()); RETURN_FALSE; } } @@ -783,16 +787,13 @@ WRONG_PARAM_COUNT; } - pen = (HPEN)emalloc(sizeof(HPEN)); - convert_to_long_ex(arg1); convert_to_long_ex(arg2); convert_to_string_ex(arg3); pen = CreatePen(Z_LVAL_PP(arg1), Z_LVAL_PP(arg2), hex_to_rgb(Z_STRVAL_PP(arg3))); - if(pen == NULL) { - efree(pen); + if(!pen) { RETURN_FALSE; } @@ -801,7 +802,7 @@ /* }}} */ -/* {{{ proto bool printer_delete_pen(resource pen_handle) +/* {{{ proto void printer_delete_pen(resource pen_handle) Delete a pen */ PHP_FUNCTION(printer_delete_pen) { @@ -814,11 +815,7 @@ ZEND_FETCH_RESOURCE(pen, HPEN, arg1, -1, "Pen Handle", le_pen); - if(DeleteObject(pen)) { - RETURN_TRUE; - } - - RETURN_FALSE; + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -856,8 +853,6 @@ WRONG_PARAM_COUNT; } - brush = (HBRUSH)emalloc(sizeof(HBRUSH)); - convert_to_long_ex(arg1); convert_to_string_ex(arg2); @@ -874,8 +869,7 @@ brush = CreateHatchBrush(Z_LVAL_PP(arg1), hex_to_rgb(Z_STRVAL_PP(arg2))); } - if(brush == NULL) { - efree(brush); + if(!brush) { RETURN_FALSE; } @@ -884,7 +878,7 @@ /* }}} */ -/* {{{ proto bool printer_delete_brush(resource brush_handle) +/* {{{ proto void printer_delete_brush(resource brush_handle) Delete a brush */ PHP_FUNCTION(printer_delete_brush) { @@ -897,11 +891,7 @@ ZEND_FETCH_RESOURCE(brush, HBRUSH, arg1, -1, "Brush Handle", le_brush); - if(DeleteObject(brush)) { - RETURN_TRUE; - } - - RETURN_FALSE; + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -938,8 +928,6 @@ WRONG_PARAM_COUNT; } - font = (HFONT)emalloc(sizeof(HFONT)); - convert_to_string_ex(arg1); face = estrndup(Z_STRVAL_PP(arg1), 32); convert_to_long_ex(arg2); @@ -953,8 +941,7 @@ font = CreateFont(Z_LVAL_PP(arg2), Z_LVAL_PP(arg3), Z_LVAL_PP(arg8), Z_LVAL_PP(arg8), Z_LVAL_PP(arg4), Z_BVAL_PP(arg5), Z_BVAL_PP(arg6), Z_BVAL_PP(arg7), DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_ROMAN, face); efree(face); - if(font == NULL) { - efree(font); + if(!font) { RETURN_FALSE; } @@ -976,7 +963,7 @@ ZEND_FETCH_RESOURCE(font, HFONT, arg1, -1, "Font Handle", le_font); - DeleteObject(font); + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -1273,7 +1260,7 @@ if(need > 0) { printer = (PRINTER_INFO_2 *)emalloc(need+1); EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, (LPBYTE)printer, need, &need, &received); - printer_name = estrdup(printer->pDriverName); + printer_name = estrdup(printer->pPrinterName); efree(printer); } } @@ -1330,10 +1317,17 @@ sprintf(string, "%02x%02x%02x", GetRValue(rgb), GetGValue(rgb),GetBValue(rgb)); return string; } -/* resource deallocation */ -static void destroy_ressources(zend_rsrc_list_entry *resource) + +static void printer_close(zend_rsrc_list_entry *resource) +{ + printer *p = (printer*)resource->ptr; + ClosePrinter(p->handle); + efree(p); +} +static void object_close(zend_rsrc_list_entry *resource) { - efree(resource); + HGDIOBJ p = (HGDIOBJ)resource->ptr; + DeleteObject(p); } #endif
Index: php4/ext/printer/php_printer.h diff -u php4/ext/printer/php_printer.h:1.8 php4/ext/printer/php_printer.h:1.9 --- php4/ext/printer/php_printer.h:1.8 Thu Apr 5 08:36:00 2001 +++ php4/ext/printer/php_printer.h Fri Jul 6 12:21:19 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_printer.h,v 1.8 2001/04/05 12:36:00 dbeu Exp $ */ +/* $Id: php_printer.h,v 1.9 2001/07/06 16:21:19 dbeu Exp $ */ #ifndef PHP_PRINTER_H #define PHP_PRINTER_H @@ -67,7 +67,6 @@ HANDLE handle; LPTSTR name; LPDEVMODE device; - DOC_INFO_1 info1; DOCINFO info; HDC dc; } printer; Index: php4/ext/printer/printer.c diff -u php4/ext/printer/printer.c:1.13 php4/ext/printer/printer.c:1.14 --- php4/ext/printer/printer.c:1.13 Wed Jun 6 09:05:48 2001 +++ php4/ext/printer/printer.c Fri Jul 6 12:21:20 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: printer.c,v 1.13 2001/06/06 13:05:48 rasmus Exp $ */ +/* $Id: printer.c,v 1.14 2001/07/06 16:21:20 dbeu Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -48,7 +48,8 @@ COLORREF hex_to_rgb(char * hex); char *rgb_to_hex(COLORREF rgb); -static void destroy_ressources(zend_rsrc_list_entry *resource); +static void printer_close(zend_rsrc_list_entry *resource); +static void object_close(zend_rsrc_list_entry *resource); char *get_default_printer(void); function_entry printer_functions[] = { @@ -109,7 +110,7 @@ php_info_print_table_row(2, "Printer Support", "enabled"); php_info_print_table_row(2, "Default printing device", PRINTERG(default_printer) ? PRINTERG(default_printer) : "<b>not detected</b>"); php_info_print_table_row(2, "Module state", "working"); - php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.13 2001/06/06 13:05:48 rasmus Exp $"); + php_info_print_table_row(2, "RCS Version", "$Id: printer.c,v 1.14 2001/07/06 16:21:20 dbeu Exp $"); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); } @@ -118,15 +119,17 @@ { PRINTERLS_FETCH(); - if(new_value != NULL && new_value != "") { - efree(PRINTERG(default_printer)); + if(new_value != "") { + if(PRINTERG(default_printer)) { + efree(PRINTERG(default_printer)); + } PRINTERG(default_printer) = estrdup(new_value); } return SUCCESS; } PHP_INI_BEGIN() - STD_PHP_INI_ENTRY("printer.default_printer", NULL, PHP_INI_ALL, OnUpdatePrinter, default_printer, zend_printer_globals, printer_globals) + STD_PHP_INI_ENTRY("printer.default_printer", "", PHP_INI_ALL, OnUpdatePrinter, default_printer, zend_printer_globals, printer_globals) PHP_INI_END() #define COPIES 0 @@ -153,14 +156,21 @@ printer_globals->default_printer = get_default_printer(); } +static void php_printer_shutdown(zend_printer_globals *printer_globals) { + if(printer_globals->default_printer) { + efree(printer_globals->default_printer); + } +} + PHP_MINIT_FUNCTION(printer) { - ZEND_INIT_MODULE_GLOBALS(printer, php_printer_init, NULL); + ZEND_INIT_MODULE_GLOBALS(printer, php_printer_init, php_printer_shutdown); REGISTER_INI_ENTRIES(); - le_printer = zend_register_list_destructors_ex(destroy_ressources, NULL, "printer", module_number); - le_pen = zend_register_list_destructors_ex(NULL, NULL, "printer pen", module_number); - le_font = zend_register_list_destructors_ex(NULL, NULL, "printer font", module_number); - le_brush = zend_register_list_destructors_ex(NULL, NULL, "printer brush", module_number); + + le_printer = zend_register_list_destructors_ex(printer_close, NULL, "printer", module_number); + le_pen = zend_register_list_destructors_ex(object_close, NULL, "printer pen", module_number); + le_font = zend_register_list_destructors_ex(object_close, NULL, "printer font", module_number); + le_brush = zend_register_list_destructors_ex(object_close, NULL, "printer brush", module_number); REGP_CONSTANT("PRINTER_COPIES", COPIES); REGP_CONSTANT("PRINTER_MODE", MODE); @@ -243,7 +253,7 @@ -/* {{{ proto mixed printer_open(string printername) +/* {{{ proto mixed printer_open([string printername]) Return a handle to the printer or false if connection failed */ PHP_FUNCTION(printer_open) { @@ -286,7 +296,7 @@ /* }}} */ -/* {{{ proto bool printer_close(resource connection) +/* {{{ proto void printer_close(resource connection) Close the printer connection */ PHP_FUNCTION(printer_close) { @@ -299,14 +309,7 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); - if( ClosePrinter(resource->handle) != 0 ) { - efree(resource->device); - RETURN_TRUE; - } - else { - php_error(E_WARNING,"the printer connection to [%s] couldn't be closed", resource->name); - RETURN_FALSE; - } + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -317,6 +320,7 @@ { pval **arg1, **arg2; printer *resource; + DOC_INFO_1 docinfo; int sd, sp = 0, recieved; if ( zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE ) { @@ -327,11 +331,11 @@ ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer); convert_to_string_ex(arg2); - resource->info1.pDocName = (PSTR)resource->info.lpszDocName; - resource->info1.pDatatype = (PSTR)resource->info.lpszDatatype; - resource->info1.pOutputFile = NULL; + docinfo.pDocName = (LPTSTR)resource->info.lpszDocName; + docinfo.pDatatype = (LPTSTR)resource->info.lpszDatatype; + docinfo.pOutputFile = NULL; - sd = StartDocPrinter(resource->handle, 1, (LPSTR)&resource->info1); + sd = StartDocPrinter(resource->handle, 1, (LPSTR)&docinfo); sp = StartPagePrinter(resource->handle); if( sd && sp ) { @@ -341,7 +345,7 @@ RETURN_TRUE; } else { - php_error(E_WARNING,"couldn't allocate the printerjob" ); + php_error(E_WARNING,"couldn't allocate the printerjob [%d]", GetLastError()); RETURN_FALSE; } } @@ -783,16 +787,13 @@ WRONG_PARAM_COUNT; } - pen = (HPEN)emalloc(sizeof(HPEN)); - convert_to_long_ex(arg1); convert_to_long_ex(arg2); convert_to_string_ex(arg3); pen = CreatePen(Z_LVAL_PP(arg1), Z_LVAL_PP(arg2), hex_to_rgb(Z_STRVAL_PP(arg3))); - if(pen == NULL) { - efree(pen); + if(!pen) { RETURN_FALSE; } @@ -801,7 +802,7 @@ /* }}} */ -/* {{{ proto bool printer_delete_pen(resource pen_handle) +/* {{{ proto void printer_delete_pen(resource pen_handle) Delete a pen */ PHP_FUNCTION(printer_delete_pen) { @@ -814,11 +815,7 @@ ZEND_FETCH_RESOURCE(pen, HPEN, arg1, -1, "Pen Handle", le_pen); - if(DeleteObject(pen)) { - RETURN_TRUE; - } - - RETURN_FALSE; + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -856,8 +853,6 @@ WRONG_PARAM_COUNT; } - brush = (HBRUSH)emalloc(sizeof(HBRUSH)); - convert_to_long_ex(arg1); convert_to_string_ex(arg2); @@ -874,8 +869,7 @@ brush = CreateHatchBrush(Z_LVAL_PP(arg1), hex_to_rgb(Z_STRVAL_PP(arg2))); } - if(brush == NULL) { - efree(brush); + if(!brush) { RETURN_FALSE; } @@ -884,7 +878,7 @@ /* }}} */ -/* {{{ proto bool printer_delete_brush(resource brush_handle) +/* {{{ proto void printer_delete_brush(resource brush_handle) Delete a brush */ PHP_FUNCTION(printer_delete_brush) { @@ -897,11 +891,7 @@ ZEND_FETCH_RESOURCE(brush, HBRUSH, arg1, -1, "Brush Handle", le_brush); - if(DeleteObject(brush)) { - RETURN_TRUE; - } - - RETURN_FALSE; + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -938,8 +928,6 @@ WRONG_PARAM_COUNT; } - font = (HFONT)emalloc(sizeof(HFONT)); - convert_to_string_ex(arg1); face = estrndup(Z_STRVAL_PP(arg1), 32); convert_to_long_ex(arg2); @@ -953,8 +941,7 @@ font = CreateFont(Z_LVAL_PP(arg2), Z_LVAL_PP(arg3), Z_LVAL_PP(arg8), Z_LVAL_PP(arg8), Z_LVAL_PP(arg4), Z_BVAL_PP(arg5), Z_BVAL_PP(arg6), Z_BVAL_PP(arg7), DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_ROMAN, face); efree(face); - if(font == NULL) { - efree(font); + if(!font) { RETURN_FALSE; } @@ -976,7 +963,7 @@ ZEND_FETCH_RESOURCE(font, HFONT, arg1, -1, "Font Handle", le_font); - DeleteObject(font); + zend_list_delete(Z_RESVAL_PP(arg1)); } /* }}} */ @@ -1273,7 +1260,7 @@ if(need > 0) { printer = (PRINTER_INFO_2 *)emalloc(need+1); EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, (LPBYTE)printer, need, &need, &received); - printer_name = estrdup(printer->pDriverName); + printer_name = estrdup(printer->pPrinterName); efree(printer); } } @@ -1330,10 +1317,17 @@ sprintf(string, "%02x%02x%02x", GetRValue(rgb), GetGValue(rgb),GetBValue(rgb)); return string; } -/* resource deallocation */ -static void destroy_ressources(zend_rsrc_list_entry *resource) + +static void printer_close(zend_rsrc_list_entry *resource) +{ + printer *p = (printer*)resource->ptr; + ClosePrinter(p->handle); + efree(p); +} +static void object_close(zend_rsrc_list_entry *resource) { - efree(resource); + HGDIOBJ p = (HGDIOBJ)resource->ptr; + DeleteObject(p); } #endif