TPCCLIB
Loading...
Searching...
No Matches
ift.c File Reference

Functions for basic processing of IFT data structure. More...

#include "libtpcmisc.h"

Go to the source code of this file.

Functions

void iftSetStatus (IFT *ift, int status)
 
void iftInit (IFT *ift)
 
void iftEmpty (IFT *ift)
 
int iftPut (IFT *ift, char *key, char *value, char *cmt_type, int verbose)
 
int iftPutDouble (IFT *ift, char *key, double value, char *cmt_type, int verbose)
 
int iftDeleteItem (IFT *ift, int item, int verbose)
 
int iftReplaceNthValue (IFT *ift, int item, char *value, int verbose)
 
int iftdup (IFT *ift1, IFT *ift2, int verbose)
 

Detailed Description

Functions for basic processing of IFT data structure.

Author
Vesa Oikonen

Definition in file ift.c.

Function Documentation

◆ iftDeleteItem()

int iftDeleteItem ( IFT * ift,
int item,
int verbose )

Remove the specified item from IFT.

Returns
0 if ok.
See also
iftPut, iftEmpty, iftReplaceNthValue
Parameters
iftPointer to IFT structure.
itemIndex [0..keyNr-1] of key and value to delete.
verboseVerbose level; if zero, then only warnings are printed into stderr

Definition at line 169 of file ift.c.

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}
void iftSetStatus(IFT *ift, int status)
Definition ift.c:29
int keyNr
Definition libtpcmisc.h:270
IFT_KEY_AND_VALUE * item
Definition libtpcmisc.h:279

Referenced by ecat7CopyHeadersNoQuant().

◆ iftdup()

int iftdup ( IFT * ift1,
IFT * ift2,
int verbose )

Make a copy (duplicate) of IFT structure.

Returns
Returns 0 (IFT_OK) when successful, otherwise an appropriate ift status code.
See also
iftInit, iftRead, iftPut, iftEmpty
Parameters
ift1Pointer to IFT structure to be copied.
ift2Pointer to initiated IFT structure; any previous contents are deleted.
verboseVerbose level; if zero, then only warnings are printed into stderr

Definition at line 235 of file ift.c.

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}
int iftPut(IFT *ift, char *key, char *value, char *cmt_type, int verbose)
Definition ift.c:82
void iftEmpty(IFT *ift)
Definition ift.c:60
int type
Definition libtpcmisc.h:275
const char * status
Definition libtpcmisc.h:277

Referenced by imgCopyhdr().

◆ iftEmpty()

void iftEmpty ( IFT * ift)

Free memory allocated for IFT. All contents are destroyed.

See also
iftInit
Parameters
iftPointer to target IFT structure.

Definition at line 60 of file ift.c.

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}
unsigned char * data
Definition libtpcmisc.h:283
size_t datasize
Definition libtpcmisc.h:281

Referenced by cptReadOne(), defRead(), dftRead(), ehdrEmpty(), ematEmpty(), hrrtMakeCalHdr(), iftdup(), iftRead(), iftReadValue(), imgEmpty(), imgReadMicropetFrame(), imgReadMicropetHeader(), interfileIsHeader(), irdRead(), niftiHeaderToIFT(), readEcat931Calibrationfile(), res2ift(), tsvRead(), vrdRead(), and xelRead().

◆ iftInit()

void iftInit ( IFT * ift)

Initiate IFT structure. This should be called once before first use.

See also
iftEmpty, iftRead, iftPut
Parameters
iftPointer to target IFT structure.

Definition at line 45 of file ift.c.

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}

Referenced by cptReadOne(), dftRead(), ehdrInitiate(), ematInitiate(), hrrtMakeCalHdr(), iftReadValue(), imgInit(), interfileIsHeader(), irdRead(), readEcat931Calibrationfile(), tsvRead(), vrdRead(), and xelRead().

◆ iftPut()

int iftPut ( IFT * ift,
char * key,
char * value,
char * cmt_type,
int verbose )

Add specified key and its value to the IFT.

Also comment type (first character pointed to) can be added. Either key or value can be empty, but not both of them.

Returns
Returns 0 if ok. Sets ift->status.
See also
iftInit, iftEmpty, iftPutDouble, iftDeleteItem
Parameters
iftPointer to initiated IFT; previous contents are not changed
keyKey string; can be empty ("" or NULL)
valueValue string; can be empty ("" or NULL)
cmt_typePointer to comment character, e.g. '#' or ';' or '!'; can be empty ("" or NULL)
verboseVerbose level; if zero, then only warnings are printed into stderr

Definition at line 82 of file ift.c.

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}

Referenced by defRead(), ecat7ImageheaderToIFT(), ecat7MHeaderToIFT(), ecat7ScanheaderToIFT(), hrrtMakeCalHdr(), iftdup(), iftPutDouble(), iftRead(), niftiHeaderToIFT(), and res2ift().

◆ iftPutDouble()

int iftPutDouble ( IFT * ift,
char * key,
double value,
char * cmt_type,
int verbose )

Add specified key and its floating point (double) value to the IFT.

Also comment type (first character pointed to) can be added. Key can be empty.

Returns
Returns 0 if ok. Sets ift->status.
See also
iftPut
Parameters
iftPointer to initiated IFT; previous contents are not changed
keyKey string; can be empty ("" or NULL)
valueValue as double
cmt_typePointer to comment character, e.g. '#' or ';' or '!'; can be empty ("" or NULL)
verboseVerbose level; if zero, then only warnings are printed into stderr

Definition at line 145 of file ift.c.

157 {
158 char dstr[128];
159 sprintf(dstr, "%g", value);
160 return(iftPut(ift, key, dstr, cmt_type, verbose));
161}

Referenced by hrrtMakeCalHdr().

◆ iftReplaceNthValue()

int iftReplaceNthValue ( IFT * ift,
int item,
char * value,
int verbose )

Replaces specified value in IFT with a new value.

Returns
0 if ok.
See also
iftDeleteItem, iftPut
Parameters
iftPointer to IFT structure.
itemIndex [0..keyNr-1] of key and value.
valueValue string; can be empty ("" or NULL).
verboseVerbose level; if zero, then only warnings are printed into stderr

Definition at line 206 of file ift.c.

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}

◆ iftSetStatus()

void iftSetStatus ( IFT * ift,
int status )

Sets IFT status

Parameters
iftPointer to target IFT structure.
statusNew status value.

Definition at line 29 of file ift.c.

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}

Referenced by defRead(), iftDeleteItem(), iftFindNthKey(), iftFindNthValue(), iftGet(), iftGetDoubleValue(), iftGetFloatValue(), iftGetFrom(), iftGetFullmatchFrom(), iftGetIntValue(), iftGetKeyNr(), iftGetNth(), iftPut(), iftRead(), iftReplaceNthValue(), iftWrite(), and iftWriteItem().