diff options
author | Hans Ulrich Niedermann <gp@n-dimensional.de> | 2005-06-13 22:41:17 +0000 |
---|---|---|
committer | Hans Ulrich Niedermann <gp@n-dimensional.de> | 2005-06-13 22:41:17 +0000 |
commit | 0d4d1e1f3c79448b15aafb8fb0b190fd74b56f23 (patch) | |
tree | d6cbb2016a4dd4e81880460926a13c405411de6e /tests | |
parent | a406a51b434ffc243facc5731d40ab1725d5a444 (diff) | |
download | libgphoto2-0d4d1e1f3c79448b15aafb8fb0b190fd74b56f23.tar.gz |
test-camera-list.c:
- get rid of warnings
- add a few command line parameters
- support multiple output formats:
CSV, flat text list, text list with headers, experimental XML,
or just the number of supported cameras.
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@8092 67ed7778-7388-44ab-90cf-0a291f65f57c
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-camera-list.c | 160 |
1 files changed, 135 insertions, 25 deletions
diff --git a/tests/test-camera-list.c b/tests/test-camera-list.c index 7d34fefb6..4df1f5c40 100644 --- a/tests/test-camera-list.c +++ b/tests/test-camera-list.c @@ -21,7 +21,8 @@ #include "config.h" #include <stdio.h> - +#include <stdlib.h> +#include <string.h> #include <time.h> #include <sys/time.h> @@ -31,11 +32,8 @@ #define CHECK(f) {int res = f; if (res < 0) {printf ("ERROR: %s\n", gp_result_as_string (res)); return (1);}} - -/* #define TEST_DEBUG */ -#undef TEST_DEBUG - -#ifdef TEST_DEBUG +/* boolean value */ +static int do_debug = 0; /* time zero for debug log time stamps */ struct timeval glob_tv_zero = { 0, 0 }; @@ -59,7 +57,6 @@ debug_func (GPLogLevel level, const char *domain, const char *format, fputc ('\n', stderr); } -#endif /* TEST_DEBUG */ static void print_headline (void) @@ -79,12 +76,13 @@ print_hline (void) "-------------------------------------------"); } +/** C equivalent of basename(1) */ static const char * basename (const char *pathname) { char *result, *tmp; /* remove path part from camlib name */ - for (result=tmp=pathname; *tmp != '\0'; tmp++) { + for (result=tmp=(char *)pathname; (*tmp!='\0'); tmp++) { if ((*tmp == gp_system_dir_delim) && (*(tmp+1) != '\0')) { result = tmp+1; @@ -93,19 +91,73 @@ basename (const char *pathname) return (const char *)result; } +typedef enum { + /* text table with headers */ + FMT_HEADED_TEXT = 0, + /* text table without headers */ + FMT_FLAT_TEXT, + /* comma separated values without headers */ + FMT_CSV, + /* Demo XML, don't use this for anything (yet) */ + FMT_XML, + /* Just print the number of supported cameras */ + FMT_COUNT +} OutputFormat; + +static OutputFormat format = FMT_HEADED_TEXT; + +// #define DEBUG_OUTPUT + +/** Parse command line and set global variables. */ +static void +parse_command_line (const int argc, char *argv[]) +{ + int i; +#ifdef DEBUG_OUTPUT + fprintf(stderr, "parsing cmdline (%d, %p)\n", argc, argv); + fprintf(stderr, "argv[0]=\"%s\"\n", argv[0]); +#endif + for (i=1; i<argc; i++) { +#ifdef DEBUG_OUTPUT + fprintf(stderr, "argv[%d]=\"%s\"\n", i, argv[i]); +#endif + if (strcmp(argv[i], "--debug") == 0) { + do_debug = 1; + } else if (strcmp(argv[i], "--format=flattext") == 0) { + format = FMT_FLAT_TEXT; + } else if (strcmp(argv[i], "--format=csv") == 0) { + format = FMT_CSV; + } else if (strcmp(argv[i], "--format=xml") == 0) { + format = FMT_XML; + } else if (strcmp(argv[i], "--format=count") == 0) { + format = FMT_COUNT; + } else { + const char * const bn = basename(argv[0]); + printf("Unknown command line parameter %d: \"%s\"\n", + i, argv[i]); + printf("%s: Aborting.\n", bn); + exit(1); + } + } +} + int -main (int argc, char *argv []) +main (int argc, char *argv[]) { CameraAbilitiesList *al; int i; int count; + const char *fmt_str = NULL; -#ifdef TEST_DEBUG - gettimeofday (&glob_tv_zero, NULL); - CHECK (gp_log_add_func (GP_LOG_ALL, debug_func, NULL)); + parse_command_line (argc, argv); + + if (do_debug) { + gettimeofday (&glob_tv_zero, NULL); + CHECK (gp_log_add_func (GP_LOG_ALL, debug_func, NULL)); + + gp_log (GP_LOG_DEBUG, "main", "test-camera-list start"); + } - gp_log (GP_LOG_DEBUG, "main", "test-camera-list start"); -#endif /* TEST_DEBUG */ CHECK (gp_abilities_list_new (&al)); CHECK (gp_abilities_list_load (al, NULL)); @@ -119,27 +171,85 @@ main (int argc, char *argv []) return(1); } - print_hline(); - print_headline(); - print_hline(); + + /* Set output format for file body, + * and print opening part of output file. */ + switch (format) { + case FMT_CSV: + fmt_str = "%d,%s,%s,%s\n"; + break; + case FMT_FLAT_TEXT: + fmt_str = "%3d|%-20s|%-20s|%s\n"; + break; + case FMT_HEADED_TEXT: + fmt_str = "%3d|%-20s|%-20s|%s\n"; + break; + case FMT_XML: + fmt_str = " <camera entry_number=\"%d\">\n" + " <camlib-name value=\"%s\"/>\n" + " <driver-name value=\"%s\"/>\n" + " <camera-name value=\"%s\"/>\n" + " </camera>\n"; + printf("<?xml version=\"%s\" encoding=\"%s\"?>\n" + "<camera-list camera-count=\"%d\">\n", + "1.0", "us-ascii", count); + break; + case FMT_COUNT: + printf("%d\n", count); + return(0); + break; + } + + /* For each camera in the list, add a text snippet to the + * output file. */ for (i = 0; i < count; i++) { CameraAbilities abilities; const char *camlib_basename; CHECK (gp_abilities_list_get_abilities (al, i, &abilities)); camlib_basename = basename(abilities.library); - if (((i%25)== 0) && ((count-i) > 5)) { - print_hline(); - print_headline(); - print_hline(); + + switch (format) { + case FMT_HEADED_TEXT: + if ( ((i%25)== 0) && + ( (i==0) || ((count-i) > 5) ) ) { + print_hline(); + print_headline(); + print_hline(); + } + break; + case FMT_XML: + break; + case FMT_CSV: + break; + case FMT_FLAT_TEXT: + break; + case FMT_COUNT: + break; } - printf("%3d|%-20s|%-20s|%s\n", i+1, + + printf(fmt_str, i+1, camlib_basename, abilities.id, abilities.model); } - print_hline(); - print_headline(); - print_hline(); + + /* Print closing part of output file. */ + switch (format) { + case FMT_HEADED_TEXT: + print_hline(); + print_headline(); + print_hline(); + break; + case FMT_XML: + printf("</camera-list>\n"); + break; + case FMT_CSV: + break; + case FMT_FLAT_TEXT: + break; + case FMT_COUNT: + break; + } CHECK (gp_abilities_list_free (al)); return (0); |