forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathname.cpp
More file actions
137 lines (118 loc) · 4.6 KB
/
name.cpp
File metadata and controls
137 lines (118 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <celutil/debug.h>
#include <celutil/gettext.h>
#include "name.h"
uint32_t NameDatabase::getNameCount() const
{
return nameIndex.size();
}
void NameDatabase::add(const AstroCatalog::IndexNumber catalogNumber, const std::string& name, bool /*replaceGreek*/)
{
if (name.length() != 0)
{
#ifdef DEBUG
AstroCatalog::IndexNumber tmp;
if ((tmp = getCatalogNumberByName(name, false)) != AstroCatalog::InvalidIndex)
DPRINTF(LOG_LEVEL_INFO,"Duplicated name '%s' on object with catalog numbers: %d and %d\n", name.c_str(), tmp, catalogNumber);
#endif
// Add the new name
//nameIndex.insert(NameIndex::value_type(name, catalogNumber));
std::string fname = ReplaceGreekLetterAbbr(name);
nameIndex[fname] = catalogNumber;
std::string lname = _(fname.c_str());
if (lname != fname)
localizedNameIndex[lname] = catalogNumber;
numberIndex.insert(NumberIndex::value_type(catalogNumber, fname));
}
}
void NameDatabase::erase(const AstroCatalog::IndexNumber catalogNumber)
{
numberIndex.erase(catalogNumber);
}
AstroCatalog::IndexNumber NameDatabase::getCatalogNumberByName(const std::string& name, bool i18n) const
{
auto iter = nameIndex.find(name);
if (iter != nameIndex.end())
return iter->second;
if (i18n)
{
iter = localizedNameIndex.find(name);
if (iter != localizedNameIndex.end())
return iter->second;
}
auto replacedGreek = ReplaceGreekLetterAbbr(name);
if (replacedGreek != name)
return getCatalogNumberByName(replacedGreek, i18n);
return AstroCatalog::InvalidIndex;
}
// Return the first name matching the catalog number or end()
// if there are no matching names. The first name *should* be the
// proper name of the OBJ, if one exists. This requires the
// OBJ name database file to have the proper names listed before
// other designations. Also, the STL implementation must
// preserve this order when inserting the names into the multimap
// (not certain whether or not this behavior is in the STL spec.
// but it works on the implementations I've tried so far.)
std::string NameDatabase::getNameByCatalogNumber(const AstroCatalog::IndexNumber catalogNumber) const
{
if (catalogNumber == AstroCatalog::InvalidIndex)
return "";
NumberIndex::const_iterator iter = numberIndex.lower_bound(catalogNumber);
if (iter != numberIndex.end() && iter->first == catalogNumber)
return iter->second;
return "";
}
// Return the first name matching the catalog number or end()
// if there are no matching names. The first name *should* be the
// proper name of the OBJ, if one exists. This requires the
// OBJ name database file to have the proper names listed before
// other designations. Also, the STL implementation must
// preserve this order when inserting the names into the multimap
// (not certain whether or not this behavior is in the STL spec.
// but it works on the implementations I've tried so far.)
NameDatabase::NumberIndex::const_iterator NameDatabase::getFirstNameIter(const AstroCatalog::IndexNumber catalogNumber) const
{
NumberIndex::const_iterator iter = numberIndex.lower_bound(catalogNumber);
if (iter == numberIndex.end() || iter->first != catalogNumber)
return getFinalNameIter();
else
return iter;
}
NameDatabase::NumberIndex::const_iterator NameDatabase::getFinalNameIter() const
{
return numberIndex.end();
}
std::vector<std::string> NameDatabase::getCompletion(const std::string& name, bool i18n, bool greek) const
{
if (greek)
{
auto compList = getGreekCompletion(name);
compList.push_back(name);
return getCompletion(compList, i18n);
}
std::vector<std::string> completion;
int name_length = UTF8Length(name);
for (NameIndex::const_iterator iter = nameIndex.begin(); iter != nameIndex.end(); ++iter)
{
if (!UTF8StringCompare(iter->first, name, name_length, true))
completion.push_back(iter->first);
}
if (i18n)
{
for (NameIndex::const_iterator iter = localizedNameIndex.begin(); iter != localizedNameIndex.end(); ++iter)
{
if (!UTF8StringCompare(iter->first, name, name_length, true))
completion.push_back(iter->first);
}
}
return completion;
}
std::vector<std::string> NameDatabase::getCompletion(const std::vector<std::string> &list, bool i18n) const
{
std::vector<std::string> completion;
for (const auto &n : list)
{
for (const auto &nn : getCompletion(n, i18n, false))
completion.emplace_back(nn);
}
return completion;
}