take care of COPYRIGHT-SIGN in UTF-8
[fdkaac.git] / src / compat_win32.c
CommitLineData
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
23typedef struct
24{
25 int newmode;
26} _startupinfo;
27
28extern
29int __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, _startupinfo *);
30
31int64_t aacenc_timer(void)
32{
33 struct __timeb64 tv;
34 _ftime64(&tv);
35 return (int64_t)tv.time * 1000 + tv.millitm;
36}
37
38static
39int 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
49static
50int 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
60FILE *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
78void aacenc_getmainargs(int *argc, char ***argv)
79{
80 int i;
81 wchar_t **wargv, **envp;
82 _startupinfo si = { 0 };
83
84 __wgetmainargs(argc, &wargv, &envp, 1, &si);
85 *argv = malloc((*argc + 1) * sizeof(char*));
86 for (i = 0; i < *argc; ++i)
87 codepage_encode_wchar(CP_UTF8, wargv[i], &(*argv)[i]);
88 (*argv)[*argc] = 0;
89}
90
91char *aacenc_to_utf8(const char *s)
92{
93 return _strdup(s);
94}
95
dac71de3 96#if defined(__MINGW32__) && !defined(HAVE__VSCPRINTF)
97int _vscprintf(const char *fmt, va_list ap)
98{
99 static int (*fp_vscprintf)(const char *, va_list) = 0;
100 if (!fp_vscprintf) {
101 HANDLE h = GetModuleHandleA("msvcrt.dll");
102 FARPROC fp = GetProcAddress(h, "_vscprintf");
103 InterlockedCompareExchangePointer(&fp_vscprintf, fp, 0);
104 }
105 assert(fp_vscprintf);
106 return fp_vscprintf(fmt, ap);
107}
108#endif
109
48e2f01c 110int aacenc_fprintf(FILE *fp, const char *fmt, ...)
111{
112 va_list ap;
113 int cnt;
114 HANDLE fh = (HANDLE)_get_osfhandle(_fileno(fp));
115
116 if (GetFileType(fh) != FILE_TYPE_CHAR) {
117 va_start(ap, fmt);
118 cnt = vfprintf(fp, fmt, ap);
119 va_end(ap);
120 } else {
121 char *s;
122 wchar_t *ws;
123 DWORD nw;
124
125 va_start(ap, fmt);
126 cnt = _vscprintf(fmt, ap);
127 va_end(ap);
128
129 s = malloc(cnt + 1);
130
131 va_start(ap, fmt);
132 cnt = _vsnprintf(s, cnt + 1, fmt, ap);
133 va_end(ap);
134
135 codepage_decode_wchar(CP_UTF8, s, &ws);
136 free(s);
137 fflush(fp);
138 WriteConsoleW(fh, ws, cnt, &nw, 0);
139 free(ws);
140 }
141 return cnt;
142}
143
5888fddc 144const char *aacenc_basename(const char *path)
145{
146/*
147 * Since path is encoded with UTF-8, naive usage of strrchr() shoule be safe.
148 */
149 const char *p = strrchr(path, '/');
150 const char *q = strrchr(path, '\\');
151 const char *r = strrchr(path, ':');
152 if (q > p) p = q;
153 if (r > p) p = r;
154 return p ? p + 1 : path;
155}
This page took 0.017939 seconds and 4 git commands to generate.