Add swap-byte-order.sh to test libexif's byte order conversion function.
[platform/upstream/libexif.git] / test / test-parse.c
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 static unsigned entry_count;
31
32 /** Callback function handling an ExifEntry. */
33 static void 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 %u: %s (%s)\n"
38          "      Size, Comps: %d, %d\n"
39          "      Value: %s\n", 
40          entry_count,
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   ++entry_count;
47 }
48
49
50 /** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */
51 static void data_foreach_func(ExifContent *content, void *callback_data)
52 {
53   static unsigned content_count;
54   entry_count = 0;
55   printf("  Content %u: ifd=%d\n", content_count, exif_content_get_ifd(content));
56   exif_content_foreach_entry(content, content_foreach_func, callback_data);
57   ++content_count;
58 }
59
60 static void dump_makernote(ExifData *d) {
61   ExifMnoteData *mn = exif_data_get_mnote_data(d);
62   if (mn) {
63     char buf[2000];
64     int i;
65     int num = exif_mnote_data_count(mn);
66     printf("  MakerNote\n");
67     for (i=0; i < num; ++i) {
68       if (exif_mnote_data_get_value(mn, i, buf, sizeof(buf))) {
69         const char *name = exif_mnote_data_get_name(mn, i);
70         unsigned int id = exif_mnote_data_get_id(mn, i);
71         if (!name)
72             name = "(unknown)";
73         printf("    Entry %u: %u, %s\n"
74                "      Size: %u\n"
75                "      Value: %s\n", i, id, name, strlen(buf), buf);
76       }
77     }
78   }
79 }
80
81 /** Run EXIF parsing test on the given file. */
82 static void test_parse(const char *filename, void *callback_data, int swap)
83 {
84   ExifData *d;
85
86   /* Skip over path to display only the file name */
87   const char *fn = strrchr(filename, '/');
88   if (fn)
89     ++fn;
90   else
91     fn = filename;
92   printf("File %s\n", fn);
93
94   d = exif_data_new_from_file(filename);
95   printf("Byte order: %s\n",
96           exif_byte_order_get_name(exif_data_get_byte_order(d)));
97
98   if (swap) {
99       ExifByteOrder order = EXIF_BYTE_ORDER_INTEL;
100       if (exif_data_get_byte_order(d) == order) {
101           order = EXIF_BYTE_ORDER_MOTOROLA;
102       }
103       /* This switches the byte order of the entire EXIF data structure,
104        * including the MakerNote */
105       exif_data_set_byte_order(d, order);
106       printf("New byte order: %s\n",
107               exif_byte_order_get_name(exif_data_get_byte_order(d)));
108   }
109
110   exif_data_foreach_content(d, data_foreach_func, callback_data);
111
112   dump_makernote(d);
113
114   exif_data_unref(d);
115 }
116
117
118 /** Callback function prototype for string parsing. */
119 typedef void (*test_parse_func) (const char *filename, void *callback_data, int swap);
120
121
122 /** Split string at whitespace and call callback with each substring. */
123 static void split_ws_string(const char *string, test_parse_func func, void *callback_data)
124 {
125   const char *start = string;
126   const char *p = start;
127   for (;;) {
128     if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == '\0' ) {
129       size_t len = p-start;
130       if (len > 0) {
131         /* emulate strndup */
132         char *str = malloc(1+len);
133         if (str) {
134           memcpy(str, start, len);
135           str[len] = '\0';
136           func(str, callback_data, 0);
137           free(str);
138           start = p+1;
139         }
140       } else {
141         start = p+1;
142       }
143     }
144     if (*p == '\0') {
145       break;
146     }
147     p++;
148   }  
149 }
150
151
152 /** Main program. */
153 int main(const int argc, const char *argv[])
154 {
155   int i;
156   void *callback_data = NULL;
157   int swap = 0;
158   int first = 1;
159
160   if (argc > 1 && !strcmp(argv[1], "--swap-byte-order")) {
161       swap = 1;
162       ++first;
163   }
164
165   if (argc > first) {
166     for (i=first; i<argc; i++) {
167       test_parse(argv[i], callback_data, swap);
168     }
169   } else {
170     /* If no command-line argument is found, get the file names from
171        the environment. */
172     const char *envar = getenv("TEST_IMAGES");
173     if (envar) {
174       split_ws_string(envar, test_parse, callback_data);
175     }
176   }
177
178   return 0;
179 }