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> |
15 | #include <io.h> |
16 | #include <fcntl.h> |
17 | #include <sys/timeb.h> |
18 | #include "compat.h" |
19 | #define WIN32_LEAN_AND_MEAN |
20 | #include <windows.h> |
21 | |
22 | typedef struct |
23 | { |
24 | int newmode; |
25 | } _startupinfo; |
26 | |
27 | extern |
28 | int __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, _startupinfo *); |
29 | |
30 | int64_t aacenc_timer(void) |
31 | { |
32 | struct __timeb64 tv; |
33 | _ftime64(&tv); |
34 | return (int64_t)tv.time * 1000 + tv.millitm; |
35 | } |
36 | |
37 | static |
38 | int codepage_decode_wchar(int codepage, const char *from, wchar_t **to) |
39 | { |
40 | int nc = MultiByteToWideChar(codepage, 0, from, -1, 0, 0); |
41 | if (nc == 0) |
42 | return -1; |
43 | *to = malloc(nc * sizeof(wchar_t)); |
44 | MultiByteToWideChar(codepage, 0, from, -1, *to, nc); |
45 | return 0; |
46 | } |
47 | |
48 | static |
49 | int codepage_encode_wchar(int codepage, const wchar_t *from, char **to) |
50 | { |
51 | int nc = WideCharToMultiByte(codepage, 0, from, -1, 0, 0, 0, 0); |
52 | if (nc == 0) |
53 | return -1; |
54 | *to = malloc(nc); |
55 | WideCharToMultiByte(codepage, 0, from, -1, *to, nc, 0, 0); |
56 | return 0; |
57 | } |
58 | |
59 | FILE *aacenc_fopen(const char *name, const char *mode) |
60 | { |
61 | wchar_t *wname, *wmode; |
62 | FILE *fp; |
63 | |
64 | if (strcmp(name, "-") == 0) { |
65 | fp = (mode[0] == 'r') ? stdin : stdout; |
66 | _setmode(_fileno(fp), _O_BINARY); |
67 | } else { |
68 | codepage_decode_wchar(CP_UTF8, name, &wname); |
69 | codepage_decode_wchar(CP_UTF8, mode, &wmode); |
70 | fp = _wfopen(wname, wmode); |
71 | free(wname); |
72 | free(wmode); |
73 | } |
74 | return fp; |
75 | } |
76 | |
77 | void aacenc_getmainargs(int *argc, char ***argv) |
78 | { |
79 | int i; |
80 | wchar_t **wargv, **envp; |
81 | _startupinfo si = { 0 }; |
82 | |
83 | __wgetmainargs(argc, &wargv, &envp, 1, &si); |
84 | *argv = malloc((*argc + 1) * sizeof(char*)); |
85 | for (i = 0; i < *argc; ++i) |
86 | codepage_encode_wchar(CP_UTF8, wargv[i], &(*argv)[i]); |
87 | (*argv)[*argc] = 0; |
88 | } |
89 | |
90 | char *aacenc_to_utf8(const char *s) |
91 | { |
92 | return _strdup(s); |
93 | } |
94 | |
95 | int aacenc_fprintf(FILE *fp, const char *fmt, ...) |
96 | { |
97 | va_list ap; |
98 | int cnt; |
99 | HANDLE fh = (HANDLE)_get_osfhandle(_fileno(fp)); |
100 | |
101 | if (GetFileType(fh) != FILE_TYPE_CHAR) { |
102 | va_start(ap, fmt); |
103 | cnt = vfprintf(fp, fmt, ap); |
104 | va_end(ap); |
105 | } else { |
106 | char *s; |
107 | wchar_t *ws; |
108 | DWORD nw; |
109 | |
110 | va_start(ap, fmt); |
111 | cnt = _vscprintf(fmt, ap); |
112 | va_end(ap); |
113 | |
114 | s = malloc(cnt + 1); |
115 | |
116 | va_start(ap, fmt); |
117 | cnt = _vsnprintf(s, cnt + 1, fmt, ap); |
118 | va_end(ap); |
119 | |
120 | codepage_decode_wchar(CP_UTF8, s, &ws); |
121 | free(s); |
122 | fflush(fp); |
123 | WriteConsoleW(fh, ws, cnt, &nw, 0); |
124 | free(ws); |
125 | } |
126 | return cnt; |
127 | } |
128 | |
5888fddc |
129 | const char *aacenc_basename(const char *path) |
130 | { |
131 | /* |
132 | * Since path is encoded with UTF-8, naive usage of strrchr() shoule be safe. |
133 | */ |
134 | const char *p = strrchr(path, '/'); |
135 | const char *q = strrchr(path, '\\'); |
136 | const char *r = strrchr(path, ':'); |
137 | if (q > p) p = q; |
138 | if (r > p) p = r; |
139 | return p ? p + 1 : path; |
140 | } |