summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2021-03-17 06:25:36 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2021-03-17 06:25:36 (EDT)
commitb93b018af83ea02d89d2aaecdc34d62be9dc0a39 (patch)
treec080355fe3ff06045606f3b2f44d8853dcdc76f6 /src
parente674ab4e41e1f4f9289e11745ef2d91f410f41ab (diff)
downloaddodge-balls-b93b018af83ea02d89d2aaecdc34d62be9dc0a39.zip
dodge-balls-b93b018af83ea02d89d2aaecdc34d62be9dc0a39.tar.gz
dodge-balls-b93b018af83ea02d89d2aaecdc34d62be9dc0a39.tar.bz2
db_locale_match(): New function
Diffstat (limited to 'src')
-rw-r--r--src/locale.c113
-rw-r--r--src/locale.h1
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_ */