Changeset 357


Ignore:
Timestamp:
10/01/07 18:54:42 (17 years ago)
Author:
r2d
Message:
  • mpc2sv8 can overwrite output (option -o)
  • mpc2sv8 can output to a directory

needs to be tested and portability to windows may not be good

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libmpc/trunk/mpc2sv8/mpc2sv8.c

    r353 r357  
    3535#include <mpc/mpcdec.h>
    3636#include <mpc/minimax.h>
     37#include <getopt.h>
     38
     39#include <sys/types.h>
     40#include <dirent.h>
     41#include <libgen.h>
    3742
    3843#include "../libmpcdec/decoder.h"
     
    4449#define MPC2SV8_MAJOR 0
    4550#define MPC2SV8_MINOR 9
    46 #define MPC2SV8_BUILD 0
     51#define MPC2SV8_BUILD 1
    4752
    4853#define _cat(a,b,c) #a"."#b"."#c
     
    8287usage(const char *exename)
    8388{
    84     printf("Usage: %s <infile.mpc> <outfile.mpc>\n", exename);
    85 }
    86 
    87 int
    88 main(int argc, char **argv)
    89 {
    90     mpc_reader reader;
     89        printf("Usage:\n"
     90               "%s <infile.mpc> <outfile.mpc>\n"
     91               "or\n"
     92               "%s <infile_1.mpc> [ <infile_2.mpc> ... <infile_n.mpc> ] <outdir>\n", exename, exename);
     93}
     94
     95int convert(char * sv7file, char * sv8file)
     96{
     97        mpc_reader reader;
    9198        mpc_demux* demux;
    9299        mpc_streaminfo si;
     
    99106        char buf[TMP_BUF_SIZE];
    100107
    101     printf(About);
    102     if(3 != argc) {
    103         usage(argv[0]);
    104         return 0;
    105     }
    106 
    107     err = mpc_reader_init_stdio(&reader, argv[1]);
    108     if(err < 0) return !MPC_STATUS_OK;
    109 
    110     demux = mpc_demux_init(&reader);
    111     if(!demux) return !MPC_STATUS_OK;
    112     mpc_demux_get_info(demux,  &si);
     108        err = mpc_reader_init_stdio(&reader, sv7file);
     109        if(err < 0) return err;
     110
     111        demux = mpc_demux_init(&reader);
     112        if(!demux) {
     113                err = !MPC_STATUS_OK;
     114                goto READER_ERR;
     115        }
     116        mpc_demux_get_info(demux,  &si);
    113117
    114118        if (si.stream_version >= 8) {
    115                 fprintf(stderr, "Error : the file \"%s\" is already a sv8 file\n", argv[1]);
    116                 exit(MPC_STATUS_INVALIDSV);
     119                fprintf(stderr, "Error : the file \"%s\" is already a sv8 file\n", sv7file);
     120                err = !MPC_STATUS_OK;
     121                goto DEMUX_ERR;
    117122        }
    118123
    119124        mpc_encoder_init(&e, si.samples, 6, 1);
    120         e.outputFile = fopen( argv[2], "rb" );
    121         if ( e.outputFile != 0 ) {
    122                 fprintf(stderr, "Error : output file \"%s\" already exists\n", argv[2]);
    123                 exit(MPC_STATUS_FILE);
    124         }
    125         e.outputFile = fopen( argv[2], "w+b" );
     125        e.outputFile = fopen( sv8file, "w+b" );
    126126        e.MS_Channelmode = si.ms;
    127127
    128128        // copy begining of file
    129         in_file = fopen(argv[1], "rb");
    130         if(in_file == 0) return !MPC_STATUS_OK;
     129        in_file = fopen(sv7file, "rb");
     130        if(in_file == 0) {
     131                err = !MPC_STATUS_OK;
     132                goto OUT_FILE_ERR;
     133        }
    131134        r_size = si.header_position;
    132135        while(r_size) {
    133136                size_t tmp_size = fread(buf, 1, mini(TMP_BUF_SIZE, r_size), in_file);
    134137                if (fwrite(buf, 1, tmp_size, e.outputFile) != tmp_size) {
    135                         fprintf(stderr, "Error writing to target file : \"%s\"\n", argv[2]);
    136                         exit(MPC_STATUS_FILE);
     138                        fprintf(stderr, "Error writing to target file : \"%s\"\n", sv8file);
     139                        err = MPC_STATUS_FILE;
     140                        goto IN_FILE_ERR;
    137141                }
    138142                r_size -= tmp_size;
     
    143147        writeMagic(&e);
    144148        writeStreamInfo( &e, si.max_band, si.ms > 0, si.samples, 0, si.sample_freq,
    145                                           si.channels);
     149                         si.channels);
    146150        si_size = writeBlock(&e, "SH", MPC_TRUE, 0);
    147151        writeGainInfo(&e, si.gain_title, si.peak_title, si.gain_album, si.peak_album);
    148152        si_size = writeBlock(&e, "RG", MPC_FALSE, 0);
    149153        writeEncoderInfo(&e, si.profile, si.pns, si.encoder_version / 100,
    150                                           si.encoder_version % 100, 0);
     154                         si.encoder_version % 100, 0);
    151155        writeBlock(&e, "EI", MPC_FALSE, 0);
    152156        e.seek_ptr = ftell(e.outputFile);
     
    185189                fseek(e.outputFile, e.seek_ref + 4, SEEK_SET);
    186190                writeStreamInfo( &e, si.max_band, si.ms > 0, demux->d->samples, 0,
    187                                                   si.sample_freq, si.channels);
     191                                 si.sample_freq, si.channels);
    188192                writeBlock(&e, "SH", MPC_TRUE, si_size);
    189193                fseek(e.outputFile, 0, SEEK_END);
     
    201205        }
    202206
     207IN_FILE_ERR:
     208        fclose ( in_file );
     209OUT_FILE_ERR:
    203210        fclose ( e.outputFile );
    204         fclose ( in_file );
     211        mpc_encoder_exit(&e);
     212DEMUX_ERR:
    205213        mpc_demux_exit(demux);
     214READER_ERR:
    206215        mpc_reader_exit_stdio(&reader);
    207         mpc_encoder_exit(&e);
    208216
    209217        return err;
    210218}
     219
     220mpc_bool_t is_dir(char * dir_path)
     221{
     222        DIR * out_dir = opendir(dir_path);
     223        if (out_dir != 0) {
     224                closedir(out_dir);
     225                return MPC_TRUE;
     226        }
     227        return MPC_FALSE;
     228}
     229
     230int
     231main(int argc, char **argv)
     232{
     233        int c, i;
     234        mpc_bool_t overwrite = MPC_FALSE, use_dir = MPC_FALSE;
     235        printf(About);
     236        int ret = MPC_STATUS_OK;
     237
     238        while ((c = getopt(argc , argv, "oh")) != -1) {
     239                switch (c) {
     240                case 'o':
     241                        overwrite = MPC_TRUE;
     242                        break;
     243                case 'h':
     244                        usage(argv[0]);
     245                        return 0;
     246                }
     247        }
     248
     249        use_dir = is_dir(argv[argc - 1]);
     250
     251        if((argc - optind) < 2 || (use_dir == MPC_FALSE && (argc - optind) > 2)) {
     252                usage(argv[0]);
     253                return 0;
     254        }
     255
     256        for (i = optind; i < argc - 1; i++) {
     257                char * in_file = argv[i];
     258                char * out_file = argv[argc - 1];
     259                if (use_dir == MPC_TRUE) {
     260                        char * file_name = basename(in_file);
     261                        out_file = malloc(strlen(file_name) + strlen(argv[argc - 1]) + 2);
     262                        sprintf(out_file, "%s/%s", argv[argc - 1], file_name);
     263                }
     264                if (overwrite == MPC_FALSE) {
     265                        FILE * test_file = fopen( out_file, "rb" );
     266                        if ( test_file != 0 ) {
     267                                fprintf(stderr, "Error : output file \"%s\" already exists\n", out_file);
     268                                fclose(test_file);
     269                                continue;
     270                        }
     271                }
     272                // FIXME : test if in and out files are the same
     273                ret = convert(in_file, out_file);
     274                if (use_dir == MPC_TRUE)
     275                        free(out_file);
     276        }
     277
     278        return ret;
     279}
Note: See TracChangeset for help on using the changeset viewer.