1 /* flac_mac - wedge utility to add FLAC support to Monkey's Audio
\r
2 * Copyright (C) 2000,2001,2002 Josh Coalson
\r
4 * This program is free software; you can redistribute it and/or
\r
5 * modify it under the terms of the GNU General Public License
\r
6 * as published by the Free Software Foundation; either version 2
\r
7 * of the License, or (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program; if not, write to the Free Software
\r
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
20 * This program can be used to allow FLAC to masquerade as one of the other
\r
21 * supported lossless codecs in Monkey's Audio. See the documentation for
\r
32 static int execit(char *prog, char *args);
\r
33 static int forkit(char *prog, char *args);
\r
35 int main(int argc, char *argv[])
\r
37 int flac_return_val = 0, opt_arg = 1, from_arg = -1, to_arg = -1, flac_level = 5, i;
\r
38 char prog[MAX_PATH], cmdline[MAX_PATH*2], from[MAX_PATH], to[MAX_PATH], macdir[MAX_PATH], options[256], *p;
\r
39 enum { WAVPACK, RKAU, SHORTEN } codec;
\r
41 /* get the directory where MAC external codecs reside */
\r
42 if(0 != (p = strrchr(argv[0],'\\'))) {
\r
43 strcpy(macdir, argv[0]);
\r
44 *(strrchr(macdir,'\\')+1) = '\0';
\r
50 /* determine which codec we were called as and parse the options */
\r
55 if(0 == strnicmp(p, "short", 5)) {
\r
58 else if(0 == strnicmp(p, "rkau", 4)) {
\r
60 if(argv[1][0] == '-' && argv[1][1] == 'l') {
\r
62 switch(argv[1][2]) {
\r
63 case '1': flac_level = 1; break;
\r
64 case '2': flac_level = 5; break;
\r
65 case '3': flac_level = 8; break;
\r
69 else if(0 == strnicmp(p, "wavpack", 7)) {
\r
71 if(argv[1][0] == '-') {
\r
73 switch(argv[1][1]) {
\r
74 case 'f': flac_level = 1; break;
\r
75 case 'h': flac_level = 8; break;
\r
76 default: opt_arg = 1;
\r
84 /* figure out which arguments are the source and destination files */
\r
85 for(i = 1; i < argc; i++)
\r
86 if(argv[i][0] != '-') {
\r
90 for( ; i < argc; i++)
\r
91 if(argv[i][0] != '-') {
\r
98 /* build the command to call flac with */
\r
99 sprintf(prog, "%sflac.exe", macdir);
\r
100 sprintf(options, "-%d", flac_level);
\r
101 for(i = opt_arg; i < argc; i++)
\r
102 if(argv[i][0] == '-') {
\r
103 strcat(options, " ");
\r
104 strcat(options, argv[i]);
\r
106 sprintf(cmdline, "\"%s\" %s -o \"%s\" \"%s\"", prog, options, argv[to_arg], argv[from_arg]);
\r
108 flac_return_val = execit(prog, cmdline);
\r
111 * Now that flac has finished, we need to fork a process that will rename
\r
112 * the resulting file with the correct extension once MAC has moved it to
\r
113 * it's final resting place.
\r
115 if(0 == flac_return_val) {
\r
116 /* get the destination directory, if any */
\r
117 if(0 != (p = strchr(argv[to_arg],'\\'))) {
\r
118 strcpy(from, argv[to_arg]);
\r
119 *(strrchr(from,'\\')+1) = '\0';
\r
125 /* for the full 'from' and 'to' paths for the renamer process */
\r
126 p = strrchr(argv[from_arg],'\\');
\r
127 strcat(from, p? p+1 : argv[from_arg]);
\r
129 if(0 == strchr(from,'.'))
\r
132 case SHORTEN: strcpy(strrchr(from,'.'), ".shn"); break;
\r
133 case WAVPACK: strcpy(strrchr(from,'.'), ".wv"); break;
\r
134 case RKAU: strcpy(strrchr(from,'.'), ".rka"); break;
\r
136 strcpy(strrchr(to,'.'), ".flac");
\r
138 sprintf(prog, "%sflac_ren.exe", macdir);
\r
139 sprintf(cmdline, "\"%s\" \"%s\" \"%s\"", prog, from, to);
\r
141 flac_return_val = forkit(prog, cmdline);
\r
144 return flac_return_val;
\r
147 int execit(char *prog, char *args)
\r
150 STARTUPINFO startup_info;
\r
151 PROCESS_INFORMATION proc_info;
\r
153 GetStartupInfo(&startup_info);
\r
155 ok = CreateProcess(
\r
158 0, /*process security attributes*/
\r
159 0, /*thread security attributes*/
\r
161 0, /*dwCreationFlags*/
\r
163 0, /*lpCurrentDirectory*/
\r
169 dw = WaitForSingleObject(proc_info.hProcess, INFINITE);
\r
170 ok = (dw != 0xFFFFFFFF);
\r
171 CloseHandle(proc_info.hThread);
\r
172 CloseHandle(proc_info.hProcess);
\r
178 int forkit(char *prog, char *args)
\r
181 STARTUPINFO startup_info;
\r
182 PROCESS_INFORMATION proc_info;
\r
184 GetStartupInfo(&startup_info);
\r
186 ok = CreateProcess(
\r
189 0, /*process security attributes*/
\r
190 0, /*thread security attributes*/
\r
192 DETACHED_PROCESS, /*dwCreationFlags*/
\r
194 0, /*lpCurrentDirectory*/
\r
199 CloseHandle(proc_info.hThread);
\r
200 CloseHandle(proc_info.hProcess);
\r