cvs: pecl /paradox paradox.c php_paradox.h

From: Date: Sun, 21 Sep 2003 13:07:06 +0000
Subject: cvs: pecl /paradox paradox.c php_paradox.h
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-463@lists.php.net to get a copy of this message
steinm Sun Sep 21 09:07:06 2003 EDT Modified files: /pecl/paradox paradox.c php_paradox.h Log: - new functions px_set_tablename(), px_put_record(), and px_create_fp()

Index: pecl/paradox/paradox.c diff -u pecl/paradox/paradox.c:1.3 pecl/paradox/paradox.c:1.4 --- pecl/paradox/paradox.c:1.3 Sun Jul 27 12:39:16 2003 +++ pecl/paradox/paradox.c Sun Sep 21 09:07:06 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: paradox.c,v 1.3 2003/07/27 16:39:16 steinm Exp $ */ +/* $Id: paradox.c,v 1.4 2003/09/21 13:07:06 steinm Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -45,14 +45,17 @@ function_entry paradox_functions[] = { PHP_FE(px_new, NULL) PHP_FE(px_open_fp, NULL) + PHP_FE(px_create_fp, NULL) PHP_FE(px_close, NULL) PHP_FE(px_delete, NULL) PHP_FE(px_numrecords, NULL) PHP_FE(px_numfields, NULL) PHP_FE(px_get_record, NULL) + PHP_FE(px_put_record, NULL) PHP_FE(px_get_field, NULL) PHP_FE(px_get_schema, NULL) PHP_FE(px_set_targetencoding, NULL) + PHP_FE(px_set_tablename, NULL) {NULL, NULL, NULL} /* Must be the last line in paradox_functions[] */ }; /* }}} */ @@ -95,8 +98,15 @@ static void custom_errorhandler(pxdoc_t *p, int type, const char *shortmsg) { TSRMLS_FETCH(); - php_error_docref(NULL TSRMLS_CC, E_ERROR, shortmsg); - switch (type){ + switch(type) { + case PX_MemoryError: + case PX_IOError: + case PX_RuntimeError: + php_error_docref(NULL TSRMLS_CC, E_ERROR, shortmsg); + break; + case PX_Warning: + php_error_docref(NULL TSRMLS_CC, E_WARNING, shortmsg); + break; } } /* }}} */ @@ -105,7 +115,10 @@ */ static void *px_emalloc(pxdoc_t *p, size_t size, const char *caller) { - return(emalloc(size)); + void *ptr; + ptr = emalloc(size); + fprintf(stderr, "Malloc: 0x%X: %s\n", ptr, caller); + return(ptr); } /* }}} */ @@ -121,6 +134,7 @@ */ static void px_efree(pxdoc_t *p, void *mem) { + fprintf(stderr, "Free: 0x%X\n", mem); efree(mem); } /* }}} */ @@ -218,7 +232,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.3 $" ); + php_info_print_table_row(2, "Revision", "$Revision: 1.4 $" ); php_info_print_table_end(); /* Remove comments if you have entries in php.ini @@ -271,6 +285,159 @@ /* }}} */ +/* {{{ proto bool px_create_fp(resource pxdoc, array fieldspec, resource filedesc) + Creates a new paradox database. */ +PHP_FUNCTION(px_create_fp) +{ + zval **arg1, **arg2, **arg3; + HashTable *lht; + zval *keydata, **keydataptr; + FILE *fp = NULL; + pxdoc_t *pxdoc = NULL; + pxfield_t *pxf; + int numfields, i; + int argc = ZEND_NUM_ARGS(); + + if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &arg1, &arg2, &arg3) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if (Z_TYPE_PP(arg2) != 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); + + if (php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void*)&fp, 1) == FAILURE) { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(pxdoc, pxdoc_t *, arg1, -1, "px object", le_pxdoc); + + lht = Z_ARRVAL_PP(arg2); + 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."))) { + RETURN_FALSE; + } + + zend_hash_internal_pointer_reset(lht); + for(i=0; i<numfields; i++) { + HashTable *lht2; + zval **value; + char *str; + zend_hash_get_current_data(lht, (void **) &keydataptr); + if(Z_TYPE_PP(keydataptr) == IS_ARRAY) { + lht2 = Z_ARRVAL_PP(keydataptr); + + /* 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); + 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); + RETURN_FALSE; + } + pxf[i].px_fname = (char *) px_strdup(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); + RETURN_FALSE; + } + convert_to_string_ex(value); + str = Z_STRVAL_PP(value); + /* verify the field length */ + switch ((int) str[0]) { + case 'L': + pxf[i].px_ftype = pxfLogical; + pxf[i].px_flen = 1; + break; + case 'S': + pxf[i].px_ftype = pxfShort; + pxf[i].px_flen = 2; + break; + case 'I': + pxf[i].px_ftype = pxfLong; + pxf[i].px_flen = 4; + break; + case '+': + pxf[i].px_ftype = pxfAutoInc; + pxf[i].px_flen = 4; + break; + case 'M': + /* This should rather be pxfMemoBLOb or pxfBLOb */ + 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); + RETURN_FALSE; + } + convert_to_long_ex(value); + pxf[i].px_flen = Z_LVAL_PP(value); + break; + case 'D': + pxf[i].px_ftype = pxfDate; + pxf[i].px_flen = 4; + break; + case 'T': + pxf[i].px_ftype = pxfTime; + pxf[i].px_flen = 4; + break; + case '@': + pxf[i].px_ftype = pxfTimestamp; + pxf[i].px_flen = 8; + break; + case 'N': + pxf[i].px_ftype = pxfNumber; + pxf[i].px_flen = 8; + break; + case '$': + pxf[i].px_ftype = pxfCurrency; + pxf[i].px_flen = 8; + break; + case 'C': + 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); + RETURN_FALSE; + } + convert_to_long_ex(value); + pxf[i].px_flen = Z_LVAL_PP(value); + break; + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown field type '%c'", value[0]); + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array as field specification number %d.", i); + } + 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; + } + + RETURN_TRUE; +} +/* }}} */ + /* {{{ proto bool px_close(resource pxdoc) Closes the paradox document */ PHP_FUNCTION(px_close) @@ -445,6 +612,81 @@ } /* }}} */ +/* {{{ proto bool px_put_record(resource pxdoc, array data) + Stores a record from the database */ +PHP_FUNCTION(px_put_record) +{ + zval **arg1, **arg2; + HashTable *lht; + pxdoc_t *pxdoc; + pxhead_t *pxh; + pxfield_t *pxf; + char *data; + int i, offset, numfields; + + if(ZEND_NUM_ARGS() != 2) { + WRONG_PARAM_COUNT; + } + if (zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { + RETURN_FALSE; + } + + if (Z_TYPE_PP(arg2) != IS_ARRAY) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array as second parameter"); + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(pxdoc, pxdoc_t *, arg1, -1, "px object", le_pxdoc); + pxh = pxdoc->px_head; + pxf = pxh->px_fields; + + lht = Z_ARRVAL_PP(arg2); + if(0 == (numfields = zend_hash_num_elements(lht))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "the data array is empty"); + RETURN_FALSE; + } + /* Don't take more columns into account then columns in the database */ + 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"); + RETURN_FALSE; + } + + offset = 0; + for(i=0; i<numfields; i++) { + zval **keydataptr; + zend_hash_get_current_data(lht, (void **) &keydataptr); + switch(pxf[i].px_ftype) { + case pxfAlpha: + convert_to_string_ex(keydataptr); + PX_put_data_alpha(pxdoc, &data[offset], pxf[i].px_flen, Z_STRVAL_PP(keydataptr)); + break; + case pxfShort: + convert_to_long_ex(keydataptr); + PX_put_data_short(pxdoc, &data[offset], pxf[i].px_flen, Z_LVAL_PP(keydataptr)); + break; + case pxfLong: + convert_to_long_ex(keydataptr); + PX_put_data_long(pxdoc, &data[offset], pxf[i].px_flen, Z_LVAL_PP(keydataptr)); + break; + case pxfNumber: + convert_to_double_ex(keydataptr); + PX_put_data_double(pxdoc, &data[offset], pxf[i].px_flen, Z_LVAL_PP(keydataptr)); + break; + + } + offset += pxf[i].px_flen; + zend_hash_move_forward(lht); + } + PX_put_record(pxdoc, data); + efree(data); + + RETURN_TRUE; +} +/* }}} */ + /* {{{ proto int px_numrecords(resource pxdoc) Returns the number of records (columns) in the database */ PHP_FUNCTION(px_numrecords) @@ -580,7 +822,6 @@ { zval **arg1, **arg2; pxdoc_t *pxdoc; - pxfield_t *pxf; if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; @@ -591,6 +832,27 @@ convert_to_string_ex(arg2); PX_set_targetencoding(pxdoc, Z_STRVAL_PP(arg2)); +} +/* }}} */ + +/* {{{ proto void px_set_tablename(resource pxdoc, string tablename) + Sets name of table as stored in database file. Calling this function + makes only sense if you create a database and if you call it before + PX_create_fp(). */ +PHP_FUNCTION(px_set_tablename) +{ + zval **arg1, **arg2; + pxdoc_t *pxdoc; + + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { + WRONG_PARAM_COUNT; + } + + ZEND_FETCH_RESOURCE(pxdoc, pxdoc_t *, arg1, -1, "px object", le_pxdoc); + + convert_to_string_ex(arg2); + + PX_set_tablename(pxdoc, Z_STRVAL_PP(arg2)); } /* }}} */ Index: pecl/paradox/php_paradox.h diff -u pecl/paradox/php_paradox.h:1.2 pecl/paradox/php_paradox.h:1.3 --- pecl/paradox/php_paradox.h:1.2 Fri Jul 25 02:42:40 2003 +++ pecl/paradox/php_paradox.h Sun Sep 21 09:07:06 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_paradox.h,v 1.2 2003/07/25 06:42:40 steinm Exp $ */ +/* $Id: php_paradox.h,v 1.3 2003/09/21 13:07:06 steinm Exp $ */ #ifndef PHP_PARADOX_H #define PHP_PARADOX_H @@ -49,14 +49,17 @@ PHP_FUNCTION(px_new); PHP_FUNCTION(px_open_fp); +PHP_FUNCTION(px_create_fp); PHP_FUNCTION(px_close); PHP_FUNCTION(px_delete); PHP_FUNCTION(px_get_record); +PHP_FUNCTION(px_put_record); PHP_FUNCTION(px_numrecords); PHP_FUNCTION(px_numfields); PHP_FUNCTION(px_get_field); PHP_FUNCTION(px_get_schema); PHP_FUNCTION(px_set_targetencoding); +PHP_FUNCTION(px_set_tablename); /* Declare any global variables you may need between the BEGIN
« previous php.pecl.cvs (#463) next »