예제 #1
0
파일: list.cpp 프로젝트: tianxiao/stl-learn
int _tmain(int argc, _TCHAR* argv[])
{
    KeyValueList kvlist;
    txKeyValue v0 = {0,10};
    txKeyValue v1 = {1,11};
    txKeyValue v2 = {2,110};
    txKeyValue v3 = {3,100};
    kvlist.push_back(v0);
    kvlist.push_back(v1);
    kvlist.push_back(v3);

    KeyValueIt v0It = kvlist.begin();
    KeyValueIt v1It = v0It;
    v0It++;
    KeyValueIt v2It = v0It;
    v2It++;
    v2It++;

    // The following code are not right
    // the list iterator are not dereferencable
    //txKeyValue v2c = *v2It;
    //txKeyValue *pv2 = &(*v2It);

    kvlist.erase(v1It);

    printf("Key:%d--Value:%d \n", v0It->id, v0It->value );
    // printf("Key:%d--Value:%d \n", v2It->id, v2It->value );
    // printf("Key:%d--Value:%d \n", pv2->id, pv2->value );
    // printf("Key:%d--Value:%d \n", v1It->id, v1It->value );

    for ( KeyValueIt it=kvlist.begin(); it!=kvlist.end(); it++ ) {
        printf("Key:%d--value:%d \n", it->id, it->value);
    }

    return 0;
}
/*++
* @method: Client::getStringFromOAuthKeyValuePairs
*
* @description: this method builds a sorted string from key-value pairs
*
* @input: rawParamMap - key-value pairs map
*         paramsSeperator - sepearator, either & or ,
*
* @output: rawParams - sorted string of OAuth parameters
*
* @remarks: internal method
*
*--*/
bool Client::getStringFromOAuthKeyValuePairs( const KeyValuePairs& rawParamMap,
        std::string& rawParams,
        const std::string& paramsSeperator )
{
    rawParams.assign( "" );
    if( rawParamMap.size() )
    {
        KeyValueList keyValueList;
        std::string dummyStr;

        /* Push key-value pairs to a list of strings */
        keyValueList.clear();
        KeyValuePairs::const_iterator itMap = rawParamMap.begin();
        for( ; itMap != rawParamMap.end(); itMap++ )
        {
            dummyStr.assign( itMap->first );
            dummyStr.append( "=" );
            if( paramsSeperator == "," )
            {
                dummyStr.append( "\"" );
            }
            dummyStr.append( itMap->second );
            if( paramsSeperator == "," )
            {
                dummyStr.append( "\"" );
            }
            keyValueList.push_back( dummyStr );
        }

        /* Sort key-value pairs based on key name */
        keyValueList.sort();

        /* Now, form a string */
        dummyStr.assign( "" );
        KeyValueList::iterator itKeyValue = keyValueList.begin();
        for( ; itKeyValue != keyValueList.end(); itKeyValue++ )
        {
            if( dummyStr.length() )
            {
                dummyStr.append( paramsSeperator );
            }
            dummyStr.append( itKeyValue->c_str() );
        }
        rawParams.assign( dummyStr );
    }
    return ( rawParams.length() ) ? true : false;
}
예제 #3
0
파일: pls.cpp 프로젝트: Cougar/mythtv
    void parse(const char *d, int l)
    {
        const char *ptr = d;
        int line = 1;
        bool done = l <= 0;

        QString current_section = "";
        KeyValueList keyvals;

        while(!done)
        {
            switch(*ptr)
            {
                case '\0':
                    done = true;
                    break;
                case '#':
                {
                    const char *end = strchr(ptr, '\n');
                    if (!end) done = true;
                    ptr = end;
                    break;
                }
                case '\n':
                    ptr ++;
                    line ++;
                    break;
                case '[':
                {
                    ptr ++;
                    const char *nl = strchr(ptr, '\n');
                    const char *end = strchr(ptr, ']');

                    if (!nl) nl = d + l;

                    if (!end || nl < end)
                    {
                        VERBOSE(VB_IMPORTANT, QString("CfgReader:: Badly formatted section, line %1").arg(line));
                        done = true;
                    }

                    if (current_section.length() > 0)
                    {
                        cfg[current_section] = keyvals;
                        keyvals = KeyValueList();
                    }

                    current_section = std::string(ptr, end - ptr).c_str();
                    if (current_section.length() == 0)
                    {
                        VERBOSE(VB_IMPORTANT, QString("CfgReader:: Badly formatted section, line %1").arg(line));
                        done = true;
                    }
                    ptr = end + 1;
                    break;
                }
                default:
                {
                    if (current_section.length() > 0)
                    {
                        const char *eq = strchr(ptr, '=');
                        const char *nl = strchr(ptr, '\n');

                        if (!nl) nl = d + l;

                        if (!eq || nl < eq) 
                        {
                            VERBOSE(VB_IMPORTANT, QString("CfgReader:: Badly formatted line %1").arg(line));
                            done = true;
                        }
                        else
                        {
                            QString key = string(ptr, eq - ptr).c_str();
                            QString val = string(eq + 1, nl - eq - 1).c_str();
                            keyvals.push_back(KeyValue(key, val));
                            ptr = nl;
                        }
                    }
                    else
                    {
                        VERBOSE(VB_IMPORTANT, QString("CfgReader:: Badly formatted line %1").arg(line));
                        done = true;
                    }
                    break;
                }
            }

            if (ptr - d == l) 
                done = true;
        }

        if (current_section.length() > 0)
            cfg[current_section] = keyvals;
    }