#include <stdio.h>
#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
bool find_value_from_key(const string& source, const string& key, string& value, const string& endmarkers) {
bool ret = false;
size_t key_start = source.find(key);
size_t val_start = key_start + key.length();
if(key_start != string::npos) {
size_t val_end = source.find_first_of(endmarkers, val_start);
if(val_end == string::npos && source.length() > val_start)
val_end = source.length();
if(val_end != string::npos) {
value = source.substr(val_start, val_end - val_start);
ret = true;
}
}
return ret;
}
int main(int argc, char* argv[])
{
//test strings to exercise find_value_from_key function
const char* test_strings[] = {
"key1 = value1",
" key1 = value1 ",
"key1 = value1,key2 = value2 ,key3 = value3",
"_key1 = value1 ,key2 = value2 ,Gkey3 = value3,",
"key1 = value1,key2 = value2 ,key3 = value3 x",
"key1 = value1,key2 = value2key3 = value3\n",
"key1 = value1_,key2 = value2 ,key3 = value3 ",
"key1 = value1$,key2 = value2 ,key3 = value3",
"key1 = value1\t,key2 = value2 ,key3 = value3\t" };
const char* keys[] = {"key1 = ", "key2 = ", "key3 = "};
int elements = sizeof(test_strings) / sizeof(test_strings[0]);
for(int i = 0; i < elements; ++i) {
cout << "processing: " << test_strings[i] << endl;
string value;
for (int k = 0; k < 3; k++) {
if(find_value_from_key(test_strings[i], keys[k], value, ", \t\n"))
cout << keys[k] << " -> " << value << endl;
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KCnVzaW5nIHN0ZDo6Y291dDsKdXNpbmcgc3RkOjplbmRsOwp1c2luZyBzdGQ6OnN0cmluZzsKCgpib29sIGZpbmRfdmFsdWVfZnJvbV9rZXkoY29uc3Qgc3RyaW5nJiBzb3VyY2UsIGNvbnN0IHN0cmluZyYga2V5LCBzdHJpbmcmIHZhbHVlLCBjb25zdCBzdHJpbmcmIGVuZG1hcmtlcnMpIHsKICAgYm9vbCByZXQgPSBmYWxzZTsKICAgc2l6ZV90IGtleV9zdGFydCA9IHNv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AgICAgImtleTEgPSB2YWx1ZTFfLGtleTIgPSB2YWx1ZTIgLGtleTMgPSB2YWx1ZTMgICAgICAgICAgICAgICAgICAgICIsIAogICAgICAia2V5MSA9IHZhbHVlMSQsa2V5MiA9IHZhbHVlMiAsa2V5MyA9ICAgdmFsdWUzIiwgCiAgICAgICJrZXkxID0gdmFsdWUxXHQsa2V5MiA9IHZhbHVlMiAgLGtleTMgPSB2YWx1ZTNcdCIgfTsKCiAgIGNvbnN0IGNoYXIqIGtleXNbXSA9IHsia2V5MSA9ICIsICJrZXkyID0gIiwgImtleTMgPSAifTsKCiAgIGludCBlbGVtZW50cyA9IHNpemVvZih0ZXN0X3N0cmluZ3MpIC8gc2l6ZW9mKHRlc3Rfc3RyaW5nc1swXSk7CgogICBmb3IoaW50IGkgPSAwOyBpIDwgZWxlbWVudHM7ICsraSkgewogICAgIGNvdXQgPDwgInByb2Nlc3Npbmc6ICIgPDwgdGVzdF9zdHJpbmdzW2ldIDw8IGVuZGw7CiAgICAgc3RyaW5nIHZhbHVlOwogICAgIGZvciAoaW50IGsgPSAwOyBrIDwgMzsgaysrKSB7CiAgICAgICBpZihmaW5kX3ZhbHVlX2Zyb21fa2V5KHRlc3Rfc3RyaW5nc1tpXSwga2V5c1trXSwgdmFsdWUsICIsIFx0XG4iKSkKICAgICAgICAgIGNvdXQgPDwga2V5c1trXSA8PCAiIC0+ICIgPDwgdmFsdWUgPDwgZW5kbDsKICAgICB9CiAgIH0KCiAgIHJldHVybiAwOwp9
processing: key1 = value1
key1 = -> value1
processing: key1 = value1
key1 = -> value1
processing: key1 = value1,key2 = value2 ,key3 = value3
key1 = -> value1
key2 = -> value2
key3 = -> value3
processing: _key1 = value1 ,key2 = value2 ,Gkey3 = value3,
key1 = -> value1
key2 = -> value2
key3 = -> value3
processing: key1 = value1,key2 = value2 ,key3 = value3 x
key1 = -> value1
key2 = ->
key3 = -> value3
processing: key1 = value1,key2 = value2key3 = value3
key1 = -> value1
key2 = -> value2key3
key3 = -> value3
processing: key1 = value1_,key2 = value2 ,key3 = value3
key1 = -> value1_
key2 = -> value2
key3 = -> value3
processing: key1 = value1$,key2 = value2 ,key3 = value3
key1 = -> value1$
key2 = -> value2
key3 = ->
processing: key1 = value1 ,key2 = value2 ,key3 = value3
key1 = -> value1
key2 = -> value2
key3 = -> value3