1 /* 2 * libusb strerror code 3 * Copyright 2013 Hans de Goede <hdegoede (at) 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 21 #include <locale.h> 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include "libusb.h" 26 #include "libusbi.h" 27 28 #if defined(_MSC_VER) 29 #define strncasecmp _strnicmp 30 #endif 31 32 static size_t usbi_locale = 0; 33 34 /** \ingroup misc 35 * How to add a new \ref libusb_strerror() translation: 36 * <ol> 37 * <li> Download the latest \c strerror.c from:<br> 38 * https://raw.github.com/libusbx/libusbx/master/libusb/sterror.c </li> 39 * <li> Open the file in an UTF-8 capable editor </li> 40 * <li> Add the 2 letter <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639-1</a> 41 * code for your locale at the end of \c usbi_locale_supported[]<br> 42 * Eg. for Chinese, you would add "zh" so that: 43 * \code... usbi_locale_supported[] = { "en", "nl", "fr" };\endcode 44 * becomes: 45 * \code... usbi_locale_supported[] = { "en", "nl", "fr", "zh" };\endcode </li> 46 * <li> Copy the <tt>{ / * English (en) * / ... }</tt> section and add it at the end of \c usbi_localized_errors<br> 47 * Eg. for Chinese, the last section of \c usbi_localized_errors could look like: 48 * \code 49 * }, { / * Chinese (zh) * / 50 * "Success", 51 * ... 52 * "Other error", 53 * } 54 * };\endcode </li> 55 * <li> Translate each of the English messages from the section you copied into your language </li> 56 * <li> Save the file (in UTF-8 format) and send it to \c libusbx-devel\@lists.sourceforge.net </li> 57 * </ol> 58 */ 59 60 static const char* usbi_locale_supported[] = { "en", "nl", "fr" }; 61 static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = { 62 { /* English (en) */ 63 "Success", 64 "Input/Output Error", 65 "Invalid parameter", 66 "Access denied (insufficient permissions)", 67 "No such device (it may have been disconnected)", 68 "Entity not found", 69 "Resource busy", 70 "Operation timed out", 71 "Overflow", 72 "Pipe error", 73 "System call interrupted (perhaps due to signal)", 74 "Insufficient memory", 75 "Operation not supported or unimplemented on this platform", 76 "Other error", 77 }, { /* Dutch (nl) */ 78 "Gelukt", 79 "Invoer-/uitvoerfout", 80 "Ongeldig argument", 81 "Toegang geweigerd (onvoldoende toegangsrechten)", 82 "Apparaat bestaat niet (verbinding met apparaat verbroken?)", 83 "Niet gevonden", 84 "Apparaat of hulpbron is bezig", 85 "Bewerking verlopen", 86 "Waarde is te groot", 87 "Gebroken pijp", 88 "Onderbroken systeemaanroep", 89 "Onvoldoende geheugen beschikbaar", 90 "Bewerking wordt niet ondersteund", 91 "Andere fout", 92 }, { /* French (fr) */ 93 "Succs", 94 "Erreur d'entre/sortie", 95 "Paramtre invalide", 96 "Accs refus (permissions insuffisantes)", 97 "Priphrique introuvable (peut-tre dconnect)", 98 "Elment introuvable", 99 "Resource dj occupe", 100 "Operation expire", 101 "Dbordement", 102 "Erreur de pipe", 103 "Appel systme abandonn (peut-tre cause dun signal)", 104 "Mmoire insuffisante", 105 "Opration non supporte or non implmente sur cette plateforme", 106 "Autre erreur" 107 } 108 }; 109 110 /** \ingroup misc 111 * Set the language, and only the language, not the encoding! used for 112 * translatable libusb messages. 113 * 114 * This takes a locale string in the default setlocale format: lang[-region] 115 * or lang[_country_region][.codeset]. Only the lang part of the string is 116 * used, and only 2 letter ISO 639-1 codes are accepted for it, such as "de". 117 * The optional region, country_region or codeset parts are ignored. This 118 * means that functions which return translatable strings will NOT honor the 119 * specified encoding. 120 * All strings returned are encoded as UTF-8 strings. 121 * 122 * If libusb_setlocale() is not called, all messages will be in English. 123 * 124 * The following functions return translatable strings: libusb_strerror(). 125 * Note that the libusb log messages controlled through libusb_set_debug() 126 * are not translated, they are always in English. 127 * 128 * For POSIX UTF-8 environments if you want libusb to follow the standard 129 * locale settings, call libusb_setlocale(setlocale(LC_MESSAGES, NULL)), 130 * after your app has done its locale setup. 131 * 132 * \param locale locale-string in the form of lang[_country_region][.codeset] 133 * or lang[-region], where lang is a 2 letter ISO 639-1 code 134 * \returns LIBUSB_SUCCESS on success 135 * \returns LIBUSB_ERROR_INVALID_PARAM if the locale doesn't meet the requirements 136 * \returns LIBUSB_ERROR_NOT_FOUND if the requested language is not supported 137 * \returns a LIBUSB_ERROR code on other errors 138 */ 139 140 int API_EXPORTED libusb_setlocale(const char *locale) 141 { 142 size_t i; 143 144 if ( (locale == NULL) || (strlen(locale) < 2) 145 || ((strlen(locale) > 2) && (locale[2] != '-') && (locale[2] != '_') && (locale[2] != '.')) ) 146 return LIBUSB_ERROR_INVALID_PARAM; 147 148 for (i=0; i<ARRAYSIZE(usbi_locale_supported); i++) { 149 if (strncasecmp(usbi_locale_supported[i], locale, 2) == 0) 150 break; 151 } 152 if (i >= ARRAYSIZE(usbi_locale_supported)) { 153 return LIBUSB_ERROR_NOT_FOUND; 154 } 155 156 usbi_locale = i; 157 158 return LIBUSB_SUCCESS; 159 } 160 161 /** \ingroup misc 162 * Returns a constant string with a short description of the given error code, 163 * this description is intended for displaying to the end user and will be in 164 * the language set by libusb_setlocale(). 165 * 166 * The returned string is encoded in UTF-8. 167 * 168 * The messages always start with a capital letter and end without any dot. 169 * The caller must not free() the returned string. 170 * 171 * \param errcode the error code whose description is desired 172 * \returns a short description of the error code in UTF-8 encoding 173 */ 174 DEFAULT_VISIBILITY const char* LIBUSB_CALL libusb_strerror(enum libusb_error errcode) 175 { 176 int errcode_index = -errcode; 177 178 if ((errcode_index < 0) || (errcode_index >= LIBUSB_ERROR_COUNT)) { 179 /* "Other Error", which should always be our last message, is returned */ 180 errcode_index = LIBUSB_ERROR_COUNT - 1; 181 } 182 183 return usbi_localized_errors[usbi_locale][errcode_index]; 184 } 185