strerror.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * libusb strerror code
  3. * Copyright © 2013 Hans de Goede <hdegoede@redhat.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "config.h"
  20. #include <locale.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "libusb.h"
  24. #include "libusbi.h"
  25. #if defined(_MSC_VER)
  26. #define strncasecmp _strnicmp
  27. #endif
  28. static size_t usbi_locale = 0;
  29. /** \ingroup misc
  30. * How to add a new \ref libusb_strerror() translation:
  31. * <ol>
  32. * <li> Download the latest \c strerror.c from:<br>
  33. * https://raw.github.com/libusbx/libusbx/master/libusb/sterror.c </li>
  34. * <li> Open the file in an UTF-8 capable editor </li>
  35. * <li> Add the 2 letter <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1</a>
  36. * code for your locale at the end of \c usbi_locale_supported[]<br>
  37. * Eg. for Chinese, you would add "zh" so that:
  38. * \code... usbi_locale_supported[] = { "en", "nl", "fr" };\endcode
  39. * becomes:
  40. * \code... usbi_locale_supported[] = { "en", "nl", "fr", "zh" };\endcode </li>
  41. * <li> Copy the <tt>{ / * English (en) * / ... }</tt> section and add it at the end of \c usbi_localized_errors<br>
  42. * Eg. for Chinese, the last section of \c usbi_localized_errors could look like:
  43. * \code
  44. * }, { / * Chinese (zh) * /
  45. * "Success",
  46. * ...
  47. * "Other error",
  48. * }
  49. * };\endcode </li>
  50. * <li> Translate each of the English messages from the section you copied into your language </li>
  51. * <li> Save the file (in UTF-8 format) and send it to \c libusbx-devel\@lists.sourceforge.net </li>
  52. * </ol>
  53. */
  54. static const char* usbi_locale_supported[] = { "en", "nl", "fr" };
  55. static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = {
  56. { /* English (en) */
  57. "Success",
  58. "Input/Output Error",
  59. "Invalid parameter",
  60. "Access denied (insufficient permissions)",
  61. "No such device (it may have been disconnected)",
  62. "Entity not found",
  63. "Resource busy",
  64. "Operation timed out",
  65. "Overflow",
  66. "Pipe error",
  67. "System call interrupted (perhaps due to signal)",
  68. "Insufficient memory",
  69. "Operation not supported or unimplemented on this platform",
  70. "Other error",
  71. }, { /* Dutch (nl) */
  72. "Gelukt",
  73. "Invoer-/uitvoerfout",
  74. "Ongeldig argument",
  75. "Toegang geweigerd (onvoldoende toegangsrechten)",
  76. "Apparaat bestaat niet (verbinding met apparaat verbroken?)",
  77. "Niet gevonden",
  78. "Apparaat of hulpbron is bezig",
  79. "Bewerking verlopen",
  80. "Waarde is te groot",
  81. "Gebroken pijp",
  82. "Onderbroken systeemaanroep",
  83. "Onvoldoende geheugen beschikbaar",
  84. "Bewerking wordt niet ondersteund",
  85. "Andere fout",
  86. }, { /* French (fr) */
  87. "Succès",
  88. "Erreur d'entrée/sortie",
  89. "Paramètre invalide",
  90. "Accès refusé (permissions insuffisantes)",
  91. "Périphérique introuvable (peut-être déconnecté)",
  92. "Elément introuvable",
  93. "Resource déjà occupée",
  94. "Operation expirée",
  95. "Débordement",
  96. "Erreur de pipe",
  97. "Appel système abandonné (peut-être à cause d’un signal)",
  98. "Mémoire insuffisante",
  99. "Opération non supportée or non implémentée sur cette plateforme",
  100. "Autre erreur"
  101. }
  102. };
  103. /** \ingroup misc
  104. * Set the language, and only the language, not the encoding! used for
  105. * translatable libusb messages.
  106. *
  107. * This takes a locale string in the default setlocale format: lang[-region]
  108. * or lang[_country_region][.codeset]. Only the lang part of the string is
  109. * used, and only 2 letter ISO 639-1 codes are accepted for it, such as "de".
  110. * The optional region, country_region or codeset parts are ignored. This
  111. * means that functions which return translatable strings will NOT honor the
  112. * specified encoding.
  113. * All strings returned are encoded as UTF-8 strings.
  114. *
  115. * If libusb_setlocale() is not called, all messages will be in English.
  116. *
  117. * The following functions return translatable strings: libusb_strerror().
  118. * Note that the libusb log messages controlled through libusb_set_debug()
  119. * are not translated, they are always in English.
  120. *
  121. * For POSIX UTF-8 environments if you want libusb to follow the standard
  122. * locale settings, call libusb_setlocale(setlocale(LC_MESSAGES, NULL)),
  123. * after your app has done its locale setup.
  124. *
  125. * \param locale locale-string in the form of lang[_country_region][.codeset]
  126. * or lang[-region], where lang is a 2 letter ISO 639-1 code
  127. * \returns LIBUSB_SUCCESS on success
  128. * \returns LIBUSB_ERROR_INVALID_PARAM if the locale doesn't meet the requirements
  129. * \returns LIBUSB_ERROR_NOT_FOUND if the requested language is not supported
  130. * \returns a LIBUSB_ERROR code on other errors
  131. */
  132. int API_EXPORTED libusb_setlocale(const char *locale)
  133. {
  134. size_t i;
  135. if ( (locale == NULL) || (strlen(locale) < 2)
  136. || ((strlen(locale) > 2) && (locale[2] != '-') && (locale[2] != '_') && (locale[2] != '.')) )
  137. return LIBUSB_ERROR_INVALID_PARAM;
  138. for (i=0; i<ARRAYSIZE(usbi_locale_supported); i++) {
  139. if (strncasecmp(usbi_locale_supported[i], locale, 2) == 0)
  140. break;
  141. }
  142. if (i >= ARRAYSIZE(usbi_locale_supported)) {
  143. return LIBUSB_ERROR_NOT_FOUND;
  144. }
  145. usbi_locale = i;
  146. return LIBUSB_SUCCESS;
  147. }
  148. /** \ingroup misc
  149. * Returns a constant string with a short description of the given error code,
  150. * this description is intended for displaying to the end user and will be in
  151. * the language set by libusb_setlocale().
  152. *
  153. * The returned string is encoded in UTF-8.
  154. *
  155. * The messages always start with a capital letter and end without any dot.
  156. * The caller must not free() the returned string.
  157. *
  158. * \param errcode the error code whose description is desired
  159. * \returns a short description of the error code in UTF-8 encoding
  160. */
  161. DEFAULT_VISIBILITY const char* LIBUSB_CALL libusb_strerror(enum libusb_error errcode)
  162. {
  163. int errcode_index = -errcode;
  164. if ((errcode_index < 0) || (errcode_index >= LIBUSB_ERROR_COUNT)) {
  165. /* "Other Error", which should always be our last message, is returned */
  166. errcode_index = LIBUSB_ERROR_COUNT - 1;
  167. }
  168. return usbi_localized_errors[usbi_locale][errcode_index];
  169. }