4 tags.c - functions for manipulating an images tags list
10 i_tags_destroy(&tags);
11 i_tags_addn(&tags, "name", code, idata);
12 i_tags_add(&tags, "name", code, data, data_size, idata);
13 if (i_tags_find(&tags, name, start, &entry)) { found }
14 if (i_tags_findn(&tags, code, start, &entry)) { found }
15 i_tags_delete(&tags, index);
16 count = i_tags_delbyname(tags, name);
17 count = i_tags_delbycode(tags, code);
21 Provides functions which give write access to the tags list of an image.
23 For read access directly access the fields (do not write any fields
26 A tag is represented by an i_img_tag structure:
34 char *name; // name of a given tag, might be NULL
35 int code; // number of a given tag, -1 if it has no meaning
36 char *data; // value of a given tag if it's not an int, may be NULL
37 int size; // size of the data
38 int idata; // value of a given tag if data is NULL
53 /* useful for debugging */
54 void i_tags_print(i_img_tags *tags);
57 =item i_tags_new(i_img_tags *tags)
59 Initialize a tags structure. Should not be used if the tags structure
60 has been previously used.
62 To destroy the contents use i_tags_destroy()
67 void i_tags_new(i_img_tags *tags) {
68 tags->count = tags->alloc = 0;
73 =item i_tags_addn(i_img_tags *tags, char *name, int code, int idata)
75 Adds a tag that has an integer value. A simple wrapper around i_tags_add().
77 Duplicate tags can be added.
79 Returns non-zero on success.
84 int i_tags_addn(i_img_tags *tags, char const *name, int code, int idata) {
85 return i_tags_add(tags, name, code, NULL, 0, idata);
89 =item i_tags_add(i_img_tags *tags, char *name, int code, char *data, int size, i_tag_type type, int idata)
91 Adds a tag to the tags list.
93 Duplicate tags can be added.
95 Returns non-zero on success.
100 int i_tags_add(i_img_tags *tags, char const *name, int code, char const *data,
101 int size, int idata) {
102 i_img_tag work = {0};
103 /*printf("i_tags_add(tags %p [count %d], name %s, code %d, data %p, size %d, idata %d)\n",
104 tags, tags->count, name, code, data, size, idata);*/
105 if (tags->tags == NULL) {
107 tags->tags = mymalloc(sizeof(i_img_tag) * alloc);
112 else if (tags->count == tags->alloc) {
113 int newalloc = tags->alloc + 10;
114 void *newtags = myrealloc(tags->tags, sizeof(i_img_tag) * newalloc);
118 tags->tags = newtags;
119 tags->alloc = newalloc;
122 work.name = mymalloc(strlen(name)+1);
125 strcpy(work.name, name);
130 work.data = mymalloc(size+1);
132 if (work.name) myfree(work.name);
135 memcpy(work.data, data, size);
136 work.data[size] = '\0'; /* convenience */
141 tags->tags[tags->count++] = work;
143 /*i_tags_print(tags);*/
148 void i_tags_destroy(i_img_tags *tags) {
151 for (i = 0; i < tags->count; ++i) {
152 if (tags->tags[i].name)
153 myfree(tags->tags[i].name);
154 if (tags->tags[i].data)
155 myfree(tags->tags[i].data);
161 int i_tags_find(i_img_tags *tags, char const *name, int start, int *entry) {
163 while (start < tags->count) {
164 if (tags->tags[start].name && strcmp(name, tags->tags[start].name) == 0) {
174 int i_tags_findn(i_img_tags *tags, int code, int start, int *entry) {
176 while (start < tags->count) {
177 if (tags->tags[start].code == code) {
187 int i_tags_delete(i_img_tags *tags, int entry) {
188 /*printf("i_tags_delete(tags %p [count %d], entry %d)\n",
189 tags, tags->count, entry);*/
190 if (tags->tags && entry >= 0 && entry < tags->count) {
191 i_img_tag old = tags->tags[entry];
192 memmove(tags->tags+entry, tags->tags+entry+1,
193 (tags->count-entry-1) * sizeof(i_img_tag));
205 int i_tags_delbyname(i_img_tags *tags, char const *name) {
208 /*printf("i_tags_delbyname(tags %p [count %d], name %s)\n",
209 tags, tags->count, name);*/
211 for (i = tags->count-1; i >= 0; --i) {
212 if (tags->tags[i].name && strcmp(name, tags->tags[i].name) == 0) {
214 i_tags_delete(tags, i);
218 /*i_tags_print(tags);*/
223 int i_tags_delbycode(i_img_tags *tags, int code) {
227 for (i = tags->count-1; i >= 0; --i) {
228 if (tags->tags[i].code == code) {
230 i_tags_delete(tags, i);
237 int i_tags_get_float(i_img_tags *tags, char const *name, int code,
243 if (!i_tags_find(tags, name, 0, &index))
247 if (!i_tags_findn(tags, code, 0, &index))
250 entry = tags->tags+index;
252 *value = atof(entry->data);
254 *value = entry->idata;
259 int i_tags_set_float(i_img_tags *tags, char const *name, int code,
263 sprintf(temp, "%.30g", value);
265 i_tags_delbyname(tags, name);
267 i_tags_delbycode(tags, code);
269 return i_tags_add(tags, name, code, temp, strlen(temp), 0);
272 int i_tags_get_int(i_img_tags *tags, char const *name, int code, int *value) {
277 if (!i_tags_find(tags, name, 0, &index))
281 if (!i_tags_findn(tags, code, 0, &index))
284 entry = tags->tags+index;
286 *value = atoi(entry->data);
288 *value = entry->idata;
293 static int parse_long(char *data, char **end, long *out) {
295 /* I wrote this without thinking about strtol */
297 int neg = *data == '-';
303 while (isdigit(*data)) {
304 /* this check doesn't guarantee we don't overflow, but it helps */
305 if (x > LONG_MAX / 10)
307 x = x * 10 + *data - '0';
318 int savederr = errno;
322 result = strtol(data, &myend, 10);
323 if ((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE
335 /* parse a comma-separated list of integers
336 returns when it has maxcount numbers, finds a non-comma after a number
337 or can't parse a number
338 if it can't parse a number after a comma, that's considered an error
340 static int parse_long_list(char *data, char **end, int maxcount, long *out) {
344 while (i < maxcount-1) {
345 if (!parse_long(data, &data, out))
353 if (!parse_long(data, &data, out))
360 /* parse "color(red,green,blue,alpha)" */
361 static int parse_color(char *data, char **end, i_color *value) {
365 if (memcmp(data, "color(", 6))
366 return 0; /* not a color */
368 count = parse_long_list(data, &data, 4, n);
371 for (i = 0; i < count; ++i)
372 value->channel[i] = n[i];
374 value->channel[3] = 255;
379 int i_tags_get_color(i_img_tags *tags, char const *name, int code,
386 if (!i_tags_find(tags, name, 0, &index))
390 if (!i_tags_findn(tags, code, 0, &index))
393 entry = tags->tags+index;
397 if (!parse_color(entry->data, &end, value))
400 /* for now we're sloppy about the end */
405 int i_tags_set_color(i_img_tags *tags, char const *name, int code,
406 i_color const *value) {
409 sprintf(temp, "color(%d,%d,%d,%d)", value->channel[0], value->channel[1],
410 value->channel[2], value->channel[3]);
412 i_tags_delbyname(tags, name);
414 i_tags_delbycode(tags, code);
416 return i_tags_add(tags, name, code, temp, strlen(temp), 0);
419 int i_tags_get_string(i_img_tags *tags, char const *name, int code,
420 char *value, size_t value_size) {
425 if (!i_tags_find(tags, name, 0, &index))
429 if (!i_tags_findn(tags, code, 0, &index))
432 entry = tags->tags+index;
434 size_t cpsize = value_size < entry->size ? value_size : entry->size;
435 memcpy(value, entry->data, cpsize);
436 if (cpsize == value_size)
438 value[cpsize] = '\0';
441 sprintf(value, "%d", entry->data);
447 void i_tags_print(i_img_tags *tags) {
449 printf("Alloc %d\n", tags->alloc);
450 printf("Count %d\n", tags->count);
451 for (i = 0; i < tags->count; ++i) {
452 i_img_tag *tag = tags->tags + i;
453 printf("Tag %d\n", i);
455 printf(" Name : %s (%p)\n", tag->name, tag->name);
456 printf(" Code : %d\n", tag->code);
459 printf(" Data : %d (%p) => '", tag->size, tag->data);
460 for (pos = 0; pos < tag->size; ++pos) {
461 if (tag->data[pos] == '\\' || tag->data[pos] == '\'') {
463 putchar(tag->data[pos]);
465 else if (tag->data[pos] < ' ' || tag->data[pos] >= '\x7E')
466 printf("\\x%02X", tag->data[pos]);
468 putchar(tag->data[pos]);
471 printf(" Idata: %d\n", tag->idata);
481 Tony Cook <tony@develop-help.com>