Improved the preferences structure

This commit is contained in:
Pierre Pronchery 2008-02-29 15:30:05 +00:00
parent 70c985c088
commit 9dd506211d

View File

@ -27,7 +27,11 @@
/* private */ /* private */
/* types */ /* types */
typedef int Prefs; typedef struct _Prefs
{
int flags;
char const * outfile;
} Prefs;
#define PREFS_c 0x1 #define PREFS_c 0x1
#define PREFS_E 0x2 #define PREFS_E 0x2
#define PREFS_g 0x4 #define PREFS_g 0x4
@ -35,69 +39,66 @@ typedef int Prefs;
/* prototypes */ /* prototypes */
static int _c99(Prefs * prefs, char const * outfile, int filec, char * filev[]); static int _c99(Prefs * prefs, int filec, char * filev[]);
/* functions */ /* functions */
/* c99 */ /* c99 */
static int _c99_do(Prefs * prefs, char const * outfile, FILE * outfp, static int _c99_do(Prefs * prefs, FILE * outfp, char const * infile);
char * infile);
static int _c99(Prefs * prefs, char const * outfile, int filec, char * filev[]) static int _c99(Prefs * prefs, int filec, char * filev[])
{ {
FILE * fp; FILE * fp;
int ret = 0; int ret = 0;
int i; int i;
if(outfile != NULL && (fp = fopen(outfile, "w")) == NULL) if(prefs->outfile != NULL && (fp = fopen(prefs->outfile, "w")) == NULL)
return error_set_print(PACKAGE, 1, "%s: %s", outfile, return error_set_print(PACKAGE, 1, "%s: %s", prefs->outfile,
strerror(errno)); strerror(errno));
for(i = 0; i < filec; i++) for(i = 0; i < filec; i++)
ret |= _c99_do(prefs, outfile, fp, filev[i]); ret |= _c99_do(prefs, fp, filev[i]);
if(fp != NULL && fclose(fp) != 0) if(fp != NULL && fclose(fp) != 0)
return error_set_print(PACKAGE, 1, "%s: %s", outfile, return error_set_print(PACKAGE, 1, "%s: %s", prefs->outfile,
strerror(errno)); strerror(errno));
if(ret != 0) if(ret != 0)
error_print(PACKAGE); error_print(PACKAGE);
return ret; return ret;
} }
static int _c99_do_c(Prefs * prefs, char const * outfile, FILE * outfp, static int _c99_do_c(Prefs * prefs, FILE * outfp, char const * infile,
char * infile, FILE * infp); FILE * infp);
static int _c99_do_E(Prefs * prefs, char const * outfile, FILE * outfp, static int _c99_do_E(Prefs * prefs, FILE * outfp, char const * infile,
char * infile, FILE * infp); FILE * infp);
static int _c99_do_o(Prefs * prefs, char const * outfile, FILE * outfp, static int _c99_do_o(Prefs * prefs, FILE * outfp, char const * infile,
char * infile, FILE * infp); FILE * infp);
static int _c99_do(Prefs * prefs, FILE * outfp, char const * infile)
static int _c99_do(Prefs * prefs, char const * outfile, FILE * outfp,
char * infile)
{ {
FILE * infp; FILE * infp;
int ret; int ret;
if((infp = fopen(infile, "r")) == NULL) if((infp = fopen(infile, "r")) == NULL)
return error_set_code(1, "%s: %s", infile, strerror(errno)); return error_set_code(1, "%s: %s", infile, strerror(errno));
if(*prefs & PREFS_c) if(prefs->flags & PREFS_c)
ret = _c99_do_c(prefs, outfile, outfp, infile, infp); ret = _c99_do_c(prefs, outfp, infile, infp);
else if(*prefs & PREFS_E) else if(prefs->flags & PREFS_E)
ret = _c99_do_E(prefs, outfile, outfp, infile, infp); ret = _c99_do_E(prefs, outfp, infile, infp);
else else
ret = _c99_do_o(prefs, outfile, outfp, infile, infp); ret = _c99_do_o(prefs, outfp, infile, infp);
/* FIXME implement */ /* FIXME implement */
if(fclose(infp) != 0 && ret == 0) if(fclose(infp) != 0 && ret == 0)
return error_set_code(1, "%s: %s", infile, strerror(errno)); return error_set_code(1, "%s: %s", infile, strerror(errno));
return ret; return ret;
} }
static int _c99_do_c(Prefs * prefs, char const * outfile, FILE * outfp, static int _c99_do_c(Prefs * prefs, FILE * outfp, char const * infile,
char * infile, FILE * infp) FILE * infp)
/* FIXME outfp should probably be opened only here */ /* FIXME outfp should probably be opened only here */
{ {
int ret = 0; int ret = 0;
size_t len; size_t len;
char * o = NULL; char * o = NULL;
if(outfile == NULL) if(prefs->outfile == NULL)
{ {
if((len = strlen(infile)) < 3 || infile[len - 2] != '.' if((len = strlen(infile)) < 3 || infile[len - 2] != '.'
|| infile[len - 1] != 'c') || infile[len - 1] != 'c')
@ -122,15 +123,15 @@ static int _c99_do_c(Prefs * prefs, char const * outfile, FILE * outfp,
return ret; return ret;
} }
static int _c99_do_E(Prefs * prefs, char const * outfile, FILE * outfp, static int _c99_do_E(Prefs * prefs, FILE * outfp, char const * infile,
char * infile, FILE * infp) FILE * infp)
{ {
/* FIXME implement */ /* FIXME implement */
return error_set_code(1, "%s", strerror(ENOSYS)); return error_set_code(1, "%s", strerror(ENOSYS));
} }
static int _c99_do_o(Prefs * prefs, char const * outfile, FILE * outfp, static int _c99_do_o(Prefs * prefs, FILE * outfp, char const * infile,
char * infile, FILE * infp) FILE * infp)
{ {
/* FIXME implement */ /* FIXME implement */
return error_set_code(1, "%s", strerror(ENOSYS)); return error_set_code(1, "%s", strerror(ENOSYS));
@ -151,7 +152,6 @@ static int _usage(void)
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
Prefs prefs; Prefs prefs;
char * outfile = NULL;
int o; int o;
memset(&prefs, 0, sizeof(prefs)); memset(&prefs, 0, sizeof(prefs));
@ -159,26 +159,26 @@ int main(int argc, char * argv[])
switch(o) switch(o)
{ {
case 'c': case 'c':
prefs |= PREFS_c; prefs.flags |= PREFS_c;
break; break;
case 'E': case 'E':
prefs |= PREFS_E; prefs.flags |= PREFS_E;
break; break;
case 'g': case 'g':
prefs |= PREFS_g; prefs.flags |= PREFS_g;
break; break;
case 'o': case 'o':
outfile = optarg; prefs.outfile = optarg;
break; break;
case 's': case 's':
prefs |= PREFS_s; prefs.flags |= PREFS_s;
break; break;
default: default:
return _usage(); return _usage();
} }
if(optind == argc) if(optind == argc)
return _usage(); return _usage();
if(prefs & PREFS_c && outfile != NULL && optind + 1 != argc) if(prefs.flags & PREFS_c && prefs.outfile != NULL && optind + 1 != argc)
return _usage(); return _usage();
return _c99(&prefs, outfile, argc - optind, &argv[optind]) == 0 ? 0 : 2; return _c99(&prefs, argc - optind, &argv[optind]) == 0 ? 0 : 2;
} }