Exemplo n.º 1
0
//찾으려는 key가 존재하는지 확인, 존재한다면 해당노드 반환, 존재하지않는다면 NULL 반환
//key: 찾으려는 key값, curKey: 현재 노드의 key값
node* isKeyExist(node* nd, int key) {
	if (nd == &nil_node) return NULL;

	int curKey = nd->key;
	if (curKey == key) return nd;
	else if (curKey > key)
		return isKeyExist(nd->left, key);
	else
		return isKeyExist(nd->right, key);
}
Exemplo n.º 2
0
Value& MapValue::at(const std::string& Key)
{
    if (!isKeyExist(Key))
        throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Requested key " + Key + " does not exist");

    return (*(m_Value[Key]));
}
Exemplo n.º 3
0
char* KConfig::getValue(char* valueBuf, const char section[],const char key[])
{
        //<断言目标串有效(查找失败时函数返回NULL,终止查找),文件有效,检索section并断言它存在
        rewind(m_pfile);
        assert(valueBuf != NULL);
        assert(m_pfile != NULL && !feof(m_pfile));

        size_t j = 0;
        long pos;
        char ch;
        if((pos = isKeyExist(section,key)) != 0)  //<该section:key存在
        {
                printf("OK");
                fseek(m_pfile,pos,SEEK_SET);              //<因为在isKeyExist中进行了rewind()

                do
                {
                        ch = fgetc(m_pfile);
                }
                while(ch == '\t' || ch == ' ' || ch == '=');    //<跳过=号和一些空白,但不允许换行

                while(true)
                {
                        valueBuf[j] = ch;
                        if(valueBuf[j] == '\n' || valueBuf[j] == '\t' || valueBuf[j] == ' ' || valueBuf[j] == EOF)//<注意可能到文件尾)
                        {
                                break;
                        }
                        ++j;
                        ch = fgetc(m_pfile);
                }

                valueBuf[j] = '\0';
                printf("[%s]:%s = %s\n",section,key,valueBuf);
                rewind(m_pfile);
                return valueBuf;   //<由于域内键值唯一性,跨域key可重复,故找到后必须立即返回函数
        }
        rewind(m_pfile);  //<重置文件指针,因为一般读取配置都会一次性读取多个,而每次查找需要从头进行
        printf("[%s]:%s not found!\n",section,key);
        return NULL;
}
Exemplo n.º 4
0
//<设置key值,分几种情况: (section是否存在)*(section:key是否存在)
long KConfig::setValue(const char *value, const char section[], const char key[])
{
        assert(value!=NULL && m_pfile!=NULL && !feof(m_pfile));
        long pos;
        char *pbuf = (char*)malloc(1024*sizeof(char));  //<用于暂存插入值后的内容
        char ch;
        size_t bufCur;

        if((pos = isKeyExist(section,key) ) != 0)//<该section:key存在,做更新
        {
                pbuf[0] = '=';
                bufCur = 1;
                while(value[bufCur-1] != '\0') //<将要写入的值先存于bufCur,后加入一个换行
                {
                        pbuf[bufCur] =  value[bufCur-1];
                        ++bufCur;
                }
                pbuf[bufCur++] = '\n';

                printf("FOUND!!!\n");
                fseek(m_pfile,pos,SEEK_SET);

                // 先定位到旧值的末尾再将后续数据并入pbuf
                do
                {
                        ch = fgetc(m_pfile);
                }
                while(ch == '\t' || ch == ' ' || ch == '=');    //<跳过=号和一些空白,但不允许换行

                while(true)
                {
                        ch = fgetc(m_pfile);
                        if(ch == '\n' || ch == '\t' || ch == ' ' || ch == EOF)//<注意可能到文件尾?)
                        {
                                break;
                        }
                }

                while((ch = fgetc(m_pfile)) != EOF)//<并入后续数据
                {
                       pbuf[bufCur++] = ch;
                }
                pbuf[bufCur] = '\0';

                //<定位到键值后,将缓冲区写入文件
                fseek(m_pfile,pos,SEEK_SET);
                for(bufCur = 0; pbuf[bufCur] != '\0' ; ++bufCur)
                {

                       fputc(pbuf[bufCur],m_pfile);
                }
                ftruncate(fileno(m_pfile),ftell(m_pfile));  //<截断文件,返回的位置被无效,需要在之前记录
        }
        else if( (pos = _selectSection(section)) != 0)//<该section存在,但key不存在,做插入
        {
                bufCur = 0;
                while(key[bufCur] != '\0') //<将要写入的值先存于bufCur,后加入一个换行
                {
                        pbuf[bufCur] =  key[bufCur];
                        ++bufCur;
                }
                pbuf[bufCur++] = '=';

                for(int i = 0; value[i] != '\0' ;++i)
                {
                        pbuf[bufCur++] = value[i];
                }
                pbuf[bufCur++] = '\n';

                fseek(m_pfile,pos,SEEK_SET);

                while((ch = fgetc(m_pfile)) != EOF)//<并入后续数据
                {
                       pbuf[bufCur++] = ch;
                }
                pbuf[bufCur] = '\0';

                //<定位到section后,将缓冲区写入文件
                fseek(m_pfile,pos,SEEK_SET);
                for(bufCur = 0; pbuf[bufCur] != '\0' ; ++bufCur)
                {
                       fputc(pbuf[bufCur],m_pfile);
                }
                //<文件内容增加,无需截断
        }
        else//<section不存在,需要在文件尾部插入section和键值对
        {
                //<先后将[section],key=name写入缓冲区
                pbuf[0] = '\n',pbuf[1] = '[';
                bufCur = 2;
                while(section[bufCur-2] != '\0')
                {
                        pbuf[bufCur] = section[bufCur-2];
                        ++bufCur;
                }
                pbuf[bufCur++] = ']';
                pbuf[bufCur++] = '\n';

                int i;
                for(i = 0; key[i] != '\0' ;++i)
                {
                        pbuf[bufCur++] = key[i];
                }
                pbuf[bufCur++] = '=';

                for(i = 0; value[i] != '\0' ;++i)
                {
                        pbuf[bufCur++] = value[i];
                }
                pbuf[bufCur++] = '\n';
                pbuf[bufCur++] = '\0';

                //<定位到文件尾,插入新section和键值对
                bufCur = 0;
                fseek(m_pfile,0L,SEEK_END);
                do
                {
                        fputc(pbuf[bufCur++],m_pfile);
                }
                while( pbuf[bufCur] != '\0');
                ftruncate(fileno(m_pfile),ftell(m_pfile));  //<截断文件
        }

        rewind(m_pfile);
        if(pbuf!=NULL)
        free(pbuf);
        pbuf = NULL;

        return pos;
}