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 #include "share/compat.h"
37 #include "share/safe_str.h"
39 static int execit(char *prog, char *args);
40 static int forkit(char *prog, char *args);
42 int main(int argc, char *argv[])
44 int flac_return_val = 0, opt_arg = 1, from_arg = -1, to_arg = -1, flac_level = 5, i;
45 char prog[MAX_PATH], cmdline[MAX_PATH*2], from[MAX_PATH], to[MAX_PATH], macdir[MAX_PATH], options[256], *p;
46 enum { WAVPACK, RKAU, SHORTEN } codec;
48 /* get the directory where MAC external codecs reside */
49 if(0 != (p = strrchr(argv[0],'\\'))) {
50 safe_strncpy(macdir, argv[0], sizeof(macdir));
51 *(strrchr(macdir,'\\')+1) = '\0';
54 safe_strncpy(macdir, "", sizeof(macdir));
57 /* determine which codec we were called as and parse the options */
62 if(0 == strnicmp(p, "short", 5)) {
65 else if(0 == strnicmp(p, "rkau", 4)) {
67 if(argv[1][0] == '-' && argv[1][1] == 'l') {
70 case '1': flac_level = 1; break;
71 case '2': flac_level = 5; break;
72 case '3': flac_level = 8; break;
76 else if(0 == strnicmp(p, "wavpack", 7)) {
78 if(argv[1][0] == '-') {
81 case 'f': flac_level = 1; break;
82 case 'h': flac_level = 8; break;
91 /* figure out which arguments are the source and destination files */
92 for(i = 1; i < argc; i++)
93 if(argv[i][0] != '-') {
98 if(argv[i][0] != '-') {
105 /* build the command to call flac with */
106 flac_snprintf(prog, sizeof (prog), "%sflac.exe", macdir);
107 flac_snprintf(options, sizeof (options), "-%d", flac_level);
108 for(i = opt_arg; i < argc; i++)
109 if(argv[i][0] == '-') {
110 safe_strncat(options, " ", sizeof(options));
111 safe_strncat(options, argv[i], sizeof(options));
113 flac_snprintf(cmdline, sizeof (cmdline), "\"%s\" %s -o \"%s\" \"%s\"", prog, options, argv[to_arg], argv[from_arg]);
115 flac_return_val = execit(prog, cmdline);
118 * Now that flac has finished, we need to fork a process that will rename
119 * the resulting file with the correct extension once MAC has moved it to
120 * it's final resting place.
122 if(0 == flac_return_val) {
124 /* get the destination directory, if any */
125 if(0 != (p = strchr(argv[to_arg],'\\'))) {
126 safe_strncpy(from, argv[to_arg], sizeof(from));
127 *(strrchr(from,'\\')+1) = '\0';
130 safe_strncpy(from, "", sizeof(from));
133 /* for the full 'from' and 'to' paths for the renamer process */
134 p = strrchr(argv[from_arg],'\\');
135 safe_strncat(from, p? p+1 : argv[from_arg], sizeof(from));
136 safe_strncpy(to, from, sizeof(to), sizeof(to));
138 cptr = strrchr(from,'.');
145 safe_strncat(from, ".shn", sizeof (from));
148 safe_strncat(from, ".wv", sizeof (from));
151 safe_strncpy(from, ".rka", sizeof (from));
157 cptr = strrchr(to,'.');
162 safe_strncat(to, ".flac", sizeof(to));
164 flac_snprintf(prog, sizeof (prog), "%sflac_ren.exe", macdir);
165 flac_snprintf(cmdline, sizeof (cmdline), "\"%s\" \"%s\" \"%s\"", prog, from, to);
167 flac_return_val = forkit(prog, cmdline);
170 return flac_return_val;
173 int execit(char *prog, char *args)
176 STARTUPINFO startup_info;
177 PROCESS_INFORMATION proc_info;
179 GetStartupInfo(&startup_info);
184 0, /*process security attributes*/
185 0, /*thread security attributes*/
187 0, /*dwCreationFlags*/
189 0, /*lpCurrentDirectory*/
195 dw = WaitForSingleObject(proc_info.hProcess, INFINITE);
196 ok = (dw != 0xFFFFFFFF);
197 CloseHandle(proc_info.hThread);
198 CloseHandle(proc_info.hProcess);
204 int forkit(char *prog, char *args)
207 STARTUPINFO startup_info;
208 PROCESS_INFORMATION proc_info;
210 GetStartupInfo(&startup_info);
215 0, /*process security attributes*/
216 0, /*thread security attributes*/
218 DETACHED_PROCESS, /*dwCreationFlags*/
220 0, /*lpCurrentDirectory*/
225 CloseHandle(proc_info.hThread);
226 CloseHandle(proc_info.hProcess);