Josh Coalson | b02574e | 2006-09-26 00:43:48 +0000 | [diff] [blame] | 1 | /* metaflac - Command-line FLAC metadata editor |
| 2 | * Copyright (C) 2001,2002,2003,2004,2005,2006 Josh Coalson |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #if HAVE_CONFIG_H |
| 20 | # include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <errno.h> |
Josh Coalson | b02574e | 2006-09-26 00:43:48 +0000 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include "options.h" |
| 26 | #include "utils.h" |
| 27 | #include "FLAC/assert.h" |
| 28 | #include "share/grabbag.h" /* for grabbag__picture_parse_specification */ |
| 29 | |
| 30 | static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write); |
| 31 | |
| 32 | FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write) |
| 33 | { |
| 34 | FLAC__bool ok = true, has_type1 = false, has_type2 = false; |
| 35 | FLAC__StreamMetadata *picture = 0; |
| 36 | FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); |
| 37 | |
| 38 | if(0 == iterator) |
| 39 | die("out of memory allocating iterator"); |
| 40 | |
| 41 | FLAC__metadata_iterator_init(iterator, chain); |
| 42 | |
| 43 | switch(operation->type) { |
| 44 | case OP__IMPORT_PICTURE: |
| 45 | ok = import_pic_from(filename, &picture, operation->argument.specification.value, needs_write); |
| 46 | if(ok) { |
| 47 | /* append PICTURE block */ |
| 48 | while(FLAC__metadata_iterator_next(iterator)) |
| 49 | ; |
| 50 | if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) { |
| 51 | print_error_with_chain_status(chain, "%s: ERROR: adding new PICTURE block to metadata", filename); |
| 52 | FLAC__metadata_object_delete(picture); |
| 53 | ok = false; |
| 54 | } |
| 55 | } |
| 56 | break; |
| 57 | default: |
| 58 | ok = false; |
| 59 | FLAC__ASSERT(0); |
| 60 | break; |
| 61 | }; |
| 62 | |
| 63 | /* check global PICTURE constraints (max 1 block each of type=1 and type=2) */ |
| 64 | while(FLAC__metadata_iterator_prev(iterator)) |
| 65 | ; |
| 66 | do { |
| 67 | FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator); |
| 68 | if(block->type == FLAC__METADATA_TYPE_PICTURE) { |
| 69 | if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) { |
| 70 | if(has_type1) { |
| 71 | print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename); |
| 72 | ok = false; |
| 73 | } |
| 74 | has_type1 = true; |
| 75 | } |
| 76 | else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) { |
| 77 | if(has_type2) { |
| 78 | print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename); |
| 79 | ok = false; |
| 80 | } |
| 81 | has_type2 = true; |
| 82 | } |
| 83 | } |
| 84 | } while(FLAC__metadata_iterator_next(iterator)); |
| 85 | |
| 86 | |
| 87 | FLAC__metadata_iterator_delete(iterator); |
| 88 | return ok; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * local routines |
| 93 | */ |
| 94 | |
| 95 | FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write) |
| 96 | { |
| 97 | const char *error_message; |
| 98 | |
| 99 | if(0 == specification || strlen(specification) == 0) { |
| 100 | fprintf(stderr, "%s: ERROR: empty picture specification\n", filename); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | *picture = grabbag__picture_parse_specification(specification, &error_message); |
| 105 | |
| 106 | if(0 == *picture) { |
| 107 | fprintf(stderr, "%s: ERROR: while parsing picture specification \"%s\": %s\n", filename, specification, error_message); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if(!FLAC__format_picture_is_legal(&(*picture)->data.picture, &error_message)) { |
| 112 | fprintf(stderr, "%s: ERROR: new PICTURE block for \"%s\" is illegal: %s\n", filename, specification, error_message); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | *needs_write = true; |
| 117 | return true; |
| 118 | } |