Use new function flac_snprintf() where ever appropriate.
[platform/upstream/flac.git] / src / test_grabbag / cuesheet / main.c
index 5dcd54c..d04fa4b 100644 (file)
@@ -1,5 +1,5 @@
 /* test_cuesheet - Simple tester for cuesheet routines in grabbag
- * Copyright (C) 2002  Josh Coalson
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -23,7 +28,7 @@
 #include "FLAC/metadata.h"
 #include "share/grabbag.h"
 
-static int do_cuesheet(const char *infilename, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
+static int do_cuesheet(const char *infilename, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
 {
        FILE *fin, *fout;
        const char *error_message;
@@ -40,10 +45,10 @@ static int do_cuesheet(const char *infilename, FLAC__bool is_cdda, FLAC__uint64
                fin = stdin;
        }
        else if(0 == (fin = fopen(infilename, "r"))) {
-               fprintf(stderr, "can't open file %s for reading\n", infilename);
+               fprintf(stderr, "can't open file %s for reading: %s\n", infilename, strerror(errno));
                return 255;
        }
-       if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, is_cdda, lead_out_offset))) {
+       if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset))) {
                if(fin != stdin)
                        fclose(fin);
        }
@@ -55,24 +60,27 @@ static int do_cuesheet(const char *infilename, FLAC__bool is_cdda, FLAC__uint64
        }
        if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
                printf("pass1: illegal cuesheet: \"%s\"\n", error_message);
+               FLAC__metadata_object_delete(cuesheet);
                return 1;
        }
-       sprintf(tmpfilename, "%s.1", infilename);
+       flac_snprintf(tmpfilename, sizeof (tmpfilename), "%s.1", infilename);
        if(0 == (fout = fopen(tmpfilename, "w"))) {
-               fprintf(stderr, "can't open file %s for writing\n", tmpfilename);
+               fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno));
+               FLAC__metadata_object_delete(cuesheet);
                return 255;
        }
-       grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE", is_cdda);
+       grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
+       FLAC__metadata_object_delete(cuesheet);
        fclose(fout);
 
        /*
         * pass 2
         */
        if(0 == (fin = fopen(tmpfilename, "r"))) {
-               fprintf(stderr, "can't open file %s for reading\n", tmpfilename);
+               fprintf(stderr, "can't open file %s for reading: %s\n", tmpfilename, strerror(errno));
                return 255;
        }
-       if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, is_cdda, lead_out_offset))) {
+       if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset))) {
                if(fin != stdin)
                        fclose(fin);
        }
@@ -84,14 +92,17 @@ static int do_cuesheet(const char *infilename, FLAC__bool is_cdda, FLAC__uint64
        }
        if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
                printf("pass2: illegal cuesheet: \"%s\"\n", error_message);
+               FLAC__metadata_object_delete(cuesheet);
                return 1;
        }
-       sprintf(tmpfilename, "%s.2", infilename);
+       flac_snprintf(tmpfilename, sizeof (tmpfilename), "%s.2", infilename);
        if(0 == (fout = fopen(tmpfilename, "w"))) {
-               fprintf(stderr, "can't open file %s for writing\n", tmpfilename);
+               fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno));
+               FLAC__metadata_object_delete(cuesheet);
                return 255;
        }
-       grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE", is_cdda);
+       grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
+       FLAC__metadata_object_delete(cuesheet);
        fclose(fout);
 
        return 0;
@@ -100,24 +111,32 @@ static int do_cuesheet(const char *infilename, FLAC__bool is_cdda, FLAC__uint64
 int main(int argc, char *argv[])
 {
        FLAC__uint64 lead_out_offset;
+       unsigned sample_rate = 48000;
        FLAC__bool is_cdda = false;
-       const char *usage = "usage: test_cuesheet cuesheet_file lead_out_offset [ cdda ]\n";
+       const char *usage = "usage: test_cuesheet cuesheet_file lead_out_offset [ [ sample_rate ] cdda ]\n";
+
+       if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
+               puts(usage);
+               return 0;
+       }
 
-       if(argc < 3 || argc > 4) {
-               fprintf(stderr, usage);
+       if(argc < 3 || argc > 5) {
+               fputs(usage, stderr);
                return 255;
        }
 
        lead_out_offset = (FLAC__uint64)strtoul(argv[2], 0, 10);
-       if(argc == 4) {
-               if(0 == strcmp(argv[3], "cdda"))
-                       is_cdda = true;
-               else {
-                       fprintf(stderr, usage);
-                       return 255;
+       if(argc >= 4) {
+               sample_rate = (unsigned)atoi(argv[3]);
+               if(argc >= 5) {
+                       if(0 == strcmp(argv[4], "cdda"))
+                               is_cdda = true;
+                       else {
+                               fputs(usage, stderr);
+                               return 255;
+                       }
                }
        }
 
-       return do_cuesheet(argv[1], is_cdda, lead_out_offset);
+       return do_cuesheet(argv[1], sample_rate, is_cdda, lead_out_offset);
 }
-FLAC__StreamMetadata *grabbag__cuesheet_parse(FILE *file, const char **error_message, unsigned *last_line_read, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset);