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

IFT struct processing. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcift.h"

Go to the source code of this file.

Functions

void iftInit (IFT *ift)
 
void iftFree (IFT *ift)
 
int iftPut (IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
 
int iftPutDouble (IFT *ift, const char *key, const double value, char comment, TPCSTATUS *status)
 
int iftPutInt (IFT *ift, const char *key, const int value, char comment, TPCSTATUS *status)
 
int iftPutUInt (IFT *ift, const char *key, const unsigned int value, char comment, TPCSTATUS *status)
 
int iftDelete (IFT *ift, int index)
 
int iftDuplicate (IFT *ift1, IFT *ift2)
 
int iftReplaceValue (IFT *ift, int i, const char *value, TPCSTATUS *status)
 
int iftReplaceKey (IFT *ift, int i, const char *key, TPCSTATUS *status)
 
int iftDeleteDuplicateKeys (IFT *ift, TPCSTATUS *status)
 
int iftCopyItems (IFT *ift1, IFT *ift2, int is_key_required, int is_value_required, int is_comment_accepted, TPCSTATUS *status)
 

Detailed Description

IFT struct processing.

Definition in file ift.c.

Function Documentation

◆ iftCopyItems()

int iftCopyItems ( IFT * ift1,
IFT * ift2,
int is_key_required,
int is_value_required,
int is_comment_accepted,
TPCSTATUS * status )

Copy items from one IFT struct into another.

Duplicates, empty source, or no copied items are not considered as errors.

See also
iftDeleteDuplicateKeys, iftPut
Returns
tpcerror (TPCERROR_OK when successful).
Parameters
ift1Pointer to target IFT.
ift2Pointer to source IFT.
is_key_requiredSpecifies whether items without key name are copied (0) or not (1).
is_value_requiredSpecifies whether items without value are copied (0) or not (1).
is_comment_acceptedSpecifies whether comment items are copied or not.
  • 0 = comment lines are not copied,
  • 1 = also comment lines are copied,
  • 2 = only comment lines are copied.
statusPointer to status data; enter NULL if not needed

Definition at line 386 of file ift.c.

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}
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
char comment
Definition tpcift.h:27
char * value
Definition tpcift.h:37
char * key
Definition tpcift.h:32
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
@ TPCERROR_FAIL
General error.
@ TPCERROR_OK
No error.

◆ iftDelete()

int iftDelete ( IFT * ift,
int index )

Remove the specified item from IFT struct.

See also
iftFree, iftPut, iftDeleteKey, iftFindKey, iftDeleteDuplicateKeys
Returns
tpcerror, TPCERROR_OK (0) when successful.
Author
Vesa Oikonen
Parameters
iftPointer to existing IFT
indexIndex [0..keyNr-1] of key and value

Definition at line 206 of file ift.c.

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}
short int sw
Definition tpcift.h:29
@ TPCERROR_NO_KEY
Key not found.

Referenced by dcmFileList(), iftDeleteDuplicateKeys(), iftDeleteKey(), parReadCSV(), parSetStudyNr(), tacReadAllogg(), tacReadGEMS(), tacReadOldAllogg(), tacReadScanditronics(), tacSetHeaderDecayCorrection(), tacSetHeaderInjectiontime(), tacSetHeaderIsotope(), tacSetHeaderScanstarttime(), tacSetHeaderStudynr(), tacSetHeaderTimeunit(), and tacSetHeaderUnit().

◆ iftDeleteDuplicateKeys()

int iftDeleteDuplicateKeys ( IFT * ift,
TPCSTATUS * status )

Find and delete items that have similar key names.

The first occurrence of the key is kept. Search is case-insensitive, but otherwise key name match must be exact.

See also
iftDeleteKey, iftFindKey, iftCopyItems
Returns
tpcerror (TPCERROR_OK when successful).
Parameters
iftPointer to IFT.
statusPointer to status data; enter NULL if not needed

Definition at line 348 of file ift.c.

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}
int iftDelete(IFT *ift, int index)
Definition ift.c:206
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30

◆ iftDuplicate()

int iftDuplicate ( IFT * ift1,
IFT * ift2 )

Make a copy (duplicate) of an IFT struct.

See also
iftInit, iftDeleteKey, iftDeleteDuplicateKeys
Returns
tpcerror, TPCERROR_OK (0) when successful.
Author
Vesa Oikonen
Parameters
ift1Pointer to source IFT struct to be copied
ift2Pointer to initiated target IFT struct; any previous contents are deleted.

Definition at line 236 of file ift.c.

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}
void iftFree(IFT *ift)
Definition ift.c:37
int space_after_eq
Definition tpcift.h:55
int type
Definition tpcift.h:51
int space_before_eq
Definition tpcift.h:53

Referenced by imgFillOHeader(), parReadCSV(), tacCopyHdr(), tacReadAllogg(), tacReadCSV(), tacReadDFT(), tacReadGEMS(), tacReadOldAllogg(), tacReadPMOD(), tacReadScanditronics(), tacReadSimple(), tacWriteCSV(), and tacWriteDFT().

◆ iftFree()

void iftFree ( IFT * ift)

Free memory allocated for IFT data. All contents are destroyed.

Precondition
Before first use initialize the IFT struct with iftInit().
See also
iftInit
Author
Vesa Oikonen
Parameters
iftPointer to IFT

Definition at line 37 of file ift.c.

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}
void iftInit(IFT *ift)
Definition ift.c:21
int _memNr
Definition tpcift.h:45

Referenced by dcmFileList(), dcmFileRemove(), iftDuplicate(), imgFree(), imgRead(), micropetExists(), micropetHeaderRead(), parFree(), parRead(), parReadLimits(), parWriteIFT(), parWriteLimits(), pathRemoveFiles(), tacCopyHdr(), tacFree(), tacRead(), tacWriteCSV(), and tacWriteDFT().

◆ iftInit()

void iftInit ( IFT * ift)

Initiate the IFT struct before any use.

See also
iftFree, iftDuplicate
Author
Vesa Oikonen
Parameters
iftPointer to IFT

Definition at line 21 of file ift.c.

24 {
25 if(ift==NULL) return;
26 ift->_memNr=ift->keyNr=0; ift->item=NULL; ift->type=0;
28}

Referenced by dcmFileRemove(), iftFree(), imgInit(), imgRead(), micropetExists(), micropetHeaderRead(), parInit(), parRead(), parReadLimits(), parWriteIFT(), parWriteLimits(), pathRemoveFiles(), tacInit(), tacRead(), tacWriteCSV(), and tacWriteDFT().

◆ iftPut()

int iftPut ( IFT * ift,
const char * key,
const char * value,
char comment,
TPCSTATUS * status )

Add specified key and its value to the IFT.

Either key or value can be empty, but not both of them.

See also
iftInit, iftFree, iftDelete, iftDeleteKey, iftPutDouble, iftCopyItems
Returns
tpcerror (TPCERROR_OK when successful).
Precondition
Before first use initialize the IFT struct with iftInit().
Author
Vesa Oikonen
Parameters
iftPointer to initiated IFT; previous contents are not changed
keyKey string; can be empty ("" or NULL)
valueValue string; can be empty ("" or NULL)
commentIs this comment line, or line that is commented out?
  • 0: not a comment line
  • 1: line will be started with '#'
statusPointer to status data; enter NULL if not needed

Definition at line 63 of file ift.c.

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}
char * strdup(const char *s)
Definition stringext.c:185
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.

Referenced by dcmFileList(), ecatReadMainheader(), iftCopyItems(), iftDuplicate(), iftPutDouble(), iftPutFromString(), iftPutFromStringWithSpaceSeparator(), iftPutInt(), iftPutUInt(), imgFillOHeader(), parAllocateWithTAC(), parExamplePerfectBolus(), parExampleTTACs(), parFromIFT(), parReadFIT(), parReadRES(), parSetStudyNr(), parToIFT(), parWriteCSV(), pathFileList(), tacAllocateWithPAR(), tacReadGEMS(), tacReadScanditronics(), tacSetHeaderDecayCorrection(), tacSetHeaderInjectiontime(), tacSetHeaderIsotope(), tacSetHeaderScanstarttime(), tacSetHeaderStudynr(), tacSetHeaderTimeunit(), and tacSetHeaderUnit().

◆ iftPutDouble()

int iftPutDouble ( IFT * ift,
const char * key,
const double value,
char comment,
TPCSTATUS * status )

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

See also
iftInit, iftPut, iftPutFromString, iftPutInt, iftGetDouble
Returns
tpcerror (TPCERROR_OK when successful).
Precondition
Before first use initialize the IFT structure with iftInit().
Parameters
iftPointer to initiated IFT; previous contents are not changed
keyKey string; can be empty ("" or NULL).
valueValue as double.
commentIs this comment line, or line that is commented out?
  • 0: not a comment line
  • 1: line will be started with '#'
statusPointer to status data; enter NULL if not needed.

Definition at line 128 of file ift.c.

141 {
142 char dstr[128];
143 sprintf(dstr, "%g", value);
144 return(iftPut(ift, key, dstr, comment, status));
145}

Referenced by parWriteLimits().

◆ iftPutInt()

int iftPutInt ( IFT * ift,
const char * key,
const int value,
char comment,
TPCSTATUS * status )

Add specified key and its integer (signed) value to the IFT.

See also
iftInit, iftPut, iftPutFromString, iftPutUint, iftPutDouble, iftGetInt
Returns
tpcerror (TPCERROR_OK when successful).
Precondition
Before first use initialize the IFT structure with iftInit().
Parameters
iftPointer to initiated IFT; previous contents are not changed
keyKey string; can be empty ("" or NULL).
valueValue as int.
commentIs this comment line, or line that is commented out?
  • 0: not a comment line
  • 1: line will be started with '#'
statusPointer to status data; enter NULL if not needed.

Definition at line 154 of file ift.c.

167 {
168 char dstr[128];
169 sprintf(dstr, "%d", value);
170 return(iftPut(ift, key, dstr, comment, status));
171}

◆ iftPutUInt()

int iftPutUInt ( IFT * ift,
const char * key,
const unsigned int value,
char comment,
TPCSTATUS * status )

Add specified key and its integer (unsigned) value to the IFT.

See also
iftInit, iftPut, iftPutFromString, iftPutInt, iftPutDouble, iftGetUint
Returns
tpcerror (TPCERROR_OK when successful).
Precondition
Before first use initialize the IFT structure with iftInit().
Parameters
iftPointer to initiated IFT; previous contents are not changed
keyKey string; can be empty ("" or NULL).
valueValue as int.
commentIs this comment line, or line that is commented out?
  • 0: not a comment line
  • 1: line will be started with '#'
statusPointer to status data; enter NULL if not needed.

Definition at line 180 of file ift.c.

193 {
194 char dstr[128];
195 sprintf(dstr, "%u", value);
196 return(iftPut(ift, key, dstr, comment, status));
197}

Referenced by imgFillOHeader().

◆ iftReplaceKey()

int iftReplaceKey ( IFT * ift,
int i,
const char * key,
TPCSTATUS * status )

Replace the key name of a specified IFT item.

See also
iftReplaceValue, iftDelete, iftDeleteKey, iftPut
Returns
tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
Parameters
iftPointer to IFT containing at least the item to be edited.
iIFT item index [0..keyNr-1] to be edited.
keyNew key string; can be empty ("" or NULL)
statusPointer to status data; enter NULL if not needed

Definition at line 307 of file ift.c.

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}

◆ iftReplaceValue()

int iftReplaceValue ( IFT * ift,
int i,
const char * value,
TPCSTATUS * status )

Replace the value of specified IFT item.

See also
iftReplaceKey, iftDelete, iftDeleteKey, iftPut
Returns
tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
Parameters
iftPointer to IFT containing at least the item to be edited.
iIFT item index [0..keyNr-1] to be edited.
valueNew value string; can be empty ("" or NULL)
statusPointer to status data; enter NULL if not needed

Definition at line 268 of file ift.c.

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}