Rotciv
Rotciv

Reputation: 39

How to Read the Control of a Debian Packages in c++?

I am trying to read the data from the control file of a Debian Package, like the next example, in C++:

Package: com.example.test
Version: 1.0
...
Homepage: http://example.com
...

I can read it with the following code, but when it is in the Homepage I only get "http":

string item;

vector<string> data;

stringstream str(line);

while(getline(str, item, ':' )) {
    data.push_back(item);
}

How can I read "Homepage" into data[0] and get data with data[1]?

Upvotes: 0

Views: 63

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490108

Only specify : as ending the field when you read the first item on a line.

std::string key, value;

while (std::getline(infile, key, ':'))
     std::getline(infile, value);

Instead of a vector of strings alternating between keys and values, I'd probably use an std::map (or possibly std::multimap), which is built fairly specifically for situations like this:

std::map<std::string, std::string> package_data;

while (std::getline(infile, key, ':')) {
     std::getline(infile, value);

     package_data[key] = value;
}

Then, you can look up values directly from keys:

 std::string homepage = *package_data.find("Homepage");

Upvotes: 1

Related Questions