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);
128 work.data = mymalloc(size+1);
130 if (work.name) myfree(work.name);
133 memcpy(work.data, data, size);
134 work.data[size] = '\0'; /* convenience */
139 tags->tags[tags->count++] = work;
141 /*i_tags_print(tags);*/
146 void i_tags_destroy(i_img_tags *tags) {
149 for (i = 0; i < tags->count; ++i) {
150 if (tags->tags[i].name)
151 myfree(tags->tags[i].name);
152 if (tags->tags[i].data)
153 myfree(tags->tags[i].data);
159 int i_tags_find(i_img_tags *tags, char const *name, int start, int *entry) {
161 while (start < tags->count) {
162 if (tags->tags[start].name && strcmp(name, tags->tags[start].name) == 0) {
172 int i_tags_findn(i_img_tags *tags, int code, int start, int *entry) {
174 while (start < tags->count) {
175 if (tags->tags[start].code == code) {
185 int i_tags_delete(i_img_tags *tags, int entry) {
186 /*printf("i_tags_delete(tags %p [count %d], entry %d)\n",
187 tags, tags->count, entry);*/
188 if (tags->tags && entry >= 0 && entry < tags->count) {
189 i_img_tag old = tags->tags[entry];
190 memmove(tags->tags+entry, tags->tags+entry+1,
191 (tags->count-entry-1) * sizeof(i_img_tag));
203 int i_tags_delbyname(i_img_tags *tags, char const *name) {
206 /*printf("i_tags_delbyname(tags %p [count %d], name %s)\n",
207 tags, tags->count, name);*/
209 for (i = tags->count-1; i >= 0; --i) {
210 if (tags->tags[i].name && strcmp(name, tags->tags[i].name) == 0) {
212 i_tags_delete(tags, i);
216 /*i_tags_print(tags);*/
221 int i_tags_delbycode(i_img_tags *tags, int code) {
225 for (i = tags->count-1; i >= 0; --i) {
226 if (tags->tags[i].code == code) {
228 i_tags_delete(tags, i);
235 int i_tags_get_float(i_img_tags *tags, char const *name, int code,
241 if (!i_tags_find(tags, name, 0, &index))
245 if (!i_tags_findn(tags, code, 0, &index))
248 entry = tags->tags+index;
250 *value = atof(entry->data);
252 *value = entry->idata;
257 int i_tags_set_float(i_img_tags *tags, char const *name, int code,
261 sprintf(temp, "%.30g", value);
263 i_tags_delbyname(tags, name);
265 i_tags_delbycode(tags, code);
267 return i_tags_add(tags, name, code, temp, strlen(temp), 0);
270 int i_tags_get_int(i_img_tags *tags, char const *name, int code, int *value) {
275 if (!i_tags_find(tags, name, 0, &index))
279 if (!i_tags_findn(tags, code, 0, &index))
282 entry = tags->tags+index;
284 *value = atoi(entry->data);
286 *value = entry->idata;
291 static int parse_long(char *data, char **end, long *out) {
293 /* I wrote this without thinking about strtol */
295 int neg = *data == '-';
301 while (isdigit(*data)) {
302 /* this check doesn't guarantee we don't overflow, but it helps */
303 if (x > LONG_MAX / 10)
305 x = x * 10 + *data - '0';
316 int savederr = errno;
320 result = strtol(data, &myend, 10);
321 if ((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE
333 /* parse a comma-separated list of integers
334 returns when it has maxcount numbers, finds a non-comma after a number
335 or can't parse a number
336 if it can't parse a number after a comma, that's considered an error
338 static int parse_long_list(char *data, char **end, int maxcount, long *out) {
342 while (i < maxcount-1) {
343 if (!parse_long(data, &data, out))
351 if (!parse_long(data, &data, out))
358 /* parse "color(red,green,blue,alpha)" */
359 static int parse_color(char *data, char **end, i_color *value) {
363 if (memcmp(data, "color(", 6))
364 return 0; /* not a color */
366 count = parse_long_list(data, &data, 4, n);
369 for (i = 0; i < count; ++i)
370 value->channel[i] = n[i];
372 value->channel[3] = 255;
377 int i_tags_get_color(i_img_tags *tags, char const *name, int code,
384 if (!i_tags_find(tags, name, 0, &index))
388 if (!i_tags_findn(tags, code, 0, &index))
391 entry = tags->tags+index;
395 if (!parse_color(entry->data, &end, value))
398 /* for now we're sloppy about the end */
403 int i_tags_set_color(i_img_tags *tags, char const *name, int code,
404 i_color const *value) {
407 sprintf(temp, "color(%d,%d,%d,%d)", value->channel[0], value->channel[1],
408 value->channel[2], value->channel[3]);
410 i_tags_delbyname(tags, name);
412 i_tags_delbycode(tags, code);
414 return i_tags_add(tags, name, code, temp, strlen(temp), 0);
417 int i_tags_get_string(i_img_tags *tags, char const *name, int code,
418 char *value, size_t value_size) {
423 if (!i_tags_find(tags, name, 0, &index))
427 if (!i_tags_findn(tags, code, 0, &index))
430 entry = tags->tags+index;
432 size_t cpsize = value_size < entry->size ? value_size : entry->size;
433 memcpy(value, entry->data, cpsize);
434 if (cpsize == value_size)
436 value[cpsize] = '\0';
439 sprintf(value, "%d", entry->data);
445 void i_tags_print(i_img_tags *tags) {
447 printf("Alloc %d\n", tags->alloc);
448 printf("Count %d\n", tags->count);
449 for (i = 0; i < tags->count; ++i) {
450 i_img_tag *tag = tags->tags + i;
451 printf("Tag %d\n", i);
453 printf(" Name : %s (%p)\n", tag->name, tag->name);
454 printf(" Code : %d\n", tag->code);
457 printf(" Data : %d (%p) => '", tag->size, tag->data);
458 for (pos = 0; pos < tag->size; ++pos) {
459 if (tag->data[pos] == '\\' || tag->data[pos] == '\'') {
461 putchar(tag->data[pos]);
463 else if (tag->data[pos] < ' ' || tag->data[pos] >= '\x7E')
464 printf("\\x%02X", tag->data[pos]);
466 putchar(tag->data[pos]);
469 printf(" Idata: %d\n", tag->idata);
479 Tony Cook <tony@develop-help.com>