如何写配置文件?用C语言完成
在没有操作系统情况下,配置文件该如何写?ini文件与xml文件哪种好?请各位大侠帮忙O(∩_∩)O~ 配置文件 XML ini
[解决办法]
不懂没有操作系统是什么意思?
[解决办法]
xml文件的话 需要解析xml
ini文件 你可以直接读
[解决办法]
没有操作系统,也就没有文件系统,当然也就没有文件的概念
[解决办法]
没有操作系统,那个配置文件是配什么的?
[解决办法]
xml文件的话 需要解析xml
ini文件 你可以直接读
意思是ini文件比较简单一些?可以用FILE * fopen(const char * path,const char * mode);来创建ini文件的吗?
是有win api的
http://blog.csdn.net/lcl_data/article/details/5049596
好的,谢谢,我看看O(∩_∩)O~
[解决办法]
没有操作系统的话;
1)你的开发系统包含文件系统,可以像有操作系统一样,读写文件,自己写文件就是了,XML自己解析,ini就是个文本文件,自己读写就是。
2)不仅没有操作系统,文件系统也没有,自己开发一个简易的文件系统,写文件就是了
3)你的个人修为比较强悍,自己开发包括文件系统在内的操作系统,这都做到了,写文件当然就没有问题了。
4)有些芯片,可以用Linux或者μLinux等操作系统,这个虽然要自己处理一下,实际上还是有操作系统的,读写文件,自然不在话下!!1
[解决办法]
以前写了一个基于c++, expat xml的配置库, 很简单的东西.
#ifndef _CONFIG_H
#define _CONFIG_H
#include <string>
#include <vector>
#include <map>
namespace cpputil {
using std::string;
using std::vector;
using std::map;
class config_node;
class config_container {
friend class config;
friend class config_node;
typedef vector<config_node *>::iterator nodes_iterator;
public:
int size() const
{
return nodes_.size();
}
const config_node &operator[](size_t index) const
{
return *nodes_[index];
}
private:
void append_node(config_node *node)
{
nodes_.push_back(node);
}
vector<config_node *> nodes_;
};
class config_node {
friend class config;
typedef map<string, string>::iterator attr_iterator;
typedef map<string, config_container>::const_iterator const_children_iterator;
typedef map<string, config_container>::iterator children_iterator;
public:
const string &cdata() const
{
return cdata_;
}
const string &name() const
{
return name_;
}
map<string, string> &attrs() const
{
return attrs_;
}
const config_container &operator[](const string &name) const
{
const_children_iterator iter = children_.find(name);
return iter->second;
}
private:
void add_children(const string &name, config_node *node)
{
children_[name].append_node(node);
}
void append_cdata(const char *data, int len)
{
if (data && len > 0)
cdata_.append(data, len);
}
void set_attr(const char *name, const char *value)
{
if (name && value)
attrs_[name] = value;
}
string name_;
string cdata_;
mutable map<string, string> attrs_;
map<string, config_container> children_;
};
class config {
public:
~config();
const config_container &operator[](const string &name) const
{
return root_[name];
}
bool parse(const char *file);
private:
static void config_start_element(void *arg, const char *name, const char **attrs);
static void config_end_element(void *arg, const char *name);
static void config_cdata(void *arg, const char *str, int len);
void _config_start_element(const char *name, const char **attrs);
void _config_end_element(const char *name);
void _config_cdata(const char *str, int len);
config_node root_;
vector<config_node *> node_path_;
};
}
#endif
#include "config.h"
#include "expat.h"
#include <stdio.h>
#include <algorithm>
namespace cpputil {
config::~config() {
config_node::children_iterator iter;
for (iter = root_.children_.begin(); iter != root_.children_.end(); ++iter) {
config_container &container = iter->second;
config_container::nodes_iterator niter;
for (niter = container.nodes_.begin(); niter != container.nodes_.end(); ++niter)
delete *niter;
}
int nsize = node_path_.size();
int i;
for (i = 1; i < nsize; ++i)
delete node_path_[i];
}
using std::reverse;
bool config::parse(const char *file)
{
if (!file)
return false;
FILE *fp = fopen(file, "r");
if (!fp)
return false;
XML_Parser parser = XML_ParserCreate(NULL);
if (!parser) {
fclose(fp);
return false;
}
XML_SetUserData(parser, this);
XML_SetElementHandler(parser, config_start_element, config_end_element);
XML_SetCharacterDataHandler(parser, config_cdata);
bool parse_error = false;
char xmlbuf[1024];
while (true) {
int len = fread(xmlbuf, 1, sizeof(xmlbuf), fp);
if (ferror(fp)) {
parse_error = true;
goto end;
}
int done = feof(fp);
if (!XML_Parse(parser, xmlbuf, len, done)) {
parse_error = true;
goto end;
}
if (done)
break;
}
end:
XML_ParserFree(parser);
fclose(fp);
return !parse_error;
}
void config::config_start_element(void *arg, const char *name, const char **attrs)
{
config *conf = static_cast<config *>(arg);
conf->_config_start_element(name, attrs);
}
void config::config_end_element(void *arg, const char *name)
{
config *conf = static_cast<config *>(arg);
conf->_config_end_element(name);
}
void config::config_cdata(void *arg, const char *str, int len)
{
config *conf = static_cast<config *>(arg);
conf->_config_cdata(str, len);
}
inline void config::_config_start_element(const char *name, const char **attrs)
{
config_node *node = NULL;
if (!node_path_.size())
node = &root_;
else
node = new config_node();
node->name_ = name;
int i = 0;
while (attrs[i]) {
node->set_attr(attrs[i], attrs[i + 1]);
i += 2;
}
node_path_.push_back(node);
}
inline void config::_config_end_element(const char *name)
{
string path;
int nsize = node_path_.size();
config_node *node = node_path_.back();
int i;
for (i = nsize - 1; i > 0; --i) {
string node_name = node_path_[i]->name_;
reverse(node_name.begin(), node_name.end());
if (!path.empty())
path += ".";
path += node_name;
reverse(path.begin(), path.end());
node_path_[i - 1]->add_children(path, node);
reverse(path.begin(), path.end());
}
node_path_.pop_back();
}
inline void config::_config_cdata(const char *str, int len)
{
config_node *node = node_path_.back();
node->append_cdata(str, len);
}
}