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