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