Skip to content

Customizing the parser algorithm

Ricardo Amores Hernández edited this page Feb 17, 2017 · 1 revision

If you need a fine-grainer customization for the parsing algorithm, you can create a derived class from IniDataParser and overwrite the methods that control the operations for the parsing (e.g. the methods that checks if a line is a comment, a section or a key value assignment, etc)

Please note that if you just want to change some small aspects of the parsing (like changing the character used to define a comment) maybe you can save time just creating a custom configuration for the parser. See the page Configuring parser behavior for more information.

All the steps in the parsing algorithm are performed using protected methods so it can be modified in derived classes. This is an special case of the [Template Method Pattern] (http://en.wikipedia.org/wiki/Template_method_pattern) where a default implementation for the template methods is provided.

Here is a list of the methods that can be changed in derived classes in order to modify the parser, the algorithm for the parser works with the following steps:

  • Reads a line at a time, discard empty lines (all whitespace) and calls ProcessLine() method for non-empty lines
  • Extract the comments if they are any (LineContainsAComment() and ExtractComment()methods)
  • If the line is a Section (LineMatchesASection() call ProcessSection() to store the name of the section
  • If the line is a key/value pair (LineMatchesAKeyValuePair()) call ProcessKeyValuePair() to store both the key and the value
  • If the line is not a comment, a section nor a key/value pair, the line can't be parsed and results in being skipped or an exception being thrown depending on the configuration.
/// <summary>
///     Processes one line and parses the data found in that line
///     (section or key/value pair who may or may not have comments)
/// </summary>
/// <param name="currentLine">The string with the line to process</param>
protected void ProcessLine(string currentLine, IniData currentIniData)

This method first extract comments if the line

/// <summary>
///     Checks if a given string contains a comment.
/// </summary>
/// <param name="line">
///     The string to be checked.
/// </param>
/// <returns>
///     <c>true</c> if any substring from s is a comment, <c>false</c> otherwise.
/// </returns>
protected bool LineContainsAComment(string line)
/// <summary>
///     Removes a comment from a string if exist, and returns the string without
///     the comment substring.
/// </summary>
/// <param name="line">
///     The string we want to remove the comments from.
/// </param>
/// <returns>
///     The string s without comments.
/// </returns>
protected string ExtractComment(string line)
/// <summary>
///     Checks if a given string represents a section delimiter.
/// </summary>
/// <param name="line">
///     The string to be checked.
/// </param>
/// <returns>
///     <c>true</c> if the string represents a section, <c>false</c> otherwise.
/// </returns>
protected bool LineMatchesASection(string line)
/// <summary>
///     Checks if a given string represents a key / value pair.
/// </summary>
/// <param name="line">
///     The string to be checked.
/// </param>
/// <returns>
///     <c>true</c> if the string represents a key / value pair, <c>false</c> otherwise.
/// </returns>
protected bool LineMatchesAKeyValuePair(string line)
/// <summary>
///     Proccess a string which contains an ini section.
/// </summary>
/// <param name="line">
///     The string to be processed
/// </param>
protected void ProcessSection(string line, IniData currentIniData)
/// <summary>
///     Processes a string containing an ini key/value pair.
/// </summary>
/// <param name="line">
///     The string to be processed
/// </param>
protected void ProcessKeyValuePair(string line, IniData currentIniData)
/// <summary>
///     Extracts the key portion of a string containing a key/value pair..
/// </summary>
/// <param name="s">    
///     The string to be processed, which contains a key/value pair
/// </param>
/// <returns>
///     The name of the extracted key.
/// </returns>
protected string ExtractKey(string s)
/// <summary>
///     Extracts the value portion of a string containing a key/value pair..
/// </summary>
/// <param name="s">
///     The string to be processed, which contains a key/value pair
/// </param>
/// <returns>
///     The name of the extracted value.
/// </returns>
protected string ExtractValue(string s)
Clone this wiki locally