lkrasner
lkrasner

Reputation: 112

Parsing a Very Simply Config File

I am writing a OBDII reading library / application in C++. Data is retrieved from the car's computer by sending a simple string command, then passing the result through a function specific to each parameter.

I would like to read a config file of all the commands I want, something like this perhaps:

Name, Command, function

Engine RPM, 010C, ((256*A)+B)/4
Speed, 010D, A

Basically, very simple, all data needs to just be read in as a string. Can anyone recommend a good simple library for this? My target is g++ and/ or Clang on Linux if that matters.

Upvotes: 0

Views: 1527

Answers (1)

NetVipeC
NetVipeC

Reputation: 4432

You could use std::ifstream to read line by line, and boost::split to split the line by ,.

Sample code:

You could check tokens size for sanity checks of file loaded.

#include <fstream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>

int main(int argc, char* argv[]) {
    std::ifstream ifs("e:\\save.txt");

    std::string line;
    std::vector<std::string> tokens;
    while (std::getline(ifs, line)) {
        boost::split(tokens, line, boost::is_any_of(","));
        if (line.empty())
            continue;

        for (const auto& t : tokens) {
            std::cout << t << std::endl;
        }
    }

    return 0;
}

You could also use String Toolkit Library if you don't want to implemented. Docs

Upvotes: 2

Related Questions