44#include " Provinces/Province.h"
55#include < fstream>
66
7+
8+
9+ namespace
10+ {
11+
12+ std::optional<std::tuple<std::string, unsigned char , unsigned char , unsigned char , std::string>> parseLine (const std::string& line)
13+ {
14+ try
15+ {
16+ auto sepLoc = line.find (' ;' );
17+ if (sepLoc == std::string::npos)
18+ return std::nullopt ;
19+ auto sepLocSave = sepLoc;
20+ auto ID = line.substr (0 , sepLoc);
21+ sepLoc = line.find (' ;' , sepLocSave + 1 );
22+ if (sepLoc == std::string::npos)
23+ return std::nullopt ;
24+ auto r = static_cast <unsigned char >(std::stoi (line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 )));
25+ sepLocSave = sepLoc;
26+ sepLoc = line.find (' ;' , sepLocSave + 1 );
27+ if (sepLoc == std::string::npos)
28+ return std::nullopt ;
29+ auto g = static_cast <unsigned char >(std::stoi (line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 )));
30+ sepLocSave = sepLoc;
31+ sepLoc = line.find (' ;' , sepLocSave + 1 );
32+ if (sepLoc == std::string::npos)
33+ return std::nullopt ;
34+ auto b = static_cast <unsigned char >(std::stoi (line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 )));
35+ sepLocSave = sepLoc;
36+ sepLoc = line.find (' ;' , sepLocSave + 1 );
37+ if (sepLoc == std::string::npos)
38+ return std::nullopt ;
39+ auto mapDataName = line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 );
40+ return std::make_tuple (ID, r, g, b, mapDataName);
41+ }
42+ catch (std::exception& e)
43+ {
44+ Log (LogLevel::Warning) << " Broken Definition Line: " << line << " - " << e.what ();
45+ return std::nullopt ;
46+ }
47+ }
48+
49+ } // namespace
50+
51+
752void Definitions::loadDefinitions (const std::string& fileName, const LocalizationMapper& localizationMapper, LocalizationMapper::LocType locType)
853{
954 if (!commonItems::DoesFileExist (fileName))
@@ -35,7 +80,7 @@ void Definitions::parseStream(std::istream& theStream, const LocalizationMapper&
3580 if (locType == LocalizationMapper::LocType::SOURCE)
3681 {
3782 // can we get a locname? Probe for PROV first.
38- auto locName = localizationMapper.getLocForSourceKey (" PROV" + std::to_string (ID) );
83+ auto locName = localizationMapper.getLocForSourceKey (" PROV" + ID );
3984 if (locName && !locName->empty ())
4085 {
4186 province->locName = locName;
@@ -51,7 +96,7 @@ void Definitions::parseStream(std::istream& theStream, const LocalizationMapper&
5196 else
5297 {
5398 // ditto for the other defs.
54- auto locName = localizationMapper.getLocForTargetKey (" PROV" + std::to_string (ID) );
99+ auto locName = localizationMapper.getLocForTargetKey (" PROV" + ID );
55100 if (locName && !locName->empty ())
56101 {
57102 province->locName = locName;
@@ -74,43 +119,6 @@ void Definitions::parseStream(std::istream& theStream, const LocalizationMapper&
74119 }
75120}
76121
77- std::optional<std::tuple<int , unsigned char , unsigned char , unsigned char , std::string>> Definitions::parseLine (const std::string& line) const
78- {
79- try
80- {
81- auto sepLoc = line.find (' ;' );
82- if (sepLoc == std::string::npos)
83- return std::nullopt ;
84- auto sepLocSave = sepLoc;
85- auto ID = std::stoi (line.substr (0 , sepLoc));
86- sepLoc = line.find (' ;' , sepLocSave + 1 );
87- if (sepLoc == std::string::npos)
88- return std::nullopt ;
89- auto r = static_cast <unsigned char >(std::stoi (line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 )));
90- sepLocSave = sepLoc;
91- sepLoc = line.find (' ;' , sepLocSave + 1 );
92- if (sepLoc == std::string::npos)
93- return std::nullopt ;
94- auto g = static_cast <unsigned char >(std::stoi (line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 )));
95- sepLocSave = sepLoc;
96- sepLoc = line.find (' ;' , sepLocSave + 1 );
97- if (sepLoc == std::string::npos)
98- return std::nullopt ;
99- auto b = static_cast <unsigned char >(std::stoi (line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 )));
100- sepLocSave = sepLoc;
101- sepLoc = line.find (' ;' , sepLocSave + 1 );
102- if (sepLoc == std::string::npos)
103- return std::nullopt ;
104- auto mapDataName = line.substr (sepLocSave + 1 , sepLoc - sepLocSave - 1 );
105- return std::make_tuple (ID, r, g, b, mapDataName);
106- }
107- catch (std::exception& e)
108- {
109- Log (LogLevel::Warning) << " Broken Definition Line: " << line << " - " << e.what ();
110- return std::nullopt ;
111- }
112- }
113-
114122void Definitions::registerPixel (int x, int y, unsigned char r, unsigned char g, unsigned char b)
115123{
116124 Pixel pixel (x, y, r, g, b);
@@ -135,7 +143,7 @@ std::optional<std::string> Definitions::getNameForChroma(const int chroma)
135143 return std::nullopt ;
136144}
137145
138- std::optional<int > Definitions::getIDForChroma (const int chroma)
146+ std::optional<std::string > Definitions::getIDForChroma (const int chroma)
139147{
140148 if (const auto & chromaCacheItr = chromaCache.find (chroma); chromaCacheItr != chromaCache.end ())
141149 return chromaCacheItr->second ->ID ;
@@ -151,7 +159,7 @@ std::shared_ptr<Province> Definitions::getProvinceForChroma(const int chroma)
151159 return nullptr ;
152160}
153161
154- std::shared_ptr<Province> Definitions::getProvinceForID (const int ID)
162+ std::shared_ptr<Province> Definitions::getProvinceForID (const std::string& ID)
155163{
156164 if (const auto & provinceItr = provinces.find (ID); provinceItr != provinces.end ())
157165 return provinceItr->second ;
0 commit comments