]>
Commit | Line | Data |
---|---|---|
48e2f01c | 1 | /* |
2 | * Copyright (C) 2013 nu774 | |
3 | * For conditions of distribution and use, see copyright notice in COPYING | |
4 | */ | |
5 | #if HAVE_CONFIG_H | |
6 | # include "config.h" | |
7 | #endif | |
8 | #if HAVE_STDINT_H | |
9 | # include <stdint.h> | |
10 | #endif | |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <stdarg.h> | |
dac71de3 | 15 | #include <assert.h> |
48e2f01c | 16 | #include <io.h> |
17 | #include <fcntl.h> | |
18 | #include <sys/timeb.h> | |
19 | #include "compat.h" | |
20 | #define WIN32_LEAN_AND_MEAN | |
21 | #include <windows.h> | |
22 | ||
23 | typedef struct | |
24 | { | |
25 | int newmode; | |
26 | } _startupinfo; | |
27 | ||
28 | extern | |
29 | int __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, _startupinfo *); | |
30 | ||
31 | int64_t aacenc_timer(void) | |
32 | { | |
33 | struct __timeb64 tv; | |
34 | _ftime64(&tv); | |
35 | return (int64_t)tv.time * 1000 + tv.millitm; | |
36 | } | |
37 | ||
38 | static | |
39 | int codepage_decode_wchar(int codepage, const char *from, wchar_t **to) | |
40 | { | |
41 | int nc = MultiByteToWideChar(codepage, 0, from, -1, 0, 0); | |
42 | if (nc == 0) | |
43 | return -1; | |
44 | *to = malloc(nc * sizeof(wchar_t)); | |
45 | MultiByteToWideChar(codepage, 0, from, -1, *to, nc); | |
46 | return 0; | |
47 | } | |
48 | ||
49 | static | |
50 | int codepage_encode_wchar(int codepage, const wchar_t *from, char **to) | |
51 | { | |
52 | int nc = WideCharToMultiByte(codepage, 0, from, -1, 0, 0, 0, 0); | |
53 | if (nc == 0) | |
54 | return -1; | |
55 | *to = malloc(nc); | |
56 | WideCharToMultiByte(codepage, 0, from, -1, *to, nc, 0, 0); | |
57 | return 0; | |
58 | } | |
59 | ||
60 | FILE *aacenc_fopen(const char *name, const char *mode) | |
61 | { | |
62 | wchar_t *wname, *wmode; | |
63 | FILE *fp; | |
64 | ||
65 | if (strcmp(name, "-") == 0) { | |
66 | fp = (mode[0] == 'r') ? stdin : stdout; | |
67 | _setmode(_fileno(fp), _O_BINARY); | |
68 | } else { | |
69 | codepage_decode_wchar(CP_UTF8, name, &wname); | |
70 | codepage_decode_wchar(CP_UTF8, mode, &wmode); | |
71 | fp = _wfopen(wname, wmode); | |
72 | free(wname); | |
73 | free(wmode); | |
74 | } | |
75 | return fp; | |
76 | } | |
77 | ||
bfb6aa3c | 78 | static char **__aacenc_argv__; |
79 | ||
80 | static | |
81 | void aacenc_free_mainargs(void) | |
82 | { | |
83 | char **p = __aacenc_argv__; | |
84 | for (; *p; ++p) | |
7b1f2136 | 85 | free(*p); |
bfb6aa3c | 86 | free(__aacenc_argv__); |
87 | } | |
88 | ||
48e2f01c | 89 | void aacenc_getmainargs(int *argc, char ***argv) |
90 | { | |
91 | int i; | |
92 | wchar_t **wargv, **envp; | |
93 | _startupinfo si = { 0 }; | |
94 | ||
95 | __wgetmainargs(argc, &wargv, &envp, 1, &si); | |
96 | *argv = malloc((*argc + 1) * sizeof(char*)); | |
97 | for (i = 0; i < *argc; ++i) | |
98 | codepage_encode_wchar(CP_UTF8, wargv[i], &(*argv)[i]); | |
99 | (*argv)[*argc] = 0; | |
bfb6aa3c | 100 | __aacenc_argv__ = *argv; |
101 | atexit(aacenc_free_mainargs); | |
48e2f01c | 102 | } |
103 | ||
104 | char *aacenc_to_utf8(const char *s) | |
105 | { | |
106 | return _strdup(s); | |
107 | } | |
108 | ||
dac71de3 | 109 | #if defined(__MINGW32__) && !defined(HAVE__VSCPRINTF) |
110 | int _vscprintf(const char *fmt, va_list ap) | |
111 | { | |
112 | static int (*fp_vscprintf)(const char *, va_list) = 0; | |
113 | if (!fp_vscprintf) { | |
114 | HANDLE h = GetModuleHandleA("msvcrt.dll"); | |
115 | FARPROC fp = GetProcAddress(h, "_vscprintf"); | |
116 | InterlockedCompareExchangePointer(&fp_vscprintf, fp, 0); | |
117 | } | |
118 | assert(fp_vscprintf); | |
119 | return fp_vscprintf(fmt, ap); | |
120 | } | |
121 | #endif | |
122 | ||
48e2f01c | 123 | int aacenc_fprintf(FILE *fp, const char *fmt, ...) |
124 | { | |
125 | va_list ap; | |
126 | int cnt; | |
127 | HANDLE fh = (HANDLE)_get_osfhandle(_fileno(fp)); | |
128 | ||
129 | if (GetFileType(fh) != FILE_TYPE_CHAR) { | |
130 | va_start(ap, fmt); | |
131 | cnt = vfprintf(fp, fmt, ap); | |
132 | va_end(ap); | |
133 | } else { | |
134 | char *s; | |
135 | wchar_t *ws; | |
136 | DWORD nw; | |
137 | ||
138 | va_start(ap, fmt); | |
139 | cnt = _vscprintf(fmt, ap); | |
140 | va_end(ap); | |
141 | ||
142 | s = malloc(cnt + 1); | |
143 | ||
144 | va_start(ap, fmt); | |
145 | cnt = _vsnprintf(s, cnt + 1, fmt, ap); | |
146 | va_end(ap); | |
147 | ||
148 | codepage_decode_wchar(CP_UTF8, s, &ws); | |
149 | free(s); | |
150 | fflush(fp); | |
151 | WriteConsoleW(fh, ws, cnt, &nw, 0); | |
152 | free(ws); | |
153 | } | |
154 | return cnt; | |
155 | } | |
156 | ||
5888fddc | 157 | const char *aacenc_basename(const char *path) |
158 | { | |
159 | /* | |
160 | * Since path is encoded with UTF-8, naive usage of strrchr() shoule be safe. | |
161 | */ | |
162 | const char *p = strrchr(path, '/'); | |
163 | const char *q = strrchr(path, '\\'); | |
164 | const char *r = strrchr(path, ':'); | |
165 | if (q > p) p = q; | |
166 | if (r > p) p = r; | |
167 | return p ? p + 1 : path; | |
168 | } |