src/lib/srdb2/db_fld.c
e7b9f9db
 /* 
  * Copyright (C) 2001-2005 FhG FOKUS
  * Copyright (C) 2006-2007 iptelorg GmbH
  *
8d8cea81
  * This file is part of Kamailio, a free SIP server.
e7b9f9db
  *
8d8cea81
  * Kamailio is free software; you can redistribute it and/or modify
e7b9f9db
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
8d8cea81
  * Kamailio is distributed in the hope that it will be useful,
e7b9f9db
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
e7b9f9db
  */
 
955468d2
 /** \ingroup DB_API 
  * @{ 
  */
0f7f49b2
 
b4f5cb26
 #include "db_fld.h"
 
cf83221d
 #include "../../core/mem/mem.h"
 #include "../../core/dprint.h"
b4f5cb26
 
 #include <string.h>
e7b9f9db
 
 
a6765986
 char* db_fld_str[] = {
 	"DB_NONE",
 	"DB_INT",
 	"DB_FLOAT",
 	"DB_DOUBLE",
 	"DB_CSTR",
 	"DB_STR",
 	"DB_DATETIME",
 	"DB_BLOB",
 	"DB_BITMAP"
 };
 
 
 
26ac6623
 int db_fld_init(db_fld_t* fld)
e7b9f9db
 {
 	int i;
 
26ac6623
 	for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
e7b9f9db
 		if (db_gen_init(&fld[i].gen) < 0) return -1;
 	}
 	return 0;
 }
 
 
26ac6623
 void db_fld_close(db_fld_t* fld)
0f7f49b2
 {
 	int i;
 
26ac6623
 	for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
0f7f49b2
 		db_gen_free(&fld[i].gen);
 	}
 }
 
 
e7b9f9db
 db_fld_t* db_fld(size_t n)
 {
26ac6623
 	int i;
955468d2
 	db_fld_t* newp;
e7b9f9db
 
955468d2
 	newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n);
 	if (newp == NULL) {
e7b9f9db
 		ERR("db_fld: No memory left\n");
 		return NULL;
 	}
955468d2
 	memset(newp, '\0', sizeof(db_fld_t) * n);
0f7f49b2
 
26ac6623
 	for(i = 0; i < n; i++) {
 		if (db_gen_init(&newp[i].gen) < 0) goto error;
 	}
 	return newp;
 
  error:
 	if (newp) {
19e73e77
 		while(i >= 0) {
 			db_gen_free(&newp[i].gen);
 			i--;
 		}
26ac6623
 		pkg_free(newp);
 	}
 	return NULL;
 }
 
 
 db_fld_t* db_fld_copy(db_fld_t* fld)
 {
19e73e77
 	int i, n;
26ac6623
 	db_fld_t* newp;
 
 	for(n = 0; fld[n].name; n++);
f7a39e39
 	n++; /* We need to copy the terminating element too */
26ac6623
 
 	newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n);
 	if (newp == NULL) {
 		ERR("db_fld: No memory left\n");
 		return NULL;
 	}
 	memcpy(newp, fld, sizeof(db_fld_t) * n);
19e73e77
 	for(i = 0; i < n; i++) {
 		if (db_gen_init(&newp[i].gen) < 0) goto error;
 	}
 	
955468d2
 	return newp;
e7b9f9db
 
  error:
0246b3d7
  	ERR("db_fld_copy() failed\n");
955468d2
 	if (newp) {
19e73e77
 		/* Free everything allocated in this function so far */
 		while(i >= 0) {
 			db_gen_free(&newp[i].gen);
 			i--;
 		}
955468d2
 		pkg_free(newp);
e7b9f9db
 	}
 	return NULL;
 }
 
 
26ac6623
 void db_fld_free(db_fld_t* fld)
e7b9f9db
 {
19e73e77
 	int i;
 	
 	if (DB_FLD_EMPTY(fld)) return;
 	for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
 		db_gen_free(&fld[i].gen);
 	}
e7b9f9db
 	pkg_free(fld);
 }
 
0f7f49b2
 /** @} */