Index: libmpcdec/branches/zorg/mpcdec/mpcdec.c
===================================================================
--- libmpcdec/branches/zorg/mpcdec/mpcdec.c	(revision 68)
+++ libmpcdec/branches/zorg/mpcdec/mpcdec.c	(revision 70)
@@ -35,14 +35,14 @@
 #include <assert.h>
 #include <time.h>
+#include <mpcdec/mpcdec.h>
 
-#include <mpcdec/mpcdec.h>
-#include <libwaveformat.h>
+#ifdef WIN32
+#include <crtdbg.h>
+#endif
 
 static void
 usage(const char *exename)
 {
-    printf
-        ("Usage: %s <infile.mpc> [<outfile.wav>]\nIf <outfile.wav> is not specified, decoder will run in benchmark mode.\n",
-         exename);
+    printf("Usage: %s <infile.mpc>\n", exename);
 }
 
@@ -50,114 +50,51 @@
 main(int argc, char **argv)
 {
-    mpc_reader reader; mpc_streaminfo si; mpc_decoder decoder; wavwriter
+    mpc_reader reader; mpc_streaminfo si; mpc_decoder* decoder; mpc_status err;
+    MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
+    clock_t begin, end, sum; int total_samples;
 
-    if (argc != 2 && argc != 3) {
-        if (argc > 0)
-            usage(argv[0]);
+    if(argc != 2)
+    {
+        usage(argv[0]);
         return 0;
     }
 
+    err = mpc_reader_init_stdio(&reader, argv[1]);
+    if(err < 0) return !MPC_STATUS_OK;
 
-    FILE *input = fopen(argv[1], "rb");
-    FILE *output = 0;
-    if (input == 0) {
-        usage(argv[0]);
-        printf("Error opening input file: \"%s\"\n", argv[1]);
-        return 1;
+    err = mpc_streaminfo_init(&si, &reader);
+    if(err < 0) return !MPC_STATUS_OK;
+
+    err = mpc_decoder_init(&decoder, &reader, &si);
+    if(err < 0) return !MPC_STATUS_OK;
+
+    sum = total_samples = 0;
+    while(TRUE)
+    {
+        mpc_uint32_t samples;
+        begin = clock();
+        samples = mpc_decoder_decode(decoder, sample_buffer, 0, 0);
+        end = clock();
+        if(samples <= 0) break;
+        if(!samples) break;
+        total_samples += samples;
+        sum           += end - begin;
     }
 
-    if (argc == 3) {
-        output = fopen(argv[2], "wb");
-        if (output == 0) {
-            fclose(input);
-            usage(argv[0]);
-            printf("Error opening output file: \"%s\"\n", argv[2]);
-            return 1;
-        }
-    }
+    printf("%u samples ", total_samples);
+    total_samples = (mpc_uint32_t) ((double) total_samples * CLOCKS_PER_SEC * 100 / (si.sample_freq * sum));
+    printf("decoded in %u ms (%u.%02ux)\n",
+           sum * 1000 / CLOCKS_PER_SEC,
+           total_samples / 100,
+           total_samples % 100
+           );
 
-    /* initialize our reader_data tag the reader will carry around with it */
-    reader_data data;
-    data.file = input;
-    data.seekable = true;
-    fseek(data.file, 0, SEEK_END);
-    data.size = ftell(data.file);
-    fseek(data.file, 0, SEEK_SET);
+    mpc_decoder_exit(decoder);
+    mpc_reader_exit_stdio(&reader);
 
-    /* set up an mpc_reader linked to our function implementations */
-    mpc_decoder decoder;
-    mpc_reader reader;
-    reader.read = read_impl;
-    reader.seek = seek_impl;
-    reader.tell = tell_impl;
-    reader.get_size = get_size_impl;
-    reader.canseek = canseek_impl;
-    reader.data = &data;
-
-    /* read file's streaminfo data */
-    mpc_streaminfo info;
-    mpc_streaminfo_init(&info);
-    if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
-        printf("Not a valid musepack file: \"%s\"\n", argv[1]);
-        return 1;
-    }
-
-    /* instantiate a decoder with our file reader */
-    mpc_decoder_setup(&decoder, &reader);
-    if (!mpc_decoder_initialize(&decoder, &info)) {
-        printf("Error initializing decoder.\n", argv[1]);
-        return 1;
-    }
-
-    /* decode the file */
-    printf("Decoding from:\n%s\nTo:\n%s\n", argv[1],
-           output ? argv[2] : "N/A");
-    WavWriter *wavwriter =
-        output ? new WavWriter(output, 2, 16, info.sample_freq) : 0;
-    MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
-    clock_t begin, end;
-    begin = clock();
-    unsigned total_samples = 0;
-    mpc_bool_t successful = FALSE;
-    for (;;) {
-        unsigned status = mpc_decoder_decode(&decoder, sample_buffer, 0, 0);
-        if (status == (unsigned)(-1)) {
-            //decode error
-            printf("Error decoding file.\n");
-            break;
-        }
-        else if (status == 0)   //EOF
-        {
-            successful = true;
-            break;
-        }
-        else                    //status>0
-        {
-            total_samples += status;
-            if (wavwriter) {
-                if (!wavwriter->
-                    WriteSamples(sample_buffer, status * /* stereo */ 2)) {
-                    printf("Write error.\n");
-                    break;
-                }
-            }
-        }
-    }
-
-    end = clock();
-
-    if (wavwriter) {
-        delete wavwriter;
-    }
-
-    if (successful) {
-        printf("\nFinished.\nTotal samples decoded: %u.\n", total_samples);
-        unsigned ms = (end - begin) * 1000 / CLOCKS_PER_SEC;
-        unsigned ratio =
-            (unsigned)((double)total_samples / (double)info.sample_freq /
-                       ((double)ms / 1000.0) * 100.0);
-        printf("Time: %u ms (%u.%02ux).\n", ms, ratio / 100, ratio % 100);
-    }
-
+#ifdef WIN32
+    assert(_CrtCheckMemory());
+    _CrtDumpMemoryLeaks();
+#endif
     return 0;
 }
