TPCCLIB
Loading...
Searching...
No Matches
iftlist.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "List on screen the specified or all key names and values in",
22 "an Interfile-type (IFT) header file.",
23 " ",
24 "Usage: @P [options] filename [key1 [key2...]] [> outputfile]",
25 " ",
26 "Options:",
27 " -a[ll]",
28 " List all values, also those with no key name",
29 " -value",
30 " Print only the key value(s) without key name(s)",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "Example:",
34 " @P iea345.hdr patient_name",
35 " ",
36 "See also: iftadd, iftdel, iftisval, iftmatch, rmcmnts",
37 " ",
38 "Keywords: header, IFT, tool",
39 0};
40/*****************************************************************************/
41
42/*****************************************************************************/
43/* Turn on the globbing of the command line, since it is disabled by default in
44 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
45 In Unix&Linux wildcard command line processing is enabled by default. */
46/*
47#undef _CRT_glob
48#define _CRT_glob -1
49*/
50int _dowildcard = -1;
51/*****************************************************************************/
52
53/*****************************************************************************/
57int main(int argc, char **argv)
58{
59 int ai, help=0, version=0, verbose=1;
60 int ret, is_key_required=1, print_value=0;
61 char *cptr, iftfile[FILENAME_MAX];
62 IFT ift;
63 char *key;
64
65 /*
66 * Get arguments
67 */
68 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
69 iftInit(&ift);
70 iftfile[0]=(char)0;
71 /* Options */
72 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
73 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
74 cptr=argv[ai]+1;
75 if(strncasecmp(cptr, "ALL", 1)==0) {
76 is_key_required=0; continue;
77 } else if(strcasecmp(cptr, "VALUE")==0) {
78 print_value=1; continue;
79 }
80 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
81 return(1);
82 } else break; // key name argument may start with '-'
83
84 /* Print help or version? */
85 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
86 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
87 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
88 /* In verbose mode print arguments and options */
89 if(verbose>1) {
90 printf("is_key_required := %d\n", is_key_required);
91 printf("print_value := %d\n", print_value);
92 }
93
94 TPCSTATUS status; statusInit(&status);
95 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
96 status.verbose=verbose;
97
98 /* Process other arguments, starting from the first non-option */
99 int n=0, m, li;
100 for(; ai<argc; ai++) {
101 if(!iftfile[0]) {
102 strlcpy(iftfile, argv[ai], FILENAME_MAX);
103 /* Read IFT file */
104 if(verbose>1) printf("reading %s\n", iftfile);
105 FILE *fp=fopen(iftfile, "r"); if(fp==NULL) {
106 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
107 return(2);
108 }
109 ret=iftRead(&ift, fp, is_key_required, 1, &status); fclose(fp);
110 if(ret) {
111 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
112 iftFree(&ift); return(2);
113 }
114 if(verbose>1) printf("list size: %d item(s)\n", ift.keyNr);
115 } else { /* Next parameters are key names; print them */
116 key=strdup(argv[ai]); m=0;
117 do {
118 li=iftSearchKey(&ift, key, m);
119 if(li<0) {
120 if(m<1) fprintf(stderr, "Key \"%s\" was not found.\n", argv[ai]);
121 } else {
122 if(print_value==0) iftWriteItem(&ift, li, stdout, NULL);
123 else fprintf(stdout, "%s\n", ift.item[li].value);
124 m=li+1;
125 }
126 } while(li>=0);
127 free(key);
128 n++;
129 }
130 }
131 /* Check that we got what we need */
132 if(!iftfile[0]) {
133 tpcPrintUsage(argv[0], info, stderr); iftFree(&ift); return(1);}
134
135 /*
136 * If no key names were specified, then list all keys and their values
137 */
138 if(n<1) {
139 if(verbose>0) printf("Parameters in %s :\n", iftfile);
140 if(print_value==0) ret=iftWrite(&ift, stdout, NULL);
141 else for(li=0; li<ift.keyNr; li++)
142 fprintf(stdout, "%s\n", ift.item[li].value);
143 }
144
145 iftFree(&ift);
146 return(0);
147}
148/*****************************************************************************/
149
150/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
int iftSearchKey(IFT *ift, const char *s, int start_index)
Definition iftfind.c:86
int iftWriteItem(IFT *ift, int item, FILE *fp, TPCSTATUS *status)
Definition iftio.c:25
int iftWrite(IFT *ift, FILE *fp, TPCSTATUS *status)
Definition iftio.c:98
int iftRead(IFT *ift, FILE *fp, int is_key_required, int is_comment_accepted, TPCSTATUS *status)
Definition iftio.c:130
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
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
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char * value
Definition tpcift.h:37
Definition tpcift.h:43
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.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.