1 /*
2  * Copyright (C) 2001-2005 FhG FOKUS
3  * Copyright (C) 2006-2007 iptelorg GmbH
4  *
5  * This file is part of Kamailio, a free SIP server.
6  *
7  * Kamailio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version
11  *
12  * Kamailio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 /** \ingroup DB_API
23  * @{
24  */
25 
26 #include "db_fld.h"
27 
28 #include "../../core/mem/mem.h"
29 #include "../../core/dprint.h"
30 
31 #include <string.h>
32 
33 
34 char* db_fld_str[] = {
35 	"DB_NONE",
36 	"DB_INT",
37 	"DB_FLOAT",
38 	"DB_DOUBLE",
39 	"DB_CSTR",
40 	"DB_STR",
41 	"DB_DATETIME",
42 	"DB_BLOB",
43 	"DB_BITMAP"
44 };
45 
46 
47 
db_fld_init(db_fld_t * fld)48 int db_fld_init(db_fld_t* fld)
49 {
50 	int i;
51 
52 	for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
53 		if (db_gen_init(&fld[i].gen) < 0) return -1;
54 	}
55 	return 0;
56 }
57 
58 
db_fld_close(db_fld_t * fld)59 void db_fld_close(db_fld_t* fld)
60 {
61 	int i;
62 
63 	for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
64 		db_gen_free(&fld[i].gen);
65 	}
66 }
67 
68 
db_fld(size_t n)69 db_fld_t* db_fld(size_t n)
70 {
71 	int i;
72 	db_fld_t* newp;
73 
74 	newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n);
75 	if (newp == NULL) {
76 		ERR("db_fld: No memory left\n");
77 		return NULL;
78 	}
79 	memset(newp, '\0', sizeof(db_fld_t) * n);
80 
81 	for(i = 0; i < n; i++) {
82 		if (db_gen_init(&newp[i].gen) < 0) goto error;
83 	}
84 	return newp;
85 
86  error:
87 	if (newp) {
88 		while(i >= 0) {
89 			db_gen_free(&newp[i].gen);
90 			i--;
91 		}
92 		pkg_free(newp);
93 	}
94 	return NULL;
95 }
96 
97 
db_fld_copy(db_fld_t * fld)98 db_fld_t* db_fld_copy(db_fld_t* fld)
99 {
100 	int i, n;
101 	db_fld_t* newp;
102 
103 	for(n = 0; fld[n].name; n++);
104 	n++; /* We need to copy the terminating element too */
105 
106 	newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n);
107 	if (newp == NULL) {
108 		ERR("db_fld: No memory left\n");
109 		return NULL;
110 	}
111 	memcpy(newp, fld, sizeof(db_fld_t) * n);
112 	for(i = 0; i < n; i++) {
113 		if (db_gen_init(&newp[i].gen) < 0) goto error;
114 	}
115 
116 	return newp;
117 
118  error:
119  	ERR("db_fld_copy() failed\n");
120 	if (newp) {
121 		/* Free everything allocated in this function so far */
122 		while(i >= 0) {
123 			db_gen_free(&newp[i].gen);
124 			i--;
125 		}
126 		pkg_free(newp);
127 	}
128 	return NULL;
129 }
130 
131 
db_fld_free(db_fld_t * fld)132 void db_fld_free(db_fld_t* fld)
133 {
134 	int i;
135 
136 	if (DB_FLD_EMPTY(fld)) return;
137 	for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
138 		db_gen_free(&fld[i].gen);
139 	}
140 	pkg_free(fld);
141 }
142 
143 /** @} */
144