⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configfile.h

📁 一个Linux下读写文件的代码
💻 H
字号:
/*  The following is the Simplified BSD License *  * Copyright (c) 2008, Warren Kurt vonRoeschlaub * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions  * are met: *  *  * Redistributions of source code must retain the above copyright  *       notice, this list of conditions and the following disclaimer. *  * Redistributions in binary form must reproduce the above copyright  *       notice, this list of conditions and the following disclaimer  *       in the documentation and/or other materials provided with the  *       distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#ifndef CONFIGFILE_H_#define CONFIGFILE_H_#include <string>#include <vector>namespace configlib{	#define ERROR_NONE 0#define ERROR_EXTRA 1#define ERROR_UNKNOWN 2#define ERROR_MISSING 3	class configitem_base;	/* * The file handler, responsible for parsing all configitem_base derived * objects assigned to it. * For each configuration file, create a configfile object and assign * configitem_base derived objects to it.  Do not free the file object * until all members are freed. */class configfile{	friend class configitem_base;public:	configfile(const std::string& file_name);	virtual ~configfile();		void read();	void write();	int parse(int argc, char *argv[]);		const std::string& name() { return prog_name; }	const char* get() { return fileName.c_str(); }		private:	// This is not a real iterator, just a class to handle internal	// iteration-like operations on the data vector.  It automatically	// skips lines that are not configuration settings and keeps track	// of the current section.	class iterator	{	public:		iterator(std::vector<std::string>& source);				const std::string& get_section() { return section; }		const std::string get_name();		const std::string get_value();		std::vector<std::string>::iterator get_next() { return ++index; }				void insert(std::size_t pos, const std::string& str) { (*index).insert(pos, str); }		void replace_value(const std::string& value);				void begin() { index = data->begin(); section.erase(); if (parse()) operator++();}		bool end() { return (data->end() == index); }				iterator& operator=(const iterator& rhs);		iterator& operator++();		iterator& operator++(int) { return operator++(); }		operator std::vector<std::string>::iterator() { return index; }			protected:		bool parse();		private:		std::string section;		std::vector<std::string>* data;		std::vector<std::string>::iterator index;	};		protected:    void parse();    void parse(configitem_base* item);    int parse_char(char item, const std::string& next);    int parse_long(const std::string& name);    int parse_index(int index, const std::string& next);    void clear(const std::string& section, const std::string& name);    void set(const std::string& section, const std::string& name, const std::string& value);    void add(const std::string& section, const std::string& line);        void push_back(configitem_base* item) { item_list.push_back(item); parse(item); }    void erase(configitem_base* item);    	static bool match(const char* pattern, const char* data);	private:	std::string prog_name;    std::string fileName;    std::vector<std::string> data;    std::vector<configitem_base*> item_list;};}#endif /*CONFIGFILE_H_*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -