diff options
-rw-r--r-- | src/locale.c | 113 | ||||
-rw-r--r-- | src/locale.h | 1 |
2 files changed, 114 insertions, 0 deletions
diff --git a/src/locale.c b/src/locale.c index d738084..f336484 100644 --- a/src/locale.c +++ b/src/locale.c @@ -18,10 +18,123 @@ */ #include <locale.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> #include "locale.h" +#include "output.h" const char * db_lc_messages_get(void) { return setlocale(LC_MESSAGES, ""); } + +int +db_locale_match(char *loc) +{ + char *sys_start; + char *sys; + char *sys_end; + char *loc_end; + int end; + + sys_end = sys = sys_start = strdup(setlocale(LC_MESSAGES, "")); + loc_end = loc; + + db_dbg("Matching locale \"%s\" against system locale \"%s\"", loc, sys); + + end = 0; + + /* Check language */ + while (*sys_end != '\0' && *sys_end != '_' && *sys_end != '-') { + ++sys_end; + } + while (*loc_end != '\0' && *loc_end != '_' && *loc_end != '-') { + ++loc_end; + } + if (*sys_end == '\0' || *loc_end == '\0') { + end = 1; + } + *sys_end = '\0'; + *loc_end = '\0'; + if (strcasecmp(sys, loc) != 0) { + db_dbg("Language \"%s\" != \"%s\"", loc, sys); + free(sys_start); + return 0; + } + db_dbg("Language \"%s\" == \"%s\"", loc, sys); + if (end == 1) { + free(sys_start); + return 1; + } + sys = ++sys_end; + loc = ++loc_end; + + /* Check territory */ + while (*sys_end != '\0' && *sys_end != '.') { + ++sys_end; + } + while (*loc_end != '\0' && *loc_end != '.') { + ++loc_end; + } + if (*sys_end == '\0' || *loc_end == '\0') { + end = 1; + } + *sys_end = '\0'; + *loc_end = '\0'; + if (strcasecmp(sys, loc) != 0) { + db_dbg("Territory \"%s\" != \"%s\"", loc, sys); + free(sys_start); + return 1; + } + db_dbg("Territory \"%s\" == \"%s\"", loc, sys); + if (end == 1) { + free(sys_start); + return 2; + } + sys = ++sys_end; + loc = ++loc_end; + + /* Check codeset */ + while (*sys_end != '\0' && *sys_end != '@') { + ++sys_end; + } + while (*loc_end != '\0' && *loc_end != '@') { + ++loc_end; + } + if (*sys_end == '\0' || *loc_end == '\0') { + end = 1; + } + *sys_end = '\0'; + *loc_end = '\0'; + if (strcasecmp(sys, loc) != 0) { + db_dbg("Codeset \"%s\" != \"%s\"", loc, sys); + free(sys_start); + return 2; + } + db_dbg("Codeset \"%s\" == \"%s\"", loc, sys); + if (end == 1) { + free(sys_start); + return 3; + } + sys = ++sys_end; + loc = ++loc_end; + + /* Check modifier */ + while (*sys_end != '\0') { + ++sys_end; + } + while (*loc_end != '\0') { + ++loc_end; + } + if (strcasecmp(sys, loc) != 0) { + db_dbg("Modifier \"%s\" != \"%s\"", loc, sys); + free(sys_start); + return 3; + } + db_dbg("Modifier \"%s\" == \"%s\"", loc, sys); + + free(sys_start); + return 4; +} diff --git a/src/locale.h b/src/locale.h index 9fced63..7813370 100644 --- a/src/locale.h +++ b/src/locale.h @@ -21,5 +21,6 @@ #define DB_LOCALE_H_ const char *db_lc_messages_get(void); +int db_locale_match(char *loc); #endif /* DB_LOCALE_H_ */ |