OSDN Git Service

最初のコミット
[winaudioj/stedx.git] / rcpconv_main.cpp
1 /*
2   RCP converter / player
3
4   Copyright 1999 by Daisuke Nagano <breeze.nagano@nifty.ne.jp>
5   Feb.05.1999
6   Oct.16.2002
7
8
9   Permission is hereby granted, free of charge, to any person obtaining a copy
10   of this software and associated documentation files (the "Software"), to deal
11   in the Software without restriction, including without limitation the rights
12   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13   copies of the Software, and to permit persons to whom the Software is
14   furnished to do so, subject to the following conditions:
15
16   The above copyright notice and this permission notice shall be included in
17   all copies or substantial portions of the Software.
18
19   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
22   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25   THE SOFTWARE.
26 */
27
28 /* ------------------------------------------------------------------- */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif /* HAVE_CONFIG_H */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #ifdef STDC_HEADERS
38 # include <string.h>
39 #else
40 # ifndef HAVE_STRCHR
41 #  define strchr index
42 #  define strrchr rindex
43 # endif
44 #endif
45
46 #ifndef USE_NETBSDGETOPT
47 # include <getopt.h>
48 #else
49 # include "netbsdgetopt.h"
50 #endif
51
52 #include "rcp.h"
53 #include "gettext_wrapper.h"
54 #include "version.h"
55
56 /* ------------------------------------------------------------------- */
57
58 #define PATH_BUF_SIZE 1024
59 #define PLAYER_NAME "rcpplay"
60
61 /* ------------------------------------------------------------------- */
62
63 void error_end( char * );
64 static int option_gets( int, char ** );
65 static void usage( void );
66 static void display_version( void );
67 static char *command_name=NULL;
68 static int verbose=FLAG_FALSE;
69 static unsigned char *copyright=NULL;
70 static unsigned char *outfile=NULL;
71 static unsigned char *outdev=NULL;
72 static int is_player   = FLAG_FALSE;
73 static int is_send_rtm = FLAG_FALSE;
74 static int is_buffered = FLAG_TRUE;
75
76 int reset_mode=0;
77
78 /* ------------------------------------------------------------------- */
79
80 extern int rcpplay( RCP_DATA *);
81
82 /* ------------------------------------------------------------------- */
83 int main( int argc, char **argv ) {
84
85   int num;
86   char *a, buf[PATH_BUF_SIZE];
87   int isonefile;
88
89   RCP_DATA *rcp;
90   unsigned char *smf;
91
92 #ifdef ENABLE_NLS
93   setlocale( LC_ALL, "" );
94   bindtextdomain( PACKAGE, LOCALEDIR );
95   textdomain( PACKAGE );
96 #endif /* ENABLE_NLS */
97
98   num = option_gets( argc, argv );
99   if ( num == argc-1 ) isonefile=FLAG_TRUE;
100   else isonefile=FLAG_FALSE;
101
102   /* main loop */
103
104   while ( num < argc ) {
105     char *name;
106     name = argv[num++];
107
108     if ( isonefile==FLAG_FALSE ) {
109       if ( (a=strrchr( name, '.' ))==NULL ) continue; /* no extension */
110
111       if ( strcasecmp( a, ".rcp" )!=0 ) {
112         if ( strcasecmp( a, ".r36" )!=0 ) continue; /* extension not RCP/R36 */
113       }
114     }
115
116     rcp = rcp_read_file( argv[num-1] );
117     if ( rcp==NULL ) {
118       snprintf( buf, PATH_BUF_SIZE, _("Cannot open file %s.\n"), argv[num-1] );
119       error_end(buf);
120     }
121
122     rcp->command_name   = command_name;
123     rcp->enable_verbose = verbose;
124     rcp->is_player      = is_player;
125     rcp->is_send_rtm    = is_send_rtm;
126     rcp->is_buffered    = is_buffered;
127
128     if ( is_player == FLAG_FALSE ) {
129
130       rcp->copyright               = copyright;
131       rcp->enable_converter_notice = FLAG_TRUE;
132
133       if ( verbose == FLAG_TRUE ) {
134         fprintf( stderr, _("Filename = %s\n"), argv[num-1] );
135         if ( outfile != NULL )
136           fprintf(stderr, _("Output filename = %s\n"), outfile );
137         if ( rcp->copyright != NULL )
138           fprintf( stderr, _("Copyright message = %s\n"), rcp->copyright );
139       }
140
141       smf=rcptomid( rcp );
142
143       if ( smf == NULL ) {
144         fprintf(stderr,_("%s: Cannot convert %s\n"), command_name, argv[num-1]);
145       }
146       else {
147         if ( outfile == NULL )
148           fwrite( smf, 1, rcp->smf_size, stdout );
149         else {
150           FILE *fp;
151           fp = fopen(outfile,"w");
152           if ( fp != NULL ) {
153             fwrite( smf, 1, rcp->smf_size, fp );
154             fclose(fp);
155           } else
156             fprintf(stderr,_("%s: Cannot open file %s\n"), command_name, outfile);
157         }
158         free(smf);
159       }
160     }
161     else {
162
163       if ( verbose == FLAG_TRUE ) {
164         fprintf( stderr, _("Filename = %s\n"), argv[num-1] );
165       }
166
167       if ( outdev != NULL )
168         rcp->output_device = outdev;
169       else
170         rcp->output_device = DEFAULT_OUTPUT_DEVICE;
171       
172       rcpplay( rcp );
173       rcp_close( rcp );
174     }
175   }
176
177   /* finished */
178
179   exit(0);
180 }
181
182 /* ------------------------------------------------------------------- */
183
184 void error_end( char *msg ) {
185
186   fprintf( stderr, "%s: %s\n", command_name, msg );
187   exit(1);
188 }
189
190 /* ------------------------------------------------------------------- */
191
192 static int option_gets( int argc, char **argv ) {
193
194   extern char *optarg;
195   extern int optind;
196
197   int c;
198   int option_index=0;
199
200   command_name =
201     (strrchr(argv[0],'/')==NULL)?argv[0]:(strrchr(argv[0],'/')+1);
202
203   if ( strcasecmp( command_name, PLAYER_NAME ) == 0 ) {
204     is_player = FLAG_TRUE;
205   }
206
207   verbose = FLAG_FALSE;
208   is_send_rtm = FLAG_FALSE;
209   is_buffered = FLAG_TRUE;
210
211   while(1) {
212     static struct option long_options[] = {
213       {"no-buffered",   0, 0, 'b'},
214       {"send-seq",      0, 0, 100},
215       {"outdev",        1, 0, 'm'},
216       {"reset-mode",    1, 0, 'r'},
217       {"outfile",       1, 0, 'o'},
218       {"version",       0, 0, 'V'},
219       {"verbose",       0, 0, 'v'},
220       {"help",          0, 0, 'h'},
221       {"copyright",     1, 0, 'c'},
222       {0, 0, 0, 0}
223     };
224
225     c = getopt_long(argc, argv, "Vvbo:m:r:h", long_options, &option_index );
226     if ( c == EOF ) break;
227
228     switch(c) {
229
230     case 100:
231       is_send_rtm = FLAG_TRUE;
232       break;
233
234     case 'b':
235       is_buffered = FLAG_FALSE;
236       break;
237
238     case 'm':
239       if ( outdev != NULL ) free(outdev);
240       outdev =  (unsigned char *)malloc(sizeof(unsigned char)*strlen(optarg)+16);
241       strcpy( outdev, optarg );
242       break;
243
244     case 'r':
245       reset_mode = atoi(optarg);
246       if ( reset_mode < 0 ) reset_mode = 0;
247       if ( reset_mode > 3 ) reset_mode = 0;
248       break;
249
250
251     case 'h': /* help */
252       usage();
253       break;
254
255     case 'V':
256       display_version(); /* version */
257       break;
258
259     case 'v':
260       verbose = FLAG_TRUE;
261       break;
262
263     case 'c':
264       if ( copyright != NULL ) free(copyright);
265       copyright = (unsigned char *)malloc(sizeof(unsigned char)*strlen(optarg)+16);
266       strcpy( copyright, optarg );
267       break;
268
269     case 'o':
270       if ( outfile != NULL ) free(outfile);
271       outfile =  (unsigned char *)malloc(sizeof(unsigned char)*strlen(optarg)+16);
272       strcpy( outfile, optarg );
273       break;
274
275     case '?':
276       break;
277     default:
278       break;
279     }
280   }
281
282   if ( optind >= argc ) {
283     fprintf(stderr, _("%s: No input filename is specified.\n"), command_name);
284     exit(1);
285   } 
286
287   return optind;
288 }
289
290 static void usage( void ) {
291
292   fprintf(stderr, "usage: %s [options] [rcp-filename]\n", command_name );
293   fprintf(stderr, _("Options:\n"));
294
295   if ( is_player == FLAG_TRUE ) {
296     fprintf(stderr, " -m,     --outdev <devname>   ");
297     fprintf(stderr, _("Output device name.\n"));
298     fprintf(stderr, " -r,     --reset-mode <val>   ");
299     fprintf(stderr, _("Send specified reset message after playing. \n"));
300     fprintf(stderr, "                               0:GM 1:GS 2:SC88 3:XG\n");
301   }
302
303   if ( is_player == FLAG_FALSE ) {
304     fprintf(stderr, " -o,     --outfile <filename> ");
305     fprintf(stderr, _("Output to file as <filename>.\n"));
306     fprintf(stderr, "         --copyright <msg>    ");
307     fprintf(stderr, _("Set copyright meta event as <msg>.\n"));
308   }
309
310   fprintf(stderr, " -v,     --verbose            ");
311   fprintf(stderr, _("Be verbose.\n"));
312   fprintf(stderr, " -V,     --version            ");
313   fprintf(stderr, _("Show version information.\n"));
314   fprintf(stderr, " -h,     --help               ");
315   fprintf(stderr, _("Show this help message.\n"));
316
317   exit(0);
318 }
319
320 static void display_version( void ) {
321
322   fprintf(stderr, "%s version ", command_name );
323   fprintf(stderr, VERSION_ID "\n");
324   if ( is_player == FLAG_TRUE ) {
325     fprintf(stderr, "tiny RCP player");
326 #ifdef HAVE_STED2_SUPPORT
327     fprintf(stderr, " <STed2 support>");
328 #endif
329     fprintf(stderr,"\n");
330   }
331   else
332     fprintf(stderr, "RCP to SMF converter\n");
333   fprintf(stderr, "Copyright 1999 by NAGANO Daisuke <breeze.nagano@nifty.ne.jp>\n");
334   fprintf(stderr, "\n");
335   fprintf(stderr, "This is free software; see the source for copying conditions.\n");
336   fprintf(stderr, "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
337   fprintf(stderr, "PARTICULAR PURPOSE.\n\n");
338
339   exit(0);
340 }