~ubuntu-branches/ubuntu/raring/flac/raring

« back to all changes in this revision

Viewing changes to src/metaflac/operations_shorthand_picture.c

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Kwan
  • Date: 2007-05-29 22:56:36 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529225636-ljeff8xxip09qaap
Tags: 1.1.4-1
* New upstream release. closes: #405167, #411311
  - libOggFLAC and libOggFLAC++ have been merged into libFLAC, so
    remove their corresponding packages.
  - Because of the API changes required to effect the above, there has
    been yet another soname bump. libflac7 -> libflac8 and
    libflac++5 -> libflac++6. Emails have been dispatched to the
    maintainers of dependent packages.
* Some notes on patches that were removed:
  - 02_stdin_stdout, 06_manpage_mention_utf8_convert: merged upstream
  - 08_manpage_warnings: Upstream has changed the manpage so it defintely
    can't fit in in 80 cols, so just forget about it. We'll live.
  - 05_eof_warnings_are_errors: Upstream decided to add a -w option to
    flac to treat all warnings as errors. I am going to defer to that
    for now, but if people think it's stupid let me know and I'll port
    the patch forward.
  - 04_stack_smasher: was a backport from 1.1.3, so it's obsolete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* metaflac - Command-line FLAC metadata editor
 
2
 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007  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>
 
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() etc */
 
29
 
 
30
static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write);
 
31
static FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename);
 
32
 
 
33
FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
 
34
{
 
35
        FLAC__bool ok = true, has_type1 = false, has_type2 = false;
 
36
        FLAC__StreamMetadata *picture = 0;
 
37
        FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
 
38
 
 
39
        if(0 == iterator)
 
40
                die("out of memory allocating iterator");
 
41
 
 
42
        FLAC__metadata_iterator_init(iterator, chain);
 
43
 
 
44
        switch(operation->type) {
 
45
                case OP__IMPORT_PICTURE_FROM:
 
46
                        ok = import_pic_from(filename, &picture, operation->argument.specification.value, needs_write);
 
47
                        if(ok) {
 
48
                                /* append PICTURE block */
 
49
                                while(FLAC__metadata_iterator_next(iterator))
 
50
                                        ;
 
51
                                if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) {
 
52
                                        print_error_with_chain_status(chain, "%s: ERROR: adding new PICTURE block to metadata", filename);
 
53
                                        FLAC__metadata_object_delete(picture);
 
54
                                        ok = false;
 
55
                                }
 
56
                        }
 
57
                        if(ok) {
 
58
                                /* check global PICTURE constraints (max 1 block each of type=1 and type=2) */
 
59
                                while(FLAC__metadata_iterator_prev(iterator))
 
60
                                        ;
 
61
                                do {
 
62
                                        FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
 
63
                                        if(block->type == FLAC__METADATA_TYPE_PICTURE) {
 
64
                                                if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
 
65
                                                        if(has_type1) {
 
66
                                                                print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename);
 
67
                                                                ok = false;
 
68
                                                        }
 
69
                                                        has_type1 = true;
 
70
                                                }
 
71
                                                else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
 
72
                                                        if(has_type2) {
 
73
                                                                print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename);
 
74
                                                                ok = false;
 
75
                                                        }
 
76
                                                        has_type2 = true;
 
77
                                                }
 
78
                                        }
 
79
                                } while(FLAC__metadata_iterator_next(iterator));
 
80
                        }
 
81
                        break;
 
82
                case OP__EXPORT_PICTURE_TO:
 
83
                        {
 
84
                                const Argument_BlockNumber *a = operation->argument.export_picture_to.block_number_link;
 
85
                                int block_number = (a && a->num_entries > 0)? (int)a->entries[0] : -1;
 
86
                                unsigned i = 0;
 
87
                                do {
 
88
                                        FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
 
89
                                        if(block->type == FLAC__METADATA_TYPE_PICTURE && (block_number < 0 || i == (unsigned)block_number))
 
90
                                                picture = block;
 
91
                                        i++;
 
92
                                } while(FLAC__metadata_iterator_next(iterator) && 0 == picture);
 
93
                                if(0 == picture) {
 
94
                                        if(block_number < 0)
 
95
                                                fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block\n", filename);
 
96
                                        else
 
97
                                                fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block at block #%d\n", filename, block_number);
 
98
                                        ok = false;
 
99
                                }
 
100
                                else
 
101
                                        ok = export_pic_to(filename, picture, operation->argument.filename.value);
 
102
                        }
 
103
                        break;
 
104
                default:
 
105
                        ok = false;
 
106
                        FLAC__ASSERT(0);
 
107
                        break;
 
108
        };
 
109
 
 
110
        FLAC__metadata_iterator_delete(iterator);
 
111
        return ok;
 
112
}
 
113
 
 
114
/*
 
115
 * local routines
 
116
 */
 
117
 
 
118
FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write)
 
119
{
 
120
        const char *error_message;
 
121
 
 
122
        if(0 == specification || strlen(specification) == 0) {
 
123
                fprintf(stderr, "%s: ERROR: empty picture specification\n", filename);
 
124
                return false;
 
125
        }
 
126
 
 
127
        *picture = grabbag__picture_parse_specification(specification, &error_message);
 
128
 
 
129
        if(0 == *picture) {
 
130
                fprintf(stderr, "%s: ERROR: while parsing picture specification \"%s\": %s\n", filename, specification, error_message);
 
131
                return false;
 
132
        }
 
133
 
 
134
        if(!FLAC__format_picture_is_legal(&(*picture)->data.picture, &error_message)) {
 
135
                fprintf(stderr, "%s: ERROR: new PICTURE block for \"%s\" is illegal: %s\n", filename, specification, error_message);
 
136
                return false;
 
137
        }
 
138
 
 
139
        *needs_write = true;
 
140
        return true;
 
141
}
 
142
 
 
143
FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename)
 
144
{
 
145
        FILE *f;
 
146
        const FLAC__uint32 len = picture->data.picture.data_length;
 
147
 
 
148
        if(0 == pic_filename || strlen(pic_filename) == 0) {
 
149
                fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
 
150
                return false;
 
151
        }
 
152
        if(0 == strcmp(pic_filename, "-"))
 
153
                f = grabbag__file_get_binary_stdout();
 
154
        else
 
155
                f = fopen(pic_filename, "wb");
 
156
 
 
157
        if(0 == f) {
 
158
                fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, pic_filename, strerror(errno));
 
159
                return false;
 
160
        }
 
161
 
 
162
        if(fwrite(picture->data.picture.data, 1, len, f) != len) {
 
163
                fprintf(stderr, "%s: ERROR: writing PICTURE data to file\n", filename);
 
164
                return false;
 
165
        }
 
166
 
 
167
        if(f != stdout)
 
168
                fclose(f);
 
169
 
 
170
        return true;
 
171
}