GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/langs.cpp Lines: 37 50 74.0 %
Date: 2017-11-29 Branches: 36 74 48.6 %

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

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

1300
    std::string lang = config.getStringValue("lang");
45
260
    if (lang.empty())
46
    {
47
254
        const char *const lng = getLangName();
48
254
        if (lng == nullptr)
49
            return langs;
50
        lang = lng;
51
    }
52
6
    else if (lang.size() > 1000)
53
    {
54
        return langs;
55
    }
56
57
230
    size_t dot = lang.find('.');
58
230
    if (dot != std::string::npos)
59
444
        lang = lang.substr(0, dot);
60
230
    langs.push_back(lang);
61
230
    dot = lang.find('_');
62
230
    if (dot != std::string::npos)
63
678
        langs.push_back(lang.substr(0, dot));
64
    return langs;
65
}
66
67
LangVect getServerLang()
68
{
69
    LangVect langs;
70
#ifndef DYECMD
71
    const int id = PlayerInfo::getServerLanguage();
72
    const std::string &lang = LanguageDb::getPo(id);
73
    if (lang.empty())
74
        return langs;
75
    langs.push_back(lang);
76
    const size_t idx = lang.find('_');
77
    if (idx != std::string::npos)
78
        langs.push_back(lang.substr(0, idx));
79
#endif  // DYECMD
80
81
    return langs;
82
}
83
84
12
std::string getLangSimple()
85
{
86

60
    const std::string lang = config.getStringValue("lang");
87
12
    if (lang.empty())
88
    {
89
6
        const char *const lng = getLangName();
90
6
        if (lng == nullptr)
91
            return "";
92
18
        return lng;
93
    }
94
6
    else if (lang.size() > 1000)
95
    {
96
        return "";
97
    }
98
    return lang;
99
}
100
101
16
std::string getLangShort()
102
{
103

80
    std::string lang = config.getStringValue("lang");
104
16
    if (lang.empty())
105
    {
106
10
        const char *const lng = getLangName();
107
10
        if (lng == nullptr)
108
            return "";
109
        lang = lng;
110
    }
111
6
    else if (lang.size() > 1000)
112
    {
113
        return "";
114
    }
115
116
16
    size_t dot = lang.find('.');
117
16
    if (dot != std::string::npos)
118
16
        lang = lang.substr(0, dot);
119
16
    dot = lang.find('_');
120
16
    if (dot != std::string::npos)
121
12
        return lang.substr(0, dot);
122
    return lang;
123
}