Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <sstream> |
| 4 | #include <fstream> |
| 5 | #include <stdexcept> |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | #include "version.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace software |
| 12 | { |
| 13 | namespace manager |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 18 | std::string Version::getValue(const std::string& manifestFilePath, |
| 19 | std::string key) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 20 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 21 | key = key + "="; |
| 22 | auto keySize = key.length(); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 23 | |
| 24 | if (manifestFilePath.empty()) |
| 25 | { |
| 26 | log<level::ERR>("Error MANIFESTFilePath is empty"); |
| 27 | throw std::runtime_error("MANIFESTFilePath is empty"); |
| 28 | } |
| 29 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 30 | std::string value{}; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 31 | std::ifstream efile; |
| 32 | std::string line; |
| 33 | efile.exceptions(std::ifstream::failbit |
| 34 | | std::ifstream::badbit |
| 35 | | std::ifstream::eofbit); |
| 36 | |
| 37 | // Too many GCC bugs (53984, 66145) to do this the right way... |
| 38 | try |
| 39 | { |
| 40 | efile.open(manifestFilePath); |
| 41 | while (getline(efile, line)) |
| 42 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 43 | if (line.compare(0, keySize, key) == 0) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 44 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 45 | value = line.substr(keySize); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | } |
| 49 | efile.close(); |
| 50 | } |
| 51 | catch (const std::exception& e) |
| 52 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 53 | log<level::ERR>("Error in reading MANIFEST file"); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 56 | return value; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::string Version::getId(const std::string& version) |
| 60 | { |
| 61 | std::stringstream hexId; |
| 62 | |
| 63 | if (version.empty()) |
| 64 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 65 | log<level::ERR>("Error version is empty"); |
| 66 | throw std::runtime_error("Version is empty"); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Only want 8 hex digits. |
| 70 | hexId << std::hex << ((std::hash<std::string> {}( |
| 71 | version)) & 0xFFFFFFFF); |
| 72 | return hexId.str(); |
| 73 | } |
| 74 | |
| 75 | } // namespace manager |
| 76 | } // namespace software |
| 77 | } // namepsace phosphor |
| 78 | |