Cross-platform way to get line number of an INI file where given option was found
Problem:
Looking for a C library (like boost::program_options) that can return the line number of an INI file where a given option or section was found.
Use cases:
Response:
Exploiting the possibilities of Boost Spirit, a solution was crafted using line_pos_iterator.
Implementation Details:
POSITIONSINFO = 0:
POSITIONSINFO = 1:
<code class="cpp">struct textnode_t { int sline, eline, scol, ecol; string_t text; };</code>
Code Snippet:
<code class="cpp">#include <map> #include <string> #include <iterator> #include <boost/tuple/tuple_comparison.hpp> template <typename S = std::string, typename Cmp = std::less<S>> class IniFile { public: IniFile(Cmp cmp = Cmp()) : _cmp(cmp) {} IniFile(const std::string& filename, Cmp cmp = Cmp()) : _cmp(cmp) { open(filename); } void open(const std::string& filename); typedef S string_t; #if POSITIONINFO struct textnode_t { int sline, eline, scol, ecol; string_t text; // ... }; #else typedef string_t textnode_t; #endif typedef std::pair<textnode_t, textnode_t> keyvalue_t; typedef std::map<textnode_t, textnode_t> section_t; typedef std::map<textnode_t, section_t> sections_t; private: Cmp _cmp; };</code>
Additional Resources:
Important Note: The provided solution does not require C 11 support, but it was used to dump the result of the parse.
The above is the detailed content of How can I get the line number of an INI file where a specific option or section is found using C ?. For more information, please follow other related articles on the PHP Chinese website!