Be a bit more robust in the face of out-of-memory errors.
[platform/upstream/libexif.git] / test / test-mnote.c
1 /* exif-mnote.c
2  *
3  * Copyright 2002 Lutz Mueller <lutz@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA.
19  */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <libexif/exif-data.h>
27
28 static int
29 test_exif_data (ExifData *d)
30 {
31         unsigned int i, c;
32         char v[1024], *p;
33         ExifMnoteData *md;
34
35         printf("Byte order: %s\n",
36                 exif_byte_order_get_name (exif_data_get_byte_order (d)));
37
38         printf("Parsing maker note...\n");
39         md = exif_data_get_mnote_data (d);
40         if (!md) {
41                 fprintf (stderr, "Could not parse maker note!\n");
42                 exif_data_unref (d);
43                 return 1;
44         }
45
46         printf("Increasing ref-count...\n");
47         exif_mnote_data_ref (md);
48
49         printf("Decreasing ref-count...\n");
50         exif_mnote_data_unref (md);
51
52         printf("Counting entries...\n");
53         c = exif_mnote_data_count (md);
54         printf("Found %i entries.\n", c);
55         for (i = 0; i < c; i++) {
56                 printf("Dumping entry number %i...\n", i);
57                 printf("  Name: '%s'\n",
58                                 exif_mnote_data_get_name (md, i));
59                 printf("  Title: '%s'\n",
60                                 exif_mnote_data_get_title (md, i));
61                 printf("  Description: '%s'\n",
62                                 exif_mnote_data_get_description (md, i));
63                 p = exif_mnote_data_get_value (md, i, v, sizeof (v));
64                 if (p) { printf("  Value: '%s'\n", v); }
65         }
66
67         return 0;
68 }
69
70 int
71 main (int argc, char **argv)
72 {
73         ExifData *d;
74         unsigned int buf_size;
75         unsigned char *buf;
76         int r;
77
78         if (argc <= 1) {
79                 fprintf (stderr, "You need to supply a filename!\n");
80                 return 1;
81         }
82
83         printf("Loading '%s'...\n", argv[1]);
84         d = exif_data_new_from_file (argv[1]);
85         if (!d) {
86                 fprintf (stderr, "Could not load data from '%s'!\n", argv[1]);
87                 return 1;
88         }
89         printf("Loaded '%s'.\n", argv[1]);
90
91         printf("######### Test 1 #########\n");
92         r = test_exif_data (d);
93         if (r) return r;
94
95         exif_data_save_data (d, &buf, &buf_size);
96         exif_data_unref (d);
97         d = exif_data_new_from_data (buf, buf_size);
98         if (!d) {
99                 fprintf (stderr, "Could not load data from buf!\n");
100                 return 1;
101         }
102         free (buf);
103
104         printf ("######### Test 2 #########\n");
105         r = test_exif_data (d);
106         if (r) return r;
107
108         printf ("Test successful!\n");
109
110         return 1;
111 }