/*
* Copyright (C) 2021 P. J. McDermott
*
* This file is part of Dodge Balls
*
* Dodge Balls is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dodge Balls is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dodge Balls. If not, see .
*/
#include
#include
#include
#include
#include "locale.h"
#include "output.h"
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;
}