minor comments
[flac.git] / src / monkeys_audio_utilities / flac_mac / main.c
1 /* flac_mac - wedge utility to add FLAC support to Monkey's Audio\r
2  * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson\r
3  *\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
8  *\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
13  *\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
17  */\r
18 \r
19 /*\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
22  * how to do this.\r
23  */\r
24 \r
25 #include<stdio.h>\r
26 #include<stdlib.h>\r
27 #include<string.h>\r
28 #include<wtypes.h>\r
29 #include<process.h>\r
30 #include<winbase.h>\r
31 \r
32 static int execit(char *prog, char *args);\r
33 static int forkit(char *prog, char *args);\r
34 \r
35 int main(int argc, char *argv[])\r
36 {\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
40 \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
45         }\r
46         else {\r
47                 strcpy(macdir, "");\r
48         }\r
49 \r
50         /* determine which codec we were called as and parse the options */\r
51         if(p == 0)\r
52                 p = argv[0];\r
53         else\r
54                 p++;\r
55         if(0 == strnicmp(p, "short", 5)) {\r
56                 codec = SHORTEN;\r
57         }\r
58         else if(0 == strnicmp(p, "rkau", 4)) {\r
59                 codec = RKAU;\r
60                 if(argv[1][0] == '-' && argv[1][1] == 'l') {\r
61                         opt_arg = 2;\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
66                         }\r
67                 }\r
68         }\r
69         else if(0 == strnicmp(p, "wavpack", 7)) {\r
70                 codec = WAVPACK;\r
71                 if(argv[1][0] == '-') {\r
72                         opt_arg = 2;\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
77                         }\r
78                 }\r
79         }\r
80         else {\r
81                 return -5;\r
82         }\r
83 \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
87                         from_arg = i++;\r
88                         break;\r
89                 }\r
90         for( ; i < argc; i++)\r
91                 if(argv[i][0] != '-') {\r
92                         to_arg = i++;\r
93                         break;\r
94                 }\r
95         if(to_arg < 0)\r
96                 return -4;\r
97 \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
105                 }\r
106         sprintf(cmdline, "\"%s\" %s -o \"%s\" \"%s\"", prog, options, argv[to_arg], argv[from_arg]);\r
107 \r
108         flac_return_val = execit(prog, cmdline);\r
109 \r
110         /*\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
114          */\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
120                 }\r
121                 else {\r
122                         strcpy(from, "");\r
123                 }\r
124 \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
128                 strcpy(to, from);\r
129                 if(0 == strchr(from,'.'))\r
130                         return -3;\r
131                 switch(codec) {\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
135                 }\r
136                 strcpy(strrchr(to,'.'), ".flac");\r
137 \r
138                 sprintf(prog, "%sflac_ren.exe", macdir);\r
139                 sprintf(cmdline, "\"%s\" \"%s\" \"%s\"", prog, from, to);\r
140 \r
141                 flac_return_val = forkit(prog, cmdline);\r
142         }\r
143 \r
144         return flac_return_val;\r
145 }\r
146 \r
147 int execit(char *prog, char *args)\r
148 {\r
149         BOOL ok;\r
150         STARTUPINFO startup_info;\r
151         PROCESS_INFORMATION proc_info;\r
152 \r
153         GetStartupInfo(&startup_info);\r
154 \r
155         ok = CreateProcess(\r
156                 prog,\r
157                 args,\r
158                 0, /*process security attributes*/\r
159                 0, /*thread security attributes*/\r
160                 FALSE,\r
161                 0, /*dwCreationFlags*/\r
162                 0, /*environment*/\r
163                 0, /*lpCurrentDirectory*/\r
164                 &startup_info,\r
165                 &proc_info\r
166         );\r
167         if(ok) {\r
168                 DWORD dw;\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
173         }\r
174 \r
175         return ok? 0 : -1;\r
176 }\r
177 \r
178 int forkit(char *prog, char *args)\r
179 {\r
180         BOOL ok;\r
181         STARTUPINFO startup_info;\r
182         PROCESS_INFORMATION proc_info;\r
183 \r
184         GetStartupInfo(&startup_info);\r
185 \r
186         ok = CreateProcess(\r
187                 prog,\r
188                 args,\r
189                 0, /*process security attributes*/\r
190                 0, /*thread security attributes*/\r
191                 FALSE,\r
192                 DETACHED_PROCESS, /*dwCreationFlags*/\r
193                 0, /*environment*/\r
194                 0, /*lpCurrentDirectory*/\r
195                 &startup_info,\r
196                 &proc_info\r
197         );\r
198         if(ok) {\r
199                 CloseHandle(proc_info.hThread);\r
200                 CloseHandle(proc_info.hProcess);\r
201         }\r
202 \r
203         return ok? 0 : -2;\r
204 }\r