| 1 |
|
| /* |
| 2 |
|
| =head1 NAME |
| 3 |
|
| |
| 4 |
|
| tags.c - functions for manipulating an images tags list |
| 5 |
|
| |
| 6 |
|
| =head1 SYNOPSIS |
| 7 |
|
| |
| 8 |
|
| i_img_tags tags; |
| 9 |
|
| i_tags_new(&tags); |
| 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); |
| 18 |
|
| if (i_tags_get_float(&tags, name, code, &float_value)) { found } |
| 19 |
|
| i_tags_set_float(&tags, name, code, value); |
| 20 |
|
| i_tags_set_float2(&tags, name, code, value, sig_digits); |
| 21 |
|
| i_tags_get_int(&tags, name, code, &int_value); |
| 22 |
|
| |
| 23 |
|
| =head1 DESCRIPTION |
| 24 |
|
| |
| 25 |
|
| Provides functions which give write access to the tags list of an image. |
| 26 |
|
| |
| 27 |
|
| For read access directly access the fields (do not write any fields |
| 28 |
|
| directly). |
| 29 |
|
| |
| 30 |
|
| A tag is represented by an i_img_tag structure: |
| 31 |
|
| |
| 32 |
|
| typedef enum { |
| 33 |
|
| itt_double, |
| 34 |
|
| iit_text |
| 35 |
|
| } i_tag_type; |
| 36 |
|
| |
| 37 |
|
| typedef struct { |
| 38 |
|
| char *name; // name of a given tag, might be NULL |
| 39 |
|
| int code; // number of a given tag, -1 if it has no meaning |
| 40 |
|
| char *data; // value of a given tag if it's not an int, may be NULL |
| 41 |
|
| int size; // size of the data |
| 42 |
|
| int idata; // value of a given tag if data is NULL |
| 43 |
|
| } i_img_tag; |
| 44 |
|
| |
| 45 |
|
| |
| 46 |
|
| =over |
| 47 |
|
| |
| 48 |
|
| =cut |
| 49 |
|
| */ |
| 50 |
|
| |
| 51 |
|
| #include "imager.h" |
| 52 |
|
| #include <string.h> |
| 53 |
|
| #include <stdlib.h> |
| 54 |
|
| #include <errno.h> |
| 55 |
|
| #include <limits.h> |
| 56 |
|
| |
| 57 |
|
| /* useful for debugging */ |
| 58 |
|
| void i_tags_print(i_img_tags *tags); |
| 59 |
|
| |
| 60 |
|
| /* |
| 61 |
|
| =item i_tags_new(i_img_tags *tags) |
| 62 |
|
| |
| 63 |
|
| =category Tags |
| 64 |
|
| |
| 65 |
|
| Initialize a tags structure. Should not be used if the tags structure |
| 66 |
|
| has been previously used. |
| 67 |
|
| |
| 68 |
|
| This should be called tags member of an i_img object on creation (in |
| 69 |
|
| i_img_*_new() functions). |
| 70 |
|
| |
| 71 |
|
| To destroy the contents use i_tags_destroy() |
| 72 |
|
| |
| 73 |
|
| =cut |
| 74 |
|
| */ |
| 75 |
|
| |
| 76 |
1517 |
| void i_tags_new(i_img_tags *tags) { |
| 77 |
1517 |
| tags->count = tags->alloc = 0; |
| 78 |
1517 |
| tags->tags = NULL; |
| 79 |
1517 |
| } |
| 80 |
|
| |
| 81 |
|
| /* |
| 82 |
|
| =item i_tags_addn(i_img_tags *tags, char *name, int code, int idata) |
| 83 |
|
| |
| 84 |
|
| Adds a tag that has an integer value. A simple wrapper around i_tags_add(). |
| 85 |
|
| |
| 86 |
|
| Use i_tags_setn() instead, this function may be removed in the future. |
| 87 |
|
| |
| 88 |
|
| Returns non-zero on success. |
| 89 |
|
| |
| 90 |
|
| =cut |
| 91 |
|
| */ |
| 92 |
|
| |
| 93 |
674 |
| int i_tags_addn(i_img_tags *tags, char const *name, int code, int idata) { |
| 94 |
674 |
| return i_tags_add(tags, name, code, NULL, 0, idata); |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| /* |
| 98 |
|
| =item i_tags_add(i_img_tags *tags, char *name, int code, char *data, int size, i_tag_type type, int idata) |
| 99 |
|
| |
| 100 |
|
| Adds a tag to the tags list. |
| 101 |
|
| |
| 102 |
|
| Use i_tags_set() instead, this function may be removed in the future. |
| 103 |
|
| |
| 104 |
|
| Returns non-zero on success. |
| 105 |
|
| |
| 106 |
|
| =cut |
| 107 |
|
| */ |
| 108 |
|
| |
| 109 |
1173 |
| int i_tags_add(i_img_tags *tags, char const *name, int code, char const *data, |
| 110 |
|
| int size, int idata) { |
| 111 |
1173 |
| i_img_tag work = {0}; |
| 112 |
|
| /*printf("i_tags_add(tags %p [count %d], name %s, code %d, data %p, size %d, idata %d)\n", |
| 113 |
|
| tags, tags->count, name, code, data, size, idata);*/ |
| 114 |
1173 |
▶ | if (tags->tags == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags == NULL) { |
283 |
890 |
1173 |
|
| 115 |
283 |
| int alloc = 10; |
| 116 |
283 |
| tags->tags = mymalloc(sizeof(i_img_tag) * alloc); |
| 117 |
283 |
▶ | if (!tags->tags) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tags->tags) |
0 |
283 |
283 |
|
| 118 |
0 |
| return 0; |
| 119 |
283 |
| tags->alloc = alloc; |
| 120 |
|
| } |
| 121 |
890 |
▶ | else if (tags->count == tags->alloc) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (tags->count == tags->alloc) { |
9 |
881 |
890 |
|
| 122 |
9 |
| int newalloc = tags->alloc + 10; |
| 123 |
9 |
| void *newtags = myrealloc(tags->tags, sizeof(i_img_tag) * newalloc); |
| 124 |
9 |
▶ | if (!newtags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!newtags) { |
0 |
9 |
9 |
|
| 125 |
0 |
| return 0; |
| 126 |
|
| } |
| 127 |
9 |
| tags->tags = newtags; |
| 128 |
9 |
| tags->alloc = newalloc; |
| 129 |
|
| } |
| 130 |
1173 |
▶ | if (name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) { |
1170 |
3 |
1173 |
|
| 131 |
1170 |
| work.name = mymalloc(strlen(name)+1); |
| 132 |
1170 |
▶ | if (!work.name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!work.name) |
0 |
1170 |
1170 |
|
| 133 |
0 |
| return 0; |
| 134 |
1170 |
| strcpy(work.name, name); |
| 135 |
|
| } |
| 136 |
1173 |
▶ | if (data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (data) { |
498 |
675 |
1173 |
|
| 137 |
498 |
▶ | if (size == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (size == -1) |
219 |
279 |
498 |
|
| 138 |
219 |
| size = strlen(data); |
| 139 |
498 |
| work.data = mymalloc(size+1); |
| 140 |
498 |
▶ | if (!work.data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!work.data) { |
0 |
498 |
498 |
|
| 141 |
0 |
▶ | if (work.name) myfree(work.name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (work.name) myfree(work.name); |
0 |
0 |
0 |
|
| 142 |
0 |
| return 0; |
| 143 |
|
| } |
| 144 |
498 |
| memcpy(work.data, data, size); |
| 145 |
498 |
| work.data[size] = '\0'; /* convenience */ |
| 146 |
498 |
| work.size = size; |
| 147 |
|
| } |
| 148 |
1173 |
| work.code = code; |
| 149 |
1173 |
| work.idata = idata; |
| 150 |
1173 |
| tags->tags[tags->count++] = work; |
| 151 |
|
| |
| 152 |
|
| /*i_tags_print(tags);*/ |
| 153 |
|
| |
| 154 |
1173 |
| return 1; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
|
| /* |
| 158 |
|
| =item i_tags_destroy(tags) |
| 159 |
|
| |
| 160 |
|
| =category Tags |
| 161 |
|
| |
| 162 |
|
| Destroys the given tags structure. Called by i_img_destroy(). |
| 163 |
|
| |
| 164 |
|
| =cut |
| 165 |
|
| */ |
| 166 |
|
| |
| 167 |
1517 |
| void i_tags_destroy(i_img_tags *tags) { |
| 168 |
1517 |
▶ | if (tags->tags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags) { |
283 |
1234 |
1517 |
|
| 169 |
|
| int i; |
| 170 |
1442 |
▶ | for (i = 0; i < tags->count; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tags->count; ++i) { |
1159 |
283 |
1442 |
|
| 171 |
1159 |
▶ | if (tags->tags[i].name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags[i].name) |
1158 |
1 |
1159 |
|
| 172 |
1158 |
| myfree(tags->tags[i].name); |
| 173 |
1159 |
▶ | if (tags->tags[i].data) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags[i].data) |
493 |
666 |
1159 |
|
| 174 |
493 |
| myfree(tags->tags[i].data); |
| 175 |
|
| } |
| 176 |
283 |
| myfree(tags->tags); |
| 177 |
|
| } |
| 178 |
1517 |
| } |
| 179 |
|
| |
| 180 |
|
| /* |
| 181 |
|
| =item i_tags_find(tags, name, start, &entry) |
| 182 |
|
| |
| 183 |
|
| =category Tags |
| 184 |
|
| |
| 185 |
|
| Searches for a tag of the given I<name> starting from index I<start>. |
| 186 |
|
| |
| 187 |
|
| On success returns true and sets *I<entry>. |
| 188 |
|
| |
| 189 |
|
| On failure returns false. |
| 190 |
|
| |
| 191 |
|
| =cut |
| 192 |
|
| */ |
| 193 |
|
| |
| 194 |
712 |
| int i_tags_find(i_img_tags *tags, char const *name, int start, int *entry) { |
| 195 |
712 |
▶ | if (tags->tags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags) { |
491 |
221 |
712 |
|
| 196 |
1936 |
▶ | while (start < tags->count) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (start < tags->count) { |
1693 |
243 |
1936 |
|
| 197 |
1693 |
▶ | if (tags->tags[start].name && strcmp(name, tags->tags[start].name) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tags->tags[start].name && strcmp(name, tags->tags[start].name) == 0) { |
1685 |
8 |
1693 |
| if (tags->tags[start].name && strcmp(name, tags->tags[start].name) == 0) { |
248 |
1437 |
1685 |
|
| 198 |
248 |
| *entry = start; |
| 199 |
248 |
| return 1; |
| 200 |
|
| } |
| 201 |
1445 |
| ++start; |
| 202 |
|
| } |
| 203 |
|
| } |
| 204 |
464 |
| return 0; |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| /* |
| 208 |
|
| =item i_tags_findn(tags, code, start, &entry) |
| 209 |
|
| |
| 210 |
|
| =category Tags |
| 211 |
|
| |
| 212 |
|
| Searches for a tag of the given I<code> starting from index I<start>. |
| 213 |
|
| |
| 214 |
|
| On success returns true and sets *I<entry>. |
| 215 |
|
| |
| 216 |
|
| On failure returns false. |
| 217 |
|
| |
| 218 |
|
| =cut |
| 219 |
|
| */ |
| 220 |
|
| |
| 221 |
5 |
| int i_tags_findn(i_img_tags *tags, int code, int start, int *entry) { |
| 222 |
5 |
▶ | if (tags->tags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags) { |
5 |
0 |
5 |
|
| 223 |
15 |
▶ | while (start < tags->count) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (start < tags->count) { |
14 |
1 |
15 |
|
| 224 |
14 |
▶ | if (tags->tags[start].code == code) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags[start].code == code) { |
4 |
10 |
14 |
|
| 225 |
4 |
| *entry = start; |
| 226 |
4 |
| return 1; |
| 227 |
|
| } |
| 228 |
10 |
| ++start; |
| 229 |
|
| } |
| 230 |
|
| } |
| 231 |
1 |
| return 0; |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
|
| /* |
| 235 |
|
| =item i_tags_delete(tags, index) |
| 236 |
|
| |
| 237 |
|
| =category Tags |
| 238 |
|
| |
| 239 |
|
| Delete a tag by index. |
| 240 |
|
| |
| 241 |
|
| Returns true on success. |
| 242 |
|
| |
| 243 |
|
| =cut |
| 244 |
|
| */ |
| 245 |
14 |
| int i_tags_delete(i_img_tags *tags, int entry) { |
| 246 |
|
| /*printf("i_tags_delete(tags %p [count %d], entry %d)\n", |
| 247 |
|
| tags, tags->count, entry);*/ |
| 248 |
14 |
▶ | if (tags->tags && entry >= 0 && entry < tags->count) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (tags->tags && entry >= 0 && entry < tags->count) { |
14 |
0 |
14 |
| if (tags->tags && entry >= 0 && entry < tags->count) { |
14 |
0 |
14 |
| if (tags->tags && entry >= 0 && entry < tags->count) { |
14 |
0 |
14 |
|
| 249 |
14 |
| i_img_tag old = tags->tags[entry]; |
| 250 |
14 |
| memmove(tags->tags+entry, tags->tags+entry+1, |
| 251 |
14 |
| (tags->count-entry-1) * sizeof(i_img_tag)); |
| 252 |
14 |
▶ | if (old.name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (old.name) |
12 |
2 |
14 |
|
| 253 |
12 |
| myfree(old.name); |
| 254 |
14 |
▶ | if (old.data) |
Branch: 1 branch
| Branch | True | False | Total |
| if (old.data) |
5 |
9 |
14 |
|
| 255 |
5 |
| myfree(old.data); |
| 256 |
14 |
| --tags->count; |
| 257 |
|
| |
| 258 |
14 |
| return 1; |
| 259 |
|
| } |
| 260 |
0 |
| return 0; |
| 261 |
|
| } |
| 262 |
|
| |
| 263 |
|
| /* |
| 264 |
|
| =item i_tags_delbyname(tags, name) |
| 265 |
|
| |
| 266 |
|
| =category Tags |
| 267 |
|
| |
| 268 |
|
| Delete any tags with the given name. |
| 269 |
|
| |
| 270 |
|
| Returns the number of tags deleted. |
| 271 |
|
| |
| 272 |
|
| =cut |
| 273 |
|
| */ |
| 274 |
|
| |
| 275 |
568 |
| int i_tags_delbyname(i_img_tags *tags, char const *name) { |
| 276 |
568 |
| int count = 0; |
| 277 |
|
| int i; |
| 278 |
|
| /*printf("i_tags_delbyname(tags %p [count %d], name %s)\n", |
| 279 |
|
| tags, tags->count, name);*/ |
| 280 |
568 |
▶ | if (tags->tags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags) { |
471 |
97 |
568 |
|
| 281 |
1395 |
▶ | for (i = tags->count-1; i >= 0; --i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = tags->count-1; i >= 0; --i) { |
924 |
471 |
1395 |
|
| 282 |
924 |
▶ | if (tags->tags[i].name && strcmp(name, tags->tags[i].name) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tags->tags[i].name && strcmp(name, tags->tags[i].name) == 0) { |
922 |
2 |
924 |
| if (tags->tags[i].name && strcmp(name, tags->tags[i].name) == 0) { |
12 |
910 |
922 |
|
| 283 |
12 |
| ++count; |
| 284 |
12 |
| i_tags_delete(tags, i); |
| 285 |
|
| } |
| 286 |
|
| } |
| 287 |
|
| } |
| 288 |
|
| /*i_tags_print(tags);*/ |
| 289 |
|
| |
| 290 |
568 |
| return count; |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
|
| /* |
| 294 |
|
| =item i_tags_delbycode(tags, code) |
| 295 |
|
| |
| 296 |
|
| =category Tags |
| 297 |
|
| |
| 298 |
|
| Delete any tags with the given code. |
| 299 |
|
| |
| 300 |
|
| Returns the number of tags deleted. |
| 301 |
|
| |
| 302 |
|
| =cut |
| 303 |
|
| */ |
| 304 |
|
| |
| 305 |
3 |
| int i_tags_delbycode(i_img_tags *tags, int code) { |
| 306 |
3 |
| int count = 0; |
| 307 |
|
| int i; |
| 308 |
3 |
▶ | if (tags->tags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags) { |
2 |
1 |
3 |
|
| 309 |
7 |
▶ | for (i = tags->count-1; i >= 0; --i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = tags->count-1; i >= 0; --i) { |
5 |
2 |
7 |
|
| 310 |
5 |
▶ | if (tags->tags[i].code == code) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tags->tags[i].code == code) { |
1 |
4 |
5 |
|
| 311 |
1 |
| ++count; |
| 312 |
1 |
| i_tags_delete(tags, i); |
| 313 |
|
| } |
| 314 |
|
| } |
| 315 |
|
| } |
| 316 |
3 |
| return count; |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
|
| /* |
| 320 |
|
| =item i_tags_get_float(tags, name, code, value) |
| 321 |
|
| |
| 322 |
|
| =category Tags |
| 323 |
|
| |
| 324 |
|
| Retrieves a tag as a floating point value. |
| 325 |
|
| |
| 326 |
|
| If the tag has a string value then that is parsed as a floating point |
| 327 |
|
| number, otherwise the integer value of the tag is used. |
| 328 |
|
| |
| 329 |
|
| On success sets *I<value> and returns true. |
| 330 |
|
| |
| 331 |
|
| On failure returns false. |
| 332 |
|
| |
| 333 |
|
| =cut |
| 334 |
|
| */ |
| 335 |
|
| |
| 336 |
50 |
| int i_tags_get_float(i_img_tags *tags, char const *name, int code, |
| 337 |
|
| double *value) { |
| 338 |
|
| int index; |
| 339 |
|
| i_img_tag *entry; |
| 340 |
|
| |
| 341 |
50 |
▶ | if (name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) { |
50 |
0 |
50 |
|
| 342 |
50 |
▶ | if (!i_tags_find(tags, name, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_find(tags, name, 0, &index)) |
24 |
26 |
50 |
|
| 343 |
24 |
| return 0; |
| 344 |
|
| } |
| 345 |
|
| else { |
| 346 |
0 |
▶ | if (!i_tags_findn(tags, code, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_findn(tags, code, 0, &index)) |
0 |
0 |
0 |
|
| 347 |
0 |
| return 0; |
| 348 |
|
| } |
| 349 |
26 |
| entry = tags->tags+index; |
| 350 |
26 |
▶ | if (entry->data) |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->data) |
25 |
1 |
26 |
|
| 351 |
25 |
| *value = atof(entry->data); |
| 352 |
|
| else |
| 353 |
1 |
| *value = entry->idata; |
| 354 |
|
| |
| 355 |
26 |
| return 1; |
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
|
| /* |
| 359 |
|
| =item i_tags_set_float(tags, name, code, value) |
| 360 |
|
| |
| 361 |
|
| =category Tags |
| 362 |
|
| |
| 363 |
|
| Equivalent to i_tags_set_float2(tags, name, code, value, 30). |
| 364 |
|
| |
| 365 |
|
| =cut |
| 366 |
|
| */ |
| 367 |
|
| |
| 368 |
0 |
| int i_tags_set_float(i_img_tags *tags, char const *name, int code, |
| 369 |
|
| double value) { |
| 370 |
0 |
| return i_tags_set_float2(tags, name, code, value, 30); |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
|
| /* |
| 374 |
|
| =item i_tags_set_float2(tags, name, code, value, places) |
| 375 |
|
| |
| 376 |
|
| =category Tags |
| 377 |
|
| |
| 378 |
|
| Sets the tag with the given name and code to the given floating point |
| 379 |
|
| value. |
| 380 |
|
| |
| 381 |
|
| Since tags are strings or ints, we convert the value to a string before |
| 382 |
|
| storage at the precision specified by C<places>. |
| 383 |
|
| |
| 384 |
|
| =cut |
| 385 |
|
| */ |
| 386 |
|
| |
| 387 |
116 |
| int i_tags_set_float2(i_img_tags *tags, char const *name, int code, |
| 388 |
|
| double value, int places) { |
| 389 |
|
| char temp[40]; |
| 390 |
|
| |
| 391 |
116 |
▶ | if (places < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (places < 0) |
0 |
116 |
116 |
|
| 392 |
0 |
| places = 30; |
| 393 |
116 |
▶ | else if (places > 30) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (places > 30) |
0 |
116 |
116 |
|
| 394 |
0 |
| places = 30; |
| 395 |
|
| |
| 396 |
116 |
| sprintf(temp, "%.*g", places, value); |
| 397 |
116 |
▶ | if (name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) |
116 |
0 |
116 |
|
| 398 |
116 |
| i_tags_delbyname(tags, name); |
| 399 |
|
| else |
| 400 |
0 |
| i_tags_delbycode(tags, code); |
| 401 |
|
| |
| 402 |
116 |
| return i_tags_add(tags, name, code, temp, strlen(temp), 0); |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
|
| /* |
| 406 |
|
| =item i_tags_get_int(tags, name, code, &value) |
| 407 |
|
| |
| 408 |
|
| =category Tags |
| 409 |
|
| |
| 410 |
|
| Retrieve a tag specified by name or code as an integer. |
| 411 |
|
| |
| 412 |
|
| On success sets the int *I<value> to the integer and returns true. |
| 413 |
|
| |
| 414 |
|
| On failure returns false. |
| 415 |
|
| |
| 416 |
|
| =cut |
| 417 |
|
| */ |
| 418 |
|
| |
| 419 |
210 |
| int i_tags_get_int(i_img_tags *tags, char const *name, int code, int *value) { |
| 420 |
|
| int index; |
| 421 |
|
| i_img_tag *entry; |
| 422 |
|
| |
| 423 |
210 |
▶ | if (name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) { |
210 |
0 |
210 |
|
| 424 |
210 |
▶ | if (!i_tags_find(tags, name, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_find(tags, name, 0, &index)) |
172 |
38 |
210 |
|
| 425 |
172 |
| return 0; |
| 426 |
|
| } |
| 427 |
|
| else { |
| 428 |
0 |
▶ | if (!i_tags_findn(tags, code, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_findn(tags, code, 0, &index)) |
0 |
0 |
0 |
|
| 429 |
0 |
| return 0; |
| 430 |
|
| } |
| 431 |
38 |
| entry = tags->tags+index; |
| 432 |
38 |
▶ | if (entry->data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->data) { |
0 |
38 |
38 |
|
| 433 |
0 |
| *value = 0; |
| 434 |
0 |
| int save_errno = errno; /* probably doesn't matter */ |
| 435 |
0 |
| errno = 0; |
| 436 |
0 |
| long work = strtol(entry->data, NULL, 10); |
| 437 |
0 |
▶ | if (errno) |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno) |
0 |
0 |
0 |
|
| 438 |
0 |
| return 0; |
| 439 |
|
| #if INT_MAX != LONG_MAX |
| 440 |
0 |
▶ | if ((int)work != work) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((int)work != work) { |
0 |
0 |
0 |
|
| 441 |
0 |
| errno = ERANGE; |
| 442 |
0 |
| return 0; |
| 443 |
|
| } |
| 444 |
|
| #endif |
| 445 |
0 |
| *value = (int)work; |
| 446 |
0 |
| errno = save_errno; |
| 447 |
|
| } |
| 448 |
|
| else |
| 449 |
38 |
| *value = entry->idata; |
| 450 |
|
| |
| 451 |
38 |
| return 1; |
| 452 |
|
| } |
| 453 |
|
| |
| 454 |
12 |
| static int parse_long(char *data, char **end, long *out) { |
| 455 |
|
| long result; |
| 456 |
12 |
| int savederr = errno; |
| 457 |
|
| char *myend; |
| 458 |
|
| |
| 459 |
12 |
| errno = 0; |
| 460 |
12 |
| result = strtol(data, &myend, 10); |
| 461 |
12 |
▶ | if (((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE) |
Branch: 3 branches
| Branch | True | False | Total |
| if (((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE) |
12 |
0 |
12 |
| if (((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE) |
0 |
12 |
12 |
| if (((result == LONG_MIN || result == LONG_MAX) && errno == ERANGE) |
0 |
0 |
0 |
|
| 462 |
12 |
▶ | || myend == data) { |
Branch: 1 branch
| Branch | True | False | Total |
| || myend == data) { |
0 |
12 |
12 |
|
| 463 |
0 |
| errno = savederr; |
| 464 |
0 |
| return 0; |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
12 |
| errno = savederr; |
| 468 |
12 |
| *out = result; |
| 469 |
12 |
| *end = myend; |
| 470 |
|
| |
| 471 |
12 |
| return 1; |
| 472 |
|
| } |
| 473 |
|
| |
| 474 |
|
| /* parse a comma-separated list of integers |
| 475 |
|
| returns when it has maxcount numbers, finds a non-comma after a number |
| 476 |
|
| or can't parse a number |
| 477 |
|
| if it can't parse a number after a comma, that's considered an error |
| 478 |
|
| */ |
| 479 |
3 |
| static int parse_long_list(char *data, char **end, int maxcount, long *out) { |
| 480 |
|
| int i; |
| 481 |
|
| |
| 482 |
3 |
| i = 0; |
| 483 |
12 |
▶ | while (i < maxcount-1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < maxcount-1) { |
9 |
3 |
12 |
|
| 484 |
9 |
▶ | if (!parse_long(data, &data, out)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_long(data, &data, out)) |
0 |
9 |
9 |
|
| 485 |
0 |
| return 0; |
| 486 |
9 |
| out++; |
| 487 |
9 |
| i++; |
| 488 |
9 |
▶ | if (*data != ',') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*data != ',') |
0 |
9 |
9 |
|
| 489 |
0 |
| return i; |
| 490 |
9 |
| ++data; |
| 491 |
|
| } |
| 492 |
3 |
▶ | if (!parse_long(data, &data, out)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_long(data, &data, out)) |
0 |
3 |
3 |
|
| 493 |
0 |
| return 0; |
| 494 |
3 |
| ++i; |
| 495 |
3 |
| *end = data; |
| 496 |
3 |
| return i; |
| 497 |
|
| } |
| 498 |
|
| |
| 499 |
|
| /* parse "color(red,green,blue,alpha)" */ |
| 500 |
3 |
| static int parse_color(char *data, char **end, i_color *value) { |
| 501 |
|
| long n[4]; |
| 502 |
|
| int count, i; |
| 503 |
|
| |
| 504 |
3 |
▶ | if (memcmp(data, "color(", 6)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(data, "color(", 6)) |
0 |
3 |
3 |
|
| 505 |
0 |
| return 0; /* not a color */ |
| 506 |
3 |
| data += 6; |
| 507 |
3 |
| count = parse_long_list(data, &data, 4, n); |
| 508 |
3 |
▶ | if (count < 3) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count < 3) |
0 |
3 |
3 |
|
| 509 |
0 |
| return 0; |
| 510 |
15 |
▶ | for (i = 0; i < count; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; ++i) |
12 |
3 |
15 |
|
| 511 |
12 |
| value->channel[i] = n[i]; |
| 512 |
3 |
▶ | if (count < 4) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count < 4) |
0 |
3 |
3 |
|
| 513 |
0 |
| value->channel[3] = 255; |
| 514 |
3 |
| *end = data; |
| 515 |
|
| |
| 516 |
3 |
| return 1; |
| 517 |
|
| } |
| 518 |
|
| |
| 519 |
|
| /* |
| 520 |
|
| =item i_tags_get_color(tags, name, code, &value) |
| 521 |
|
| |
| 522 |
|
| =category Tags |
| 523 |
|
| |
| 524 |
|
| Retrieve a tag specified by name or code as color. |
| 525 |
|
| |
| 526 |
|
| On success sets the i_color *I<value> to the color and returns true. |
| 527 |
|
| |
| 528 |
|
| On failure returns false. |
| 529 |
|
| |
| 530 |
|
| =cut |
| 531 |
|
| */ |
| 532 |
|
| |
| 533 |
49 |
| int i_tags_get_color(i_img_tags *tags, char const *name, int code, |
| 534 |
|
| i_color *value) { |
| 535 |
|
| int index; |
| 536 |
|
| i_img_tag *entry; |
| 537 |
|
| char *end; |
| 538 |
|
| |
| 539 |
49 |
▶ | if (name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) { |
49 |
0 |
49 |
|
| 540 |
49 |
▶ | if (!i_tags_find(tags, name, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_find(tags, name, 0, &index)) |
46 |
3 |
49 |
|
| 541 |
46 |
| return 0; |
| 542 |
|
| } |
| 543 |
|
| else { |
| 544 |
0 |
▶ | if (!i_tags_findn(tags, code, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_findn(tags, code, 0, &index)) |
0 |
0 |
0 |
|
| 545 |
0 |
| return 0; |
| 546 |
|
| } |
| 547 |
3 |
| entry = tags->tags+index; |
| 548 |
3 |
▶ | if (!entry->data) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!entry->data) |
0 |
3 |
3 |
|
| 549 |
0 |
| return 0; |
| 550 |
|
| |
| 551 |
3 |
▶ | if (!parse_color(entry->data, &end, value)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_color(entry->data, &end, value)) |
0 |
3 |
3 |
|
| 552 |
0 |
| return 0; |
| 553 |
|
| |
| 554 |
|
| /* for now we're sloppy about the end */ |
| 555 |
|
| |
| 556 |
3 |
| return 1; |
| 557 |
|
| } |
| 558 |
|
| |
| 559 |
|
| /* |
| 560 |
|
| =item i_tags_set_color(tags, name, code, &value) |
| 561 |
|
| |
| 562 |
|
| =category Tags |
| 563 |
|
| |
| 564 |
|
| Stores the given color as a tag with the given name and code. |
| 565 |
|
| |
| 566 |
|
| =cut |
| 567 |
|
| */ |
| 568 |
|
| |
| 569 |
0 |
| int i_tags_set_color(i_img_tags *tags, char const *name, int code, |
| 570 |
|
| i_color const *value) { |
| 571 |
|
| char temp[80]; |
| 572 |
|
| |
| 573 |
0 |
| sprintf(temp, "color(%d,%d,%d,%d)", value->channel[0], value->channel[1], |
| 574 |
0 |
| value->channel[2], value->channel[3]); |
| 575 |
0 |
▶ | if (name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) |
0 |
0 |
0 |
|
| 576 |
0 |
| i_tags_delbyname(tags, name); |
| 577 |
|
| else |
| 578 |
0 |
| i_tags_delbycode(tags, code); |
| 579 |
|
| |
| 580 |
0 |
| return i_tags_add(tags, name, code, temp, strlen(temp), 0); |
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
|
| /* |
| 584 |
|
| =item i_tags_get_string(tags, name, code, value, value_size) |
| 585 |
|
| |
| 586 |
|
| =category Tags |
| 587 |
|
| |
| 588 |
|
| Retrieves a tag by name or code as a string. |
| 589 |
|
| |
| 590 |
|
| On success copies the string to value for a max of value_size and |
| 591 |
|
| returns true. |
| 592 |
|
| |
| 593 |
|
| On failure returns false. |
| 594 |
|
| |
| 595 |
|
| value_size must be at least large enough for a string representation |
| 596 |
|
| of an integer. |
| 597 |
|
| |
| 598 |
|
| The copied value is always C<NUL> terminated. |
| 599 |
|
| |
| 600 |
|
| =cut |
| 601 |
|
| */ |
| 602 |
|
| |
| 603 |
21 |
| int i_tags_get_string(i_img_tags *tags, char const *name, int code, |
| 604 |
|
| char *value, size_t value_size) { |
| 605 |
|
| int index; |
| 606 |
|
| i_img_tag *entry; |
| 607 |
|
| |
| 608 |
21 |
▶ | if (name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) { |
20 |
1 |
21 |
|
| 609 |
20 |
▶ | if (!i_tags_find(tags, name, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_find(tags, name, 0, &index)) |
17 |
3 |
20 |
|
| 610 |
17 |
| return 0; |
| 611 |
|
| } |
| 612 |
|
| else { |
| 613 |
1 |
▶ | if (!i_tags_findn(tags, code, 0, &index)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_findn(tags, code, 0, &index)) |
0 |
1 |
1 |
|
| 614 |
0 |
| return 0; |
| 615 |
|
| } |
| 616 |
4 |
| entry = tags->tags+index; |
| 617 |
4 |
▶ | if (entry->data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->data) { |
3 |
1 |
4 |
|
| 618 |
3 |
| size_t cpsize = value_size < (size_t)entry->size |
| 619 |
|
| ? value_size : (size_t)entry->size; |
| 620 |
3 |
| memcpy(value, entry->data, cpsize); |
| 621 |
3 |
▶ | if (cpsize == value_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cpsize == value_size) |
0 |
3 |
3 |
|
| 622 |
0 |
| --cpsize; |
| 623 |
3 |
| value[cpsize] = '\0'; |
| 624 |
|
| } |
| 625 |
|
| else { |
| 626 |
1 |
| sprintf(value, "%d", entry->idata); |
| 627 |
|
| } |
| 628 |
|
| |
| 629 |
4 |
| return 1; |
| 630 |
|
| } |
| 631 |
|
| |
| 632 |
|
| /* |
| 633 |
|
| =item i_tags_set(tags, name, data, size) |
| 634 |
|
| =synopsis i_tags_set(&img->tags, "i_comment", -1); |
| 635 |
|
| =category Tags |
| 636 |
|
| |
| 637 |
|
| Sets the given tag to the string I<data> |
| 638 |
|
| |
| 639 |
|
| If size is -1 then the strlen(I<data>) bytes are stored. |
| 640 |
|
| |
| 641 |
|
| Even on failure, if an existing tag I<name> exists, it will be |
| 642 |
|
| removed. |
| 643 |
|
| |
| 644 |
|
| =cut |
| 645 |
|
| */ |
| 646 |
|
| |
| 647 |
|
| int |
| 648 |
104 |
| i_tags_set(i_img_tags *tags, char const *name, char const *data, int size) { |
| 649 |
104 |
| i_tags_delbyname(tags, name); |
| 650 |
|
| |
| 651 |
104 |
| return i_tags_add(tags, name, 0, data, size, 0); |
| 652 |
|
| } |
| 653 |
|
| |
| 654 |
|
| /* |
| 655 |
|
| =item i_tags_setn(C<tags>, C<name>, C<idata>) |
| 656 |
|
| =synopsis i_tags_setn(&img->tags, "i_xres", 204); |
| 657 |
|
| =synopsis i_tags_setn(&img->tags, "i_yres", 196); |
| 658 |
|
| =category Tags |
| 659 |
|
| |
| 660 |
|
| Sets the given tag to the integer C<idata> |
| 661 |
|
| |
| 662 |
|
| Even on failure, if an existing tag C<name> exists, it will be |
| 663 |
|
| removed. |
| 664 |
|
| |
| 665 |
|
| =cut |
| 666 |
|
| */ |
| 667 |
|
| |
| 668 |
|
| int |
| 669 |
307 |
| i_tags_setn(i_img_tags *tags, char const *name, int idata) { |
| 670 |
307 |
| i_tags_delbyname(tags, name); |
| 671 |
|
| |
| 672 |
307 |
| return i_tags_addn(tags, name, 0, idata); |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
0 |
| void i_tags_print(i_img_tags *tags) { |
| 676 |
|
| int i; |
| 677 |
0 |
| printf("Alloc %d\n", tags->alloc); |
| 678 |
0 |
| printf("Count %d\n", tags->count); |
| 679 |
0 |
▶ | for (i = 0; i < tags->count; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tags->count; ++i) { |
0 |
0 |
0 |
|
| 680 |
0 |
| i_img_tag *tag = tags->tags + i; |
| 681 |
0 |
| printf("Tag %d\n", i); |
| 682 |
0 |
▶ | if (tag->name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tag->name) |
0 |
0 |
0 |
|
| 683 |
0 |
| printf(" Name : %s (%p)\n", tag->name, tag->name); |
| 684 |
0 |
| printf(" Code : %d\n", tag->code); |
| 685 |
0 |
▶ | if (tag->data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tag->data) { |
0 |
0 |
0 |
|
| 686 |
|
| int pos; |
| 687 |
0 |
| printf(" Data : %d (%p) => '", tag->size, tag->data); |
| 688 |
0 |
▶ | for (pos = 0; pos < tag->size; ++pos) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pos = 0; pos < tag->size; ++pos) { |
0 |
0 |
0 |
|
| 689 |
0 |
▶ | if (tag->data[pos] == '\\' || tag->data[pos] == '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tag->data[pos] == '\\' || tag->data[pos] == '\'') { |
0 |
0 |
0 |
| if (tag->data[pos] == '\\' || tag->data[pos] == '\'') { |
0 |
0 |
0 |
|
| 690 |
0 |
| putchar('\\'); |
| 691 |
0 |
| putchar(tag->data[pos]); |
| 692 |
|
| } |
| 693 |
0 |
▶ | else if (tag->data[pos] < ' ' || tag->data[pos] >= '\x7E') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (tag->data[pos] < ' ' || tag->data[pos] >= '\x7E') |
0 |
0 |
0 |
| else if (tag->data[pos] < ' ' || tag->data[pos] >= '\x7E') |
0 |
0 |
0 |
|
| 694 |
0 |
| printf("\\x%02X", tag->data[pos]); |
| 695 |
|
| else |
| 696 |
0 |
| putchar(tag->data[pos]); |
| 697 |
|
| } |
| 698 |
0 |
| printf("'\n"); |
| 699 |
0 |
| printf(" Idata: %d\n", tag->idata); |
| 700 |
|
| } |
| 701 |
|
| } |
| 702 |
0 |
| } |
| 703 |
|
| |
| 704 |
|
| /* |
| 705 |
|
| =back |
| 706 |
|
| |
| 707 |
|
| =head1 AUTHOR |
| 708 |
|
| |
| 709 |
|
| Tony Cook <tony@develop-help.com> |
| 710 |
|
| |
| 711 |
|
| =head1 SEE ALSO |
| 712 |
|
| |
| 713 |
|
| Imager(3) |
| 714 |
|
| |
| 715 |
|
| =cut |
| 716 |
|
| */ |