编写一个简单的license文件控制器

来源:互联网 发布:美国 量子计算机 知乎 编辑:程序博客网 时间:2024/05/16 17:01


编写一个file-drive的软件权限控制器:

文件内容可以如下:


#注释

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;}

0 0
原创粉丝点击