Commit f469e4fe authored by Administrator's avatar Administrator
Browse files

add setup loading from file

parent 48df3400
......@@ -10,5 +10,8 @@
# Include all h file under all folder
!**/*.h
# Include all configure file under all folder
!**/*.conf
# Include .gitignore itself
!.gitignore
#include <iostream>
#include <fstream>
#include <map>
#include "Constant.h"
using namespace std;
map<string, string> loadSetup(string setupFileName) {
// 定义文件流,用于打开文件
fstream fs;
fs.open(setupFileName, ios::in);
// 定义变量,用于存放行
string line;
// 定义map
map<string, string> mapSetup;
// 逐行读取
if (!fs) {
cout << "Cannot open file.\n";
return mapSetup;
} else {
// 逐行读取
while (getline(fs, line)) {
// 移除空格
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
// 是否以#开头
if (line[0] == '#') {
continue;
}
// 定义分隔符
char delimiter = '=';
// 取分隔符的位置
size_t pos = line.find(delimiter);
// 如果分隔符找不到
if (pos == string::npos) {
cout << "Not found!" << endl;
return mapSetup;
}
// 取key
string key = line.substr(0, pos);
// 取value
string value = line.substr(pos + 1);
mapSetup.insert(std::pair<string, string>(key, value));
}
}
return mapSetup;
};
\ No newline at end of file
#pragma once // 防止头文件被重复包含
#include <iostream>
#include <map>
using namespace std;
static string SETUP_FILE_NAME = "common/Setup.conf";
static string RESOLUTION_WIDTH = "resolution_width";
static string RESOLUTION_HEIGHT = "resolution_height";
map<string, string> loadSetup(string setupFileName);
\ No newline at end of file
#This is setup file
resolution_width = 1920
resolution_height = 1080
mode = 1
multiuser_allowed = false
\ No newline at end of file
#include <iostream>
#include "Setup.h"
#include "Constant.h"
using namespace std;
map<string, string> setup();
// 构造函数
Setup::Setup(string setupFileName) {
this->mapSetup = loadSetup(setupFileName);
this->setResolution();
};
// 打印所有设定项
void Setup::printSetup() {
cout << "----- Print all setup -----" << endl;
for (auto & x: this->mapSetup) {
std::cout << "[" << x.first << "]:[" << x.second << "]" << endl;
}
};
// 设定分辨率
void Setup::setResolution() {
this->resolution_width = 1920; // 从设定处取得
this->resolution_height = 1080; // 从设定处取得
this->resolution_width = stoi(mapSetup.at(RESOLUTION_WIDTH)); // 从设定处取得
this->resolution_height = stoi(mapSetup.at(RESOLUTION_HEIGHT)); // 从设定处取得
};
......
#pragma once // 防止头文件被重复包含
#include <iostream>
#include <map>
using namespace std;
......@@ -13,9 +14,17 @@ class Setup {
int resolution_width;
// 垂直分辨率
int resolution_height;
// 定义存放设定值的map
map<string, string> mapSetup;
public:
// 构造函数
Setup(string setupFileName);
// 打印所有设定项
void printSetup();
// 设定分辨率
void setResolution();
......
#include <iostream>
#include "common/Setup.h"
#include "common/Constant.h"
int main() {
Setup s;
s.setResolution();
// 读取文件,装载设定项
Setup s(SETUP_FILE_NAME);
// 打印所有装载的设定项
s.printSetup();
// 打印分辨率设定
cout << "----- Resolution setup -----" << endl;
cout << "Resolution: "<< s.getResolution() << endl;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment