TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcmisc
ift.c
Go to the documentation of this file.
1
5
/*****************************************************************************/
6
#include "
libtpcmisc.h
"
7
/*****************************************************************************/
8
9
/*****************************************************************************/
11
static
const
char
*ift_status[] = {
12
/* 0 */
"ok"
,
13
/* 1 */
"fault in calling routine"
,
14
/* 2 */
"out of memory"
,
15
/* 3 */
"cannot open file"
,
16
/* 4 */
"cannot write file"
,
17
/* 5 */
"unsupported file type"
,
18
/* 6 */
"key not found"
,
19
/* 7 */
"file contains no data"
,
20
/* 8 */
"value not found"
,
21
// /* 9 */ "key not found",
22
0
23
};
24
/*****************************************************************************/
25
26
/*****************************************************************************/
29
void
iftSetStatus
(
31
IFT
*ift,
33
int
status
34
) {
35
if
(ift==NULL)
return
;
36
if
(status<0 || status>8) ift->
status
=ift_status[IFT_FAULT];
37
else
ift->
status
=ift_status[status];
38
}
39
/*****************************************************************************/
40
41
/*****************************************************************************/
45
void
iftInit
(
47
IFT
*ift
48
) {
49
if
(ift==NULL)
return
;
50
memset(ift, 0,
sizeof
(
IFT
));
51
ift->_memNr=ift->
keyNr
=0; ift->
item
=NULL;
52
ift->
data
=NULL; ift->
datasize
=0;
53
}
54
/*****************************************************************************/
55
56
/*****************************************************************************/
60
void
iftEmpty
(
62
IFT
*ift
63
) {
64
if
(ift==NULL)
return
;
65
for
(
int
i=0; i<ift->_memNr; i++) {
66
if
(ift->
item
[i].
key
!=NULL) free(ift->
item
[i].
key
);
67
if
(ift->
item
[i].
value
!=NULL) free(ift->
item
[i].
value
);
68
}
69
free(ift->
item
); ift->
item
=NULL; ift->_memNr=ift->
keyNr
=0;
70
ift->
data
=NULL; ift->
datasize
=0;
71
}
72
/*****************************************************************************/
73
74
/*****************************************************************************/
82
int
iftPut
(
84
IFT
*ift,
86
char
*key,
88
char
*value,
90
char
*cmt_type,
92
int
verbose
93
) {
94
int
i, add_nr=1000;
95
96
if
(ift==NULL) {
return
(1);}
97
if
((key==NULL || strlen(key)<1) && (value==NULL || strlen(value)<1)) {
98
iftSetStatus
(ift, IFT_FAULT);
return
(2);}
99
if
(verbose>0) {
100
printf(
"%s(ift, "
, __func__);
101
if
(key!=NULL) printf(
"\"%s\", "
, key);
else
printf(
"NULL, "
);
102
if
(value!=NULL) printf(
"\"%s\", "
, value);
else
printf(
"NULL, "
);
103
if
(cmt_type!=NULL) printf(
"\"%1.1s\")\n"
, cmt_type);
else
printf(
"NULL)\n"
);
104
}
105
106
/* If necessary, allocate more memory for items */
107
if
(ift->_memNr<=ift->
keyNr
) {
108
ift->
item
=realloc(ift->
item
, (ift->_memNr+add_nr)*
sizeof
(
IFT_KEY_AND_VALUE
));
109
if
(ift->
item
==NULL) {
iftSetStatus
(ift, IFT_NOMEMORY);
return
(5);}
110
ift->_memNr+=add_nr;
111
for
(i=ift->
keyNr
; i<ift->_memNr; i++) {
112
ift->
item
[i].
type
=(char)0;
113
ift->
item
[i].
sw
=(
short
int)0;
114
ift->
item
[i].
key
=NULL;
115
ift->
item
[i].
value
=NULL;
116
}
117
}
118
119
/* Set the contents */
120
/* type */
121
if
(cmt_type!=NULL) ift->
item
[ift->
keyNr
].
type
=cmt_type[0];
122
/* key */
123
if
(key!=NULL) ift->
item
[ift->
keyNr
].
key
=strdup(key);
124
else
ift->
item
[ift->
keyNr
].
key
=strdup(
""
);
125
if
(ift->
item
[ift->
keyNr
].
key
==NULL) {
iftSetStatus
(ift, IFT_NOMEMORY);
return
(7);}
126
/* value */
127
if
(value!=NULL) ift->
item
[ift->
keyNr
].
value
=strdup(value);
128
else
ift->
item
[ift->
keyNr
].
value
=strdup(
""
);
129
if
(ift->
item
[ift->
keyNr
].
value
==NULL) {
iftSetStatus
(ift, IFT_NOMEMORY);
return
(8);}
130
131
ift->
keyNr
++;
132
iftSetStatus
(ift, IFT_OK);
133
return
(0);
134
}
135
/*****************************************************************************/
136
137
/*****************************************************************************/
145
int
iftPutDouble
(
147
IFT
*ift,
149
char
*key,
151
double
value,
154
char
*cmt_type,
156
int
verbose
157
) {
158
char
dstr[128];
159
sprintf(dstr,
"%g"
, value);
160
return
(
iftPut
(ift, key, dstr, cmt_type, verbose));
161
}
162
/*****************************************************************************/
163
164
/*****************************************************************************/
169
int
iftDeleteItem
(
171
IFT
*ift,
173
int
item,
175
int
verbose
176
) {
177
int
i;
178
179
if
(verbose>0) printf(
"%s(*ift, %d)\n"
, __func__, item);
180
if
(ift==NULL)
return
(1);
181
iftSetStatus
(ift, IFT_FAULT);
182
if
(ift==NULL) {
return
(1);}
183
if
(item<0 || item>=ift->
keyNr
) {
return
(2);}
184
185
if
(ift->
item
[item].
key
!=NULL) free(ift->
item
[item].
key
);
186
if
(ift->
item
[item].
value
!=NULL) free(ift->
item
[item].
value
);
187
ift->
item
[item].
key
=ift->
item
[item].
value
=NULL;
188
for
(i=item+1; i<ift->
keyNr
; i++) {
189
ift->
item
[i-1].
type
=ift->
item
[i].
type
;
190
ift->
item
[i-1].
sw
=ift->
item
[i].
sw
;
191
ift->
item
[i-1].
key
=ift->
item
[i].
key
;
192
ift->
item
[i-1].
value
=ift->
item
[i].
value
;
193
ift->
item
[i].
key
=ift->
item
[i].
value
=NULL;
194
}
195
ift->
keyNr
--;
196
iftSetStatus
(ift, IFT_OK);
197
return
(0);
198
}
199
/*****************************************************************************/
200
201
/*****************************************************************************/
206
int
iftReplaceNthValue
(
208
IFT
*ift,
210
int
item,
212
char
*value,
214
int
verbose
215
) {
216
if
(ift==NULL) {
return
(1);}
217
if
(item>=ift->
keyNr
) {
iftSetStatus
(ift, IFT_FAULT);
return
(2);}
218
if
(verbose>0) printf(
"%s(ift, %d, %s)\n"
, __func__, item, value);
219
/* Delete old value */
220
if
(ift->
item
[item].
value
!=NULL) free(ift->
item
[item].
value
);
221
/* Set new value */
222
if
(value!=NULL) ift->
item
[item].
value
=strdup(value);
223
else
ift->
item
[item].
value
=strdup(
""
);
224
if
(ift->
item
[item].
value
==NULL) {
iftSetStatus
(ift, IFT_NOMEMORY);
return
(8);}
225
iftSetStatus
(ift, IFT_OK);
226
return
(0);
227
}
228
/*****************************************************************************/
229
230
/*****************************************************************************/
235
int
iftdup
(
237
IFT
*ift1,
239
IFT
*ift2,
241
int
verbose
242
) {
243
int
ret, li;
244
245
if
(verbose>0) printf(
"%s(*ift1, *ift2)\n"
, __func__);
246
/* Check the input */
247
if
(ift1==NULL || ift2==NULL)
return
IFT_FAULT;
248
249
/* Empty the new IFT */
250
iftEmpty
(ift2);
251
252
/* Copy the contents */
253
ift2->
type
=ift1->
type
;
254
ift2->
status
=ift1->
status
;
255
for
(li=0; li<ift1->
keyNr
; li++) {
256
ret=
iftPut
(ift2, ift1->
item
[li].
key
, ift1->
item
[li].
value
, NULL, verbose);
257
if
(ret!=IFT_OK) {
iftEmpty
(ift2);
return
(ret);}
258
ift2->
item
[li].
type
=ift1->
item
[li].
type
;
259
ift2->
item
[li].
sw
=ift1->
item
[li].
sw
;
260
}
261
// keyNr was set by iftPut()
262
return
IFT_OK;
263
}
264
/*****************************************************************************/
265
266
/*****************************************************************************/
iftdup
int iftdup(IFT *ift1, IFT *ift2, int verbose)
Definition
ift.c:235
iftPutDouble
int iftPutDouble(IFT *ift, char *key, double value, char *cmt_type, int verbose)
Definition
ift.c:145
iftPut
int iftPut(IFT *ift, char *key, char *value, char *cmt_type, int verbose)
Definition
ift.c:82
iftDeleteItem
int iftDeleteItem(IFT *ift, int item, int verbose)
Definition
ift.c:169
iftEmpty
void iftEmpty(IFT *ift)
Definition
ift.c:60
iftSetStatus
void iftSetStatus(IFT *ift, int status)
Definition
ift.c:29
iftInit
void iftInit(IFT *ift)
Definition
ift.c:45
iftReplaceNthValue
int iftReplaceNthValue(IFT *ift, int item, char *value, int verbose)
Definition
ift.c:206
libtpcmisc.h
Header file for libtpcmisc.
IFT_KEY_AND_VALUE
Definition
libtpcmisc.h:253
IFT_KEY_AND_VALUE::sw
short int sw
Definition
libtpcmisc.h:257
IFT_KEY_AND_VALUE::type
char type
Definition
libtpcmisc.h:255
IFT_KEY_AND_VALUE::value
char * value
Definition
libtpcmisc.h:261
IFT_KEY_AND_VALUE::key
char * key
Definition
libtpcmisc.h:259
IFT
Definition
libtpcmisc.h:264
IFT::data
unsigned char * data
Definition
libtpcmisc.h:283
IFT::datasize
size_t datasize
Definition
libtpcmisc.h:281
IFT::type
int type
Definition
libtpcmisc.h:275
IFT::keyNr
int keyNr
Definition
libtpcmisc.h:270
IFT::status
const char * status
Definition
libtpcmisc.h:277
IFT::item
IFT_KEY_AND_VALUE * item
Definition
libtpcmisc.h:279
Generated on
for TPCCLIB by
1.17.0