Saqib Khan | 167601b | 2017-06-18 23:33:46 -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 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include "xyz/openbmc_project/Common/error.hpp" |
| 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace software |
| 14 | { |
| 15 | namespace updater |
| 16 | { |
| 17 | |
| 18 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 19 | using namespace phosphor::logging; |
| 20 | |
| 21 | std::string Version::getId(const std::string& version) |
| 22 | { |
| 23 | std::stringstream hexId; |
| 24 | |
| 25 | if (version.empty()) |
| 26 | { |
| 27 | log<level::ERR>("Error version is empty"); |
| 28 | elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument:: |
| 29 | ARGUMENT_NAME("Version"), |
| 30 | xyz::openbmc_project::Common::InvalidArgument:: |
| 31 | ARGUMENT_VALUE(version.c_str())); |
| 32 | } |
| 33 | |
| 34 | // Only want 8 hex digits. |
| 35 | hexId << std::hex << ((std::hash<std::string> {}( |
| 36 | version)) & 0xFFFFFFFF); |
| 37 | return hexId.str(); |
| 38 | } |
| 39 | |
| 40 | std::map<std::string, std::string> Version::getValue( |
| 41 | const std::string& filePath, std::map<std::string, std::string> keys) |
| 42 | { |
| 43 | |
| 44 | if (filePath.empty()) |
| 45 | { |
| 46 | log<level::ERR>("Error filePath is empty"); |
| 47 | elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument:: |
| 48 | ARGUMENT_NAME("FilePath"), |
| 49 | xyz::openbmc_project::Common::InvalidArgument:: |
| 50 | ARGUMENT_VALUE(filePath.c_str())); |
| 51 | } |
| 52 | |
| 53 | std::ifstream efile; |
| 54 | std::string line; |
| 55 | efile.exceptions(std::ifstream::failbit |
| 56 | | std::ifstream::badbit |
| 57 | | std::ifstream::eofbit); |
| 58 | |
| 59 | try |
| 60 | { |
| 61 | efile.open(filePath); |
| 62 | while (getline(efile, line)) |
| 63 | { |
| 64 | for(auto& key : keys) |
| 65 | { |
| 66 | auto value = key.first + "="; |
| 67 | auto keySize = value.length(); |
| 68 | if (line.compare(0, keySize, value) == 0) |
| 69 | { |
| 70 | key.second = line.substr(keySize); |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | efile.close(); |
| 76 | } |
| 77 | catch (const std::exception& e) |
| 78 | { |
| 79 | log<level::ERR>("Error in reading file"); |
| 80 | } |
| 81 | |
| 82 | return keys; |
| 83 | } |
| 84 | |
| 85 | } // namespace updater |
| 86 | } // namespace software |
| 87 | } // namespace openpower |