1159b3361Sopenharmony_ci/*
2159b3361Sopenharmony_ci *      Command line frontend program
3159b3361Sopenharmony_ci *
4159b3361Sopenharmony_ci *      Copyright (c) 1999 Mark Taylor
5159b3361Sopenharmony_ci *                    2000 Takehiro TOMINAGA
6159b3361Sopenharmony_ci *                    2010-2012 Robert Hegemann
7159b3361Sopenharmony_ci *
8159b3361Sopenharmony_ci * This library is free software; you can redistribute it and/or
9159b3361Sopenharmony_ci * modify it under the terms of the GNU Library General Public
10159b3361Sopenharmony_ci * License as published by the Free Software Foundation; either
11159b3361Sopenharmony_ci * version 2 of the License, or (at your option) any later version.
12159b3361Sopenharmony_ci *
13159b3361Sopenharmony_ci * This library is distributed in the hope that it will be useful,
14159b3361Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
15159b3361Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16159b3361Sopenharmony_ci * Library General Public License for more details.
17159b3361Sopenharmony_ci *
18159b3361Sopenharmony_ci * You should have received a copy of the GNU Library General Public
19159b3361Sopenharmony_ci * License along with this library; if not, write to the
20159b3361Sopenharmony_ci * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21159b3361Sopenharmony_ci * Boston, MA 02111-1307, USA.
22159b3361Sopenharmony_ci */
23159b3361Sopenharmony_ci
24159b3361Sopenharmony_ci/* $Id$ */
25159b3361Sopenharmony_ci
26159b3361Sopenharmony_ci#ifdef HAVE_CONFIG_H
27159b3361Sopenharmony_ci# include <config.h>
28159b3361Sopenharmony_ci#endif
29159b3361Sopenharmony_ci
30159b3361Sopenharmony_ci#include <assert.h>
31159b3361Sopenharmony_ci#include <stdio.h>
32159b3361Sopenharmony_ci
33159b3361Sopenharmony_ci#ifdef STDC_HEADERS
34159b3361Sopenharmony_ci# include <stdlib.h>
35159b3361Sopenharmony_ci# include <string.h>
36159b3361Sopenharmony_ci#else
37159b3361Sopenharmony_ci# ifndef HAVE_STRCHR
38159b3361Sopenharmony_ci#  define strchr index
39159b3361Sopenharmony_ci#  define strrchr rindex
40159b3361Sopenharmony_ci# endif
41159b3361Sopenharmony_cichar   *strchr(), *strrchr();
42159b3361Sopenharmony_ci# ifndef HAVE_MEMCPY
43159b3361Sopenharmony_ci#  define memcpy(d, s, n) bcopy ((s), (d), (n))
44159b3361Sopenharmony_ci#  define memmove(d, s, n) bcopy ((s), (d), (n))
45159b3361Sopenharmony_ci# endif
46159b3361Sopenharmony_ci#endif
47159b3361Sopenharmony_ci
48159b3361Sopenharmony_ci#ifdef HAVE_FCNTL_H
49159b3361Sopenharmony_ci# include <fcntl.h>
50159b3361Sopenharmony_ci#endif
51159b3361Sopenharmony_ci
52159b3361Sopenharmony_ci#ifdef __sun__
53159b3361Sopenharmony_ci/* woraround for SunOS 4.x, it has SEEK_* defined here */
54159b3361Sopenharmony_ci#include <unistd.h>
55159b3361Sopenharmony_ci#endif
56159b3361Sopenharmony_ci
57159b3361Sopenharmony_ci#ifdef __OS2__
58159b3361Sopenharmony_ci#define INCL_DOS
59159b3361Sopenharmony_ci#include <os2.h>
60159b3361Sopenharmony_ci#define PRTYC_IDLE 1
61159b3361Sopenharmony_ci#define PRTYC_REGULAR 2
62159b3361Sopenharmony_ci#define PRTYD_MINIMUM -31
63159b3361Sopenharmony_ci#define PRTYD_MAXIMUM 31
64159b3361Sopenharmony_ci#endif
65159b3361Sopenharmony_ci
66159b3361Sopenharmony_ci#if defined(_WIN32)
67159b3361Sopenharmony_ci# include <windows.h>
68159b3361Sopenharmony_ci#endif
69159b3361Sopenharmony_ci
70159b3361Sopenharmony_ci#ifdef __EMX__
71159b3361Sopenharmony_ci# include <float.h>
72159b3361Sopenharmony_ci#endif
73159b3361Sopenharmony_ci
74159b3361Sopenharmony_ci
75159b3361Sopenharmony_ci/*
76159b3361Sopenharmony_ci main.c is example code for how to use libmp3lame.a.  To use this library,
77159b3361Sopenharmony_ci you only need the library and lame.h.  All other .h files are private
78159b3361Sopenharmony_ci to the library.
79159b3361Sopenharmony_ci*/
80159b3361Sopenharmony_ci#include "lame.h"
81159b3361Sopenharmony_ci
82159b3361Sopenharmony_ci#include "console.h"
83159b3361Sopenharmony_ci#include "main.h"
84159b3361Sopenharmony_ci
85159b3361Sopenharmony_ci/* PLL 14/04/2000 */
86159b3361Sopenharmony_ci#if macintosh
87159b3361Sopenharmony_ci#include <console.h>
88159b3361Sopenharmony_ci#endif
89159b3361Sopenharmony_ci
90159b3361Sopenharmony_ci#ifdef WITH_DMALLOC
91159b3361Sopenharmony_ci#include <dmalloc.h>
92159b3361Sopenharmony_ci#endif
93159b3361Sopenharmony_ci
94159b3361Sopenharmony_ci
95159b3361Sopenharmony_cistatic int c_main(int argc, char *argv[]);
96159b3361Sopenharmony_ciextern int lame_main(lame_t gf, int argc, char *argv[]);
97159b3361Sopenharmony_ci
98159b3361Sopenharmony_ci
99159b3361Sopenharmony_ci/************************************************************************
100159b3361Sopenharmony_ci*
101159b3361Sopenharmony_ci* main
102159b3361Sopenharmony_ci*
103159b3361Sopenharmony_ci* PURPOSE:  MPEG-1,2 Layer III encoder with GPSYCHO
104159b3361Sopenharmony_ci* psychoacoustic model.
105159b3361Sopenharmony_ci*
106159b3361Sopenharmony_ci************************************************************************/
107159b3361Sopenharmony_ci
108159b3361Sopenharmony_ci
109159b3361Sopenharmony_ci#if defined( _WIN32 ) && !defined(__MINGW32__)
110159b3361Sopenharmony_cistatic void
111159b3361Sopenharmony_ciset_process_affinity()
112159b3361Sopenharmony_ci{
113159b3361Sopenharmony_ci#if 0
114159b3361Sopenharmony_ci    /* rh 061207
115159b3361Sopenharmony_ci       the following fix seems to be a workaround for a problem in the
116159b3361Sopenharmony_ci       parent process calling LAME. It would be better to fix the broken
117159b3361Sopenharmony_ci       application => code disabled.
118159b3361Sopenharmony_ci     */
119159b3361Sopenharmony_ci#if defined(_WIN32)
120159b3361Sopenharmony_ci    /* set affinity back to all CPUs.  Fix for EAC/lame on SMP systems from
121159b3361Sopenharmony_ci       "Todd Richmond" <todd.richmond@openwave.com> */
122159b3361Sopenharmony_ci    typedef BOOL(WINAPI * SPAMFunc) (HANDLE, DWORD_PTR);
123159b3361Sopenharmony_ci    SPAMFunc func;
124159b3361Sopenharmony_ci    SYSTEM_INFO si;
125159b3361Sopenharmony_ci
126159b3361Sopenharmony_ci    if ((func = (SPAMFunc) GetProcAddress(GetModuleHandleW(L"KERNEL32.DLL"),
127159b3361Sopenharmony_ci                                          "SetProcessAffinityMask")) != NULL) {
128159b3361Sopenharmony_ci        GetSystemInfo(&si);
129159b3361Sopenharmony_ci        func(GetCurrentProcess(), si.dwActiveProcessorMask);
130159b3361Sopenharmony_ci    }
131159b3361Sopenharmony_ci#endif
132159b3361Sopenharmony_ci#endif
133159b3361Sopenharmony_ci}
134159b3361Sopenharmony_ci#endif
135159b3361Sopenharmony_ci
136159b3361Sopenharmony_ci#if defined(WIN32)
137159b3361Sopenharmony_ci
138159b3361Sopenharmony_ci/**
139159b3361Sopenharmony_ci *  Long Filename support for the WIN32 platform
140159b3361Sopenharmony_ci *
141159b3361Sopenharmony_ci */
142159b3361Sopenharmony_ci
143159b3361Sopenharmony_civoid
144159b3361Sopenharmony_cidosToLongFileName(char *filename)
145159b3361Sopenharmony_ci{
146159b3361Sopenharmony_ci    const size_t MSIZE = PATH_MAX + 1 - 4; /*  we wanna add ".mp3" later */
147159b3361Sopenharmony_ci    WIN32_FIND_DATAA lpFindFileData;
148159b3361Sopenharmony_ci    HANDLE  h = FindFirstFileA(filename, &lpFindFileData);
149159b3361Sopenharmony_ci    if (h != INVALID_HANDLE_VALUE) {
150159b3361Sopenharmony_ci        size_t  a;
151159b3361Sopenharmony_ci        char   *q, *p;
152159b3361Sopenharmony_ci        FindClose(h);
153159b3361Sopenharmony_ci        for (a = 0; a < MSIZE; a++) {
154159b3361Sopenharmony_ci            if ('\0' == lpFindFileData.cFileName[a])
155159b3361Sopenharmony_ci                break;
156159b3361Sopenharmony_ci        }
157159b3361Sopenharmony_ci        if (a >= MSIZE || a == 0)
158159b3361Sopenharmony_ci            return;
159159b3361Sopenharmony_ci        q = strrchr(filename, '\\');
160159b3361Sopenharmony_ci        p = strrchr(filename, '/');
161159b3361Sopenharmony_ci        if (p - q > 0)
162159b3361Sopenharmony_ci            q = p;
163159b3361Sopenharmony_ci        if (q == NULL)
164159b3361Sopenharmony_ci            q = strrchr(filename, ':');
165159b3361Sopenharmony_ci        if (q == NULL)
166159b3361Sopenharmony_ci            strncpy(filename, lpFindFileData.cFileName, a);
167159b3361Sopenharmony_ci        else {
168159b3361Sopenharmony_ci            a += q - filename + 1;
169159b3361Sopenharmony_ci            if (a >= MSIZE)
170159b3361Sopenharmony_ci                return;
171159b3361Sopenharmony_ci            strncpy(++q, lpFindFileData.cFileName, MSIZE - a);
172159b3361Sopenharmony_ci        }
173159b3361Sopenharmony_ci    }
174159b3361Sopenharmony_ci}
175159b3361Sopenharmony_ci
176159b3361Sopenharmony_ciBOOL
177159b3361Sopenharmony_ciSetPriorityClassMacro(DWORD p)
178159b3361Sopenharmony_ci{
179159b3361Sopenharmony_ci    HANDLE  op = GetCurrentProcess();
180159b3361Sopenharmony_ci    return SetPriorityClass(op, p);
181159b3361Sopenharmony_ci}
182159b3361Sopenharmony_ci
183159b3361Sopenharmony_civoid
184159b3361Sopenharmony_cisetProcessPriority(int priority)
185159b3361Sopenharmony_ci{
186159b3361Sopenharmony_ci    switch (priority) {
187159b3361Sopenharmony_ci    case 0:
188159b3361Sopenharmony_ci    case 1:
189159b3361Sopenharmony_ci        SetPriorityClassMacro(IDLE_PRIORITY_CLASS);
190159b3361Sopenharmony_ci        console_printf("==> Priority set to Low.\n");
191159b3361Sopenharmony_ci        break;
192159b3361Sopenharmony_ci    default:
193159b3361Sopenharmony_ci    case 2:
194159b3361Sopenharmony_ci        SetPriorityClassMacro(NORMAL_PRIORITY_CLASS);
195159b3361Sopenharmony_ci        console_printf("==> Priority set to Normal.\n");
196159b3361Sopenharmony_ci        break;
197159b3361Sopenharmony_ci    case 3:
198159b3361Sopenharmony_ci    case 4:
199159b3361Sopenharmony_ci        SetPriorityClassMacro(HIGH_PRIORITY_CLASS);
200159b3361Sopenharmony_ci        console_printf("==> Priority set to High.\n");
201159b3361Sopenharmony_ci        break;
202159b3361Sopenharmony_ci    }
203159b3361Sopenharmony_ci}
204159b3361Sopenharmony_ci#endif
205159b3361Sopenharmony_ci
206159b3361Sopenharmony_ci
207159b3361Sopenharmony_ci#if defined(__OS2__)
208159b3361Sopenharmony_ci/* OS/2 priority functions */
209159b3361Sopenharmony_civoid
210159b3361Sopenharmony_cisetProcessPriority(int priority)
211159b3361Sopenharmony_ci{
212159b3361Sopenharmony_ci    int     rc;
213159b3361Sopenharmony_ci
214159b3361Sopenharmony_ci    switch (priority) {
215159b3361Sopenharmony_ci
216159b3361Sopenharmony_ci    case 0:
217159b3361Sopenharmony_ci        rc = DosSetPriority(0, /* Scope: only one process */
218159b3361Sopenharmony_ci                            PRTYC_IDLE, /* select priority class (idle, regular, etc) */
219159b3361Sopenharmony_ci                            0, /* set delta */
220159b3361Sopenharmony_ci                            0); /* Assume current process */
221159b3361Sopenharmony_ci        console_printf("==> Priority set to 0 (Low priority).\n");
222159b3361Sopenharmony_ci        break;
223159b3361Sopenharmony_ci
224159b3361Sopenharmony_ci    case 1:
225159b3361Sopenharmony_ci        rc = DosSetPriority(0, /* Scope: only one process */
226159b3361Sopenharmony_ci                            PRTYC_IDLE, /* select priority class (idle, regular, etc) */
227159b3361Sopenharmony_ci                            PRTYD_MAXIMUM, /* set delta */
228159b3361Sopenharmony_ci                            0); /* Assume current process */
229159b3361Sopenharmony_ci        console_printf("==> Priority set to 1 (Medium priority).\n");
230159b3361Sopenharmony_ci        break;
231159b3361Sopenharmony_ci
232159b3361Sopenharmony_ci    case 2:
233159b3361Sopenharmony_ci        rc = DosSetPriority(0, /* Scope: only one process */
234159b3361Sopenharmony_ci                            PRTYC_REGULAR, /* select priority class (idle, regular, etc) */
235159b3361Sopenharmony_ci                            PRTYD_MINIMUM, /* set delta */
236159b3361Sopenharmony_ci                            0); /* Assume current process */
237159b3361Sopenharmony_ci        console_printf("==> Priority set to 2 (Regular priority).\n");
238159b3361Sopenharmony_ci        break;
239159b3361Sopenharmony_ci
240159b3361Sopenharmony_ci    case 3:
241159b3361Sopenharmony_ci        rc = DosSetPriority(0, /* Scope: only one process */
242159b3361Sopenharmony_ci                            PRTYC_REGULAR, /* select priority class (idle, regular, etc) */
243159b3361Sopenharmony_ci                            0, /* set delta */
244159b3361Sopenharmony_ci                            0); /* Assume current process */
245159b3361Sopenharmony_ci        console_printf("==> Priority set to 3 (High priority).\n");
246159b3361Sopenharmony_ci        break;
247159b3361Sopenharmony_ci
248159b3361Sopenharmony_ci    case 4:
249159b3361Sopenharmony_ci        rc = DosSetPriority(0, /* Scope: only one process */
250159b3361Sopenharmony_ci                            PRTYC_REGULAR, /* select priority class (idle, regular, etc) */
251159b3361Sopenharmony_ci                            PRTYD_MAXIMUM, /* set delta */
252159b3361Sopenharmony_ci                            0); /* Assume current process */
253159b3361Sopenharmony_ci        console_printf("==> Priority set to 4 (Maximum priority). I hope you enjoy it :)\n");
254159b3361Sopenharmony_ci        break;
255159b3361Sopenharmony_ci
256159b3361Sopenharmony_ci    default:
257159b3361Sopenharmony_ci        console_printf("==> Invalid priority specified! Assuming idle priority.\n");
258159b3361Sopenharmony_ci    }
259159b3361Sopenharmony_ci}
260159b3361Sopenharmony_ci#endif
261159b3361Sopenharmony_ci
262159b3361Sopenharmony_ci
263159b3361Sopenharmony_ci/***********************************************************************
264159b3361Sopenharmony_ci*
265159b3361Sopenharmony_ci*  Message Output
266159b3361Sopenharmony_ci*
267159b3361Sopenharmony_ci***********************************************************************/
268159b3361Sopenharmony_ci
269159b3361Sopenharmony_ci
270159b3361Sopenharmony_ci#if defined( _WIN32 ) && !defined(__MINGW32__)
271159b3361Sopenharmony_ci/* Idea for unicode support in LAME, work in progress
272159b3361Sopenharmony_ci * - map UTF-16 to UTF-8
273159b3361Sopenharmony_ci * - advantage, the rest can be kept unchanged (mostly)
274159b3361Sopenharmony_ci * - make sure, fprintf on console is in correct code page
275159b3361Sopenharmony_ci *   + normal text in source code is in ASCII anyway
276159b3361Sopenharmony_ci *   + ID3 tags and filenames coming from command line need attention
277159b3361Sopenharmony_ci * - call wfopen with UTF-16 names where needed
278159b3361Sopenharmony_ci *
279159b3361Sopenharmony_ci * why not wchar_t all the way?
280159b3361Sopenharmony_ci * well, that seems to be a big mess and not portable at all
281159b3361Sopenharmony_ci */
282159b3361Sopenharmony_ci#ifndef NDEBUG
283159b3361Sopenharmony_ci#define _CRTDBG_MAP_ALLOC
284159b3361Sopenharmony_ci#include <stdlib.h>
285159b3361Sopenharmony_ci#include <crtdbg.h>
286159b3361Sopenharmony_ci#endif
287159b3361Sopenharmony_ci#include <wchar.h>
288159b3361Sopenharmony_ci#include <mbstring.h>
289159b3361Sopenharmony_ci
290159b3361Sopenharmony_cistatic wchar_t *mbsToUnicode(const char *mbstr, int code_page)
291159b3361Sopenharmony_ci{
292159b3361Sopenharmony_ci  int n = MultiByteToWideChar(code_page, 0, mbstr, -1, NULL, 0);
293159b3361Sopenharmony_ci  wchar_t* wstr = malloc( n*sizeof(wstr[0]) );
294159b3361Sopenharmony_ci  if ( wstr !=0 ) {
295159b3361Sopenharmony_ci    n = MultiByteToWideChar(code_page, 0, mbstr, -1, wstr, n);
296159b3361Sopenharmony_ci    if ( n==0 ) {
297159b3361Sopenharmony_ci      free( wstr );
298159b3361Sopenharmony_ci      wstr = 0;
299159b3361Sopenharmony_ci    }
300159b3361Sopenharmony_ci  }
301159b3361Sopenharmony_ci  return wstr;
302159b3361Sopenharmony_ci}
303159b3361Sopenharmony_ci
304159b3361Sopenharmony_cistatic char *unicodeToMbs(const wchar_t *wstr, int code_page)
305159b3361Sopenharmony_ci{
306159b3361Sopenharmony_ci  int n = 1+WideCharToMultiByte(code_page, 0, wstr, -1, 0, 0, 0, 0);
307159b3361Sopenharmony_ci  char* mbstr = malloc( n*sizeof(mbstr[0]) );
308159b3361Sopenharmony_ci  if ( mbstr !=0 ) {
309159b3361Sopenharmony_ci    n = WideCharToMultiByte(code_page, 0, wstr, -1, mbstr, n, 0, 0);
310159b3361Sopenharmony_ci    if( n == 0 ){
311159b3361Sopenharmony_ci      free( mbstr );
312159b3361Sopenharmony_ci      mbstr = 0;
313159b3361Sopenharmony_ci    }
314159b3361Sopenharmony_ci  }
315159b3361Sopenharmony_ci  return mbstr;
316159b3361Sopenharmony_ci}
317159b3361Sopenharmony_ci
318159b3361Sopenharmony_cichar* mbsToMbs(const char* str, int cp_from, int cp_to)
319159b3361Sopenharmony_ci{
320159b3361Sopenharmony_ci  wchar_t* wstr = mbsToUnicode(str, cp_from);
321159b3361Sopenharmony_ci  if ( wstr != 0 ) {
322159b3361Sopenharmony_ci    char* local8bit = unicodeToMbs(wstr, cp_to);
323159b3361Sopenharmony_ci    free( wstr );
324159b3361Sopenharmony_ci    return local8bit;
325159b3361Sopenharmony_ci  }
326159b3361Sopenharmony_ci  return 0;
327159b3361Sopenharmony_ci}
328159b3361Sopenharmony_ci
329159b3361Sopenharmony_cienum { cp_utf8, cp_console, cp_actual };
330159b3361Sopenharmony_ci
331159b3361Sopenharmony_ciwchar_t *utf8ToUnicode(const char *mbstr)
332159b3361Sopenharmony_ci{
333159b3361Sopenharmony_ci  return mbsToUnicode(mbstr, CP_UTF8);
334159b3361Sopenharmony_ci}
335159b3361Sopenharmony_ci
336159b3361Sopenharmony_cichar *unicodeToUtf8(const wchar_t *wstr)
337159b3361Sopenharmony_ci{
338159b3361Sopenharmony_ci  return unicodeToMbs(wstr, CP_UTF8);
339159b3361Sopenharmony_ci}
340159b3361Sopenharmony_ci
341159b3361Sopenharmony_cichar* utf8ToLocal8Bit(const char* str)
342159b3361Sopenharmony_ci{
343159b3361Sopenharmony_ci  return mbsToMbs(str, CP_UTF8, CP_ACP);
344159b3361Sopenharmony_ci}
345159b3361Sopenharmony_ci
346159b3361Sopenharmony_cichar* utf8ToConsole8Bit(const char* str)
347159b3361Sopenharmony_ci{
348159b3361Sopenharmony_ci  return mbsToMbs(str, CP_UTF8, GetConsoleOutputCP());
349159b3361Sopenharmony_ci}
350159b3361Sopenharmony_ci
351159b3361Sopenharmony_cichar* local8BitToUtf8(const char* str)
352159b3361Sopenharmony_ci{
353159b3361Sopenharmony_ci  return mbsToMbs(str, CP_ACP, CP_UTF8);
354159b3361Sopenharmony_ci}
355159b3361Sopenharmony_ci
356159b3361Sopenharmony_cichar* console8BitToUtf8(const char* str)
357159b3361Sopenharmony_ci{
358159b3361Sopenharmony_ci  return mbsToMbs(str, GetConsoleOutputCP(), CP_UTF8);
359159b3361Sopenharmony_ci}
360159b3361Sopenharmony_ci
361159b3361Sopenharmony_cichar* utf8ToLatin1(char const* str)
362159b3361Sopenharmony_ci{
363159b3361Sopenharmony_ci  return mbsToMbs(str, CP_UTF8, 28591); /* Latin-1 is code page 28591 */
364159b3361Sopenharmony_ci}
365159b3361Sopenharmony_ci
366159b3361Sopenharmony_ciunsigned short* utf8ToUtf16(char const* mbstr) /* additional Byte-Order-Marker */
367159b3361Sopenharmony_ci{
368159b3361Sopenharmony_ci  int n = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, NULL, 0);
369159b3361Sopenharmony_ci  wchar_t* wstr = malloc( (n+1)*sizeof(wstr[0]) );
370159b3361Sopenharmony_ci  if ( wstr !=0 ) {
371159b3361Sopenharmony_ci    wstr[0] = 0xfeff; /* BOM */
372159b3361Sopenharmony_ci    n = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, wstr+1, n);
373159b3361Sopenharmony_ci    if ( n==0 ) {
374159b3361Sopenharmony_ci      free( wstr );
375159b3361Sopenharmony_ci      wstr = 0;
376159b3361Sopenharmony_ci    }
377159b3361Sopenharmony_ci  }
378159b3361Sopenharmony_ci  return wstr;
379159b3361Sopenharmony_ci}
380159b3361Sopenharmony_ci
381159b3361Sopenharmony_cistatic
382159b3361Sopenharmony_civoid setDebugMode()
383159b3361Sopenharmony_ci{
384159b3361Sopenharmony_ci#ifndef NDEBUG
385159b3361Sopenharmony_ci    if ( IsDebuggerPresent() ) {
386159b3361Sopenharmony_ci        // Get current flag
387159b3361Sopenharmony_ci        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
388159b3361Sopenharmony_ci        //tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
389159b3361Sopenharmony_ci        tmpFlag |= _CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF;
390159b3361Sopenharmony_ci        // Set flag to the new value.
391159b3361Sopenharmony_ci        _CrtSetDbgFlag( tmpFlag );
392159b3361Sopenharmony_ci        _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
393159b3361Sopenharmony_ci        _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
394159b3361Sopenharmony_ci    }
395159b3361Sopenharmony_ci#endif
396159b3361Sopenharmony_ci}
397159b3361Sopenharmony_ci
398159b3361Sopenharmony_ciint wmain(int argc, wchar_t* argv[])
399159b3361Sopenharmony_ci{
400159b3361Sopenharmony_ci  char **utf8_argv;
401159b3361Sopenharmony_ci  int i, ret;
402159b3361Sopenharmony_ci  setDebugMode();
403159b3361Sopenharmony_ci  utf8_argv = calloc(argc, sizeof(char*));
404159b3361Sopenharmony_ci  for (i = 0; i < argc; ++i) {
405159b3361Sopenharmony_ci    utf8_argv[i] = unicodeToUtf8(argv[i]);
406159b3361Sopenharmony_ci  }
407159b3361Sopenharmony_ci  ret = c_main(argc, utf8_argv);
408159b3361Sopenharmony_ci  for (i = 0; i < argc; ++i) {
409159b3361Sopenharmony_ci    free( utf8_argv[i] );
410159b3361Sopenharmony_ci  }
411159b3361Sopenharmony_ci  free( utf8_argv );
412159b3361Sopenharmony_ci  return ret;
413159b3361Sopenharmony_ci}
414159b3361Sopenharmony_ci
415159b3361Sopenharmony_ciFILE* lame_fopen(char const* file, char const* mode)
416159b3361Sopenharmony_ci{
417159b3361Sopenharmony_ci    FILE* fh = 0;
418159b3361Sopenharmony_ci    wchar_t* wfile = utf8ToUnicode(file);
419159b3361Sopenharmony_ci    wchar_t* wmode = utf8ToUnicode(mode);
420159b3361Sopenharmony_ci    if (wfile != 0 && wmode != 0) {
421159b3361Sopenharmony_ci        fh = _wfopen(wfile, wmode);
422159b3361Sopenharmony_ci    }
423159b3361Sopenharmony_ci    else {
424159b3361Sopenharmony_ci        fh = fopen(file, mode);
425159b3361Sopenharmony_ci    }
426159b3361Sopenharmony_ci    free(wfile);
427159b3361Sopenharmony_ci    free(wmode);
428159b3361Sopenharmony_ci    return fh;
429159b3361Sopenharmony_ci}
430159b3361Sopenharmony_ci
431159b3361Sopenharmony_cichar* lame_getenv(char const* var)
432159b3361Sopenharmony_ci{
433159b3361Sopenharmony_ci    char* str = 0;
434159b3361Sopenharmony_ci    wchar_t* wvar = utf8ToUnicode(var);
435159b3361Sopenharmony_ci    if (wvar != 0) {
436159b3361Sopenharmony_ci        wchar_t* wstr = _wgetenv(wvar);
437159b3361Sopenharmony_ci        if (wstr != 0) {
438159b3361Sopenharmony_ci            str = unicodeToUtf8(wstr);
439159b3361Sopenharmony_ci        }
440159b3361Sopenharmony_ci    }
441159b3361Sopenharmony_ci    free(wvar);
442159b3361Sopenharmony_ci    return str;
443159b3361Sopenharmony_ci}
444159b3361Sopenharmony_ci
445159b3361Sopenharmony_ci#else
446159b3361Sopenharmony_ci
447159b3361Sopenharmony_ciFILE* lame_fopen(char const* file, char const* mode)
448159b3361Sopenharmony_ci{
449159b3361Sopenharmony_ci    return fopen(file, mode);
450159b3361Sopenharmony_ci}
451159b3361Sopenharmony_ci
452159b3361Sopenharmony_cichar* lame_getenv(char const* var)
453159b3361Sopenharmony_ci{
454159b3361Sopenharmony_ci    char* str = getenv(var);
455159b3361Sopenharmony_ci    if (str) {
456159b3361Sopenharmony_ci        return strdup(str);
457159b3361Sopenharmony_ci    }
458159b3361Sopenharmony_ci    return 0;
459159b3361Sopenharmony_ci}
460159b3361Sopenharmony_ci
461159b3361Sopenharmony_ciint main(int argc, char *argv[])
462159b3361Sopenharmony_ci{
463159b3361Sopenharmony_ci    return c_main(argc, argv);
464159b3361Sopenharmony_ci}
465159b3361Sopenharmony_ci
466159b3361Sopenharmony_ci#endif
467159b3361Sopenharmony_ci
468159b3361Sopenharmony_ci
469159b3361Sopenharmony_ci
470159b3361Sopenharmony_ci
471159b3361Sopenharmony_cistatic int
472159b3361Sopenharmony_cic_main(int argc, char *argv[])
473159b3361Sopenharmony_ci{
474159b3361Sopenharmony_ci    lame_t  gf;
475159b3361Sopenharmony_ci    int     ret;
476159b3361Sopenharmony_ci
477159b3361Sopenharmony_ci#if macintosh
478159b3361Sopenharmony_ci    argc = ccommand(&argv);
479159b3361Sopenharmony_ci#endif
480159b3361Sopenharmony_ci#ifdef __EMX__
481159b3361Sopenharmony_ci    /* This gives wildcard expansion on Non-POSIX shells with OS/2 */
482159b3361Sopenharmony_ci    _wildcard(&argc, &argv);
483159b3361Sopenharmony_ci
484159b3361Sopenharmony_ci    /* This prevents SIGFPE */
485159b3361Sopenharmony_ci    _control87(MCW_EM, MCW_EM);
486159b3361Sopenharmony_ci#endif
487159b3361Sopenharmony_ci#if defined( _WIN32 ) && !defined(__MINGW32__)
488159b3361Sopenharmony_ci    set_process_affinity();
489159b3361Sopenharmony_ci#endif
490159b3361Sopenharmony_ci
491159b3361Sopenharmony_ci    frontend_open_console();
492159b3361Sopenharmony_ci    gf = lame_init(); /* initialize libmp3lame */
493159b3361Sopenharmony_ci    if (NULL == gf) {
494159b3361Sopenharmony_ci        error_printf("fatal error during initialization\n");
495159b3361Sopenharmony_ci        ret = 1;
496159b3361Sopenharmony_ci    }
497159b3361Sopenharmony_ci    else {
498159b3361Sopenharmony_ci        ret = lame_main(gf, argc, argv);
499159b3361Sopenharmony_ci        lame_close(gf);
500159b3361Sopenharmony_ci    }
501159b3361Sopenharmony_ci    frontend_close_console();
502159b3361Sopenharmony_ci    return ret;
503159b3361Sopenharmony_ci}
504