main.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:5k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* flac_mac - wedge utility to add FLAC support to Monkey's Audio
  2.  * Copyright (C) 2000,2001,2002,2003,2004,2005  Josh Coalson
  3.  *
  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.
  8.  *
  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.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. /*
  19.  * This program can be used to allow FLAC to masquerade as one of the other
  20.  * supported lossless codecs in Monkey's Audio.  See the documentation for
  21.  * how to do this.
  22.  */
  23. #include<stdio.h>
  24. #include<stdlib.h>
  25. #include<string.h>
  26. #include<wtypes.h>
  27. #include<process.h>
  28. #include<winbase.h>
  29. static int execit(char *prog, char *args);
  30. static int forkit(char *prog, char *args);
  31. int main(int argc, char *argv[])
  32. {
  33. int flac_return_val = 0, opt_arg = 1, from_arg = -1, to_arg = -1, flac_level = 5, i;
  34. char prog[MAX_PATH], cmdline[MAX_PATH*2], from[MAX_PATH], to[MAX_PATH], macdir[MAX_PATH], options[256], *p;
  35. enum { WAVPACK, RKAU, SHORTEN } codec;
  36. /* get the directory where MAC external codecs reside */
  37. if(0 != (p = strrchr(argv[0],'\'))) {
  38. strcpy(macdir, argv[0]);
  39. *(strrchr(macdir,'\')+1) = '';
  40. }
  41. else {
  42. strcpy(macdir, "");
  43. }
  44. /* determine which codec we were called as and parse the options */
  45. if(p == 0)
  46. p = argv[0];
  47. else
  48. p++;
  49. if(0 == strnicmp(p, "short", 5)) {
  50. codec = SHORTEN;
  51. }
  52. else if(0 == strnicmp(p, "rkau", 4)) {
  53. codec = RKAU;
  54. if(argv[1][0] == '-' && argv[1][1] == 'l') {
  55. opt_arg = 2;
  56. switch(argv[1][2]) {
  57. case '1': flac_level = 1; break;
  58. case '2': flac_level = 5; break;
  59. case '3': flac_level = 8; break;
  60. }
  61. }
  62. }
  63. else if(0 == strnicmp(p, "wavpack", 7)) {
  64. codec = WAVPACK;
  65. if(argv[1][0] == '-') {
  66. opt_arg = 2;
  67. switch(argv[1][1]) {
  68. case 'f': flac_level = 1; break;
  69. case 'h': flac_level = 8; break;
  70. default: opt_arg = 1;
  71. }
  72. }
  73. }
  74. else {
  75. return -5;
  76. }
  77. /* figure out which arguments are the source and destination files */
  78. for(i = 1; i < argc; i++)
  79. if(argv[i][0] != '-') {
  80. from_arg = i++;
  81. break;
  82. }
  83. for( ; i < argc; i++)
  84. if(argv[i][0] != '-') {
  85. to_arg = i++;
  86. break;
  87. }
  88. if(to_arg < 0)
  89. return -4;
  90. /* build the command to call flac with */
  91. sprintf(prog, "%sflac.exe", macdir);
  92. sprintf(options, "-%d", flac_level);
  93. for(i = opt_arg; i < argc; i++)
  94. if(argv[i][0] == '-') {
  95. strcat(options, " ");
  96. strcat(options, argv[i]);
  97. }
  98. sprintf(cmdline, ""%s" %s -o "%s" "%s"", prog, options, argv[to_arg], argv[from_arg]);
  99. flac_return_val = execit(prog, cmdline);
  100. /*
  101.  * Now that flac has finished, we need to fork a process that will rename
  102.  * the resulting file with the correct extension once MAC has moved it to
  103.  * it's final resting place.
  104.  */
  105. if(0 == flac_return_val) {
  106. /* get the destination directory, if any */
  107. if(0 != (p = strchr(argv[to_arg],'\'))) {
  108. strcpy(from, argv[to_arg]);
  109. *(strrchr(from,'\')+1) = '';
  110. }
  111. else {
  112. strcpy(from, "");
  113. }
  114. /* for the full 'from' and 'to' paths for the renamer process */
  115. p = strrchr(argv[from_arg],'\');
  116. strcat(from, p? p+1 : argv[from_arg]);
  117. strcpy(to, from);
  118. if(0 == strchr(from,'.'))
  119. return -3;
  120. switch(codec) {
  121. case SHORTEN: strcpy(strrchr(from,'.'), ".shn"); break;
  122. case WAVPACK: strcpy(strrchr(from,'.'), ".wv"); break;
  123. case RKAU: strcpy(strrchr(from,'.'), ".rka"); break;
  124. }
  125. strcpy(strrchr(to,'.'), ".flac");
  126. sprintf(prog, "%sflac_ren.exe", macdir);
  127. sprintf(cmdline, ""%s" "%s" "%s"", prog, from, to);
  128. flac_return_val = forkit(prog, cmdline);
  129. }
  130. return flac_return_val;
  131. }
  132. int execit(char *prog, char *args)
  133. {
  134. BOOL ok;
  135. STARTUPINFO startup_info;
  136. PROCESS_INFORMATION proc_info;
  137. GetStartupInfo(&startup_info);
  138. ok = CreateProcess(
  139. prog,
  140. args,
  141. 0, /*process security attributes*/
  142. 0, /*thread security attributes*/
  143. FALSE,
  144. 0, /*dwCreationFlags*/
  145. 0, /*environment*/
  146. 0, /*lpCurrentDirectory*/
  147. &startup_info,
  148. &proc_info
  149. );
  150. if(ok) {
  151. DWORD dw;
  152. dw = WaitForSingleObject(proc_info.hProcess, INFINITE);
  153. ok = (dw != 0xFFFFFFFF);
  154. CloseHandle(proc_info.hThread);
  155. CloseHandle(proc_info.hProcess);
  156. }
  157. return ok? 0 : -1;
  158. }
  159. int forkit(char *prog, char *args)
  160. {
  161. BOOL ok;
  162. STARTUPINFO startup_info;
  163. PROCESS_INFORMATION proc_info;
  164. GetStartupInfo(&startup_info);
  165. ok = CreateProcess(
  166. prog,
  167. args,
  168. 0, /*process security attributes*/
  169. 0, /*thread security attributes*/
  170. FALSE,
  171. DETACHED_PROCESS, /*dwCreationFlags*/
  172. 0, /*environment*/
  173. 0, /*lpCurrentDirectory*/
  174. &startup_info,
  175. &proc_info
  176. );
  177. if(ok) {
  178. CloseHandle(proc_info.hThread);
  179. CloseHandle(proc_info.hProcess);
  180. }
  181. return ok? 0 : -2;
  182. }