Recursive Parsing in C++

Hi,
I have a config file which has entries like
[code]
DYNAMICBASEDIR : /var/maildir
DYNAMICTEMPDIR : {DYNAMICBASEDIR}/tmp/
USERDATAMAILFILEDIR : {DYNAMICBASEDIR}/domains/
[/code]

This is a common config file which is also being parsed by php script and perl scripts.
I have to parse this file in C++, I can get the key value pairs like

[code]
key = DYNAMICTEMPDIR and value = {DYNAMICBASEDIR}/tmp/
[/code]

Now I have to further parse this key value pair and replace the variable within the curly braces by it's value i.e. /var/maildir,
and create a hash, so that I can reference any of the values by their key.
Appreciate if anybody can help me out with this.

Thanks,
Dynamo

Comments

  • : Hi,
    : I have a config file which has entries like
    : [code]
    : DYNAMICBASEDIR : /var/maildir
    : DYNAMICTEMPDIR : {DYNAMICBASEDIR}/tmp/
    : USERDATAMAILFILEDIR : {DYNAMICBASEDIR}/domains/
    : [/code]
    :
    : This is a common config file which is also being parsed by php script and perl scripts.
    : I have to parse this file in C++, I can get the key value pairs like
    :
    : [code]
    : key = DYNAMICTEMPDIR and value = {DYNAMICBASEDIR}/tmp/
    : [/code]
    :
    : Now I have to further parse this key value pair and replace the variable within the curly braces by it's value i.e. /var/maildir,
    : and create a hash, so that I can reference any of the values by their key.
    : Appreciate if anybody can help me out with this.
    :
    : Thanks,
    : Dynamo
    :

    using c++ std::string that would be pretty trivel
    [code]
    std::string basedir = "/var/maildir"; // or whatever in the config file
    std::string tmpdir = "/tmp/"; // or whatever
    std::string path = basedir + tmpdir;
    [/code]


  • Hi stober,
    I cannot change the format of the present config file as it is also being parsed by php and perl scripts, so have to parse the present config file, where I have to recursively replace the variables inside the curly braces with their values which are already defined.

    Thanks.
    Hi,
    : : I have a config file which has entries like
    : : [code]
    : : DYNAMICBASEDIR : /var/maildir
    : : DYNAMICTEMPDIR : {DYNAMICBASEDIR}/tmp/
    : : USERDATAMAILFILEDIR : {DYNAMICBASEDIR}/domains/
    : : [/code]
    : :
    : : This is a common config file which is also being parsed by php script and perl scripts.
    : : I have to parse this file in C++, I can get the key value pairs like
    : :
    : : [code]
    : : key = DYNAMICTEMPDIR and value = {DYNAMICBASEDIR}/tmp/
    : : [/code]
    : :
    : : Now I have to further parse this key value pair and replace the variable within the curly braces by it's value i.e. /var/maildir,
    : : and create a hash, so that I can reference any of the values by their key.
    : : Appreciate if anybody can help me out with this.
    : :
    : : Thanks,
    : : Dynamo
    : :
    :
    : using c++ std::string that would be pretty trivel
    : [code]
    : std::string basedir = "/var/maildir"; // or whatever in the config file
    : std::string tmpdir = "/tmp/"; // or whatever
    : std::string path = basedir + tmpdir;
    : [/code]
    :
    :
    :
  • [b][red]This message was edited by istrasci at 2005-8-31 16:29:58[/red][/b][hr]
    : Hi,
    : I have a config file which has entries like
    : [code]
    : DYNAMICBASEDIR : /var/maildir
    : DYNAMICTEMPDIR : {DYNAMICBASEDIR}/tmp/
    : USERDATAMAILFILEDIR : {DYNAMICBASEDIR}/domains/
    : [/code]
    :
    : This is a common config file which is also being parsed by php script and perl scripts.
    : I have to parse this file in C++, I can get the key value pairs like
    :
    : [code]
    : key = DYNAMICTEMPDIR and value = {DYNAMICBASEDIR}/tmp/
    : [/code]
    :
    : Now I have to further parse this key value pair and replace the variable within the curly braces by it's value i.e. /var/maildir,
    : and create a hash, so that I can reference any of the values by their key.
    : Appreciate if anybody can help me out with this.
    :
    : Thanks,
    : Dynamo
    :


    Doesn't sound hard, just maybe a lot of bookkeeping... Try something like this:

    [code]
    parse_one_element(string_to_parse_of_the_form_KEY_:_VALUE)
    {
    str = string_to_parse_of_the_form_KEY_:_VALUE;
    first_: = find the first ':' ;
    key = everything up to first_: ;
    value = everything after first_: ;
    value_with_substitutions = parse_value(value);
    // let any exceptions propogate upward
    insert_into_Hash_Table(key, value_with_substitutions);
    }

    string_type parse_value(value_to_parse)
    {
    if (value_to_parse has no '{' in it)
    return value_to_parse;
    else
    {
    if (value_to_parse has no '}' in it)
    throw Parse_Exception;
    before_key = substring from beginning to '{';
    key = substring from '{' to next '}' ;
    after_key = substring from after '}' to end;
    substitution = Get_Value_From_Hash_Table(key);
    // recursion on the rest of the string here: VVVV
    return concat(before_key, substitution, parse_value(after_key) );
    }
    }
    [/code]


    Of course, just replace the pseudocode with the correct string/hash_table functions... Hope this helps...


  • The example I posted did not change the config file -- just how its interpreted.

    Why recursion? that will eat a lot of memory. You should be able to do it in a loop instead. It's not really all that difficult a task. I'm assuming your program has complete control over the config file, that is, it can open it, read its contents, then close it.
    [code]
    open the config file
    start of loop
    read a line
    parse it into its individual parts
    add to map
    end of loop
    close the config file
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories