1/** \file test-parse.c 2 * \brief Completely parse all files given on the command line. 3 * 4 * Copyright (C) 2007 Hans Ulrich Niedermann <gp@n-dimensional.de> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the 18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301 USA. 20 * 21 */ 22 23#include "libexif/exif-data.h" 24#include "libexif/exif-system.h" 25 26#include <string.h> 27#include <stdio.h> 28#include <stdlib.h> 29 30 31/** Callback function handling an ExifEntry. */ 32void content_foreach_func(ExifEntry *entry, void *callback_data); 33void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data)) 34{ 35 char buf[2000]; 36 exif_entry_get_value(entry, buf, sizeof(buf)); 37 printf(" Entry %p: %s (%s)\n" 38 " Size, Comps: %d, %d\n" 39 " Value: %s\n", 40 entry, 41 exif_tag_get_name(entry->tag), 42 exif_format_get_name(entry->format), 43 entry->size, 44 (int)(entry->components), 45 exif_entry_get_value(entry, buf, sizeof(buf))); 46} 47 48 49/** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */ 50void data_foreach_func(ExifContent *content, void *callback_data); 51void data_foreach_func(ExifContent *content, void *callback_data) 52{ 53 printf(" Content %p: ifd=%d\n", content, exif_content_get_ifd(content)); 54 exif_content_foreach_entry(content, content_foreach_func, callback_data); 55} 56 57 58/** Run EXIF parsing test on the given file. */ 59void test_parse(const char *filename, void *callback_data); 60void test_parse(const char *filename, void *callback_data) 61{ 62 ExifData *d; 63 printf("File %s\n", filename); 64 65 d = exif_data_new_from_file(filename); 66 exif_data_foreach_content(d, data_foreach_func, callback_data); 67 exif_data_unref(d); 68} 69 70 71/** Callback function prototype for string parsing. */ 72typedef void (*test_parse_func) (const char *filename, void *callback_data); 73 74 75/** Split string at whitespace and call callback with each substring. */ 76void split_ws_string(const char *string, test_parse_func func, void *callback_data); 77void split_ws_string(const char *string, test_parse_func func, void *callback_data) 78{ 79 const char *start = string; 80 const char *p = start; 81 for (;;) { 82 if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == '\0' ) { 83 size_t len = p-start; 84 if (len > 0) { 85 /* emulate strndup */ 86 char *str = malloc(1+len); 87 if (str) { 88 memcpy(str, start, len); 89 str[len] = '\0'; 90 func(str, callback_data); 91 free(str); 92 start = p+1; 93 } 94 } else { 95 start = p+1; 96 } 97 } 98 if (*p == '\0') { 99 break; 100 } 101 p++; 102 } 103} 104 105 106/** Main program. */ 107int main(const int argc, const char *argv[]) 108{ 109 int i; 110 void *callback_data = NULL; 111 112 const char *envar = getenv("TEST_IMAGES"); 113 if (envar) { 114 split_ws_string(envar, test_parse, callback_data); 115 } 116 117 for (i=1; i<argc; i++) { 118 test_parse(argv[i], callback_data); 119 } 120 121 return 0; 122} 123