changeset 4:c8875256b767

Use stdio for console output, update usage, and use sensible exit values.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 21 May 2013 12:10:27 +0200
parents 8ad174416431
children c4218fbe158f
files pmd_play.c
diffstat 1 files changed, 26 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/pmd_play.c
+++ b/pmd_play.c
@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include <stdint.h>
 #include <string.h>
 #include <fcntl.h>
@@ -114,25 +115,16 @@ static int music_load(char *filename)
 }
 #endif
 
-#if defined(WIN32) || defined(WIN64)
-#define WriteToConsole(str, len) WriteFile(errh, (str), (len), &done, NULL)
-#else
-#define WriteToConsole(str, len) write(2, (str), (len))
-#endif
-
-static void usage(void) {
-#if defined(WIN32) || defined(WIN64)
-    DWORD done;
-    HANDLE errh = GetStdHandle(STD_ERROR_HANDLE);
-#endif
+static void usage(char *argv0) {
     const char *usage_str = 
-"pmdwin -f <output file> -l <loop count> -m <device mask> -c <channel mask> -t <time> [PMD File]\n\
+"%s [-f <output file>] [-l <loop count>] [-m <device mask>] [-c <channel mask>] [-t <time>] [-r <samplerate>] <PMD File>\n\
        -f - Write output to wavfile\n\
        -l - Loop n times (default is once - use 0 for infinite loop)\n\
        -m - Device mask: 1 - OPNA, 2 - PSG, 4 - Rhythm (or them together)\n\
        -c - Channel mask: 1,2,4,8,16,32 are OPNA channels 1-6, 64,128,256 are PSG channels 1-3. Once again, or them together\n\
-       -t - Play song for n seconds\n\n";
-    WriteToConsole(usage_str, strlen(usage_str));
+       -t - Play song for n seconds\n\
+       -r - Set the output sample rate\n\n";
+    fprintf(stderr, usage_str, argv0);
 }
 
 int main(int argc, char **argv) {
@@ -144,8 +136,6 @@ int main(int argc, char **argv) {
     char pmd_title[1024];
     char pmd_compo[1024];
 #if defined(WIN32) || defined(WIN64)
-    DWORD done;
-    HANDLE errh = GetStdHandle(STD_ERROR_HANDLE);
     HWAVEOUT hWaveOut;
 #endif
     char buf[1024];
@@ -174,8 +164,8 @@ int main(int argc, char **argv) {
 			  samplerate_out = atoui((uint8_t*)optarg);
 			  break;
             default:
-              usage();
-              break;
+              usage(argv[0]);
+              return 1;
         }
     }
 
@@ -189,24 +179,27 @@ int main(int argc, char **argv) {
         if((out_fd = oss_audio_open(samplerate_out, 1)) < 0) {
 #endif
 #endif
-            WriteToConsole("Cannot open sound device, exiting.\n", 35);
-            return -1;
+            fprintf(stderr, "Cannot open sound device, exiting.\n");
+            return 3;
         }
     } else {
         write_wav_header(out_fd, samplerate_out);
     }
-    music_load(argv[optind]);
-    memcpy(pmd_title, "Title = ", 8);
-    getmemo3(pmd_title+8, NULL, 0, 1);
-    i = strlen(pmd_title); pmd_title[i++] = '\n';
+    if (!argv[optind] || optind + 1 != argc) {
+        usage(argv[0]);
+        return 1;
+    }
+    if (music_load(argv[optind]) == ERR_OPEN_MUSIC_FILE) {
+        fprintf(stderr, "Cannot open music file ā€œ%sā€, exiting.\n", argv[optind]);
+        return 2;
+    }
+    getmemo3(pmd_title, NULL, 0, 1);
 #if defined(WIN32) || defined(WIN64)
     SetConsoleOutputCP(65001); // UTF-8
 #endif
-    WriteToConsole(pmd_title, i);
-    memcpy(pmd_compo, "Composer = ", 11);
-    getmemo3(pmd_compo+11, NULL, 0, 2);
-    i = strlen(pmd_compo); pmd_compo[i++] = '\n';
-    WriteToConsole(pmd_compo, i);
+    printf("Title = %s\n", pmd_title);
+    getmemo3(pmd_compo, NULL, 0, 2);
+    printf("Composer = %s\n", pmd_compo);
 	setpcmrate(samplerate_out);
     music_start();
     i = 0;
@@ -215,10 +208,10 @@ int main(int argc, char **argv) {
     } else {
         frameno = 0;
     }
-    buf[0] = '\r';
     while(getloopcount() < pmd_loopcount || infloop || i < frameno) {
-        int ret = getstatus(buf+1, 1022);
-        WriteToConsole(buf, ret+1);
+        getstatus(buf, 1023);
+        printf("\r%s", buf);
+        fflush(stdout);
         getpcmdata(pcmbuf, 4096);
 #if defined(WIN32) || defined(WIN64)
         if(!tofile) {
@@ -236,7 +229,7 @@ int main(int argc, char **argv) {
             write(out_fd, pcmbuf, 8192);
         i++;
     }
-    WriteToConsole("\n", 1);
+    fputc('\n', stdout);
 #if defined(WIN32) || defined(WIN64)
     if(!tofile) {
         wave_out_close(hWaveOut);