I have a vector of unsigned char values. The data inside is keys and values in the format "key=value". Each pair is terminated by a '\0' character and the last pair is terminated by a double '/0'. A value can contain a "=" as well, so this delimiter shouldnt be a criteria for the pair, only the terminating '\0'
From this vector of single unsigned char I want to get a std::map<std::string, std::string> object.
What would be an effective way to go through the vector and fill the map?
Thanks in advance!
This code finds all pairs, but when printing them, my console seems to mess things up, so I suspect invalid characters... maybe it has to do with the fact that I'm using unsigned char?
unsigned char *env, *nxt;
std::string delimiter = "=";
std::string line;
std::map<string, string> myMap;
for (env = &myVector[0]; *env != '\0'; env = nxt + 1)
{
for (nxt = env; *nxt != '\0'; ++nxt)
{
if (nxt >= &myVector[myVector.size()])
{
printf("string not terminated\n");
return -1;
}
}
line = std::string(env, env + myVector.size());
myMap.insert(std::pair<string, string>(
line.substr(0, line.find(delimiter)),
line.substr(line.find(delimiter) + 1, line.size())));
}