~ubuntu-branches/debian/sid/vorbis-tools/sid

« back to all changes in this revision

Viewing changes to vorbiscomment/vcomment.c

  • Committer: Bazaar Package Importer
  • Author(s): Jesus Climent
  • Date: 2005-04-10 09:22:24 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050410092224-xtukpa3qghghhjje
Tags: 1.0.1-1.3
* Authorized NMU.
* Modified alsa to mention alsa09 (although the device might be nowadays
  alsa, back, since alsa1.0 has been already released). (Closes: #258286)
* Modified the manpage/help message for vorbiscomment to make it a bit more
  userfiendly: Closes: #252531.
* Added oggdec to the long description field, so that it triggers apt-cache
  searches: Closes: #274894.
* Typos in manpages: Closes: #302150.
* Escaped dashes in manpage: Closes: #264365.
* Quiet option is actually with -Q, not -q (Closes: #211289) Reported
  upstream but patched for Debian.
* Change input.wav with inputfile, since we accept flac-formated files:
  Closes: #262509.
* Translation bits:
  * Updated translation hu.po: Closes: #272037.
  * French translation correction: Encodage -> Codage (Closes: #248431).
  * debian/rules: remove .gmo's to avoid clash with uploaded tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* This program is licensed under the GNU General Public License, 
2
2
 * version 2, a copy of which is included with this program.
3
3
 *
4
 
 * (c) 2000-2001 Michael Smith <msmith@labyrinth.net.au>
 
4
 * (c) 2000-2002 Michael Smith <msmith@xiph.org>
5
5
 * (c) 2001 Ralph Giles <giles@ashlu.bc.ca>
6
6
 *
7
7
 * Front end to show how to use vcedit;
15
15
#include <locale.h>
16
16
#include "getopt.h"
17
17
#include "utf8.h"
 
18
#include "i18n.h"
18
19
 
19
20
#include "vcedit.h"
20
21
 
 
22
 
21
23
/* getopt format struct */
22
24
struct option long_options[] = {
23
25
        {"list",0,0,'l'},
24
26
        {"write",0,0,'w'},
25
27
        {"help",0,0,'h'},
26
28
        {"quiet",0,0,'q'},
 
29
    {"version", 0, 0, 'V'},
27
30
        {"commentfile",1,0,'c'},
 
31
    {"raw", 0,0,'R'},
28
32
        {NULL,0,0,0}
29
33
};
30
34
 
37
41
        int commentcount;
38
42
        char **comments;
39
43
        int tempoutfile;
 
44
    int raw;
40
45
} param_t;
41
46
 
42
47
#define MODE_NONE  0
46
51
 
47
52
/* prototypes */
48
53
void usage(void);
49
 
void print_comments(FILE *out, vorbis_comment *vc);
50
 
int  add_comment(char *line, vorbis_comment *vc);
 
54
void print_comments(FILE *out, vorbis_comment *vc, int raw);
 
55
int  add_comment(char *line, vorbis_comment *vc, int raw);
51
56
 
52
57
param_t *new_param(void);
 
58
void free_param(param_t *param);
53
59
void parse_options(int argc, char *argv[], param_t *param);
54
60
void open_files(param_t *p);
55
61
void close_files(param_t *p);
74
80
        param_t *param;
75
81
        int i;
76
82
 
 
83
        setlocale(LC_ALL, "");
 
84
        bindtextdomain(PACKAGE, LOCALEDIR);
 
85
        textdomain(PACKAGE);
 
86
 
77
87
        /* initialize the cmdline interface */
78
88
        param = new_param();
79
89
        parse_options(argc, argv, param);
90
100
 
91
101
                if(vcedit_open(state, param->in) < 0)
92
102
                {
93
 
                        fprintf(stderr, "Failed to open file as vorbis: %s\n", 
 
103
                        fprintf(stderr, _("Failed to open file as vorbis: %s\n"), 
94
104
                                        vcedit_error(state));
 
105
            close_files(param);
 
106
            free_param(param);
 
107
            vcedit_clear(state);
95
108
                        return 1;
96
109
                }
97
110
 
98
111
                /* extract and display the comments */
99
112
                vc = vcedit_comments(state);
100
 
                print_comments(param->com, vc);
 
113
                print_comments(param->com, vc, param->raw);
101
114
 
102
115
                /* done */
103
116
                vcedit_clear(state);
104
117
 
105
118
                close_files(param);
 
119
        free_param(param);
106
120
                return 0;               
107
121
        }
108
122
 
112
126
 
113
127
                if(vcedit_open(state, param->in) < 0)
114
128
                {
115
 
                        fprintf(stderr, "Failed to open file as vorbis: %s\n", 
 
129
                        fprintf(stderr, _("Failed to open file as vorbis: %s\n"), 
116
130
                                        vcedit_error(state));
 
131
            close_files(param);
 
132
            free_param(param);
 
133
            vcedit_clear(state);
117
134
                        return 1;
118
135
                }
119
136
 
127
144
 
128
145
                for(i=0; i < param->commentcount; i++)
129
146
                {
130
 
                        if(add_comment(param->comments[i], vc) < 0)
131
 
                                fprintf(stderr, "Bad comment: \"%s\"\n", param->comments[i]);
 
147
                        if(add_comment(param->comments[i], vc, param->raw) < 0)
 
148
                                fprintf(stderr, _("Bad comment: \"%s\"\n"), param->comments[i]);
132
149
                }
133
150
 
134
151
                /* build the replacement structure */
138
155
                        char *buf = (char *)malloc(sizeof(char)*1024);
139
156
 
140
157
                        while (fgets(buf, 1024, param->com))
141
 
                                if (add_comment(buf, vc) < 0) {
 
158
                                if (add_comment(buf, vc, param->raw) < 0) {
142
159
                                        fprintf(stderr,
143
 
                                                "bad comment: \"%s\"\n",
 
160
                                                _("bad comment: \"%s\"\n"),
144
161
                                                buf);
145
162
                                }
146
163
                        
150
167
                /* write out the modified stream */
151
168
                if(vcedit_write(state, param->out) < 0)
152
169
                {
153
 
                        fprintf(stderr, "Failed to write comments to output file: %s\n", 
 
170
                        fprintf(stderr, _("Failed to write comments to output file: %s\n"), 
154
171
                                        vcedit_error(state));
 
172
            close_files(param);
 
173
            free_param(param);
 
174
            vcedit_clear(state);
155
175
                        return 1;
156
176
                }
157
177
 
159
179
                vcedit_clear(state);
160
180
                
161
181
                close_files(param);
 
182
        free_param(param);
162
183
                return 0;
163
184
        }
164
185
 
165
186
        /* should never reach this point */
166
 
        fprintf(stderr, "no action specified\n");
 
187
        fprintf(stderr, _("no action specified\n"));
 
188
    free_param(param);
167
189
        return 1;
168
190
}
169
191
 
176
198
 
177
199
***********/
178
200
 
179
 
void print_comments(FILE *out, vorbis_comment *vc)
 
201
void print_comments(FILE *out, vorbis_comment *vc, int raw)
180
202
{
181
203
        int i;
182
204
    char *decoded_value;
183
205
 
184
206
        for (i = 0; i < vc->comments; i++)
185
207
    {
186
 
            if (utf8_decode(vc->user_comments[i], &decoded_value) >= 0)
 
208
            if (!raw && utf8_decode(vc->user_comments[i], &decoded_value) >= 0)
187
209
        {
188
210
                fprintf(out, "%s\n", decoded_value);
189
211
            free(decoded_value);
204
226
 
205
227
***********/
206
228
 
207
 
int  add_comment(char *line, vorbis_comment *vc)
 
229
int  add_comment(char *line, vorbis_comment *vc, int raw)
208
230
{
209
231
        char    *mark, *value, *utf8_value;
210
232
 
232
254
        *mark = '\0';   
233
255
        value++;
234
256
 
 
257
    if(raw) {
 
258
        vorbis_comment_add_tag(vc, line, value);
 
259
        return 0;
 
260
    }
235
261
        /* convert the value from the native charset to UTF-8 */
236
 
        if (utf8_encode(value, &utf8_value) >= 0) {
 
262
    else if (utf8_encode(value, &utf8_value) >= 0) {
237
263
                
238
264
                /* append the comment and return */
239
265
                vorbis_comment_add_tag(vc, line, utf8_value);
240
266
        free(utf8_value);
241
267
                return 0;
242
268
        } else {
243
 
                fprintf(stderr, "Couldn't convert comment to UTF8, "
244
 
                        "cannot add\n");
 
269
                fprintf(stderr, _("Couldn't convert comment to UTF8, "
 
270
                        "cannot add\n"));
245
271
                return -1;
246
272
        }
247
273
}
258
284
void usage(void)
259
285
{
260
286
        fprintf(stderr, 
261
 
                "Usage: \n"
262
 
                "  vorbiscomment [-l] file.ogg (to list the comments)\n"
263
 
                "  vorbiscomment -a in.ogg out.ogg (to append comments)\n"
264
 
                "  vorbiscomment -w in.ogg out.ogg (to modify comments)\n"
265
 
                "       in the write case, a new set of comments in the form\n"
266
 
                "       'TAG=value' is expected on stdin. This set will\n"
267
 
                "       completely replace the existing set.\n"
268
 
                "   Either of -a and -w can take only a single filename,\n"
269
 
                "   in which case a temporary file will be used.\n"
270
 
                "   -c can be used to take comments from a specified file\n"
271
 
                "   instead of stdin.\n"
272
 
                "   Example: vorbiscomment -a in.ogg -c comments.txt\n"
273
 
                "   will append the comments in comments.txt to in.ogg\n"
274
 
                "   Finally, you may specify any number of tags to add on\n"
275
 
                "   the command line using the -t option. e.g.\n"
 
287
                _("Usage: \n"
 
288
                "  vorbiscomment [-Vh]\n" 
 
289
                "  vorbiscomment [-lR] file\n"
 
290
                "  vorbiscomment [-qR] [-c file] [-t tag] <-a|-w> inputfile [outputfile]\n"
 
291
                "\n"
 
292
                "    -l           list the comments\n"
 
293
                "    -a           append comments\n"
 
294
                "    -w           write comments, replacing the existing ones\n"
 
295
                "    -c <file>    read the comments from the specified file\n"
 
296
                "    -q           quiet mode\n"
 
297
                "    -V           version\n"
 
298
                "    -h           help\n"
 
299
                "    -R, --raw    read and write comments in utf8\n"
 
300
                "\n"
 
301
                "  If no OUTPUTFILE is specified, vorbiscomment will use a temporary file\n"
 
302
                "\n"
 
303
                "  vorbiscomment reads comments from:\n"
 
304
                "   - stdin in the form of 'TAG=value'\n"
 
305
                "   - a specified file instead of stdin, using '-c <FILE>'\n"
 
306
                "   - the command line, using '-t <TAG>' (disables reading from stdin)\n"
 
307
                "\n"
 
308
                "  Examples:\n"
 
309
                "   vorbiscomment -a in.ogg -c comments.txt\n"
276
310
                "   vorbiscomment -a in.ogg -t \"ARTIST=Some Guy\" -t \"TITLE=A Title\"\n"
277
 
                "   (note that when using this, reading comments from the comment\n"
278
 
                "   file or stdin is disabled)\n"
 
311
                "\n"
 
312
                "  NOTE: Raw mode (--raw, -R) will read and write comments in utf8,\n"
 
313
                "  rather than converting to the user's character set. This is\n"
 
314
                "  useful for using vorbiscomment in scripts. However, this is\n"
 
315
                "  not sufficient for general round-tripping of comments in all\n"
 
316
                "  cases.\n")
 
317
        
279
318
        ); 
280
319
}
281
320
 
 
321
void free_param(param_t *param) {
 
322
    free(param->infilename);
 
323
    free(param->outfilename);
 
324
    free(param);
 
325
}
282
326
 
283
327
/**********
284
328
 
306
350
        param->comments=NULL;
307
351
        param->tempoutfile=0;
308
352
 
 
353
    param->raw = 0;
 
354
 
309
355
        return param;
310
356
}
311
357
 
325
371
 
326
372
        setlocale(LC_ALL, "");
327
373
 
328
 
        while ((ret = getopt_long(argc, argv, "alwhqc:t:",
 
374
        while ((ret = getopt_long(argc, argv, "alwhqVc:t:R",
329
375
                        long_options, &option_index)) != -1) {
330
376
                switch (ret) {
331
377
                        case 0:
332
 
                                fprintf(stderr, "Internal error parsing command options\n");
 
378
                                fprintf(stderr, _("Internal error parsing command options\n"));
333
379
                                exit(1);
334
380
                                break;
335
381
                        case 'l':
336
382
                                param->mode = MODE_LIST;
337
383
                                break;
 
384
            case 'R':
 
385
                param->raw = 1;
 
386
                break;
338
387
                        case 'w':
339
388
                                param->mode = MODE_WRITE;
340
389
                                break;
341
390
                        case 'a':
342
391
                                param->mode = MODE_APPEND;
343
392
                                break;
 
393
            case 'V':
 
394
                fprintf(stderr, "Vorbiscomment " VERSION "\n");
 
395
                exit(0);
 
396
                break;
344
397
                        case 'h':
345
398
                                usage();
346
399
                                exit(0);
408
461
        }
409
462
        if (p->in == NULL) {
410
463
                fprintf(stderr,
411
 
                        "Error opening input file '%s'.\n",
 
464
                        _("Error opening input file '%s'.\n"),
412
465
                        p->infilename);
413
466
                exit(1);
414
467
        }
416
469
        if (p->mode == MODE_WRITE || p->mode == MODE_APPEND) { 
417
470
 
418
471
                /* open output for write mode */
 
472
        if(!strcmp(p->infilename, p->outfilename)) {
 
473
            fprintf(stderr, _("Input filename may not be the same as output filename\n"));
 
474
            exit(1);
 
475
        }
419
476
 
420
477
                if (strncmp(p->outfilename,"-",2) == 0) {
421
478
                        p->out = stdout;
424
481
                }
425
482
                if(p->out == NULL) {
426
483
                        fprintf(stderr,
427
 
                                "Error opening output file '%s'.\n",
 
484
                                _("Error opening output file '%s'.\n"),
428
485
                                p->outfilename);
429
486
                        exit(1);
430
487
                }
435
492
                                (strncmp(p->commentfilename,"-",2) == 0)) {
436
493
                        p->com = stdin;
437
494
                } else {
438
 
                        p->com = fopen(p->commentfilename, "rb");
 
495
                        p->com = fopen(p->commentfilename, "r");
439
496
                }
440
497
                if (p->com == NULL) {
441
498
                        fprintf(stderr,
442
 
                                "Error opening comment file '%s'.\n",
 
499
                                _("Error opening comment file '%s'.\n"),
443
500
                                p->commentfilename);
444
501
                        exit(1);
445
502
                }
452
509
                                (strncmp(p->commentfilename,"-",2) == 0)) {
453
510
                        p->com = stdout;
454
511
                } else {
455
 
                        p->com = fopen(p->commentfilename, "wb");
 
512
                        p->com = fopen(p->commentfilename, "w");
456
513
                }
457
514
                if (p->com == NULL) {
458
515
                        fprintf(stderr,
459
 
                                "Error opening comment file '%s'\n",
 
516
                                _("Error opening comment file '%s'\n"),
460
517
                                p->commentfilename);
461
518
                        exit(1);
462
519
                }
478
535
        if (p->out != NULL && p->out != stdout) fclose(p->out);
479
536
        if (p->com != NULL && p->com != stdout && p->com != stdin) fclose(p->com);
480
537
 
481
 
        if(p->tempoutfile)
482
 
                rename(p->outfilename, p->infilename);
 
538
        if(p->tempoutfile) {
 
539
        /* Some platforms fail to rename a file if the new name already exists,
 
540
         * so we need to remove, then rename. How stupid.
 
541
         */
 
542
                if(rename(p->outfilename, p->infilename)) {
 
543
            if(remove(p->infilename))
 
544
                fprintf(stderr, _("Error removing old file %s\n"), p->infilename);
 
545
            else if(rename(p->outfilename, p->infilename)) 
 
546
                fprintf(stderr, _("Error renaming %s to %s\n"), p->outfilename, 
 
547
                        p->infilename);
 
548
        }
 
549
    }
483
550
}
 
551