config file interface

This is a discussion on config file interface within the C Programming forums, part of the General Programming Boards category; As a part of my current project I'm writing a configuration file interface. Originally I had thought it would be ...

  1. #1
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357

    config file interface

    As a part of my current project I'm writing a configuration file interface.

    Originally I had thought it would be easier for people to just use a parser generator, but with something as simple as a config file it's easier to simply call a few functions sacrificing speed for ease (and speed) of implementation. Currently only three functions need to be called for the simplest method, init, parse, and shutdown.
    I currently have some level of complexity (like expressions) but I still want to keep it largely simplistic because it's not a scripting language.

    My first question is: if you were using a config file api what kinds of functions would you like to see in it? Are extra things like structure definition useless bulk compared to the simple ini [section] style?

    And secondly: For parsing large sections at once (like chunks of vars), do you like the idea of accessing information with an associative array type method; or would something else be better?

    Thanks ahead.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Depending on what I need I either like a small interface that can do simple [section] key=value style. Or sometimes I want stuff that does a lot more complicated stuff.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If you were using C++, you can create associative arrays using std::map and save yourself the trouble. You could imitate it and create a char[]-indexed binary tree (more work to implement) or a linked list (slower code).

    Flexibility and simplicity should be key. As for me, I would have this done:

    Code:
    [names]
    #this is a comment
    Doe=John
    Age=34
    Famous quote=Multi-line\
    stuff
    
    This variable is not assigned a value...
    While this is assigned an empty string=
    
    [math]
    1 + 2=3
    1\=\=2=false #"1==2" = false
    1!\=2=true #"1!=2" = true

    But just so you know, if I wanted more complex stuff like structures and attributes I'd just dump ini files and go XML.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    This is my disclaimer for a long and ugly post, also I'm obviously not trying to make a replacement for xml, just an alternative I can use and others if I decide my stuff isn't too ........ty to put up somewhere.

    The xml thing gave me a very good idea. I've always hated xml, I find it cluttered and difficult to read, I slapped together something very similar to xml in idea but very different in syntax.
    Structural Component Descriptions will define (obviously) the structure of configuration files much like dtd's do in xml.
    I hacked together what that part of the language will look like, I would appreciate input from you all (syntactic and functional):
    Code:
    SDC ::= header SCD_construct+
    header ::= SCD_global_descriptor
    
    SCD_global_descriptor ::= '[+scd]' ('{' version scd_version scd_name '}' | version scd_version scd_name)
    version ::= num '.' num
    SCD_construct ::= cname (enum_construct | structure_construct)
    
    enum_construct ::= '=' edescriptor ('|' edescriptor)*
    edescriptor ::= [_A-Za-z][_A-Za-z0-9]*
    
    structure_construct ::= '{' (presence_modifier? type_check? fieldname ('=' fvdefault)? fterminator)+ '}'
    presence_modifier ::= '?' | '+' | '*' | '['num '-' num? ']'
    type_check ::= native or defined type names separated by commas
    fieldname ::= [_A-Za-z][_A-Za-z0-9]*
    fvdefault ::= value which is valid for this type (if a typecheck exists)
    fterminator ::= ',' | ';' | '\n' | '\r'
    type_check makes sure that the type used in the configuration file matches one of the types specified, otherwise any type is valid. Native types will be integral, floating point, and string. presence_modifier will state how many times the field can or must exist in the configuration file.
    I also have the binary form layed out but this is already a fat post.

    And finally an example scd
    Code:
    [+scd]
    version=0.1
    scd_name="sample scd"
    scd_version=1
    
    aWindow { ?integral x = 100, ?integral y = 100;
    *string child
    width; float height
    [19-]something_else}
    
    apart = PART_ONE | PART_TWO | PART_SIX
    If an aWindow appears in the configuration file then it may or may not define x and y, it can define zero or more instances of child, must define width and height, and 19 or more instances of something_else. if an apart appears then it's value can only be PART_ONE PART_TWO or PART_SIX.

    I haven't layed out the configuration files yet but I plan on making them pretty flexible, not just C89 or C99 structure definitions in any case.

    Please--even if you wouldn't use something like this, give me input so I can make it easier, cleaner, and better. It's a first rough draft and I would like to produce something reuseable.
    Last edited by valis; 08-18-2006 at 12:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 12
    Last Post: 03-10-2005, 07:48 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21