10#include "tpcclibConfig.h"
23static char *info[] = {
24 "Set or get the valid TPC study number in regional TAC or SIF file.",
25 "Valid TPC study number can not exceed 10 characters, and it must not",
26 "contain spaces; it contains 1-5 letters (upper- or lowercase)",
27 "followed by 1-5 digits.",
29 "If study number is not given as argument, or file does not contain it,",
30 "it is automatically extracted from filename, when possible, by this and",
31 "other applications.",
33 "Note that many file formats (including PMOD) do not support study numbers,",
34 "but this application can still optionally add the study number as",
35 "a comment, for example:",
36 "# studynr := ab1006",
38 "Usage: @P [Options] filename [studynumber]",
42 " Get study number from filename, optionally from the N first letters",
43 " (max 10) of file name, excluding possible path and extension.",
45 " Delete the first N letters from study number.",
47 " Convert study number letters to lowercase.",
49 " Validity of new study number is not verified, and written in file",
50 " as comment line, if not otherwise supported. Maximum length for",
51 " study number is either 255 or 10 characters, depending on file format.",
53 " Print only the study number without file name.",
56 "See also: tacunit, tacadd, iftlist, iftedit, tacformat",
58 "Keywords: TAC, tool, simulation, study number",
77int main(
int argc,
char **argv)
79 int ai, help=0, version=0, verbose=1;
80 int ret, toLower=0, force=0, print_value=0;
81 unsigned int fromFilename=0, deleteFirst=0;
82 char *cptr, datfile[FILENAME_MAX], studynr[FILENAME_MAX];
83 char oldstudynr[FILENAME_MAX], tmp[FILENAME_MAX];
90 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
92 datfile[0]=studynr[0]=oldstudynr[0]=(char)0;
94 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
96 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
97 if(strcasecmp(cptr,
"LOWER")==0) {
99 }
else if(strncasecmp(cptr,
"DF=", 3)==0) {
100 cptr+=3; deleteFirst=atoi(cptr);
101 if(deleteFirst>0 && deleteFirst<11)
continue;
102 }
else if(strcasecmp(cptr,
"FN")==0) {
103 fromFilename=1;
continue;
104 }
else if(strncasecmp(cptr,
"FN=", 3)==0) {
105 cptr+=3; fromFilename=atoi(cptr);
106 if(fromFilename>0)
continue;
107 }
else if(strcasecmp(cptr,
"FORCE")==0) {
109 }
else if(strcasecmp(cptr,
"VALUE")==0) {
110 print_value=1;
continue;
112 fprintf(stderr,
"Error: invalid option '%s'\n", argv[ai]);
117 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
122 for(; ai<argc; ai++) {
124 strlcpy(datfile, argv[ai], FILENAME_MAX);
continue;
125 }
else if(!studynr[0]) {
126 strlcpy(studynr, argv[ai], FILENAME_MAX);
130 if(force==1 && strlen(studynr)>1)
continue;
136 if(fromFilename>1) studynr[fromFilename]=(char)0;
139 fprintf(stderr,
"Error: invalid argument for study number '%s'\n", argv[ai]);
142 fprintf(stderr,
"Error: invalid argument '%s'\n", argv[ai]);
147 if(!datfile[0]) {
tpcPrintUsage(argv[0], info, stdout);
return(1);}
151 printf(
"datfile := %s\n", datfile);
152 printf(
"studynr := %s\n", studynr);
153 printf(
"force := %d\n", force);
154 printf(
"toLower := %d\n", toLower);
155 printf(
"fromFilename := %d\n", fromFilename);
156 printf(
"deleteFirst := %d\n", deleteFirst);
157 printf(
"print_value := %d\n", print_value);
166 if(fromFilename>1 && ((
int)fromFilename-(
int)deleteFirst)>10) {
167 fprintf(stderr,
"Error: too high value with option -fn.\n");
172 if(!studynr[0] && fromFilename>0) {
173 if(verbose>1) printf(
"getting new study number from filename\n");
176 tmp[fromFilename+deleteFirst]=(char)0;
177 strcpy(studynr, tmp);
181 if(verbose>2) printf(
"studynr := %s\n", studynr);
182 if(strlen(studynr)<1) {
183 fprintf(stderr,
"Error: cannot get study number from filename.\n");
191 if(verbose>1) printf(
"reading %s\n", datfile);
192 ret=
tacRead(&tac, datfile, &status);
194 fprintf(stderr,
"Error: %s (%s)\n",
errorMsg(status.
error), datfile);
201 if(strcmp(oldstudynr,
".")==0) strcpy(oldstudynr,
"");
202 if(verbose>1) printf(
"infile.studynr := %s\n", oldstudynr);
206 len=strlen(oldstudynr);
207 for(i=0; i<len; i++) {
208 if(isdigit((
int)oldstudynr[i]))
continue;
209 oldstudynr[i]=(char)tolower((
int)oldstudynr[i]);
213 if(deleteFirst>0 && deleteFirst<strlen(oldstudynr)) {
214 strcpy(tmp, oldstudynr+deleteFirst); strcpy(oldstudynr, tmp);
220 if(print_value) printf(
"%s\n", oldstudynr);
221 else printf(
"%s := %s\n", tmp, oldstudynr);
228 if(verbose>2) printf(
"processing specified study number\n");
232 if(verbose>2) printf(
"converting to lower case letters\n");
236 for(i=0; i<len; i++) {
237 if(isdigit((
int)studynr[i]))
continue;
238 studynr[i]=(char)tolower((
int)studynr[i]);
244 if(verbose>2) printf(
"deleting first characters\n");
245 if(studynr[0] && deleteFirst<strlen(studynr)) {
246 strcpy(tmp, studynr+deleteFirst); strcpy(studynr, tmp);}
251 if(verbose>2) printf(
"new study number was too long, shortened\n");
254 if(verbose>1) printf(
"studynr := %s\n", studynr);
258 if(verbose>2) printf(
"verifying new study number because not forced\n");
260 fprintf(stderr,
"Error: invalid study number.\n");
261 if(verbose>2) printf(
" studynr := '%s'\n", studynr);
270 fprintf(stdout,
"%s: '%s' -> '%s'\n", datfile, oldstudynr, studynr);
273 fprintf(stderr,
"Error: cannot set new study number.\n");
274 if(verbose>2) printf(
" ret := %d\n", ret);
279 if(verbose>1) printf(
"writing %s\n", datfile);
280 FILE *fp; fp=fopen(datfile,
"w");
282 fprintf(stderr,
"Error: cannot open file for writing (%s)\n", datfile);
void filenameRmPath(char *s)
int filenameRmExtension(char *s)
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)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
IFT h
Optional (but often useful) header information.
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int studynrFromFilename(const char *fname, char *studynr, int force)
int studynrVerify(const char *s, int zero_ok)
int tacGetHeaderStudynr(IFT *h, char *s, TPCSTATUS *status)
int tacSetHeaderStudynr(IFT *h, const char *s)
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Header file for library libtpcextensions.
#define MAX_STUDYNR_LEN
Define max study number length.
Header file for library libtpcift.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.