2
0

locale_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #if HAVE_LOCALE_H
  26. #include <locale.h>
  27. #endif
  28. #if OS_IS_WIN32
  29. #include <windows.h>
  30. #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1
  31. #endif
  32. #include "sndfile.h"
  33. #include "utils.h"
  34. static void utf8_test (void) ;
  35. static void wchar_test (void) ;
  36. int
  37. main (void)
  38. {
  39. utf8_test () ;
  40. wchar_test () ;
  41. return 0 ;
  42. } /* main */
  43. /*==============================================================================
  44. */
  45. static void
  46. wchar_test (void)
  47. {
  48. #if OS_IS_WIN32
  49. SNDFILE * file ;
  50. SF_INFO info ;
  51. LPCWSTR filename = L"test.wav" ;
  52. print_test_name (__func__, "test.wav") ;
  53. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  54. info.channels = 1 ;
  55. info.samplerate = 44100 ;
  56. file = sf_wchar_open (filename, SFM_WRITE, &info) ;
  57. exit_if_true (file == NULL, "\n\nLine %d : sf_wchar_open failed : %s\n\n", __LINE__, sf_strerror (NULL)) ;
  58. sf_close (file) ;
  59. /* This should check that the file did in fact get created with a
  60. ** wchar_t * filename.
  61. */
  62. exit_if_true (
  63. GetFileAttributesW (filename) == INVALID_FILE_ATTRIBUTES,
  64. "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__
  65. ) ;
  66. /* Use this because the file was created with CreateFileW. */
  67. DeleteFileW (filename) ;
  68. puts ("ok") ;
  69. #endif
  70. } /* wchar_test */
  71. /*==============================================================================
  72. */
  73. typedef struct
  74. { const char *locale ;
  75. int utf8 ;
  76. const char *filename ;
  77. int width ;
  78. } LOCALE_DATA ;
  79. static void locale_test (const LOCALE_DATA * locdata) ;
  80. static void
  81. utf8_test (void)
  82. { LOCALE_DATA ldata [] =
  83. { { "de_DE", 1, "F\303\274\303\237e.au", 7 },
  84. { "en_AU", 1, "kangaroo.au", 11 },
  85. { "POSIX", 0, "posix.au", 8 },
  86. { "pt_PT", 1, "concei\303\247\303\243o.au", 12 },
  87. #if OS_IS_WIN32 == 0
  88. { "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 },
  89. #endif
  90. { "vi_VN", 1, "qu\341\273\221c ng\341\273\257.au", 11 },
  91. { NULL, 0, NULL, 0 }
  92. } ;
  93. int k ;
  94. for (k = 0 ; ldata [k].locale != NULL ; k++)
  95. locale_test (ldata + k) ;
  96. } /* utf8_test */
  97. static void
  98. locale_test (const LOCALE_DATA * ldata)
  99. {
  100. #if (HAVE_LOCALE_H == 0 || HAVE_SETLOCALE == 0)
  101. locname = filename = NULL ;
  102. width = 0 ;
  103. return ;
  104. #else
  105. const short wdata [] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;
  106. short rdata [ARRAY_LEN (wdata)] ;
  107. const char *old_locale ;
  108. char utf8_locname [32] ;
  109. SNDFILE *file ;
  110. SF_INFO sfinfo ;
  111. snprintf (utf8_locname, sizeof (utf8_locname), "%s%s", ldata->locale, ldata->utf8 ? ".UTF-8" : "") ;
  112. /* Change the locale saving the old one. */
  113. if ((old_locale = setlocale (LC_CTYPE, utf8_locname)) == NULL)
  114. return ;
  115. printf (" locale_test %-8s : %s %*c ", ldata->locale, ldata->filename, 24 - ldata->width, ' ') ;
  116. fflush (stdout) ;
  117. sfinfo.format = SF_FORMAT_AU | SF_FORMAT_PCM_16 ;
  118. sfinfo.channels = 1 ;
  119. sfinfo.samplerate = 44100 ;
  120. file = test_open_file_or_die (ldata->filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
  121. test_write_short_or_die (file, 0, wdata, ARRAY_LEN (wdata), __LINE__) ;
  122. sf_close (file) ;
  123. file = test_open_file_or_die (ldata->filename, SFM_READ, &sfinfo, 0, __LINE__) ;
  124. test_read_short_or_die (file, 0, rdata, ARRAY_LEN (rdata), __LINE__) ;
  125. sf_close (file) ;
  126. unlink (ldata->filename) ;
  127. /* Restore old locale. */
  128. setlocale (LC_CTYPE, old_locale) ;
  129. puts ("ok") ;
  130. #endif
  131. } /* locale_test */