cvs: pecl /paradox paradox.c
| From: | Uwe Steinmann | Date: | Mon, 22 Sep 2003 09:52:15 +0000 |
| Subject: | cvs: pecl /paradox paradox.c | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-469@lists.php.net to get a copy of this message | ||
steinm Mon Sep 22 05:52:15 2003 EDT
Modified files:
/pecl/paradox paradox.c
Log:
- fixed memory allocation\n- can save boolean values into record now\n- switch parameter fields
and fp in px_create_fp()\n- new function px_estrdup()\n- evaluate define for memory debugging
Index: pecl/paradox/paradox.c diff -u pecl/paradox/paradox.c:1.4 pecl/paradox/paradox.c:1.5 --- pecl/paradox/paradox.c:1.4 Sun Sep 21 09:07:06 2003 +++ pecl/paradox/paradox.c Mon Sep 22 05:52:15 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: paradox.c,v 1.4 2003/09/21 13:07:06 steinm Exp $ */ +/* $Id: paradox.c,v 1.5 2003/09/22 09:52:15 steinm Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -117,14 +117,16 @@ { void *ptr; ptr = emalloc(size); - fprintf(stderr, "Malloc: 0x%X: %s\n", ptr, caller); +#ifdef PARADOX_MEMDEBUG + fprintf(stderr, "Malloc: 0x%X (%d Bytes): %s\n", ptr, size, caller); +#endif return(ptr); } /* }}} */ /* {{{ px_realloc */ -static void *px_realloc(pxdoc_t *p, void *mem, size_t size, const char *caller) +static void *px_erealloc(pxdoc_t *p, void *mem, size_t size, const char *caller) { return(erealloc(mem, size)); } @@ -134,11 +136,28 @@ */ static void px_efree(pxdoc_t *p, void *mem) { +#ifdef PARADOX_MEMDEBUG fprintf(stderr, "Free: 0x%X\n", mem); +#endif efree(mem); } /* }}} */ +/* {{{ px_estrdup + */ +static char* px_estrdup(pxdoc_t *p, const char *str) { + char *tmp; + int len; + if(str == NULL) + return(NULL); + len = strlen(str)+1; + if(NULL == (tmp = px_emalloc(p, len, "px_estrdup"))) + return(NULL); + memcpy(tmp, str, len); + return(tmp); +} +/* }}} */ + /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini @@ -232,7 +251,7 @@ php_info_print_table_start(); php_info_print_table_header(2, "paradox support", "enabled"); php_info_print_table_row(2, "pxlib Version", tmp ); - php_info_print_table_row(2, "Revision", "$Revision: 1.4 $" ); + php_info_print_table_row(2, "Revision", "$Revision: 1.5 $" ); php_info_print_table_end(); /* Remove comments if you have entries in php.ini @@ -247,7 +266,7 @@ { pxdoc_t *pxdoc; - pxdoc = PX_new2(custom_errorhandler, px_emalloc, px_realloc, px_efree); + pxdoc = PX_new2(custom_errorhandler, px_emalloc, px_erealloc, px_efree); ZEND_REGISTER_RESOURCE(return_value, pxdoc, le_pxdoc); } @@ -302,13 +321,13 @@ WRONG_PARAM_COUNT; } - if (Z_TYPE_PP(arg2) != IS_ARRAY) { + if (Z_TYPE_PP(arg3) != IS_ARRAY) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array as second parameter"); RETURN_FALSE; } php_stream *stream; - php_stream_from_zval(stream, arg3); + php_stream_from_zval(stream, arg2); if (php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void*)&fp, 1) == FAILURE) { RETURN_FALSE; @@ -316,12 +335,13 @@ ZEND_FETCH_RESOURCE(pxdoc, pxdoc_t *, arg1, -1, "px object", le_pxdoc); - lht = Z_ARRVAL_PP(arg2); + lht = Z_ARRVAL_PP(arg3); if(0 == (numfields = zend_hash_num_elements(lht))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "the column specification array is empty"); RETURN_FALSE; } - if(NULL == (pxf = (pxfield_t *)px_malloc(pxdoc, numfields * sizeof(pxfield_t), "Could not allocate memory for field specification."))) { + if(NULL == (pxf = (pxfield_t *)px_emalloc(pxdoc, numfields * sizeof(pxfield_t), "px_create_fp: Allocate memory for field specification."))) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for field specification."); RETURN_FALSE; } @@ -337,22 +357,22 @@ /* field name */ if (zend_hash_index_find(lht2, 0, (void **)&value) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field name as first element of list in field %d", i); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_string_ex(value); if (Z_STRLEN_PP(value) > 10 || Z_STRLEN_PP(value) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid field name '%s' (must be non-empty and less than or equal to 10 characters)", Z_STRVAL_PP(value)); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf); RETURN_FALSE; } - pxf[i].px_fname = (char *) px_strdup(pxdoc, Z_STRVAL_PP(value)); + pxf[i].px_fname = px_estrdup(pxdoc, Z_STRVAL_PP(value)); /* field type */ if (zend_hash_index_find(lht2, 1, (void **)&value) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field type as second element of list in field %d", i); - px_free(pxdoc, pxf[i].px_fname); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf[i].px_fname); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_string_ex(value); @@ -380,8 +400,8 @@ pxf[i].px_ftype = pxfBytes; if (zend_hash_index_find(lht2, 2, (void **)&value) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field length as third element of list in field %d", i); - px_free(pxdoc, pxf[i].px_fname); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf[i].px_fname); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_long_ex(value); @@ -407,13 +427,13 @@ pxf[i].px_ftype = pxfCurrency; pxf[i].px_flen = 8; break; - case 'C': + case 'A': pxf[i].px_ftype = pxfAlpha; /* field length */ if (zend_hash_index_find(lht2, 2, (void **)&value) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field length as third element of list in field %d", i); - px_free(pxdoc, pxf[i].px_fname); - px_free(pxdoc, pxf); + php_error_docref(NULL TSRMLS_CC, E_ERROR, "expected field length as third element of list in field %d", i); + px_efree(pxdoc, pxf[i].px_fname); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_long_ex(value); @@ -428,7 +448,6 @@ zend_hash_move_forward(lht); } - fprintf(stderr, "Erzeuge Paradox datei\n"); if (PX_create_fp(pxdoc, pxf, numfields, fp) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not create paradox file"); RETURN_FALSE; @@ -512,7 +531,8 @@ pxh = pxdoc->px_head; - if((selectedfields = (char *) px_malloc(pxdoc, pxh->px_numfields, "Could not allocate memory for array of selected fields.")) == NULL) { + if((selectedfields = (char *) px_emalloc(pxdoc, pxh->px_numfields, "px_get_record: Allocate memory for array of selected fields.")) == NULL) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for array of selected fields."); RETURN_FALSE; } memset(selectedfields, '\0', pxh->px_numfields); @@ -522,9 +542,10 @@ pxf++; } - if((data = (char *) px_malloc(pxdoc, pxh->px_recordsize, "Could not allocate memory for record.")) == NULL) { + if((data = (char *) px_emalloc(pxdoc, pxh->px_recordsize, "px_get_record: Allocate memory for record.")) == NULL) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for record data."); if(selectedfields) - px_free(pxdoc, selectedfields); + px_efree(pxdoc, selectedfields); RETURN_FALSE; } @@ -607,8 +628,8 @@ } } } - px_free(pxdoc, selectedfields); - px_free(pxdoc, data); + px_efree(pxdoc, selectedfields); + px_efree(pxdoc, data); } /* }}} */ @@ -649,8 +670,8 @@ if(numfields > pxh->px_numfields) numfields = pxh->px_numfields; - if(NULL == (data = (char *) px_malloc(pxdoc, pxh->px_recordsize, "Could not allocate memory for record."))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not allocate memory for record data"); + if(NULL == (data = (char *) px_emalloc(pxdoc, pxh->px_recordsize, "px_put_record: Allocate memory for record data."))) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for record data"); RETURN_FALSE; } @@ -671,9 +692,16 @@ convert_to_long_ex(keydataptr); PX_put_data_long(pxdoc, &data[offset], pxf[i].px_flen, Z_LVAL_PP(keydataptr)); break; + case pxfLogical: { + char c; + convert_to_long_ex(keydataptr); + c = (char) Z_LVAL_PP(keydataptr); + PX_put_data_byte(pxdoc, &data[offset], 1, c); + break; + } case pxfNumber: convert_to_double_ex(keydataptr); - PX_put_data_double(pxdoc, &data[offset], pxf[i].px_flen, Z_LVAL_PP(keydataptr)); + PX_put_data_double(pxdoc, &data[offset], pxf[i].px_flen, Z_DVAL_PP(keydataptr)); break; }
Index: pecl/paradox/paradox.c diff -u pecl/paradox/paradox.c:1.4 pecl/paradox/paradox.c:1.5 --- pecl/paradox/paradox.c:1.4 Sun Sep 21 09:07:06 2003 +++ pecl/paradox/paradox.c Mon Sep 22 05:52:15 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: paradox.c,v 1.4 2003/09/21 13:07:06 steinm Exp $ */ +/* $Id: paradox.c,v 1.5 2003/09/22 09:52:15 steinm Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -117,14 +117,16 @@ { void *ptr; ptr = emalloc(size); - fprintf(stderr, "Malloc: 0x%X: %s\n", ptr, caller); +#ifdef PARADOX_MEMDEBUG + fprintf(stderr, "Malloc: 0x%X (%d Bytes): %s\n", ptr, size, caller); +#endif return(ptr); } /* }}} */ /* {{{ px_realloc */ -static void *px_realloc(pxdoc_t *p, void *mem, size_t size, const char *caller) +static void *px_erealloc(pxdoc_t *p, void *mem, size_t size, const char *caller) { return(erealloc(mem, size)); } @@ -134,11 +136,28 @@ */ static void px_efree(pxdoc_t *p, void *mem) { +#ifdef PARADOX_MEMDEBUG fprintf(stderr, "Free: 0x%X\n", mem); +#endif efree(mem); } /* }}} */ +/* {{{ px_estrdup + */ +static char* px_estrdup(pxdoc_t *p, const char *str) { + char *tmp; + int len; + if(str == NULL) + return(NULL); + len = strlen(str)+1; + if(NULL == (tmp = px_emalloc(p, len, "px_estrdup"))) + return(NULL); + memcpy(tmp, str, len); + return(tmp); +} +/* }}} */ + /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini @@ -232,7 +251,7 @@ php_info_print_table_start(); php_info_print_table_header(2, "paradox support", "enabled"); php_info_print_table_row(2, "pxlib Version", tmp ); - php_info_print_table_row(2, "Revision", "$Revision: 1.4 $" ); + php_info_print_table_row(2, "Revision", "$Revision: 1.5 $" ); php_info_print_table_end(); /* Remove comments if you have entries in php.ini @@ -247,7 +266,7 @@ { pxdoc_t *pxdoc; - pxdoc = PX_new2(custom_errorhandler, px_emalloc, px_realloc, px_efree); + pxdoc = PX_new2(custom_errorhandler, px_emalloc, px_erealloc, px_efree); ZEND_REGISTER_RESOURCE(return_value, pxdoc, le_pxdoc); } @@ -302,13 +321,13 @@ WRONG_PARAM_COUNT; } - if (Z_TYPE_PP(arg2) != IS_ARRAY) { + if (Z_TYPE_PP(arg3) != IS_ARRAY) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array as second parameter"); RETURN_FALSE; } php_stream *stream; - php_stream_from_zval(stream, arg3); + php_stream_from_zval(stream, arg2); if (php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void*)&fp, 1) == FAILURE) { RETURN_FALSE; @@ -316,12 +335,13 @@ ZEND_FETCH_RESOURCE(pxdoc, pxdoc_t *, arg1, -1, "px object", le_pxdoc); - lht = Z_ARRVAL_PP(arg2); + lht = Z_ARRVAL_PP(arg3); if(0 == (numfields = zend_hash_num_elements(lht))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "the column specification array is empty"); RETURN_FALSE; } - if(NULL == (pxf = (pxfield_t *)px_malloc(pxdoc, numfields * sizeof(pxfield_t), "Could not allocate memory for field specification."))) { + if(NULL == (pxf = (pxfield_t *)px_emalloc(pxdoc, numfields * sizeof(pxfield_t), "px_create_fp: Allocate memory for field specification."))) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for field specification."); RETURN_FALSE; } @@ -337,22 +357,22 @@ /* field name */ if (zend_hash_index_find(lht2, 0, (void **)&value) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field name as first element of list in field %d", i); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_string_ex(value); if (Z_STRLEN_PP(value) > 10 || Z_STRLEN_PP(value) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid field name '%s' (must be non-empty and less than or equal to 10 characters)", Z_STRVAL_PP(value)); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf); RETURN_FALSE; } - pxf[i].px_fname = (char *) px_strdup(pxdoc, Z_STRVAL_PP(value)); + pxf[i].px_fname = px_estrdup(pxdoc, Z_STRVAL_PP(value)); /* field type */ if (zend_hash_index_find(lht2, 1, (void **)&value) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field type as second element of list in field %d", i); - px_free(pxdoc, pxf[i].px_fname); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf[i].px_fname); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_string_ex(value); @@ -380,8 +400,8 @@ pxf[i].px_ftype = pxfBytes; if (zend_hash_index_find(lht2, 2, (void **)&value) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field length as third element of list in field %d", i); - px_free(pxdoc, pxf[i].px_fname); - px_free(pxdoc, pxf); + px_efree(pxdoc, pxf[i].px_fname); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_long_ex(value); @@ -407,13 +427,13 @@ pxf[i].px_ftype = pxfCurrency; pxf[i].px_flen = 8; break; - case 'C': + case 'A': pxf[i].px_ftype = pxfAlpha; /* field length */ if (zend_hash_index_find(lht2, 2, (void **)&value) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "expected field length as third element of list in field %d", i); - px_free(pxdoc, pxf[i].px_fname); - px_free(pxdoc, pxf); + php_error_docref(NULL TSRMLS_CC, E_ERROR, "expected field length as third element of list in field %d", i); + px_efree(pxdoc, pxf[i].px_fname); + px_efree(pxdoc, pxf); RETURN_FALSE; } convert_to_long_ex(value); @@ -428,7 +448,6 @@ zend_hash_move_forward(lht); } - fprintf(stderr, "Erzeuge Paradox datei\n"); if (PX_create_fp(pxdoc, pxf, numfields, fp) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not create paradox file"); RETURN_FALSE; @@ -512,7 +531,8 @@ pxh = pxdoc->px_head; - if((selectedfields = (char *) px_malloc(pxdoc, pxh->px_numfields, "Could not allocate memory for array of selected fields.")) == NULL) { + if((selectedfields = (char *) px_emalloc(pxdoc, pxh->px_numfields, "px_get_record: Allocate memory for array of selected fields.")) == NULL) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for array of selected fields."); RETURN_FALSE; } memset(selectedfields, '\0', pxh->px_numfields); @@ -522,9 +542,10 @@ pxf++; } - if((data = (char *) px_malloc(pxdoc, pxh->px_recordsize, "Could not allocate memory for record.")) == NULL) { + if((data = (char *) px_emalloc(pxdoc, pxh->px_recordsize, "px_get_record: Allocate memory for record.")) == NULL) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for record data."); if(selectedfields) - px_free(pxdoc, selectedfields); + px_efree(pxdoc, selectedfields); RETURN_FALSE; } @@ -607,8 +628,8 @@ } } } - px_free(pxdoc, selectedfields); - px_free(pxdoc, data); + px_efree(pxdoc, selectedfields); + px_efree(pxdoc, data); } /* }}} */ @@ -649,8 +670,8 @@ if(numfields > pxh->px_numfields) numfields = pxh->px_numfields; - if(NULL == (data = (char *) px_malloc(pxdoc, pxh->px_recordsize, "Could not allocate memory for record."))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not allocate memory for record data"); + if(NULL == (data = (char *) px_emalloc(pxdoc, pxh->px_recordsize, "px_put_record: Allocate memory for record data."))) { + php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for record data"); RETURN_FALSE; } @@ -671,9 +692,16 @@ convert_to_long_ex(keydataptr); PX_put_data_long(pxdoc, &data[offset], pxf[i].px_flen, Z_LVAL_PP(keydataptr)); break; + case pxfLogical: { + char c; + convert_to_long_ex(keydataptr); + c = (char) Z_LVAL_PP(keydataptr); + PX_put_data_byte(pxdoc, &data[offset], 1, c); + break; + } case pxfNumber: convert_to_double_ex(keydataptr); - PX_put_data_double(pxdoc, &data[offset], pxf[i].px_flen, Z_LVAL_PP(keydataptr)); + PX_put_data_double(pxdoc, &data[offset], pxf[i].px_flen, Z_DVAL_PP(keydataptr)); break; }