TPCCLIB
Loading...
Searching...
No Matches
ift.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
10#include <time.h>
11#include <string.h>
12/*****************************************************************************/
13#include "tpcift.h"
14/*****************************************************************************/
15
16/*****************************************************************************/
23 IFT *ift
24) {
25 if(ift==NULL) return;
26 ift->_memNr=ift->keyNr=0; ift->item=NULL; ift->type=0;
28}
29/*****************************************************************************/
30
31/*****************************************************************************/
39 IFT *ift
40) {
41 int i;
42
43 if(ift==NULL) return;
44 for(i=0; i<ift->_memNr; i++) {
45 free(ift->item[i].key);
46 free(ift->item[i].value);
47 }
48 free(ift->item); ift->item=NULL; ift->_memNr=ift->keyNr=0;
49 iftInit(ift);
50}
51/*****************************************************************************/
52
53/*****************************************************************************/
65 IFT *ift,
67 const char *key,
69 const char *value,
73 char comment,
75 TPCSTATUS *status
76) {
77 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
78 if(ift==NULL) return TPCERROR_FAIL;
79 if((key==NULL || strlen(key)<1) && (value==NULL || strlen(value)<1)) return TPCERROR_FAIL;
80 int verbose=0; if(status!=NULL) verbose=status->verbose;
81 if(verbose>10) {
82 printf("iftPut(ift, ");
83 if(key!=NULL) printf("\"%s\", ", key); else printf("NULL, ");
84 if(value!=NULL) printf("\"%s\", ", value); else printf("NULL, ");
85 printf("%d)\n", comment);
86 fflush(stdout);
87 }
88
89 /* If necessary, allocate more memory for items */
90 if(ift->_memNr<=ift->keyNr) {
91 int i, add_nr=20;
92 ift->item=(IFT_ITEM*)realloc(ift->item, (ift->_memNr+add_nr)*sizeof(IFT_ITEM));
93 if(ift->item==NULL) {
94 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OUT_OF_MEMORY);
96 }
97 for(i=ift->_memNr; i<ift->_memNr+add_nr; i++) {
98 ift->item[i].comment=(char)0;
99 ift->item[i].sw=(short int)0;
100 ift->item[i].key=NULL;
101 ift->item[i].value=NULL;
102 }
103 ift->_memNr+=add_nr;
104 }
105
106 /* Set the contents */
107 /* type */
108 if(comment!=0) ift->item[ift->keyNr].comment=(char)1;
109 /* key (do not put NULL because it would segfault in std functions) */
110 if(key!=NULL) ift->item[ift->keyNr].key=strdup(key);
111 else ift->item[ift->keyNr].key=strdup("");
112 /* value (do not put NULL because it would segfault in std functions) */
113 if(value!=NULL) ift->item[ift->keyNr].value=strdup(value);
114 else ift->item[ift->keyNr].value=strdup("");
115
116 ift->keyNr++;
117 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
118 return TPCERROR_OK;
119}
120/*****************************************************************************/
121
122/*****************************************************************************/
130 IFT *ift,
132 const char *key,
134 const double value,
138 char comment,
140 TPCSTATUS *status
141) {
142 char dstr[128];
143 sprintf(dstr, "%g", value);
144 return(iftPut(ift, key, dstr, comment, status));
145}
146/*****************************************************************************/
147
148/*****************************************************************************/
156 IFT *ift,
158 const char *key,
160 const int value,
164 char comment,
166 TPCSTATUS *status
167) {
168 char dstr[128];
169 sprintf(dstr, "%d", value);
170 return(iftPut(ift, key, dstr, comment, status));
171}
172/*****************************************************************************/
173
174/*****************************************************************************/
182 IFT *ift,
184 const char *key,
186 const unsigned int value,
190 char comment,
192 TPCSTATUS *status
193) {
194 char dstr[128];
195 sprintf(dstr, "%u", value);
196 return(iftPut(ift, key, dstr, comment, status));
197}
198/*****************************************************************************/
199
200/*****************************************************************************/
208 IFT *ift,
210 int index
211) {
212 if(ift==NULL) return TPCERROR_FAIL;
213 if(index<0 || index>=ift->keyNr) return TPCERROR_NO_KEY;
214
215 free(ift->item[index].key); ift->item[index].key=NULL;
216 free(ift->item[index].value); ift->item[index].value=NULL;
217 ift->item[index].key=ift->item[index].value=NULL;
218 for(int i=index+1; i<ift->keyNr; i++) {
219 ift->item[i-1].comment=ift->item[i].comment;
220 ift->item[i-1].sw=ift->item[i].sw;
221 ift->item[i-1].key=ift->item[i].key;
222 ift->item[i-1].value=ift->item[i].value;
223 ift->item[i].key=ift->item[i].value=NULL;
224 }
225 ift->keyNr--;
226 return(0);
227}
228/*****************************************************************************/
229
230/*****************************************************************************/
238 IFT *ift1,
240 IFT *ift2
241) {
242 if(ift1==NULL || ift2==NULL) return TPCERROR_FAIL;
243 /* Empty the new IFT */
244 iftFree(ift2);
245
246 /* Copy the contents */
247 ift2->type=ift1->type;
249 ift2->space_after_eq=ift1->space_after_eq;
250 int ret;
251 for(int i=0; i<ift1->keyNr; i++) {
252 ret=iftPut(ift2, ift1->item[i].key, ift1->item[i].value, ift1->item[i].comment, NULL);
253 if(ret!=TPCERROR_OK) {iftFree(ift2); return(ret);}
254 ift2->item[i].sw=ift1->item[i].sw;
255 }
256 // keyNr was set by iftPut()
257 return TPCERROR_OK;
258}
259/*****************************************************************************/
260
261/*****************************************************************************/
270 IFT *ift,
272 int i,
274 const char *value,
276 TPCSTATUS *status
277) {
278 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
279 if(ift==NULL) return TPCERROR_FAIL;
280 int verbose=0; if(status!=NULL) verbose=status->verbose-1;
281 if(verbose>10) {
282 printf("iftReplaceValue(ift, %d", i);
283 if(value!=NULL) printf("\"%s\")\n", value); else printf("NULL)\n");
284 fflush(stdout);
285 }
286 if(i<0 || i>=ift->keyNr) return TPCERROR_FAIL;
287
288 /* Delete the previous value */
289 free(ift->item[i].value);
290 /* Set the new value;
291 do not put NULL because it would segfault in std functions */
292 if(value!=NULL) ift->item[i].value=strdup(value);
293 else ift->item[i].value=strdup("");
294
295 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
296 return TPCERROR_OK;
297}
298/*****************************************************************************/
299
300/*****************************************************************************/
309 IFT *ift,
311 int i,
313 const char *key,
315 TPCSTATUS *status
316) {
317 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
318 if(ift==NULL) return TPCERROR_FAIL;
319 int verbose=0; if(status!=NULL) verbose=status->verbose-1;
320 if(verbose>10) {
321 printf("iftReplaceKey(ift, %d", i);
322 if(key!=NULL) printf("\"%s\")\n", key); else printf("NULL)\n");
323 fflush(stdout);
324 }
325 if(i<0 || i>=ift->keyNr) return TPCERROR_FAIL;
326
327 /* Delete the previous value */
328 free(ift->item[i].key);
329 /* Set the new value;
330 do not put NULL because it would segfault in std functions */
331 if(key!=NULL) ift->item[i].key=strdup(key);
332 else ift->item[i].key=strdup("");
333
334 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
335 return TPCERROR_OK;
336}
337/*****************************************************************************/
338
339/*****************************************************************************/
350 IFT *ift,
352 TPCSTATUS *status
353) {
354 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
355 if(ift==NULL) return TPCERROR_FAIL;
356 int verbose=0; if(status!=NULL) verbose=status->verbose-1;
357 if(verbose>0) printf("%s()\n", __func__);
358 if(ift->keyNr<2) {
359 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
360 return TPCERROR_OK;
361 }
362
363 /* Go through the (remaining) items */
364 int i=0;
365 while(i<ift->keyNr-1) {
366 int j=i+1;
367 while(j<ift->keyNr) {
368 int k=iftFindKey(ift, ift->item[i].key, j);
369 if(k>=0) iftDelete(ift, k); else j++;
370 }
371 i++;
372 }
373 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
374 return TPCERROR_OK;
375}
376/*****************************************************************************/
377
378/*****************************************************************************/
388 IFT *ift1,
390 IFT *ift2,
392 int is_key_required,
394 int is_value_required,
399 int is_comment_accepted,
401 TPCSTATUS *status
402) {
403 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
404 if(ift1==NULL) return TPCERROR_FAIL;
405 int verbose=0; if(status!=NULL) verbose=status->verbose-1;
406 if(verbose>0)
407 printf("%s(*ift1, *ift2, %d, %d, %d, status)\n", __func__,
408 is_key_required, is_value_required, is_comment_accepted);
409 if(ift2==NULL || ift2->keyNr<1) {
410 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
411 return TPCERROR_OK;
412 }
413
414 /* Copy the appropriate items */
415 int n=0;
416 for(int i=0; i<ift2->keyNr; i++) {
417 if(is_key_required && (ift2->item[i].key==NULL || strlen(ift2->item[i].key)<1)) continue;
418 if(is_value_required && (ift2->item[i].value==NULL || strlen(ift2->item[i].value)<1)) continue;
419 if(ift2->item[i].comment!=0 && is_comment_accepted==0) continue;
420 if(ift2->item[i].comment==0 && is_comment_accepted==2) continue;
421 int ret=iftPut(ift1, ift2->item[i].key, ift2->item[i].value, ift2->item[i].comment, status);
422 if(ret!=TPCERROR_OK) return(status->error);
423 n++;
424 }
425 if(verbose>1) printf(" %d item(s) copied.\n", n);
426
427 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
428 return TPCERROR_OK;
429}
430/*****************************************************************************/
431
432/*****************************************************************************/
int iftPutInt(IFT *ift, const char *key, const int value, char comment, TPCSTATUS *status)
Definition ift.c:154
void iftFree(IFT *ift)
Definition ift.c:37
int iftPutUInt(IFT *ift, const char *key, const unsigned int value, char comment, TPCSTATUS *status)
Definition ift.c:180
int iftReplaceValue(IFT *ift, int i, const char *value, TPCSTATUS *status)
Definition ift.c:268
int iftCopyItems(IFT *ift1, IFT *ift2, int is_key_required, int is_value_required, int is_comment_accepted, TPCSTATUS *status)
Definition ift.c:386
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
void iftInit(IFT *ift)
Definition ift.c:21
int iftReplaceKey(IFT *ift, int i, const char *key, TPCSTATUS *status)
Definition ift.c:307
int iftDeleteDuplicateKeys(IFT *ift, TPCSTATUS *status)
Definition ift.c:348
int iftPutDouble(IFT *ift, const char *key, const double value, char comment, TPCSTATUS *status)
Definition ift.c:128
int iftDelete(IFT *ift, int index)
Definition ift.c:206
int iftDuplicate(IFT *ift1, IFT *ift2)
Definition ift.c:236
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
char * strdup(const char *s)
Definition stringext.c:185
short int sw
Definition tpcift.h:29
char comment
Definition tpcift.h:27
char * value
Definition tpcift.h:37
char * key
Definition tpcift.h:32
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
int space_after_eq
Definition tpcift.h:55
int type
Definition tpcift.h:51
int keyNr
Definition tpcift.h:47
int space_before_eq
Definition tpcift.h:53
int _memNr
Definition tpcift.h:45
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
@ TPCERROR_FAIL
General error.
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.
@ TPCERROR_NO_KEY
Key not found.
@ TPCERROR_OK
No error.
Header file for library libtpcift.