Symphony Of Empires
string.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // string.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <unordered_map>
26 #include <cstring>
27 #include <mutex>
28 #include <memory>
29 #include "eng3d/string.hpp"
30 
31 //
32 // StringRef
33 //
34 Eng3D::StringRef::StringRef(const std::string_view str) {
35  *this = Eng3D::StringManager::get_instance().insert(std::string{str});
36 }
37 
38 const std::string_view Eng3D::StringRef::get_string() const {
39  if(this->id == static_cast<size_t>(-1)) return "";
41 }
42 
43 //
44 // StringManager
45 //
46 static Eng3D::StringManager *g_string_man = nullptr;
48  : s{ _s }
49 {
50  g_string_man = this;
51 }
52 
54  return *g_string_man;
55 }
56 
57 static std::unordered_map<std::string, std::string> trans_msg;
58 static std::mutex trans_lock;
59 void Eng3D::Locale::from_file(const std::string& filename) {
60  std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "rt"), fclose);
61  char tmp[100];
62  while(fgets(tmp, sizeof tmp, fp.get()) != nullptr) {
63  if(!strncmp(tmp, "msgid", 5)) {
64  char msgid[100];
65  sscanf(tmp + 5, " %*c%[^\"]s%*c ", msgid);
66  fgets(tmp, sizeof tmp, fp.get());
67  if(!strncmp(tmp, "msgstr", 6)) {
68  char msgstr[100];
69  sscanf(tmp + 6, " %*c%[^\"]s%*c ", msgstr);
70  trans_msg[msgid] = msgstr;
71  }
72  }
73  }
74 }
75 
76 std::string Eng3D::Locale::translate(const std::string_view str) {
77  std::scoped_lock lock(trans_lock);
78  if(trans_msg[str.data()].empty())
79  return std::string(str);
80  return trans_msg[str.data()];
81 }
The string pool manager (singleton), used mainly for translation purpouses. But also helps to reduce ...
Definition: string.hpp:70
const std::string_view get_by_id(const Eng3D::StringRef ref) const
Definition: string.hpp:84
static StringManager & get_instance()
Definition: string.cpp:53
StringManager(Eng3D::State &_s)
Definition: string.cpp:47
Eng3D::StringRef insert(const std::string &str)
Definition: string.hpp:76
void from_file(const std::string &filename)
Definition: string.cpp:59
std::string translate(const std::string_view str)
Definition: string.cpp:76
constexpr StringRef()=default
const std::string_view get_string() const
Definition: string.cpp:38