Alexandre Lision | 7c6f4a6 | 2013-09-05 13:27:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (C) 2009-2011 Erik de Castro Lopo <erikd@mega-nerd.com> |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or modify |
| 5 | ** it under the terms of the GNU Lesser General Public License as published by |
| 6 | ** the Free Software Foundation; either version 2.1 of the License, or |
| 7 | ** (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | ** GNU Lesser General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU Lesser General Public License |
| 15 | ** along with this program; if not, write to the Free Software |
| 16 | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | ** This needs to be a separate file so that we don't have to include |
| 21 | ** <windows.h> elsewhere (too many symbol clashes). |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include "sfconfig.h" |
| 26 | |
| 27 | #if OS_IS_WIN32 |
| 28 | #include <windows.h> |
| 29 | |
| 30 | #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1 |
| 31 | #include "sndfile.h" |
| 32 | #include "common.h" |
| 33 | |
| 34 | extern int sf_errno ; |
| 35 | |
| 36 | static void copy_filename (SF_PRIVATE * psf, LPCWSTR wpath) ; |
| 37 | |
| 38 | SNDFILE* |
| 39 | sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo) |
| 40 | { SF_PRIVATE *psf ; |
| 41 | char utf8name [512] ; |
| 42 | |
| 43 | if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL) |
| 44 | { sf_errno = SFE_MALLOC_FAILED ; |
| 45 | return NULL ; |
| 46 | } ; |
| 47 | |
| 48 | memset (psf, 0, sizeof (SF_PRIVATE)) ; |
| 49 | psf_init_files (psf) ; |
| 50 | |
| 51 | if (WideCharToMultiByte (CP_UTF8, 0, wpath, -1, utf8name, sizeof (utf8name), NULL, NULL) == 0) |
| 52 | psf->file.path.wc [0] = 0 ; |
| 53 | |
| 54 | psf_log_printf (psf, "File : '%s' (utf-8 converted from ucs-2)\n", utf8name) ; |
| 55 | |
| 56 | copy_filename (psf, wpath) ; |
| 57 | psf->file.use_wchar = SF_TRUE ; |
| 58 | psf->file.mode = mode ; |
| 59 | |
| 60 | psf->error = psf_fopen (psf) ; |
| 61 | |
| 62 | return psf_open_file (psf, sfinfo) ; |
| 63 | } /* sf_wchar_open */ |
| 64 | |
| 65 | |
| 66 | static void |
| 67 | copy_filename (SF_PRIVATE *psf, LPCWSTR wpath) |
| 68 | { const wchar_t *cwcptr ; |
| 69 | wchar_t *wcptr ; |
| 70 | |
| 71 | wcsncpy (psf->file.path.wc, wpath, ARRAY_LEN (psf->file.path.wc)) ; |
| 72 | psf->file.path.wc [ARRAY_LEN (psf->file.path.wc) - 1] = 0 ; |
| 73 | if ((cwcptr = wcsrchr (wpath, '/')) || (cwcptr = wcsrchr (wpath, '\\'))) |
| 74 | cwcptr ++ ; |
| 75 | else |
| 76 | cwcptr = wpath ; |
| 77 | |
| 78 | wcsncpy (psf->file.name.wc, cwcptr, ARRAY_LEN (psf->file.name.wc)) ; |
| 79 | psf->file.name.wc [ARRAY_LEN (psf->file.name.wc) - 1] = 0 ; |
| 80 | |
| 81 | /* Now grab the directory. */ |
| 82 | wcsncpy (psf->file.dir.wc, wpath, ARRAY_LEN (psf->file.dir.wc)) ; |
| 83 | psf->file.dir.wc [ARRAY_LEN (psf->file.dir.wc) - 1] = 0 ; |
| 84 | |
| 85 | if ((wcptr = wcsrchr (psf->file.dir.wc, '/')) || (wcptr = wcsrchr (psf->file.dir.wc, '\\'))) |
| 86 | wcptr [1] = 0 ; |
| 87 | else |
| 88 | psf->file.dir.wc [0] = 0 ; |
| 89 | |
| 90 | return ; |
| 91 | } /* copy_filename */ |
| 92 | |
| 93 | #endif |