Merge branch 'feature/findconfig-verbose' into 'master'

add verbose option to findconfig

See merge request megapixels-org/libmegapixels!1
This commit is contained in:
Martijn Braam
2023-11-14 13:20:50 +00:00
3 changed files with 31 additions and 6 deletions

View File

@@ -10,6 +10,9 @@
EXPORT int EXPORT int
libmegapixels_find_config(char *configfile); libmegapixels_find_config(char *configfile);
EXPORT int
libmegapixels_find_config_verbose(char *configfile, int print);
#define LIBMEGAPIXELS_CMD_LINK 1 #define LIBMEGAPIXELS_CMD_LINK 1
#define LIBMEGAPIXELS_CMD_MODE 2 #define LIBMEGAPIXELS_CMD_MODE 2
#define LIBMEGAPIXELS_CMD_CROP 3 #define LIBMEGAPIXELS_CMD_CROP 3

View File

@@ -10,30 +10,42 @@
#endif #endif
static int static int
find_device_by_model(char *conffile, char *model) find_device_by_model(char *conffile, char *model, int print)
{ {
// Check config/%model.conf in the current working directory // Check config/%model.conf in the current working directory
sprintf(conffile, "config/%s.conf", model); sprintf(conffile, "config/%s.conf", model);
if (print) {
printf("- %s\n", conffile);
}
if (access(conffile, F_OK) != -1) { if (access(conffile, F_OK) != -1) {
return 1; return 1;
} }
// Check user overridden /etc/megapixels/config/%model.conf // Check user overridden /etc/megapixels/config/%model.conf
sprintf(conffile, "%s/megapixels/config/%s.conf", SYSCONFDIR, model); sprintf(conffile, "%s/megapixels/config/%s.conf", SYSCONFDIR, model);
if (print) {
printf("- %s\n", conffile);
}
if (access(conffile, F_OK) != -1) { if (access(conffile, F_OK) != -1) {
return 1; return 1;
} }
// Check packaged /usr/share/megapixels/config/%model.conf // Check packaged /usr/share/megapixels/config/%model.conf
sprintf(conffile, "%s/megapixels/config/%s.conf", DATADIR, model); sprintf(conffile, "%s/megapixels/config/%s.conf", DATADIR, model);
if (print) {
printf("- %s\n", conffile);
}
if (access(conffile, F_OK) != -1) { if (access(conffile, F_OK) != -1) {
return 1; return 1;
} }
if (print) {
printf("no config for '%s'\n", model);
}
return 0; return 0;
} }
int int
libmegapixels_find_config(char *configfile) libmegapixels_find_config_verbose(char *configfile, int print)
{ {
char model[512]; char model[512];
FILE *fp; FILE *fp;
@@ -50,10 +62,16 @@ libmegapixels_find_config(char *configfile)
} }
*(modelptr++) = (char) c; *(modelptr++) = (char) c;
if (c == 0) { if (c == 0) {
if (find_device_by_model(configfile, model)) { if (find_device_by_model(configfile, model, print)) {
return 1; return 1;
} }
modelptr = model; modelptr = model;
} }
} }
} }
int
libmegapixels_find_config(char *configfile)
{
return libmegapixels_find_config_verbose(configfile, 0);
}

View File

@@ -10,14 +10,18 @@ main(int argc, char *argv[])
int c; int c;
char configpath[PATH_MAX]; char configpath[PATH_MAX];
int ret = libmegapixels_find_config(configpath); int ret;
int verbose = 0;
while ((c = getopt(argc, argv, "c:")) != -1) { while ((c = getopt(argc, argv, "c:v")) != -1) {
switch (c) { switch (c) {
case 'c': case 'c':
sprintf(configpath, "%s", optarg); sprintf(configpath, "%s", optarg);
ret = 1; ret = 1;
break; break;
case 'v':
verbose = 1;
break;
case '?': case '?':
if (optopt == 'd' || optopt == 'l') { if (optopt == 'd' || optopt == 'l') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt); fprintf(stderr, "Option -%c requires an argument.\n", optopt);
@@ -31,7 +35,7 @@ main(int argc, char *argv[])
return 1; return 1;
} }
} }
ret = libmegapixels_find_config_verbose(configpath, verbose);
libmegapixels_devconfig *config = {0}; libmegapixels_devconfig *config = {0};
libmegapixels_init(&config); libmegapixels_init(&config);