1 /*
2 ** Copyright (C) 2005-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 General Public License as published by
6 ** the Free Software Foundation; either version 2 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 General Public License for more details.
13 **
14 ** You should have received a copy of the GNU 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 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #else
28 #include "sf_unistd.h"
29 #endif
30
31 #if HAVE_LOCALE_H
32 #include <locale.h>
33 #endif
34
35 #if OS_IS_WIN32
36 #include <windows.h>
37 #endif
38
39 #include "sndfile.h"
40 #include "utils.h"
41
42 static void utf8_test (void) ;
43 static void wchar_test (void) ;
44
45 int
main(void)46 main (void)
47 {
48 utf8_test () ;
49 wchar_test () ;
50
51 return 0 ;
52 } /* main */
53
54 /*==============================================================================
55 */
56
57 static void
wchar_test(void)58 wchar_test (void)
59 {
60 #if OS_IS_WIN32
61 SNDFILE * file ;
62 SF_INFO info ;
63 LPCWSTR filename = L"test.wav" ;
64
65 print_test_name (__func__, "test.wav") ;
66
67 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
68 info.channels = 1 ;
69 info.samplerate = 44100 ;
70
71 file = sf_wchar_open (filename, SFM_WRITE, &info) ;
72 exit_if_true (file == NULL, "\n\nLine %d : sf_wchar_open failed : %s\n\n", __LINE__, sf_strerror (NULL)) ;
73 sf_close (file) ;
74
75 /* This should check that the file did in fact get created with a
76 ** wchar_t * filename.
77 */
78 exit_if_true (
79 GetFileAttributesW (filename) == INVALID_FILE_ATTRIBUTES,
80 "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__
81 ) ;
82
83 /* Use this because the file was created with CreateFileW. */
84 DeleteFileW (filename) ;
85
86 puts ("ok") ;
87 #endif
88 } /* wchar_test */
89
90 /*==============================================================================
91 */
92
93 typedef struct
94 { const char *locale ;
95 int utf8 ;
96 const char *filename ;
97 int width ;
98 } LOCALE_DATA ;
99
100 static void locale_test (const LOCALE_DATA * locdata) ;
101
102 static void
utf8_test(void)103 utf8_test (void)
104 { LOCALE_DATA ldata [] =
105 { { "de_DE", 1, "F\303\274\303\237e.au", 7 },
106 { "en_AU", 1, "kangaroo.au", 11 },
107 { "POSIX", 0, "posix.au", 8 },
108 { "pt_PT", 1, "concei\303\247\303\243o.au", 12 },
109
110 #if OS_IS_WIN32 == 0
111 { "ja_JP", 1, "\343\201\212\343\201\257\343\202\210\343\201\206\343\201\224\343\201\226\343\201\204\343\201\276\343\201\231.au", 21 },
112 #endif
113
114 { "vi_VN", 1, "qu\341\273\221c ng\341\273\257.au", 11 },
115 { NULL, 0, NULL, 0 }
116 } ;
117 int k ;
118
119 for (k = 0 ; ldata [k].locale != NULL ; k++)
120 locale_test (ldata + k) ;
121 } /* utf8_test */
122
123
124 static void
locale_test(const LOCALE_DATA * ldata)125 locale_test (const LOCALE_DATA * ldata)
126 {
127 #if (HAVE_LOCALE_H == 0 || HAVE_SETLOCALE == 0)
128 locname = filename = NULL ;
129 width = 0 ;
130 return ;
131 #else
132 const short wdata [] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;
133 short rdata [ARRAY_LEN (wdata)] ;
134 const char *old_locale ;
135 char utf8_locname [32] ;
136 SNDFILE *file ;
137 SF_INFO sfinfo ;
138
139 snprintf (utf8_locname, sizeof (utf8_locname), "%s%s", ldata->locale, ldata->utf8 ? ".UTF-8" : "") ;
140
141 /* Change the locale saving the old one. */
142 if ((old_locale = setlocale (LC_CTYPE, utf8_locname)) == NULL)
143 return ;
144
145 printf (" locale_test %-8s : %s %*c ", ldata->locale, ldata->filename, 24 - ldata->width, ' ') ;
146 fflush (stdout) ;
147
148 sfinfo.format = SF_FORMAT_AU | SF_FORMAT_PCM_16 ;
149 sfinfo.channels = 1 ;
150 sfinfo.samplerate = 44100 ;
151
152 file = test_open_file_or_die (ldata->filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
153 test_write_short_or_die (file, 0, wdata, ARRAY_LEN (wdata), __LINE__) ;
154 sf_close (file) ;
155
156 file = test_open_file_or_die (ldata->filename, SFM_READ, &sfinfo, 0, __LINE__) ;
157 test_read_short_or_die (file, 0, rdata, ARRAY_LEN (rdata), __LINE__) ;
158 sf_close (file) ;
159
160 unlink (ldata->filename) ;
161
162 /* Restore old locale. */
163 setlocale (LC_CTYPE, old_locale) ;
164
165 puts ("ok") ;
166 #endif
167 } /* locale_test */
168
169