zynq_7010/PropertiesParser.cpp

79 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include "PropertiesParser.h"
#include "PropertiesUtils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
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<std::string, std::string> 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<std::string>& keys = props.GetPropertyNames();
for (std::vector<std::string>::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 */