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)
36 return (int64_t)tv
.time
* 1000 + tv
.millitm
;
40 int codepage_decode_wchar(int codepage
, const char *from
, wchar_t **to
)
42 int nc
= MultiByteToWideChar(codepage
, 0, from
, -1, 0, 0);
45 *to
= malloc(nc
* sizeof(wchar_t));
46 MultiByteToWideChar(codepage
, 0, from
, -1, *to
, nc
);
51 int codepage_encode_wchar(int codepage
, const wchar_t *from
, char **to
)
53 int nc
= WideCharToMultiByte(codepage
, 0, from
, -1, 0, 0, 0, 0);
57 WideCharToMultiByte(codepage
, 0, from
, -1, *to
, nc
, 0, 0);
61 FILE *aacenc_fopen(const char *name
, const char *mode
)
63 wchar_t *wname
, *wmode
;
66 if (strcmp(name
, "-") == 0) {
67 fp
= (mode
[0] == 'r') ? stdin
: stdout
;
68 _setmode(_fileno(fp
), _O_BINARY
);
70 int share
= _SH_DENYRW
;
71 if (strchr(mode
, 'r') && !strchr(mode
, '+'))
73 codepage_decode_wchar(CP_UTF8
, name
, &wname
);
74 codepage_decode_wchar(CP_UTF8
, mode
, &wmode
);
75 fp
= _wfsopen(wname
, wmode
, share
);
82 static char **__aacenc_argv__
;
85 void aacenc_free_mainargs(void)
87 char **p
= __aacenc_argv__
;
90 free(__aacenc_argv__
);
93 void aacenc_getmainargs(int *argc
, char ***argv
)
96 wchar_t **wargv
, **envp
;
97 _startupinfo si
= { 0 };
99 __wgetmainargs(argc
, &wargv
, &envp
, 1, &si
);
100 *argv
= malloc((*argc
+ 1) * sizeof(char*));
101 for (i
= 0; i
< *argc
; ++i
)
102 codepage_encode_wchar(CP_UTF8
, wargv
[i
], &(*argv
)[i
]);
104 __aacenc_argv__
= *argv
;
105 atexit(aacenc_free_mainargs
);
108 char *aacenc_to_utf8(const char *s
)
113 #if defined(__MINGW32__) && !defined(HAVE__VSCPRINTF)
114 int _vscprintf(const char *fmt
, va_list ap
)
116 static int (*fp_vscprintf
)(const char *, va_list) = 0;
118 HANDLE h
= GetModuleHandleA("msvcrt.dll");
119 FARPROC fp
= GetProcAddress(h
, "_vscprintf");
120 InterlockedCompareExchangePointer(&fp_vscprintf
, fp
, 0);
122 assert(fp_vscprintf
);
123 return fp_vscprintf(fmt
, ap
);
127 int aacenc_fprintf(FILE *fp
, const char *fmt
, ...)
131 HANDLE fh
= (HANDLE
)_get_osfhandle(_fileno(fp
));
133 if (GetFileType(fh
) != FILE_TYPE_CHAR
) {
135 cnt
= vfprintf(fp
, fmt
, ap
);
143 cnt
= _vscprintf(fmt
, ap
);
149 cnt
= _vsnprintf(s
, cnt
+ 1, fmt
, ap
);
152 codepage_decode_wchar(CP_UTF8
, s
, &ws
);
155 WriteConsoleW(fh
, ws
, cnt
, &nw
, 0);
161 const char *aacenc_basename(const char *path
)
164 * Since path is encoded with UTF-8, naive usage of strrchr() shoule be safe.
166 const char *p
= strrchr(path
, '/');
167 const char *q
= strrchr(path
, '\\');
168 const char *r
= strrchr(path
, ':');
171 return p
? p
+ 1 : path
;
This page took 0.025682 seconds and 4 git commands to generate.