9#include "tpcclibConfig.h"
23static char *info[] = {
24 "Rename the specified TAC(s) inside a TAC file.",
26 "Usage: @P [options] file tac_id newname",
28 "Enter the number or (partial) name of the TAC to be renamed.",
29 "Set tac_id to 0, if all TAC names are to be changed.",
31 "For newname, use '_' to separate for example hemisphere or other position",
32 "from the target name. Character '#' is replaced by sequential number.",
33 "Character '@' specifies that previous partial name is to be kept.",
35 "Note that not all TAC file formats can store TAC names.",
36 "At least PMOD and DFT formats support TAC names.",
40 " Nothing is actually renamed, application only lists which TAC(s) would",
41 " be renamed and how.",
43 " New field name is added to given position; enter large number to add",
47 "Example 1: Rename TAC number 3 to 'str_dx'",
48 " @P a123.tac 3 str_dx",
50 "Example 2: Rename all TACs to TACx where x is the number of the TAC",
51 " @P a234.tac 0 TAC#",
53 "Example 3: Rename name field 'sn' to 'sin'",
54 " @P a345.tac sn @_sin_@",
56 "Example 4: Add TAC nr as the first name field, and keep the next fields intact",
57 " @P -add=1 a456.tac 0 #_@_@_@",
59 "See also: taclist, tacnames, tacadd, tacformat, tacsort, tacunit, tacdel",
61 "Keywords: TAC, tool, rename, simulation",
80int main(
int argc,
char **argv)
82 int ai, help=0, version=0, verbose=1;
83 int ret, dryMode=0, addField=0;
84 char *cptr, tacfile[FILENAME_MAX], tacid[256], tacname[256];
92 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
94 tacfile[0]=tacid[0]=tacname[0]=(char)0;
96 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
98 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
99 if(strcasecmp(cptr,
"DRY")==0) {
101 }
else if(strncasecmp(cptr,
"ADD=", 4)==0) {
102 addField=atoi(cptr+4);
if(addField>0)
continue;
104 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
110 if(verbose>1) status.
verbose=verbose-1;
113 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
119 strlcpy(tacfile, argv[ai], FILENAME_MAX); ai++;
121 fprintf(stderr,
"Error: missing filename.\n");
126 strlcpy(tacid, argv[ai], 256); ai++;
128 fprintf(stderr,
"Error: missing TAC name or number.\n");
132 if(ai>=argc && addField==0) {
133 fprintf(stderr,
"Error: missing new name for TAC(s).\n");
137 for(; ai<argc; ai++) {
138 if( (1+strlen(argv[ai])+strlen(tacname)) >= 255 ) {
139 fprintf(stderr,
"Error: new name for TAC(s) is too long.\n");
142 if(n>0) strncat(tacname,
"_", 255-strlen(tacname));
143 strncat(tacname, argv[ai], 255-strlen(tacname));
150 printf(
"tacfile := %s\n", tacfile);
151 printf(
"dryMode := %d\n", dryMode);
152 printf(
"addField := %d\n", addField);
153 printf(
"tac_id := '%s'\n", tacid);
154 printf(
"new_tacname := '%s'\n", tacname);
161 int toNum=0, toKeep=0;
162 for(
int unsigned i=0; i<strlen(tacname); i++) {
163 if(tacname[i]==
'#') {toNum++;
continue;}
164 if(tacname[i]==
'@') {toKeep++;
continue;}
165 if(tacname[i]==
'_')
continue;
166 if(tacname[i]==
'-')
continue;
167 if(isalnum(tacname[i]))
continue;
168 fprintf(stderr,
"Error: invalid character '%c' in new tacname.\n", tacname[i]);
172 printf(
"edited_new_tacname := %s\n", tacname);
173 printf(
"toNum := %d\n", toNum);
174 printf(
"toKeep := %d\n", toKeep);
178 fprintf(stderr,
"Error: do not use '#' more than once.\n");
184 for(
int unsigned i=1; i<strlen(tacname)-1; i++) {
185 if(tacname[i]!=
'@')
continue;
186 if(tacname[i-1]!=
'_' || tacname[i+1]!=
'_') ret++;
189 fprintf(stderr,
"Error: '@' must be separated by '_'.\n");
197 if(verbose>1) printf(
"reading %s\n", tacfile);
198 ret=
tacRead(&tac, tacfile, &status);
205 printf(
"tacNr := %d\n", tac.
tacNr);
206 printf(
"sampleNr := %d\n", tac.
sampleNr);
210 fprintf(stderr,
"Error: no TACs in %s\n", tacfile);
215 if(strcmp(tacid,
"0")==0) {
216 for(
int j=0; j<tac.
tacNr; j++) tac.
c[j].
sw=1;
218 if(verbose>1) printf(
"All %d tac(s) selected.\n", n);
222 fprintf(stderr,
"Error: specified TACs not found in %s.\n", tacfile);
225 printf(
"%d tac(s) match tac_id '%s'\n", n, tacid);
234 for(
int j=0; j<tac.
tacNr; j++)
if(tac.
c[j].
sw) {
235 if(verbose>1) printf(
"\nold_name := '%s'\n", tac.
c[j].
name);
239 if(strcmp(oldname,
".")==0) strcpy(oldname,
"");
243 fprintf(stderr,
"Error: cannot add name field.\n");
248 if(strlen(tacname)==0) {
251 if(addField>0) strcpy(tmp, oldname);
else strcpy(tmp,
"");
254 strcpy(tmp, tacname);
257 if(verbose>10) printf(
"j+1=%d tmp := '%s'\n", j+1, tmp);
258 i=strcspn(tacname,
"#"); tmp[i]=(char)0;
259 if(verbose>10) printf(
"tmp := '%s' i=%d\n", tmp, i);
261 if(verbose>10) printf(
"tmp := '%s'\n", tmp);
262 strncat(tmp, tacname+i+1, 1024-strlen(tmp)-1);
263 if(verbose>10) printf(
"tmp := '%s'\n", tmp);
265 fprintf(stderr,
"Error: too long TAC name '%s'\n", tmp);
272 fprintf(stderr,
"Error: invalid TAC name '%s'\n", tmp);
277 if(verbose>0 || dryMode) {printf(
"%s -> %s\n", tac.
c[j].
name, newname); fflush(stdout);}
282 fprintf(stderr,
"Error: too long TAC name '%s'\n", newname);
285 strcpy(tac.
c[j].
name, newname);
300 if(verbose>1) printf(
"writing %s\n", tacfile);
301 FILE *fp; fp=fopen(tacfile,
"w");
303 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", tacfile);
310 fprintf(stderr,
"Error (%d): %s\n", ret,
errorMsg(status.
error));
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
char * roinameAddField(char *roiname, const char *field, const unsigned int in, const unsigned int count)
char * roinameEditByTemplate(const char *template, const char *currname, char *newname, const unsigned int count)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
char * strncatIntZ(char *str1, const int n, const int maxn, size_t count)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int strncpyClean(char *s1, const char *s2, int maxlen)
char name[MAX_TACNAME_LEN+1]
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
char * tacFormattxt(tacformat c)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
int tacSelectTACs(TAC *d, const char *region_name, int reset, TPCSTATUS *status)
Header file for library libtpcextensions.
#define MAX_TACNAME_LEN
Max length of TAC ID name (not including trailing zero).
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
@ TAC_FORMAT_SIMPLE
x and y's with space delimiters
@ TAC_FORMAT_DFT
Data format of Turku PET Centre.