1 /* flac_mac - wedge utility to add FLAC support to Monkey's Audio
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * This program can be used to allow FLAC to masquerade as one of the other
21 * supported lossless codecs in Monkey's Audio. See the documentation for
36 static int execit(char *prog
, char *args
);
37 static int forkit(char *prog
, char *args
);
39 int main(int argc
, char *argv
[])
41 int flac_return_val
= 0, opt_arg
= 1, from_arg
= -1, to_arg
= -1, flac_level
= 5, i
;
42 char prog
[MAX_PATH
], cmdline
[MAX_PATH
*2], from
[MAX_PATH
], to
[MAX_PATH
], macdir
[MAX_PATH
], options
[256], *p
;
43 enum { WAVPACK
, RKAU
, SHORTEN
} codec
;
45 /* get the directory where MAC external codecs reside */
46 if(0 != (p
= strrchr(argv
[0],'\\'))) {
47 strcpy(macdir
, argv
[0]);
48 *(strrchr(macdir
,'\\')+1) = '\0';
54 /* determine which codec we were called as and parse the options */
59 if(0 == strnicmp(p
, "short", 5)) {
62 else if(0 == strnicmp(p
, "rkau", 4)) {
64 if(argv
[1][0] == '-' && argv
[1][1] == 'l') {
67 case '1': flac_level
= 1; break;
68 case '2': flac_level
= 5; break;
69 case '3': flac_level
= 8; break;
73 else if(0 == strnicmp(p
, "wavpack", 7)) {
75 if(argv
[1][0] == '-') {
78 case 'f': flac_level
= 1; break;
79 case 'h': flac_level
= 8; break;
88 /* figure out which arguments are the source and destination files */
89 for(i
= 1; i
< argc
; i
++)
90 if(argv
[i
][0] != '-') {
95 if(argv
[i
][0] != '-') {
102 /* build the command to call flac with */
103 flac_snprintf(prog
, sizeof (prog
), "%sflac.exe", macdir
);
104 flac_snprintf(options
, sizeof (options
), "-%d", flac_level
);
105 for(i
= opt_arg
; i
< argc
; i
++)
106 if(argv
[i
][0] == '-') {
107 strcat(options
, " ");
108 strcat(options
, argv
[i
]);
110 flac_snprintf(cmdline
, sizeof (cmdline
), "\"%s\" %s -o \"%s\" \"%s\"", prog
, options
, argv
[to_arg
], argv
[from_arg
]);
112 flac_return_val
= execit(prog
, cmdline
);
115 * Now that flac has finished, we need to fork a process that will rename
116 * the resulting file with the correct extension once MAC has moved it to
117 * it's final resting place.
119 if(0 == flac_return_val
) {
120 /* get the destination directory, if any */
121 if(0 != (p
= strchr(argv
[to_arg
],'\\'))) {
122 strcpy(from
, argv
[to_arg
]);
123 *(strrchr(from
,'\\')+1) = '\0';
129 /* for the full 'from' and 'to' paths for the renamer process */
130 p
= strrchr(argv
[from_arg
],'\\');
131 strcat(from
, p
? p
+1 : argv
[from_arg
]);
133 if(0 == strchr(from
,'.'))
136 case SHORTEN
: strcpy(strrchr(from
,'.'), ".shn"); break;
137 case WAVPACK
: strcpy(strrchr(from
,'.'), ".wv"); break;
138 case RKAU
: strcpy(strrchr(from
,'.'), ".rka"); break;
140 strcpy(strrchr(to
,'.'), ".flac");
142 flac_snprintf(prog
, sizeof (prog
), "%sflac_ren.exe", macdir
);
143 flac_snprintf(cmdline
, sizeof (smdline
), "\"%s\" \"%s\" \"%s\"", prog
, from
, to
);
145 flac_return_val
= forkit(prog
, cmdline
);
148 return flac_return_val
;
151 int execit(char *prog
, char *args
)
154 STARTUPINFO startup_info
;
155 PROCESS_INFORMATION proc_info
;
157 GetStartupInfo(&startup_info
);
162 0, /*process security attributes*/
163 0, /*thread security attributes*/
165 0, /*dwCreationFlags*/
167 0, /*lpCurrentDirectory*/
173 dw
= WaitForSingleObject(proc_info
.hProcess
, INFINITE
);
174 ok
= (dw
!= 0xFFFFFFFF);
175 CloseHandle(proc_info
.hThread
);
176 CloseHandle(proc_info
.hProcess
);
182 int forkit(char *prog
, char *args
)
185 STARTUPINFO startup_info
;
186 PROCESS_INFORMATION proc_info
;
188 GetStartupInfo(&startup_info
);
193 0, /*process security attributes*/
194 0, /*thread security attributes*/
196 DETACHED_PROCESS
, /*dwCreationFlags*/
198 0, /*lpCurrentDirectory*/
203 CloseHandle(proc_info
.hThread
);
204 CloseHandle(proc_info
.hProcess
);