Sorry for the late reply,

> 2013/6/5 Hans de Goede <hdego...@redhat.com>:
>> +int API_EXPORTED libusb_setlocale(const char *locale)
>> +{
>> +       const char * const iso639[] = {
>> +               /* Note the order here MUST match the libusb_locale_enum */
>> +               "en", /* LANG_EN */
>> +               "nl", /* LANG_NL */
>> +               NULL
>> +       };
>> +       char lang[3] = { 0, };
>> +       int i;
>> +
>> +       for (i = 0;
>> +            i < 3 && locale[i] && locale[i] != '_' && locale[i] != '.';
>> +            i++)
>> +               lang[i] = locale[i];
>> +
>> +       if (lang[2] != 0) {
>> +               usbi_warn(NULL, "Unrecognized locale format: %s", locale);
>> +               return LIBUSB_ERROR_INVALID_PARAM;
>> +       }
>> +
>> +       for (i = 0; iso639[i]; i++) {
>> +               if (strcmp(lang, iso639[i]) == 0)
>> +                       break;
>> +       }
>> +       if (!iso639[i]) {
>> +               usbi_warn(NULL, "Unsupported language: %s", lang);
>> +               return LIBUSB_ERROR_NOT_FOUND;
>> +       }
>> +
>> +       libusb_locale = i;
>> +
>> +       return LIBUSB_SUCCESS;
>> +}

With the converting the iso639 language string to an enum and the 
multiplication of function calls for each language, this still looks 
kind of awfully complex...

If it were up to me, I'd try to go with something like this:

---------------------------------------------------------------------
int internal_locale = 0;  // en by default
char* locale_supported[] = {"en", "nl"};
char* 
localized_errors[ARRAY_SIZE(locale_supported)][LIBUSB_NB_ERROR_CODES] = {
   // English
   {
      "Success",
      "Input/Output Error",
      ...
      "Invalid errcode"
   },
   // Dutch
   {
      "Gelukt",
      "Invoer-/uitvoerfout",
      ...
      "Ongeldig foutcode"
   }
}

libusb_init() {
...
internal_locale = 0;    // may need to reinit?
...
}

int API_EXPORTED libusb_setlocale(const char *locale) {
    int i;
    for (i=0; i<ARRAY_SIZE(locale_supported); i++) {
      if (strnicmp(locale_supported[i], locale, 2) == 0)
        break;
    }
    if (i >= ARRAY_SIZE(locale_supported)) {
       usbi_error("locale %s is not supported", locale);
       return LIBUSB_ERROR_NOT_SUPPORTED;
       // internal_locale remains to 0(en) or whatever valid value it had
    }
    internal_locale = i;
    return LIBUSB_SUCCESS;
}

DEFAULT_VISIBILITY const char* libusb_strerror(enum libusb_error errcode) {
   int errcode_index = errcode * -1; // to positive value
   if (errcode_index == 99)     // LIBUSB_ERROR_OTHER defined as -99
     errcode_index = LIBUSB_NB_ERROR_CODES - 2;

   if ((errcode_index < 0) || (errcode_index >= LIBUSB_NB_ERROR_CODES)) {
      // tried to pass an invalid errcode value
      errcode_index = LIBUSB_NB_ERROR_CODES - 1;
   }

   // our indexes should always be within bonds here
   return localized_errors[internal_locale][errcode_index];
}
---------------------------------------------------------------------

How does that sound? I can produce a formal patch if you like this 
solution enough.

Regards,

/Pete

------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to