📄 conf.cpp
字号:
/* * $File: conf.cpp * $Author: Jiakai -- gy_jk@126.com * $Date: Sat Nov 29 00:24:19 2008*//*Copyright (C) (2008) (Jiakai) <gy_jk@126.com>This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "conf.h"#include <fstream>#include <string>#include <cstring>#include <cctype>#include <cstdlib>#include <cstdio>using namespace std;//-------------Function Statements----------------static bool readline(ifstream &fin, string &line); //read the next legitimate line in "fin".static void translateline(const string &line, string &name, string &value); //translate a string like "a=b".//------------------------------------------------struct Conf::List_section{ List_item *itemhead, *itemtail; List_section *next; string name; List_section(const string &name_) : itemhead(NULL), itemtail(NULL), next(NULL), name(name_) {}};struct Conf::List_item{ List_item *next; string name, value; List_item(const string &name_, const string &value_) : next(NULL), name(name_), value(value_) {}};Conf::Conf(const char *file) : datahead(NULL), datatail(NULL){ filename = new char[strlen(file) + 1]; strcpy(filename, file); init();}Conf::~Conf(){ del_all(); delete []filename;}void Conf::init(){ ifstream fin(filename); string tmp; if (!readline(fin, tmp)) return; if (tmp[0] == '[') datahead = new List_section(tmp.substr(1, tmp.length() - 2)); else datahead = new List_section(""); List_section *ps = datahead; while (fin) { if (!readline(fin, tmp)) break; if (tmp[0] == '[') { ps->next = new List_section(tmp.substr(1, tmp.length() - 2)); ps = ps->next; continue; } string name, value; translateline(tmp, name, value); if (ps->itemhead == NULL) { ps->itemhead = new List_item(name, value); ps->itemtail = ps->itemhead; continue; } ps->itemtail->next = new List_item(name, value); ps->itemtail = ps->itemtail->next; } datatail = ps;}bool readline(ifstream &fin, string &line){ while (fin) { getline(fin, line); if (line.empty()) continue; if (line[0] == '[') { if (*line.rbegin() != ']') continue; return true; } if (line[0] != '=' && !isalpha(line[0]) && !isdigit(line[0])) continue; for (string::iterator i = line.begin(); i != line.end(); i ++) if (*i == '=') return true; } return false;}void translateline(const string &line, string &name, string &value){ string::size_type loc = line.find('=', 0); name = line.substr(0, loc); value = line.substr(loc + 1);}bool Conf::read(const char *section, const char *item, double &value) const{ char ret[DOUBLE_LENGTH + 1]; if (!read(section, item, ret, DOUBLE_LENGTH)) return false; return sscanf(ret, "%le", &value) != EOF;}bool Conf::read(const char *section, const char *item, int &value) const{ char ret[INTEGER_LENGTH + 1]; if (!read(section, item, ret, INTEGER_LENGTH)) return false; return sscanf(ret, "%d", &value) != EOF;}bool Conf::read(const char *section, const char *item, char *value, unsigned maxlen) const{ List_item *pi = lookfor_item(lookfor_section(section), item); if (pi == NULL) return false; if (pi->value.length() > maxlen) return false; strcpy(value, pi->value.c_str()); return true;}Conf::List_section* Conf::lookfor_section(const char *name) const{ List_section *ps = datahead; string tmp(name); while (ps != NULL) { if (ps->name == tmp) return ps; ps = ps->next; } return NULL;}Conf::List_item* Conf::lookfor_item(const List_section *section, const char *name) const{ if (section == NULL) return NULL; List_item *pi = section->itemhead; string tmp(name); while (pi != NULL) { if (pi->name == tmp) return pi; pi = pi->next; } return NULL;}void Conf::write(const char *section, const char *item, const char *value){ if (datahead == NULL) { datahead = new List_section(section); datatail = datahead; datahead->itemhead = new List_item(item, value); datahead->itemtail = datahead->itemhead; return; } List_section *ps = lookfor_section(section); if (ps == NULL) { datatail->next = new List_section(section); datatail = datatail->next; datatail->itemhead = new List_item(item, value); datatail->itemtail = datatail->itemhead; return; } List_item *pi = lookfor_item(ps, item); if (pi == NULL) { ps->itemtail->next = new List_item(item, value); ps->itemtail = ps->itemtail->next; return; } pi->value = value;}void Conf::write(const char *section, const char *item, int value){ char tmp[INTEGER_LENGTH]; sprintf(tmp, "%d", value); write(section, item, tmp);}void Conf::write(const char *section, const char *item, double value){ char tmp[DOUBLE_LENGTH]; sprintf(tmp, "%le", value); write(section, item, tmp);}bool Conf::save() const{ ofstream fout(filename); List_section *ps = datahead; while (ps != NULL) { fout<<'['<<ps->name<<"]\n"; List_item *pi = ps->itemhead; while (pi != NULL) { fout<<pi->name<<'='<<pi->value<<endl; pi = pi->next; } ps = ps->next; fout<<endl; } return fout.good();}void Conf::delitem(const char *section, const char *item){ List_section *ps = lookfor_section(section); if (ps != NULL) { List_item *pi_prev = NULL, *pi = ps->itemhead; while (pi != NULL && pi->name != item) { pi_prev = pi; pi = pi->next; } if (pi != NULL) { if (pi_prev == NULL && pi->next == NULL) { delsection(section); return; } if (pi_prev == NULL) ps->itemhead = pi->next; else pi_prev->next = pi->next; if (pi->next == NULL) ps->itemtail = pi_prev; delete pi; } }}void Conf::delsection(const char *section){ List_section *ps = datahead, *ps_prev = NULL; while (ps != NULL && ps->name != section) { ps_prev = ps; ps = ps->next; } if (ps != NULL) { if (ps_prev == NULL) datahead = ps->next; else ps_prev->next = ps->next; if (ps->next == NULL) datatail = ps_prev; List_item *pi = ps->itemhead; while (pi != NULL) { List_item *tmp = pi->next; delete pi; pi = tmp; } delete ps; }}unsigned long int Conf::item_len(const char *section, const char *item) const{ List_item *pi = lookfor_item(lookfor_section(section), item); if (pi == NULL) return 0; return pi->value.length();}void Conf::del_all(){ List_section *ps = datahead; while (ps != NULL) { List_item *pi = ps->itemhead; while (pi != NULL) { List_item *tmp = pi->next; delete pi; pi = tmp; } List_section *tmp = ps->next; delete ps; ps = tmp; } datahead = datatail = NULL;}const char * Conf::get_file_path() const{ return filename;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -