#include "Settings.h" #include "BoostLog.h" #include #include #include constexpr auto SettingsFilePath = "settings.xml"; Settings::Settings() { if (!std::filesystem::exists(SettingsFilePath)) { save(); } } void Settings::save() { boost::property_tree::ptree ptree; ptree.put("Application.DataCollection.ImageFormat", static_cast(m_imageFormat)); std::ostringstream oss; for (int i = 0; i < static_cast(ImageFormat::None); i++) { oss << i << ":" << static_cast(i) << " "; } ptree.put("Application.DataCollection.ImageFormat.", oss.str()); ptree.put("Application.DataCollection.ImageQuality", m_imageQuality); ptree.put("Application.DataCollection.ImageQuality.", "0-100,仅对jpg有效"); for (auto &device : m_supportedDevices) { ptree.add("Application.SupportedDevices.Model", device); } boost::property_tree::xml_writer_settings settings('\t', 1); boost::property_tree::write_xml(SettingsFilePath, ptree, std::locale(), settings); } void Settings::load() { boost::property_tree::ptree ptree; m_supportedDevices.clear(); try { boost::property_tree::read_xml(SettingsFilePath, ptree); m_imageFormat = static_cast(ptree.get("Application.DataCollection.ImageFormat")); m_imageQuality = ptree.get("Application.DataCollection.ImageQuality"); for (auto &child : ptree.get_child("Application.SupportedDevices")) { if (child.first != "Model") continue; m_supportedDevices.push_back(child.second.get("")); } } catch (...) { LOG(error) << "parse " << SettingsFilePath << " failed."; } std::ostringstream oss; oss << "["; for (auto &device : m_supportedDevices) { oss << device << ", "; } oss << "]"; LOG(info) << "image format: " << m_imageFormat << ", quality: " << m_imageQuality << ", supported devices: " << oss.str(); } ImageFormat Settings::imageFormat() const { return m_imageFormat; } int Settings::imageQuality() const { return m_imageQuality; } std::list Settings::supportedDevices() const { return m_supportedDevices; }