Bump version.
[LameXP.git] / etc / Patches / deprecated / SoX-v14.4.1-MSVC10-UTF8.V2.diff
blob6a7e2787a3b3a8f1221b458c8e42d49f87bb6574
1 src/8svx.c | 3 +-
2 src/adpcm.c | 1 +
3 src/effects_i.c | 3 +-
4 src/formats.c | 9 ++-
5 src/libsox.c | 2 +
6 src/libsox_i.c | 7 +-
7 src/noiseprof.c | 3 +-
8 src/sox.c | 37 +++++++---
9 src/unicode_support.c | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++
10 src/unicode_support.h | 49 +++++++++++++
11 src/util.h | 5 ++
12 11 files changed, 299 insertions(+), 19 deletions(-)
14 diff --git a/src/8svx.c b/src/8svx.c
15 index 63c30e6..fd712f4 100644
16 --- a/src/8svx.c
17 +++ b/src/8svx.c
18 @@ -1,6 +1,7 @@
19 /* Amiga 8SVX format handler: W V Neisius, February 1992 */
21 #include "sox_i.h"
22 +#include "unicode_support.h"
24 #include <errno.h>
25 #include <string.h>
26 @@ -161,7 +162,7 @@ static int startread(sox_format_t * ft)
27 chan1_pos = lsx_tell(ft);
29 for (i = 1; i < channels; i++) {
30 - if ((p->ch[i] = fopen(ft->filename, "rb")) == NULL)
31 + if ((p->ch[i] = fopen_utf8(ft->filename, "rb")) == NULL)
33 lsx_fail_errno(ft,errno,"Can't open channel file '%s'",
34 ft->filename);
35 diff --git a/src/adpcm.c b/src/adpcm.c
36 index 2e13867..15c27c2 100644
37 --- a/src/adpcm.c
38 +++ b/src/adpcm.c
39 @@ -33,6 +33,7 @@
41 #include "sox_i.h"
42 #include "adpcm.h"
43 +#include "unicode_support.h"
45 #include <sys/types.h>
46 #include <stdio.h>
47 diff --git a/src/effects_i.c b/src/effects_i.c
48 index 7d72166..65d6a0b 100644
49 --- a/src/effects_i.c
50 +++ b/src/effects_i.c
51 @@ -20,6 +20,7 @@
53 #define LSX_EFF_ALIAS
54 #include "sox_i.h"
55 +#include "unicode_support.h"
56 #include <string.h>
57 #include <ctype.h>
59 @@ -355,7 +356,7 @@ FILE * lsx_open_input_file(sox_effect_t * effp, char const * filename)
60 effp->global_info->global_info->stdin_in_use_by = effp->handler.name;
61 file = stdin;
63 - else if (!(file = fopen(filename, "r"))) {
64 + else if (!(file = fopen_utf8(filename, "r"))) {
65 lsx_fail("couldn't open file %s: %s", filename, strerror(errno));
66 return NULL;
68 diff --git a/src/formats.c b/src/formats.c
69 index 785eca7..31993a5 100644
70 --- a/src/formats.c
71 +++ b/src/formats.c
72 @@ -19,6 +19,7 @@
75 #include "sox_i.h"
76 +#include "unicode_support.h"
78 #include <assert.h>
79 #include <ctype.h>
80 @@ -397,7 +398,7 @@ static FILE * xfopen(char const * identifier, char const * mode, lsx_io_type * i
81 #endif
82 return f;
84 - return fopen(identifier, mode);
85 + return fopen_utf8(identifier, mode);
88 /* Hack to rewind pipes (a small amount).
89 @@ -848,8 +849,8 @@ static sox_format_t * open_write(
90 ft->fp = stdout;
92 else {
93 - struct stat st;
94 - if (!stat(path, &st) && (st.st_mode & S_IFMT) == S_IFREG &&
95 + struct _stat st;
96 + if (!stat_utf8(path, &st) && (st.st_mode & S_IFMT) == S_IFREG &&
97 (overwrite_permitted && !overwrite_permitted(path))) {
98 lsx_fail("permission to overwrite `%s' denied", path);
99 goto error;
100 @@ -859,7 +860,7 @@ static sox_format_t * open_write(
101 buffer? fmemopen(buffer, buffer_size, "w+b") :
102 buffer_ptr? open_memstream(buffer_ptr, buffer_size_ptr) :
103 #endif
104 - fopen(path, "w+b");
105 + fopen_utf8(path, "w+b");
106 if (ft->fp == NULL) {
107 lsx_fail("can't open output file `%s': %s", path, strerror(errno));
108 goto error;
109 diff --git a/src/libsox.c b/src/libsox.c
110 index 75354e4..a766aa9 100644
111 --- a/src/libsox.c
112 +++ b/src/libsox.c
113 @@ -19,6 +19,8 @@
116 #include "sox_i.h"
117 +#include "unicode_support.h"
119 #include <string.h>
121 const char *sox_version(void)
122 diff --git a/src/libsox_i.c b/src/libsox_i.c
123 index 8a7074a..b498cc0 100644
124 --- a/src/libsox_i.c
125 +++ b/src/libsox_i.c
126 @@ -20,6 +20,7 @@
129 #include "sox_i.h"
130 +#include "unicode_support.h"
132 #ifdef HAVE_IO_H
133 #include <io.h>
134 @@ -48,8 +49,8 @@
135 #ifdef WIN32
136 static int check_dir(char * buf, size_t buflen, char const * name)
138 - struct stat st;
139 - if (!name || stat(name, &st) || (st.st_mode & S_IFMT) != S_IFDIR)
140 + struct _stat st;
141 + if (!name || stat_utf8(name, &st) || (st.st_mode & S_IFMT) != S_IFDIR)
143 return 0;
145 @@ -102,7 +103,7 @@ FILE * lsx_tmpfile(void)
146 fildes = mkstemp(name);
147 #ifdef HAVE_UNISTD_H
148 lsx_debug(FAKE_MKSTEMP "mkstemp, name=%s (unlinked)", name);
149 - unlink(name);
150 + unlink_utf8(name);
151 #else
152 lsx_debug(FAKE_MKSTEMP "mkstemp, name=%s (O_TEMPORARY)", name);
153 #endif
154 diff --git a/src/noiseprof.c b/src/noiseprof.c
155 index 603402f..d46c280 100644
156 --- a/src/noiseprof.c
157 +++ b/src/noiseprof.c
158 @@ -19,6 +19,7 @@
161 #include "noisered.h"
162 +#include "unicode_support.h"
164 #include <assert.h>
165 #include <string.h>
166 @@ -75,7 +76,7 @@ static int sox_noiseprof_start(sox_effect_t * effp)
167 effp->global_info->global_info->stdout_in_use_by = effp->handler.name;
168 data->output_file = stdout;
170 - else if ((data->output_file = fopen(data->output_filename, "w")) == NULL) {
171 + else if ((data->output_file = fopen_utf8(data->output_filename, "w")) == NULL) {
172 lsx_fail("Couldn't open profile file %s: %s", data->output_filename, strerror(errno));
173 return SOX_EOF;
175 diff --git a/src/sox.c b/src/sox.c
176 index 8160fc1..74cad4d 100644
177 --- a/src/sox.c
178 +++ b/src/sox.c
179 @@ -24,6 +24,7 @@
180 #include "soxconfig.h"
181 #include "sox.h"
182 #include "util.h"
183 +#include "unicode_support.h"
185 #include <ctype.h>
186 #include <errno.h>
187 @@ -236,12 +237,12 @@ static void cleanup(void)
188 if (file_count) {
189 if (ofile->ft) {
190 if (!success && ofile->ft->fp) { /* If we failed part way through */
191 - struct stat st; /* writing a normal file, remove it. */
192 - if (!stat(ofile->ft->filename, &st) &&
193 + struct _stat st; /* writing a normal file, remove it. */
194 + if (!stat_utf8(ofile->ft->filename, &st) &&
195 (st.st_mode & S_IFMT) == S_IFREG)
196 - unlink(ofile->ft->filename);
197 + unlink_utf8(ofile->ft->filename);
199 - sox_close(ofile->ft); /* Assume we can unlink a file before closing it. */
200 + sox_close(ofile->ft); /* Assume we can unlink_utf8 a file before closing it. */
202 free(ofile->filename);
203 free(ofile);
204 @@ -293,8 +294,8 @@ static char const * str_time(double seconds)
206 static char const * size_and_bitrate(sox_format_t * ft, char const * * text)
208 - struct stat st; /* ft->fp may validly be NULL, so stat not fstat */
209 - if (stat(ft->filename, &st) || (st.st_mode & S_IFMT) != S_IFREG)
210 + struct _stat st; /* ft->fp may validly be NULL, so stat not fstat */
211 + if (stat_utf8(ft->filename, &st) || (st.st_mode & S_IFMT) != S_IFREG)
212 return NULL;
213 if (ft->signal.length && ft->signal.channels && ft->signal.rate && text) {
214 double secs = ft->signal.length / ft->signal.channels / ft->signal.rate;
215 @@ -906,7 +907,7 @@ static char * * strtoargv(char * s, int * argc)
217 static void read_user_effects(char const *filename)
219 - FILE *file = fopen(filename, "rt");
220 + FILE *file = fopen_utf8(filename, "rt");
221 const size_t buffer_size_step = 1024;
222 size_t buffer_size = buffer_size_step;
223 char *s = lsx_malloc(buffer_size); /* buffer for one input line */
224 @@ -1269,6 +1270,7 @@ static void display_status(sox_bool all_done)
225 lsx_sigfigs3((double)output_samples),
226 vu(0), vu(1), headroom(), lsx_sigfigs3((double)total_clips()));
228 + fflush(stderr);
229 if (all_done)
230 fputc('\n', stderr);
232 @@ -2127,7 +2129,7 @@ static void read_comment_file(sox_comments_t * comments, char const * const file
233 int c;
234 size_t text_length = 100;
235 char * text = lsx_malloc(text_length + 1);
236 - FILE * file = fopen(filename, "rt");
237 + FILE * file = fopen_utf8(filename, "rt");
239 if (file == NULL) {
240 lsx_fail("Cannot open comment file `%s'", filename);
241 @@ -2853,7 +2855,7 @@ static sox_bool cmp_comment_text(char const * c1, char const * c2)
242 return c1 && c2 && !strcasecmp(c1, c2);
245 -int main(int argc, char **argv)
246 +static int sox_main(int argc, char **argv)
248 size_t i;
249 char mybase[6];
250 @@ -3055,3 +3057,20 @@ int main(int argc, char **argv)
252 return 0;
255 +int main( int argc, char **argv )
257 + int sox_argc;
258 + char **sox_argv;
259 + int exit_code;
261 + init_console_utf8();
262 + init_commandline_arguments_utf8(&sox_argc, &sox_argv);
264 + exit_code = sox_main(sox_argc, sox_argv);
266 + uninit_console_utf8();
267 + free_commandline_arguments_utf8(&sox_argc, &sox_argv);
269 + return exit_code;
271 diff --git a/src/unicode_support.c b/src/unicode_support.c
272 new file mode 100644
273 index 0000000..f2a0a9e
274 --- /dev/null
275 +++ b/src/unicode_support.c
276 @@ -0,0 +1,199 @@
277 +/* Copyright (c) 2004-2012 LoRd_MuldeR <mulder2@gmx.de>
278 + File: unicode_support.c
280 + This file was originally part of a patch included with LameXP,
281 + released under the same license as the original audio tools.
283 + Redistribution and use in source and binary forms, with or without
284 + modification, are permitted provided that the following conditions
285 + are met:
287 + - Redistributions of source code must retain the above copyright
288 + notice, this list of conditions and the following disclaimer.
290 + - Redistributions in binary form must reproduce the above copyright
291 + notice, this list of conditions and the following disclaimer in the
292 + documentation and/or other materials provided with the distribution.
294 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
295 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
296 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
297 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
298 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
299 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
300 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
301 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
302 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
303 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
304 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
306 +#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
308 +#include "unicode_support.h"
310 +#include <windows.h>
311 +#include <io.h>
313 +static UINT g_old_output_cp = ((UINT)-1);
315 +char *utf16_to_utf8(const wchar_t *input)
317 + char *Buffer;
318 + int BuffSize = 0, Result = 0;
320 + BuffSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
321 + Buffer = (char*) malloc(sizeof(char) * BuffSize);
322 + if(Buffer)
324 + Result = WideCharToMultiByte(CP_UTF8, 0, input, -1, Buffer, BuffSize, NULL, NULL);
327 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
330 +char *utf16_to_ansi(const wchar_t *input)
332 + char *Buffer;
333 + int BuffSize = 0, Result = 0;
335 + BuffSize = WideCharToMultiByte(CP_ACP, 0, input, -1, NULL, 0, NULL, NULL);
336 + Buffer = (char*) malloc(sizeof(char) * BuffSize);
337 + if(Buffer)
339 + Result = WideCharToMultiByte(CP_ACP, 0, input, -1, Buffer, BuffSize, NULL, NULL);
342 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
345 +wchar_t *utf8_to_utf16(const char *input)
347 + wchar_t *Buffer;
348 + int BuffSize = 0, Result = 0;
350 + BuffSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
351 + Buffer = (wchar_t*) malloc(sizeof(wchar_t) * BuffSize);
352 + if(Buffer)
354 + Result = MultiByteToWideChar(CP_UTF8, 0, input, -1, Buffer, BuffSize);
357 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
360 +void init_commandline_arguments_utf8(int *argc, char ***argv)
362 + int i, nArgs;
363 + LPWSTR *szArglist;
365 + szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
367 + if(NULL == szArglist)
369 + fprintf(stderr, "\nFATAL: CommandLineToArgvW failed\n\n");
370 + exit(-1);
373 + *argv = (char**) malloc(sizeof(char*) * nArgs);
374 + *argc = nArgs;
376 + if(NULL == *argv)
378 + fprintf(stderr, "\nFATAL: Malloc failed\n\n");
379 + exit(-1);
382 + for(i = 0; i < nArgs; i++)
384 + (*argv)[i] = utf16_to_utf8(szArglist[i]);
385 + if(NULL == (*argv)[i])
387 + fprintf(stderr, "\nFATAL: utf16_to_utf8 failed\n\n");
388 + exit(-1);
392 + LocalFree(szArglist);
395 +void free_commandline_arguments_utf8(int *argc, char ***argv)
397 + int i = 0;
399 + if(*argv != NULL)
401 + for(i = 0; i < *argc; i++)
403 + if((*argv)[i] != NULL)
405 + free((*argv)[i]);
406 + (*argv)[i] = NULL;
409 + free(*argv);
410 + *argv = NULL;
414 +FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8)
416 + FILE *ret = NULL;
417 + wchar_t *filename_utf16 = utf8_to_utf16(filename_utf8);
418 + wchar_t *mode_utf16 = utf8_to_utf16(mode_utf8);
420 + if(filename_utf16 && mode_utf16)
422 + FILE *fh = NULL;
423 + int err = _wfopen_s(&fh, filename_utf16, mode_utf16);
424 + if(err == 0) ret = fh;
427 + if(filename_utf16) free(filename_utf16);
428 + if(mode_utf16) free(mode_utf16);
430 + return ret;
433 +int stat_utf8(const char *path_utf8, struct _stat *buf)
435 + int ret = -1;
437 + wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
438 + if(path_utf16)
440 + ret = _wstat(path_utf16, buf);
441 + free(path_utf16);
444 + return ret;
447 +int unlink_utf8(const char *path_utf8)
449 + int ret = -1;
451 + wchar_t *path_utf16 = utf8_to_utf16(path_utf8);
452 + if(path_utf16)
454 + ret = _wunlink(path_utf16);
455 + free(path_utf16);
458 + return ret;
461 +void init_console_utf8(void)
463 + g_old_output_cp = GetConsoleOutputCP();
464 + SetConsoleOutputCP(CP_UTF8);
467 +void uninit_console_utf8(void)
469 + if(g_old_output_cp != ((UINT)-1))
471 + SetConsoleOutputCP(g_old_output_cp);
475 +#endif
476 \ No newline at end of file
477 diff --git a/src/unicode_support.h b/src/unicode_support.h
478 new file mode 100644
479 index 0000000..cf55719
480 --- /dev/null
481 +++ b/src/unicode_support.h
482 @@ -0,0 +1,49 @@
483 +/* Copyright (c) 2004-2012 LoRd_MuldeR <mulder2@gmx.de>
484 + File: unicode_support.h
486 + This file was originally part of a patch included with LameXP,
487 + released under the same license as the original audio tools.
489 + Redistribution and use in source and binary forms, with or without
490 + modification, are permitted provided that the following conditions
491 + are met:
493 + - Redistributions of source code must retain the above copyright
494 + notice, this list of conditions and the following disclaimer.
496 + - Redistributions in binary form must reproduce the above copyright
497 + notice, this list of conditions and the following disclaimer in the
498 + documentation and/or other materials provided with the distribution.
500 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
501 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
502 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
503 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
504 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
505 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
506 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
507 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
508 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
509 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
510 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
512 +#ifndef UNICODE_SUPPORT_H_INCLUDED
513 +#define UNICODE_SUPPORT_H_INCLUDED
515 +#include <stdio.h>
516 +#include <sys/stat.h>
518 +#define WIN_UNICODE 1
520 +char *utf16_to_utf8(const wchar_t *input);
521 +char *utf16_to_ansi(const wchar_t *input);
522 +wchar_t *utf8_to_utf16(const char *input);
523 +void init_commandline_arguments_utf8(int *argc, char ***argv);
524 +void free_commandline_arguments_utf8(int *argc, char ***argv);
525 +FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8);
526 +int stat_utf8(const char *path_utf8, struct _stat *buf);
527 +int unlink_utf8(const char *path_utf8);
528 +void init_console_utf8(void);
529 +void uninit_console_utf8(void);
531 +#endif
532 \ No newline at end of file
533 diff --git a/src/util.h b/src/util.h
534 index 89bbe75..941d727 100644
535 --- a/src/util.h
536 +++ b/src/util.h
537 @@ -91,6 +91,10 @@
539 #ifdef _MSC_VER
541 +#define inline __inline
542 +#define snprintf _snprintf
545 #define __STDC__ 1
546 #define O_BINARY _O_BINARY
547 #define O_CREAT _O_CREAT
548 @@ -140,6 +144,7 @@
549 #define off_t __int64
550 #define HAVE_FSEEKO 1
551 #endif
554 #elif defined(__MINGW32__)