Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加配置文件系统 #2

Open
wants to merge 1 commit into
base: toad_dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,597 changes: 2,597 additions & 0 deletions config/common/rapidxml.hpp

Large diffs are not rendered by default.

447 changes: 447 additions & 0 deletions config/common/rapidxml_print.hpp

Large diffs are not rendered by default.

123 changes: 123 additions & 0 deletions config/common/rapidxml_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#pragma once
#ifndef RAPIDXML_UTILS_HPP_INCLUDED
#define RAPIDXML_UTILS_HPP_INCLUDED

// Copyright (C) 2006, 2009 Marcin Kalicinski
// Version 1.13
// Revision $DateTime: 2009/05/13 01:46:17 $
//! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful
//! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective.

#include "rapidxml.hpp"
#include <vector>
#include <string>
#include <fstream>
#include <stdexcept>

namespace rapidxml
{

//! Represents data loaded from a file
template<class Ch = char>
class file
{

public:

//! Loads file into the memory. Data will be automatically destroyed by the destructor.
//! \param filename Filename to load.
file(const char *filename)
{
using namespace std;

// Open stream
basic_ifstream<Ch> stream(filename, ios::binary);
if (!stream)
throw runtime_error(string("cannot open file ") + filename);
stream.unsetf(ios::skipws);

// Determine stream size
stream.seekg(0, ios::end);
size_t size = stream.tellg();
stream.seekg(0);

// Load data and add terminating 0
m_data.resize(size + 1);
stream.read(&m_data.front(), static_cast<streamsize>(size));
m_data[size] = 0;
}

//! Loads file into the memory. Data will be automatically destroyed by the destructor
//! \param stream Stream to load from
file(std::basic_istream<Ch> &stream)
{
using namespace std;

// Load data and add terminating 0
stream.unsetf(ios::skipws);
m_data.assign(istreambuf_iterator<Ch>(stream), istreambuf_iterator<Ch>());
if (stream.fail() || stream.bad())
throw runtime_error("error reading stream");
m_data.push_back(0);
}

//! Gets file data.
//! \return Pointer to data of file.
Ch *data()
{
return &m_data.front();
}

//! Gets file data.
//! \return Pointer to data of file.
const Ch *data() const
{
return &m_data.front();
}

//! Gets file data size.
//! \return Size of file data, in characters.
std::size_t size() const
{
return m_data.size();
}

private:

std::vector<Ch> m_data; // File data

};

//! Counts children of node. Time complexity is O(n).
//! \return Number of children of node
template<class Ch>
inline std::size_t count_children(xml_node<Ch> *node)
{
xml_node<Ch> *child = node->first_node();
std::size_t count = 0;
while (child)
{
++count;
child = child->next_sibling();
}
return count;
}

//! Counts attributes of node. Time complexity is O(n).
//! \return Number of attributes of node
template<class Ch>
inline std::size_t count_attributes(xml_node<Ch> *node)
{
xml_attribute<Ch> *attr = node->first_attribute();
std::size_t count = 0;
while (attr)
{
++count;
attr = attr->next_attribute();
}
return count;
}

}

#endif
5 changes: 5 additions & 0 deletions config/process/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Server:*.cpp
g++ -g -std=c++11 *.cpp -o server

.PHONY clean:
rm -rf *.o
109 changes: 109 additions & 0 deletions config/process/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "config.h"
#include <cstring>

#define GetInt32Attribute(key, node) \
if (!key || !node){\
pszValue = NULL; \
} \
else{\
rapidxml::xml_attribute<>* attr = node->first_attribute(key); \
pszValue = attr ? attr->value() : NULL; \
if (pszValue && strlen(pszValue) > 0){\
iValue = atoi(pszValue); \
}\
}

#define GetStringAttrbute(key, node) \
if (!key || !node){\
pszValue = NULL; \
}\
else{ \
rapidxml::xml_attribute<>* attr = node->first_attribute(key); \
pszValue = attr ? attr->value() : NULL; \
}

#define STRNCPY(dst, src, length) \
do \
{ \
strncpy(dst, src, (length-1)); \
dst[(length-1)] = '\0'; \
} \
while(0)


template <typename T>
void Config::ParseIntegerValue(rapidxml::xml_node<>* node, const char* key, T& ret, T default_value){
GetInt32Attribute(key, node);
if (NULL == pszValue) {
ret = default_value;
} else {
ret = iValue;
}
printf("[Config][key=%s][value=%d]\n", key, ret);
}

void Config::ParseDecimalValue(rapidxml::xml_node<>* node, const char* key, double& ret, double default_value){
GetStringAttrbute(key, node);
if (NULL == pszValue) {
ret = default_value;
} else {
memset(m_szTmp, 0, sizeof(m_szTmp));
STRNCPY(m_szTmp, pszValue, sizeof(m_szTmp));
ret = strtod(m_szTmp, NULL);
}
printf("[Config][key=%s][value=%f]\n", key, ret);
}

void Config::ParseStringValue(rapidxml::xml_node<>* node, const char* key, std::string& ret, std::string default_value){
GetStringAttrbute(key, node);
if (NULL == pszValue) {
ret = default_value;
} else {
memset(m_szTmp, 0, sizeof(m_szTmp));
STRNCPY(m_szTmp, pszValue, sizeof(m_szTmp));
ret = std::string(m_szTmp);
}
printf("[Config][key=%s][value=%s]\n", key, ret.c_str());
}

bool Config::LoadConfig(const char* pszFileName)
{
rapidxml::file<> stFile(pszFileName);
if (!stFile.size())
{
printf("LoadFile(%s) error!\n", pszFileName);
return false;
}
rapidxml::xml_document<> stDoc;
stDoc.parse<0>(stFile.data());

rapidxml::xml_node<>* node = stDoc.first_node("process"); //看这里。。第一个节点 配合配置文件看
if (NULL == node)
{
printf("LoadFile(%s) do not have (process) node!\n", pszFileName);
return false;
}
rapidxml::xml_node<>* childNode = NULL;
rapidxml::xml_node<>* grandNode = NULL;

ParseIntegerValue(node, "log_level", m_iLogLevel, (decltype(m_iLogLevel))-1); //在看这里 然后往下面看就懂了。。
// 配置文件字段名/要赋值的变量/ 默认的值

childNode = node->first_node("testnode");
ParseIntegerValue(childNode, "test_int", test_int, (decltype(m_iLogLevel))666);
ParseStringValue(childNode, "test_string", test_string, std::string("xxx"));


//下面是数组的示例
childNode = node->first_node("test_array");
test.clear();
while(childNode != nullptr){
Test tempTest;
ParseStringValue(childNode, "name",tempTest.name, std::string("xxx"));
ParseIntegerValue(childNode, "age", tempTest.age, (decltype(tempTest.age))1000);
test.push_back(tempTest);
childNode = childNode ->next_sibling("test_array");
}

return true;
}
33 changes: 33 additions & 0 deletions config/process/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_
#include "../common/rapidxml.hpp"
#include "../common/rapidxml_print.hpp"
#include "../common/rapidxml_utils.hpp"
#include <string>
#include <vector>

struct Test{
std::string name;
int32_t age;
};

class Config{
public:
bool LoadConfig(const char* pszFileName);
template <typename T>
void ParseIntegerValue(rapidxml::xml_node<>* node, const char* key, T& ret, T default_value);
void ParseDecimalValue(rapidxml::xml_node<>* node, const char* key, double& ret, double default_value);
void ParseStringValue(rapidxml::xml_node<>* node, const char* key, std::string& ret, std::string default_value);
public:
const char *pszValue = NULL;
int32_t iValue = 0;
char m_szTmp[2048];

public:
int32_t m_iLogLevel;
int32_t test_int;
std::string test_string;
std::vector<Test> test;
};

#endif
6 changes: 6 additions & 0 deletions config/process/config/process.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<process log_level="-1">
<testnode test_int="333" test_string="wocalei"/>
<test_array name="test1" age="66"/>
<test_array name="test2" age="666"/>
</process>
29 changes: 29 additions & 0 deletions config/process/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <cstdio>
#include <cstring>
#include "config.h"

#define CONFIG "./config/process.xml"

Config config;


bool load_config(){
if (true != config.LoadConfig(CONFIG)) {
printf("failed loadconfig\n");
return false;
}
else {
printf("success loadconfig\n");
return true;
}
}

int main(){
load_config();
printf("[m_iLogLevel=%d]\n",config.m_iLogLevel);
printf("[test_int=%d][test_string=%s]",config.test_int,config.test_string.c_str());
for(auto v : config.test){
printf("[name=%s][age=%d]\n",v.name.c_str(),v.age);
}
return 0;
}
Binary file added config/process/server
Binary file not shown.