@@ -10,36 +10,90 @@ LocationManager::LocationManager(std::string mainHeaderPath)
10
10
void LocationManager::loadConfig (const std::string &path) {
11
11
std::string realPath = getRealPath (path.c_str ());
12
12
13
+ std::stringstream s;
13
14
std::ifstream input (realPath);
14
15
for (std::string line; getline (input, line);) {
15
- size_t f = line.find (' =' );
16
- if (f != std::string::npos) {
17
- std::string header = line.substr (0 , f);
18
- std::string import = line.substr (f + 1 , std::string::npos);
19
- existingBindings[header] = import ;
20
- }
16
+ s << line;
21
17
}
18
+ config = json::parse (s.str ());
19
+ assert (config.is_object ());
22
20
}
23
21
24
22
bool LocationManager::inMainFile (const Location &location) const {
25
23
return location.getPath () == mainHeaderPath;
26
24
}
27
25
28
26
bool LocationManager::isImported (const Location &location) const {
29
- for (const auto &existingBinding : existingBindings) {
30
- if (endsWith (location.getPath (), existingBinding.first )) {
31
- return true ;
27
+ for (auto it = config.begin (); it != config.end (); ++it) {
28
+ json libObject = it.value ();
29
+ for (const auto &header : libObject[" headers" ]) {
30
+ if (equal (header, location)) {
31
+ return true ;
32
+ }
32
33
}
33
34
}
34
35
return false ;
35
36
}
36
37
38
+ bool LocationManager::equal (json header, const Location &location) const {
39
+ std::string headerName = getHeaderName (header);
40
+ return endsWith (location.getPath (), headerName);
41
+ }
42
+
37
43
std::string
38
44
LocationManager::getContainingObject (const Location &location) const {
39
- for (const auto &existingBinding : existingBindings) {
40
- if (endsWith (location.getPath (), existingBinding.first )) {
41
- return existingBinding.second ;
45
+ for (auto it = config.begin (); it != config.end (); ++it) {
46
+ json libObject = it.value ();
47
+ for (const json &header : libObject[" headers" ]) {
48
+ if (equal (header, location)) {
49
+ return getContainingObject (libObject, header);
50
+ }
42
51
}
43
52
}
44
- assert (false && " Location is not in the list of imported bindings" );
53
+ throw std::logic_error (" Location: " + location.getPath () +
54
+ " does not belong to any known library" );
55
+ }
56
+
57
+ std::string LocationManager::getHeaderName (const json &header) const {
58
+ if (header.is_string ()) {
59
+ return header.get <std::string>();
60
+ } else {
61
+ return header[" header" ].get <std::string>();
62
+ }
63
+ }
64
+
65
+ std::string LocationManager::getContainingObject (const json &libObject,
66
+ const json &header) const {
67
+ std::string package;
68
+ std::string name;
69
+ if (libObject.find (" package" ) != libObject.end ()) {
70
+ package = libObject[" package" ].get <std::string>();
71
+ }
72
+ if (libObject.find (" name" ) != libObject.end ()) {
73
+ package = libObject[" name" ].get <std::string>();
74
+ }
75
+ if (header.is_object ()) {
76
+ /* override default values */
77
+ if (header.find (" package" ) != header.end ()) {
78
+ package = header[" package" ].get <std::string>();
79
+ }
80
+ if (header.find (" name" ) != header.end ()) {
81
+ name = header[" name" ].get <std::string>();
82
+ }
83
+ }
84
+ if (name.empty ()) {
85
+ /* extract object name from header name */
86
+ name = getHeaderName (header);
87
+ auto slashPos = name.find_last_of (' /' );
88
+ if (slashPos != std::string::npos) {
89
+ name = name.substr (slashPos + 1 , name.length ());
90
+ }
91
+ if (endsWith (name, " .h" )) {
92
+ name = name.substr (0 , name.length () - 2 );
93
+ }
94
+ }
95
+ if (!package.empty ()) {
96
+ return package + " ." + name;
97
+ }
98
+ return name;
45
99
}
0 commit comments