zynq_7010/PropertiesParser.cpp

79 lines
2.3 KiB
C++
Raw Normal View History

2023-07-17 03:29:37 +00:00
#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() {
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>map // fpga.correctColor.BOffset = 119,119,119,119,119,119
//·<><C2B7> <20><>/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;
}
//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
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 */