CVS update: php3/functions

From: Date: Fri, 25 Jun 1999 22:37:28 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-7563@lists.php.net to get a copy of this message
Date: Friday June 25, 1999 @ 18:37 Author: sas Update of /repository/php3/functions In directory php:/tmp/cvs-serv1410/functions Modified Files: dbase.c Log Message: add dbase_replace_record Index: php3/functions/dbase.c diff -u php3/functions/dbase.c:1.73 php3/functions/dbase.c:1.74 --- php3/functions/dbase.c:1.73 Wed Jun 23 18:20:31 1999 +++ php3/functions/dbase.c Fri Jun 25 18:37:28 1999 @@ -27,7 +27,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: dbase.c,v 1.73 1999/06/23 22:20:31 sas Exp $ */ +/* $Id: dbase.c,v 1.74 1999/06/25 22:37:28 sas Exp $ */ #if defined(COMPILE_DL) #include "dl/phpdl.h" #endif @@ -316,6 +316,142 @@ } /* }}} */ +/* {{{ proto bool dbase_replace_record(int identifier, array data, int recnum) + Replaces a record to the database */ +PHP_FUNCTION(dbase_replace_record) { + pval *dbh_id, *fields, *field, *recnum; + dbhead_t *dbh; + int dbh_type; + + int num_fields; + dbfield_t *dbf, *cur_f; + char *cp, *t_cp; + int i; + DBase_TLS_VARS; + + if (ARG_COUNT(ht) != 3 || getParameters(ht,3,&dbh_id,&fields,&recnum)==FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long(dbh_id); + convert_to_long(recnum); + if (fields->type != IS_ARRAY) { + php3_error(E_WARNING, "Expected array as second parameter"); + RETURN_FALSE; + } + + dbh = php3_list_find(dbh_id->value.lval, &dbh_type); + if (!dbh || dbh_type != DBase_GLOBAL(le_dbhead)) { + php3_error(E_WARNING, "Unable to find database for identifier %d", dbh_id->value.lval); + RETURN_FALSE; + } + + num_fields = _php3_hash_num_elements(fields->value.ht); + + if (num_fields != dbh->db_nfields) { + php3_error(E_WARNING, "Wrong number of fields specified"); + RETURN_FALSE; + } + + cp = t_cp = (char *)emalloc(dbh->db_rlen + 1); + if (!cp) { + php3_error(E_WARNING, "unable to allocate memory"); + RETURN_FALSE; + } + *t_cp++ = VALID_RECORD; + + dbf = dbh->db_fields; + for (i = 0, cur_f = dbf; cur_f < &dbf[num_fields]; i++, cur_f++) { + if (_php3_hash_index_find(fields->value.ht, i, (void **)&field) == FAILURE) { + php3_error(E_WARNING, "unexpected error"); + efree(cp); + RETURN_FALSE; + } + convert_to_string(field); + sprintf(t_cp, cur_f->db_format, field->value.str.val); + t_cp += cur_f->db_flen; + } + + if (put_dbf_record(dbh, recnum->value.lval, cp) < 0) { + php3_error(E_WARNING, "unable to put record at %ld", dbh->db_records); + efree(cp); + RETURN_FALSE; + } + + put_dbf_info(dbh); + efree(cp); + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool dbase_replace_record(int identifier, array data, int recnum) + Replaces a record to the database */ +void php3_dbase_replace_record(INTERNAL_FUNCTION_PARAMETERS) { + pval *dbh_id, *fields, *field, *recnum; + dbhead_t *dbh; + int dbh_type; + + int num_fields; + dbfield_t *dbf, *cur_f; + char *cp, *t_cp; + int i; + DBase_TLS_VARS; + + if (ARG_COUNT(ht) != 3 || getParameters(ht,3,&dbh_id,&fields,&recnum)==FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long(dbh_id); + convert_to_long(recnum); + if (fields->type != IS_ARRAY) { + php3_error(E_WARNING, "Expected array as second parameter"); + RETURN_FALSE; + } + + dbh = php3_list_find(dbh_id->value.lval, &dbh_type); + if (!dbh || dbh_type != DBase_GLOBAL(le_dbhead)) { + php3_error(E_WARNING, "Unable to find database for identifier %d", dbh_id->value.lval); + RETURN_FALSE; + } + + num_fields = _php3_hash_num_elements(fields->value.ht); + + if (num_fields != dbh->db_nfields) { + php3_error(E_WARNING, "Wrong number of fields specified"); + RETURN_FALSE; + } + + cp = t_cp = (char *)emalloc(dbh->db_rlen + 1); + if (!cp) { + php3_error(E_WARNING, "unable to allocate memory"); + RETURN_FALSE; + } + *t_cp++ = VALID_RECORD; + + dbf = dbh->db_fields; + for (i = 0, cur_f = dbf; cur_f < &dbf[num_fields]; i++, cur_f++) { + if (_php3_hash_index_find(fields->value.ht, i, (void **)&field) == FAILURE) { + php3_error(E_WARNING, "unexpected error"); + efree(cp); + RETURN_FALSE; + } + convert_to_string(field); + sprintf(t_cp, cur_f->db_format, field->value.str.val); + t_cp += cur_f->db_flen; + } + + if (put_dbf_record(dbh, recnum->value.lval, cp) < 0) { + php3_error(E_WARNING, "unable to put record at %ld", dbh->db_records); + efree(cp); + RETURN_FALSE; + } + + put_dbf_info(dbh); + efree(cp); + + RETURN_TRUE; +} +/* }}} */ + /* {{{ proto bool dbase_delete_record(int identifier, int record) Marks a record to be deleted */ void php3_dbase_delete_record(INTERNAL_FUNCTION_PARAMETERS) { @@ -663,10 +799,12 @@ {"dbase_numrecords", php3_dbase_numrecords, NULL}, {"dbase_numfields", php3_dbase_numfields, NULL}, {"dbase_add_record", php3_dbase_add_record, NULL}, + {"dbase_replace_record", php3_dbase_replace_record, NULL}, {"dbase_get_record", php3_dbase_get_record, NULL}, {"dbase_get_record_with_names", php3_dbase_get_record_with_names, NULL}, {"dbase_delete_record", php3_dbase_delete_record, NULL}, {"dbase_pack", php3_dbase_pack, NULL}, + PHP_FE(dbase_replace_record, NULL) {NULL, NULL, NULL} };

« previous php.dev (#7563) next »