Home > Backend Development > C++ > How can I get the line number of an INI file where a specific option or section is found using C ?

How can I get the line number of an INI file where a specific option or section is found using C ?

Linda Hamilton
Release: 2024-10-25 23:04:28
Original
344 people have browsed it

How can I get the line number of an INI file where a specific option or section is found using C  ?

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:

  1. Ask the library to find the value "vvv" in the section "[SSS]." The library should return the line number where "vvv" is found in the "[SSS]" section or -1. This allows us to say, for example, "line 55: vvv must be < 256."
  2. Iterate through the INI file for sections and validate their names. When an unknown section is found, we can report "line 55: section [Hahaha] is unknown."

Response:

Exploiting the possibilities of Boost Spirit, a solution was crafted using line_pos_iterator.

Implementation Details:

  • POSITIONSINFO = 0:

    • Input is streaming.
    • Output is in raw string format, or map> for sections.
    • POSITIONSINFO = 1:

      • Input is buffered.
      • Output is in textnode_t format:
      <code class="cpp">struct textnode_t {
        int sline, eline, scol, ecol;
        string_t text;
      };</code>
      Copy after login
      • This enables the resulting map> to report the exact line and column starting and ending points for individual text elements.
      • Comments (#, / ... / style) are implemented.
      • Whitespace is tolerated.
      • De-escaping of slashes is left as an exercise.
      • Errors are reported with full position information when enabled.
    • 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>
      Copy after login

      Additional Resources:

      • [Gist with complete code, makefile, and example.ini](https://gist.github.com/1425972)
      • [Boost Spirit documentation](https://www.boost.org/doc/libs/1_65_1/libs/spirit/doc/)

      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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template