#include #include #include #include #include "PropertiesParser.h" #include "PropertiesUtils.h" #include #include #include namespace cppproperties { PropertiesParser::PropertiesParser() { } PropertiesParser::~PropertiesParser() { } //读取配置文件参数放入map // fpga.correctColor.BOffset = 119,119,119,119,119,119 //路径 :/usr/local/huago/fpga.properties //key //val Properties PropertiesParser::Read(const std::string& file) { Properties properties; std::ifstream is; is.open(file.c_str()); if (is.is_open()) { try { size_t linenr = 0; std::string line; while (getline(is, line)) { if (PropertiesUtils::IsEmptyLine(line) || PropertiesUtils::IsComment(line)) { // ignore it } else if (PropertiesUtils::IsProperty(line)) { std::pair prop = PropertiesUtils::ParseProperty(line); properties.AddProperty(prop.first, prop.second); // if(prop.first == "fpga.correctColor.BOffset") // printf("uuuuuuuuuuuuuuuuuuuu %s\r\n",prop.second.c_str()); } ++linenr; } is.close(); } catch (...) { // don't forget to close the ifstream is.close(); } } return properties; } //写入配置文件 void PropertiesParser::Write(const std::string& file, const Properties& props) { std::ofstream os; os.open(file.c_str()); if (!os.is_open()) { throw PropertiesException("PropertiesParser::Write(" + file + "): Unable to open file for writing."); } try { const std::vector& keys = props.GetPropertyNames(); for (std::vector::const_iterator i = keys.begin(); i != keys.end(); ++i) { os << *i << " = " << props.GetProperty(*i) << std::endl; } os.close(); } catch (...) { // don't forget to close the ofstream os.close(); throw; } int fd = open(file.c_str(), O_RDONLY); if (fd > 0) { fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); close(fd); } } } /* namespace cppproperties */