]>
iEval git - fdkaac.git/blob - src/compat_win32.c
2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
19 #include <sys/timeb.h>
21 #define WIN32_LEAN_AND_MEAN
30 int __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, _startupinfo
*);
32 int64_t aacenc_timer(void)
34 #if HAVE_STRUCT___TIMEB64
41 return (int64_t)tv
.time
* 1000 + tv
.millitm
;
45 int codepage_decode_wchar(int codepage
, const char *from
, wchar_t **to
)
47 int nc
= MultiByteToWideChar(codepage
, 0, from
, -1, 0, 0);
50 *to
= malloc(nc
* sizeof(wchar_t));
51 MultiByteToWideChar(codepage
, 0, from
, -1, *to
, nc
);
56 int codepage_encode_wchar(int codepage
, const wchar_t *from
, char **to
)
58 int nc
= WideCharToMultiByte(codepage
, 0, from
, -1, 0, 0, 0, 0);
62 WideCharToMultiByte(codepage
, 0, from
, -1, *to
, nc
, 0, 0);
66 FILE *aacenc_fopen(const char *name
, const char *mode
)
68 wchar_t *wname
, *wmode
;
71 if (strcmp(name
, "-") == 0) {
72 fp
= (mode
[0] == 'r') ? stdin
: stdout
;
73 _setmode(_fileno(fp
), _O_BINARY
);
75 int share
= _SH_DENYRW
;
76 if (strchr(mode
, 'r') && !strchr(mode
, '+'))
78 codepage_decode_wchar(CP_UTF8
, name
, &wname
);
79 codepage_decode_wchar(CP_UTF8
, mode
, &wmode
);
80 fp
= _wfsopen(wname
, wmode
, share
);
87 static char **__aacenc_argv__
;
90 void aacenc_free_mainargs(void)
92 char **p
= __aacenc_argv__
;
95 free(__aacenc_argv__
);
98 void aacenc_getmainargs(int *argc
, char ***argv
)
101 wchar_t **wargv
, **envp
;
102 _startupinfo si
= { 0 };
104 __wgetmainargs(argc
, &wargv
, &envp
, 1, &si
);
105 *argv
= malloc((*argc
+ 1) * sizeof(char*));
106 for (i
= 0; i
< *argc
; ++i
)
107 codepage_encode_wchar(CP_UTF8
, wargv
[i
], &(*argv
)[i
]);
109 __aacenc_argv__
= *argv
;
110 atexit(aacenc_free_mainargs
);
113 char *aacenc_to_utf8(const char *s
)
118 #if defined(__MINGW32__) && !defined(HAVE__VSCPRINTF)
119 int _vscprintf(const char *fmt
, va_list ap
)
121 static int (*fp_vscprintf
)(const char *, va_list) = 0;
123 HANDLE h
= GetModuleHandleA("msvcrt.dll");
124 FARPROC fp
= GetProcAddress(h
, "_vscprintf");
125 InterlockedCompareExchangePointer(&fp_vscprintf
, fp
, 0);
127 assert(fp_vscprintf
);
128 return fp_vscprintf(fmt
, ap
);
132 int aacenc_fprintf(FILE *fp
, const char *fmt
, ...)
136 HANDLE fh
= (HANDLE
)_get_osfhandle(_fileno(fp
));
138 if (GetFileType(fh
) != FILE_TYPE_CHAR
) {
140 cnt
= vfprintf(fp
, fmt
, ap
);
148 cnt
= _vscprintf(fmt
, ap
);
154 cnt
= _vsnprintf(s
, cnt
+ 1, fmt
, ap
);
157 codepage_decode_wchar(CP_UTF8
, s
, &ws
);
160 WriteConsoleW(fh
, ws
, cnt
, &nw
, 0);
166 const char *aacenc_basename(const char *path
)
169 * Since path is encoded with UTF-8, naive usage of strrchr() shoule be safe.
171 const char *p
= strrchr(path
, '/');
172 const char *q
= strrchr(path
, '\\');
173 const char *r
= strrchr(path
, ':');
176 return p
? p
+ 1 : path
;
This page took 0.068111 seconds and 4 git commands to generate.