|
| 1 | +#include <errno.h> |
| 2 | +#include <getopt.h> |
| 3 | +#include <limits.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include "picture.h" |
| 9 | +#include "version.h" |
| 10 | + |
| 11 | +struct option options[] = { |
| 12 | + {"help", no_argument, 0, 'h'}, |
| 13 | + {"desc", required_argument, 0, 'd'}, |
| 14 | + {"type", required_argument, 0, 't'}, |
| 15 | + {"output", required_argument, 0, 'o'}, |
| 16 | + {NULL, 0, 0, 0} |
| 17 | +}; |
| 18 | + |
| 19 | +int main(int argc, char **argv) |
| 20 | +{ |
| 21 | + if(argc == 1) |
| 22 | + { |
| 23 | + fprintf(stdout, "opuspic2tag version %s\n", OPUSCOMMENT_VERSION); |
| 24 | + fprintf(stdout, "Usage: %s [OPTIONS] FILE{.jpg,.png,.gif}\n", argv[0]); |
| 25 | + return EXIT_SUCCESS; |
| 26 | + } |
| 27 | + char *path_picture = NULL, *picture_data = NULL, *picture_desc = NULL, *path_out = NULL; |
| 28 | + const char *error_message; |
| 29 | + int print_help = 0; |
| 30 | + unsigned long picture_type = 3; |
| 31 | + int c; |
| 32 | + while((c = getopt_long(argc, argv, "hd:t:o:", options, NULL)) != -1) |
| 33 | + { |
| 34 | + switch(c){ |
| 35 | + case 'h': |
| 36 | + print_help = 1; |
| 37 | + break; |
| 38 | + case 'd': |
| 39 | + picture_desc = optarg; |
| 40 | + break; |
| 41 | + case 't': |
| 42 | + picture_type = atoi(optarg); |
| 43 | + picture_type = (picture_type > 20) ? 3 : picture_type; |
| 44 | + break; |
| 45 | + case 'o': |
| 46 | + path_out = optarg; |
| 47 | + break; |
| 48 | + default: |
| 49 | + return EXIT_FAILURE; |
| 50 | + } |
| 51 | + } |
| 52 | + if(print_help) |
| 53 | + { |
| 54 | + fprintf(stdout, "opuspic2tag version %s\n", OPUSCOMMENT_VERSION); |
| 55 | + fprintf(stdout, "Usage: %s [OPTIONS] FILE{.jpg,.png,.gif}\n", argv[0]); |
| 56 | + fprintf(stdout, "Options:\n"); |
| 57 | + fprintf(stdout, " -h, --help print this help\n"); |
| 58 | + fprintf(stdout, " -d, --desc STRING description\n"); |
| 59 | + fprintf(stdout, " -t, --type NUM set image type by 0-20 or keywords. Default: 3\n"); |
| 60 | + fprintf(stdout, " -o, --output FILE write tag to a file (default=stdout)\n"); |
| 61 | + fprintf(stdout, "See the man page for extensive documentation."); |
| 62 | + return EXIT_SUCCESS; |
| 63 | + } |
| 64 | + if(optind != argc - 1) |
| 65 | + { |
| 66 | + fputs("invalid arguments\n", stderr); |
| 67 | + return EXIT_FAILURE; |
| 68 | + } |
| 69 | + path_picture = argv[optind]; |
| 70 | + if(path_picture != NULL) |
| 71 | + { |
| 72 | + int seen_file_icons=0; |
| 73 | + picture_data = opustags_picture_specification_parse(path_picture, &error_message, picture_desc, picture_type, &seen_file_icons); |
| 74 | + if(picture_data == NULL) |
| 75 | + { |
| 76 | + fprintf(stderr,"Not read picture: %s\n", error_message); |
| 77 | + } |
| 78 | + } |
| 79 | + FILE *out = NULL; |
| 80 | + if(path_out != NULL) |
| 81 | + { |
| 82 | + if(strcmp(path_picture, path_out) == 0) |
| 83 | + { |
| 84 | + fputs("error: the input and output files are the same\n", stderr); |
| 85 | + return EXIT_FAILURE; |
| 86 | + } |
| 87 | + out = fopen(path_out, "w"); |
| 88 | + if(!out) |
| 89 | + { |
| 90 | + perror("fopen"); |
| 91 | + return EXIT_FAILURE; |
| 92 | + } |
| 93 | + } else { |
| 94 | + out = stdout; |
| 95 | + } |
| 96 | + if(picture_data != NULL) |
| 97 | + { |
| 98 | + char *picture_tag = "METADATA_BLOCK_PICTURE"; |
| 99 | + char *picture_meta = NULL; |
| 100 | + int picture_meta_len = strlen(picture_tag) + strlen(picture_data) + 1; |
| 101 | + picture_meta = malloc(picture_meta_len); |
| 102 | + if(picture_meta == NULL) |
| 103 | + { |
| 104 | + fprintf(stderr,"Bad picture size: %d\n", picture_meta_len); |
| 105 | + free(picture_meta); |
| 106 | + } else { |
| 107 | + strcpy(picture_meta, picture_tag); |
| 108 | + strcat(picture_meta, "="); |
| 109 | + strcat(picture_meta, picture_data); |
| 110 | + strcat(picture_meta, "\0"); |
| 111 | + } |
| 112 | + fwrite(picture_meta, 1, picture_meta_len, out); |
| 113 | + free(picture_meta); |
| 114 | + } |
| 115 | + if(out) |
| 116 | + fclose(out); |
| 117 | + return EXIT_SUCCESS; |
| 118 | +} |
0 commit comments