GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/langs.cpp Lines: 37 50 74.0 %
Date: 2021-03-17 Branches: 34 70 48.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "utils/langs.h"
23
24
#include "configuration.h"
25
26
#ifndef DYECMD
27
#include "being/playerinfo.h"
28
29
#include "resources/db/languagedb.h"
30
#endif  // DYECMD
31
32
#include "debug.h"
33
34
138
static const char *getLangName()
35
{
36
138
    const char *const lang = getenv("LANG");
37

138
    if ((lang != nullptr) && strlen(lang) > 1000)
38
        return nullptr;
39
138
    return lang;
40
}
41
42
133
LangVect getLang()
43
{
44
133
    LangVect langs;
45

665
    std::string lang = config.getStringValue("lang");
46
133
    if (lang.empty())
47
    {
48
130
        const char *const lng = getLangName();
49
130
        if (lng == nullptr)
50
            return langs;
51
        lang = lng;
52
    }
53
3
    else if (lang.size() > 1000)
54
    {
55
        return langs;
56
    }
57
58
117
    size_t dot = lang.find('.');
59
117
    if (dot != std::string::npos)
60
226
        lang = lang.substr(0, dot);
61
117
    langs.push_back(lang);
62
117
    dot = lang.find('_');
63
117
    if (dot != std::string::npos)
64
345
        langs.push_back(lang.substr(0, dot));
65
    return langs;
66
}
67
68
LangVect getServerLang()
69
{
70
    LangVect langs;
71
#ifndef DYECMD
72
    const int id = PlayerInfo::getServerLanguage();
73
    const std::string &lang = LanguageDb::getPo(id);
74
    if (lang.empty())
75
        return langs;
76
    langs.push_back(lang);
77
    const size_t idx = lang.find('_');
78
    if (idx != std::string::npos)
79
        langs.push_back(lang.substr(0, idx));
80
#endif  // DYECMD
81
82
    return langs;
83
}
84
85
6
std::string getLangSimple()
86
{
87
30
    const std::string lang = config.getStringValue("lang");
88
6
    if (lang.empty())
89
    {
90
3
        const char *const lng = getLangName();
91
3
        if (lng == nullptr)
92
            return "";
93
6
        return lng;
94
    }
95
3
    else if (lang.size() > 1000)
96
    {
97
        return "";
98
    }
99
    return lang;
100
}
101
102
8
std::string getLangShort()
103
{
104
40
    std::string lang = config.getStringValue("lang");
105
8
    if (lang.empty())
106
    {
107
5
        const char *const lng = getLangName();
108
5
        if (lng == nullptr)
109
            return "";
110
        lang = lng;
111
    }
112
3
    else if (lang.size() > 1000)
113
    {
114
        return "";
115
    }
116
117
8
    size_t dot = lang.find('.');
118
8
    if (dot != std::string::npos)
119
8
        lang = lang.substr(0, dot);
120
8
    dot = lang.find('_');
121
8
    if (dot != std::string::npos)
122
6
        return lang.substr(0, dot);
123
    return lang;
124
}