编写一个简单的license文件控制器
2015-04-14 10:00
423 查看
编写一个file-drive的软件权限控制器:
文件内容可以如下:
#注释
Car.IsPrivate True
转载请注明出处 : http://blog.csdn.net/elfprincexu
关于文件的读写,是一个循环,每读取一行,就会去解析每一行的license,分理出里面的key value对,并且把它们放入我们的 KVPair存储起来
通过阅读以上的文件来获得软件所有的权限控制。
1. 先编辑一个存储 key/value 对的数据结构 KVPair
然后定义一个控制器, Params, 当然,我们不止一个KVPair,所以在Params类中,我们有一连串的KVPair
关于文件的读写,是一个循环,每读取一行,就会去解析每一行的license,分理出里面的key value对,并且把它们放入我们的 KVPair存储起来
其中关键是去读去文件中每一行的key/value 值,我们允许注释行的加入,所以在解析的时候要去忽略注释行,好在我们可以统一规定#开头的为注释行。
还有一个设置key/value的函数, set
所有的set函数都会用到以下的set函数
当然,我们在核对license的时候,也会去从KVPair中去获取相应的key value对
总结:
1. 上面的file-drive方式在软件开发当中十分普遍,通过改变文件的方式来改变软件的整体控制也是十分普遍。
2. 上面的案例是一个简单的权限控制器,当然我们还需当心多线程间的同时读取,加以锁的保护来进行处理,当然涉及到文件的不同进程读取,也要加file-lock进行保护。
关于文件的读写,是一个循环,每读取一行,就会去解析每一行的license,分理出里面的key value对,并且把它们放入我们的 KVPair存储起来
文件内容可以如下:
#注释
Car.IsPrivate True
转载请注明出处 : http://blog.csdn.net/elfprincexu
关于文件的读写,是一个循环,每读取一行,就会去解析每一行的license,分理出里面的key value对,并且把它们放入我们的 KVPair存储起来
bool Params::readFileParams (const Str& filename) { _filename = filename; ifstream ifs (filename.c_str(), ios::in); if (!ifs.good()) { return false; } Str key, value; while (getFileEntry(ifs, key, value)) this->set(key, value); return true; }
通过阅读以上的文件来获得软件所有的权限控制。
1. 先编辑一个存储 key/value 对的数据结构 KVPair
struct KVPair { KVPair(void) : _key(), _value(), _readonly(false) { return ;} KVPair(const KVPair& src) {*this=src;} KVPair& operator= (const KVPair& src) { if ( this != &src) { _key = src._key; _value = src._value; _readonly = src._readonly; } return *this; } Str _key; Str _value; Str _readonly; }
然后定义一个控制器, Params, 当然,我们不止一个KVPair,所以在Params类中,我们有一连串的KVPair
class Params { public : ... bool getFileEntry(ifstream& ifs, Str& key, Str& value) const bool set(Str key,Str value,bool readonly = true) ... private: struct KVPair { ... } std::vector<KVPair> _kvpList; }
关于文件的读写,是一个循环,每读取一行,就会去解析每一行的license,分理出里面的key value对,并且把它们放入我们的 KVPair存储起来
bool Params::readFileParams (const Str& filename) { _filename = filename; ifstream ifs (filename.c_str(), ios::in); if (!ifs.good()) { return false; } Str key, value; while (getFileEntry(ifs, key, value)) this->set(key, value); return true; }
其中关键是去读去文件中每一行的key/value 值,我们允许注释行的加入,所以在解析的时候要去忽略注释行,好在我们可以统一规定#开头的为注释行。
bool Params::getFileEntry (ifstream& ifs, Str& key, Str& value) const { char line[StringMax]; bool result = false; do { memset(line, 0, StringMax); if (ifs.getline (line, StringMax).good()) { char* c = line; // // ignore comment lines... // if (*c != '#') { // // find first Whitespace... // for (c=line; *c && (*c != ' ') && (*c != '\t'); ++c) ; // // before the end? // if (*c) { *c = '\0'; // split line into key and value, at first whitespace while (*++c && ((*c == ' ') || (*c == '\t'))) ; key = line; value = Str(c).stripBlanks(); result = true; } } } else return false; } while (!result); return result; }
还有一个设置key/value的函数, set
// set a "key" bool set (const Str& key, const Str& value); // { (*this)[key] = value; } bool set (const Str& key, int value); // { (*this)[key] = Str (value); } bool setBool(const Str& key, bool value); // { (*this)[key] = Str (value ? "True" : "False"); } bool set (const Str& key, double value); // { (*this)[key] = Str (value); } bool set (const Str& key, Point value); // { (*this)[key] = Str (value); } bool Params::set (const Str& key, const Str& value, bool readOnlyFlag = false) ; //
所有的set函数都会用到以下的set函数
// Functions to set a "key". // bool Params::set (const Str& key, const Str& value, bool readOnlyFlag) { vector<Params::KVPair>::iterator p = _kvpList.begin(); for (p = _kvpList.begin(); p<_kvpList.end(); p++) { if (p->_key == key) // key existing, so replace the value. { if(p->_readOnly == false) //replace value only if key is not read only { p->_value = value; p->_readOnly = readOnlyFlag; return true; } //key is read only return false; } } // // A new key, so add to the list. // struct Params::KVPair kvp; kvp._key = key; kvp._value = value; kvp._readOnly = readOnlyFlag; _kvpList.push_back(kvp); return true; }
当然,我们在核对license的时候,也会去从KVPair中去获取相应的key value对
bool Params::getStr (const Str& key, Str& value) const { //now look for key in this object vector<Params::KVPair>::const_iterator p = _kvpList.begin(); while (p < _kvpList.end()) { if (p->_key == key) // found the key, so get the value. { value = p->_value; return true; } else ++p; } // Key not found. return false; }
总结:
1. 上面的file-drive方式在软件开发当中十分普遍,通过改变文件的方式来改变软件的整体控制也是十分普遍。
2. 上面的案例是一个简单的权限控制器,当然我们还需当心多线程间的同时读取,加以锁的保护来进行处理,当然涉及到文件的不同进程读取,也要加file-lock进行保护。
关于文件的读写,是一个循环,每读取一行,就会去解析每一行的license,分理出里面的key value对,并且把它们放入我们的 KVPair存储起来
bool Params::readFileParams (const Str& filename) { _filename = filename; ifstream ifs (filename.c_str(), ios::in); if (!ifs.good()) { return false; } Str key, value; while (getFileEntry(ifs, key, value)) this->set(key, value); return true; }
相关文章推荐
- Linux下,使用Makefile文件编写一个简单的彩色进度条
- 编写一个程序,一行行地读取输入行,直至到达文件尾。算出每行输入行的长度,然后把最长的那行打印出来。为了简单起见,你可以假定所有的输入行均不超过1000个字符
- 编写一个简单的proc文件 http://blog.csdn.net/mockbird/article/details/6674017
- 编写一个程序,一行行地读取输入行,直至到达文件尾。算出每行输入行的长度,然后把最长的那行打印出来。为了简单起见,你可以假定所有的输入行均不超过1000个字符
- 编写一个简单的配置文件
- 一个简单的 C 程序文件,经过 0、编写,1、预处理,2、编译,3、链接,终于生成了一个可执行文件
- 用Python编写一个简单的FUSE文件系统的教程
- 使用cmake 编写一个简单的CMakeLists.txt文件
- 1.编写一个简单的C语言程序:计算输入多个整数的平均值,并将此程序分割成多个小文件。 2.为第1题中的程序编写makefile文件,用make编译后改成返回最小值,再编译,观察有多少文件不需要重新
- 使用PyQt5编写一个简单的GUI程序(pyside 有 pyside-uic 把ui文件转成py文件,pyside-rcc 把qrc文件转成 py文件导入就行了)
- 1.编写一个简单的C语言程序:计算输入多个整数的平均值,并将此程序分割成多个小文件。 2.为第1题中的程序编写makefile文件,用make编译后改成返回最小值,再编译,观察有多少文件不需要重新
- 一个简单的makefile编写所需的文件树
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- 在FPGA中简单的建立一个工程,并编写测试文件
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- Linux操作系统的简单指令及如何使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。
- 编写一个程序,一行行地读取输入行,直至到达文件尾。算出每行输入行的长度,然后把最长的那行打印出来。为了简单起见,你可以假定所有的输入行均不超过1000个字符。
- linux下编写的一个最简单的Makefile文件
- Erlang笔记(02) - 编写并运行一个简单的 Erlang 文件
- DirectShow学习之二做一个简单的媒体文件播放器