• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (C) 2009-2017 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*
sf_wchar_open(LPCWSTR wpath,int mode,SF_INFO * sfinfo)39 sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo)
40 {	SF_PRIVATE 	*psf ;
41 	char utf8name [512] ;
42 
43 	if ((psf = psf_allocate ()) == NULL)
44 	{	sf_errno = SFE_MALLOC_FAILED ;
45 		return	NULL ;
46 		} ;
47 
48 	psf_init_files (psf) ;
49 
50 	if (WideCharToMultiByte (CP_UTF8, 0, wpath, -1, utf8name, sizeof (utf8name), NULL, NULL) == 0)
51 		psf->file.path.wc [0] = 0 ;
52 
53 	psf_log_printf (psf, "File : '%s' (utf-8 converted from ucs-2)\n", utf8name) ;
54 
55 	copy_filename (psf, wpath) ;
56 	psf->file.use_wchar = SF_TRUE ;
57 	psf->file.mode = mode ;
58 
59 	psf->error = psf_fopen (psf) ;
60 
61 	return psf_open_file (psf, sfinfo) ;
62 } /* sf_wchar_open */
63 
64 
65 static void
copy_filename(SF_PRIVATE * psf,LPCWSTR wpath)66 copy_filename (SF_PRIVATE *psf, LPCWSTR wpath)
67 {	const wchar_t *cwcptr ;
68 	wchar_t *wcptr ;
69 
70 	wcsncpy (psf->file.path.wc, wpath, ARRAY_LEN (psf->file.path.wc)) ;
71 	psf->file.path.wc [ARRAY_LEN (psf->file.path.wc) - 1] = 0 ;
72 	if ((cwcptr = wcsrchr (wpath, '/')) || (cwcptr = wcsrchr (wpath, '\\')))
73 		cwcptr ++ ;
74 	else
75 		cwcptr = wpath ;
76 
77 	wcsncpy (psf->file.name.wc, cwcptr, ARRAY_LEN (psf->file.name.wc)) ;
78 	psf->file.name.wc [ARRAY_LEN (psf->file.name.wc) - 1] = 0 ;
79 
80 	/* Now grab the directory. */
81 	wcsncpy (psf->file.dir.wc, wpath, ARRAY_LEN (psf->file.dir.wc)) ;
82 	psf->file.dir.wc [ARRAY_LEN (psf->file.dir.wc) - 1] = 0 ;
83 
84 	if ((wcptr = wcsrchr (psf->file.dir.wc, '/')) || (wcptr = wcsrchr (psf->file.dir.wc, '\\')))
85 		wcptr [1] = 0 ;
86 	else
87 		psf->file.dir.wc [0] = 0 ;
88 
89 	return ;
90 } /* copy_filename */
91 
92 #endif
93