TPCCLIB
Loading...
Searching...
No Matches
ana_ehdr.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 <math.h>
13#include <string.h>
14#include <time.h>
15/*****************************************************************************/
16#include "libtpcmisc.h"
17#include "libtpcimgio.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Edits the information in Analyze 7.5 header file.",
23 "Note that frame times and isotope information are not stored in Analyze,",
24 "header but in SIF.",
25 " ",
26 "Usage: @P [Options] headerfile fieldname value ",
27 " ",
28 "Options:",
29 " -stdoptions", // List standard options like --help, -v, etc
30 " ",
31 "Example:",
32 " @P s2345dy1.hdr patient_id 'PD013' ",
33 " ",
34 "See also: ana_lhdr, nii_lhdr, iftlist, eframe, ecat2ana",
35 " ",
36 "Keywords: image, Analyze, header, IFT",
37 0};
38/*****************************************************************************/
39
40/*****************************************************************************/
41/* Turn on the globbing of the command line, since it is disabled by default in
42 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
43 In Unix&Linux wildcard command line processing is enabled by default. */
44/*
45#undef _CRT_glob
46#define _CRT_glob -1
47*/
48int _dowildcard = -1;
49/*****************************************************************************/
50
51/*****************************************************************************/
55int main(int argc, char **argv)
56{
57 int ai, help=0, version=0, verbose=1;
58 int ret;
59 char hdrfile[FILENAME_MAX], *cptr;
60 char fieldname[31], fieldvalue[256];
61 ANALYZE_DSR dsr;
62
63
64 /*
65 * Get arguments
66 */
67 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
68 hdrfile[0]=fieldname[0]=fieldvalue[0]=(char)0;
69 /* Options */
70 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') { /* options */
71 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
72 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
73 return(1);
74 } else break;
75
76 /* Print help or version? */
77 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
78 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
79 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
80
81 /* Process other arguments, starting from the first non-option */
82 for(; ai<argc; ai++) {
83 if(!hdrfile[0]) {strcpy(hdrfile, argv[ai]); continue;}
84 if(!fieldname[0]) {strcpy(fieldname, argv[ai]); continue;}
85 if(strcmp(argv[ai], ":=")==0) continue;
86 if(!fieldvalue[0]) {strcpy(fieldvalue, argv[ai]); continue;}
87 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
88 return(1);
89 }
90
91 /* Is something missing? */
92 if(!fieldvalue[0]) {
93 fprintf(stderr, "Error: missing command-line argument.\n");
94 return(1);
95 }
96
97 /* In verbose mode print arguments and options */
98 if(verbose>1) {
99 printf("hdrfile := %s\n", hdrfile);
100 printf("fieldname := %s\n", fieldname);
101 printf("fieldvalue := %s\n", fieldvalue);
102 }
103 if(verbose>2) ANALYZE_TEST=verbose-2; else ANALYZE_TEST=0;
104
105
106 /*
107 * Read Analyze header file
108 */
109 /* Check that filename contains .hdr extension */
110 cptr=filenameGetExtension(hdrfile);
111 if(cptr==NULL || strcmp(cptr, ".hdr")!=0) strcat(hdrfile, ".hdr");
112 /* Read header file */
113 if(verbose>1) printf("reading %s\n", hdrfile);
114 ret=anaReadHeader(hdrfile, &dsr);
115 if(ret) {
116 fprintf(stderr, "Error: cannot read %s.\n", hdrfile);
117 if(verbose>1) fprintf(stderr, " ret := %d\n", ret);
118 return(2);
119 }
120
121
122 /*
123 * Change header field
124 */
125 ret=anaEditHeader(&dsr, fieldname, fieldvalue);
126 if(ret==1) {
127 fprintf(stderr, "Error: invalid field name: %s\n", fieldname);
128 return(1);
129 } else if(ret==2) {
130 fprintf(stderr, "Error: invalid field value: %s\n", fieldvalue);
131 return(1);
132 }
133 if(verbose>8) anaPrintHeader(&dsr, stdout);
134
135
136 /*
137 * Write header
138 */
139 if(verbose>1) printf("writing %s\n", hdrfile);
140 ret=anaWriteHeader(hdrfile, &dsr);
141 if(ret) {
142 fprintf(stderr, "Error: cannot write %s (%d).\n", hdrfile, ret);
143 return(11);
144 }
145
146 return(0);
147}
148/*****************************************************************************/
149
150/*****************************************************************************/
int anaEditHeader(ANALYZE_DSR *h, char *field, char *value)
Definition analyze.c:783
int anaWriteHeader(char *filename, ANALYZE_DSR *h)
Definition analyze.c:282
int anaPrintHeader(ANALYZE_DSR *h, FILE *fp)
Definition analyze.c:380
int ANALYZE_TEST
Definition analyze.c:8
int anaReadHeader(char *filename, ANALYZE_DSR *h)
Definition analyze.c:131
char * filenameGetExtension(char *s)
Get the last extension of a filename.
Definition filename.c:139
Header file for libtpcimgio.
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:40
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:213
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:383
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:158