Read a string from ini?

edited April 2012 in Help request
Hey guys, I am trying to create a 2,5d game using orx.
Right now I am trying to use the Ini config file to hold the scene drawing, my idea is to have the following entry in the configuration file:
[Map]
Scenario_graphic1 = Graphic
Scenario_graphic1_code = 1
Begin = Graphic
Begin_code = 2
End = Graphic
End_code = 3
Map_line1 = 1113111
Map_line2 = 1111111
Map_line3 = 1111111
Map_line4 = 1112111

So the scene I would convert this entry into my scenario struct an then render it.

I am using the following code to get those values:
    int i = 1;
    orxSTRING scenario;
    orxConfig_SelectSection("Map");
    do{
        snprintf(constantName, sizeof(constantName), "%s%d", MAP_CONSTANT_NAME, i);
        scenario = orxConfig_GetString(constantName);
        orxLOG("%s - %s
", constantName, scenario);
        if (*scenario == ''){
            orxLOG("Nothing more to print.
");
        }
        else{
            orxLOG("Read the string: '%s'.
", scenario);
        }
        i++;
    }while(i < 5);

(The i < 5 is just a temporary expression so I can test it)

For some reason the string always come back empty... Any ideas why?
According to the docs, the getString function should receive a orxSTRING variable, I am passing a stack located char pointer, could this be the problem?

EDIT:

Curiously enough, changing the "orxSTRING scenario;" to char*, makes it work o.O

Comments

  • edited April 2012
    I think orxSTRINGs are static...
  • edited April 2012
    Knolan wrote:
    Hey guys, I am trying to create a 2,5d game using orx.
    Right now I am trying to use the Ini config file to hold the scene drawing, my idea is to have the following entry in the configuration file:
    [Map]
    Scenario_graphic1 = Graphic
    Scenario_graphic1_code = 1
    Begin = Graphic
    Begin_code = 2
    End = Graphic
    End_code = 3
    Map_line1 = 1113111
    Map_line2 = 1111111
    Map_line3 = 1111111
    Map_line4 = 1112111
    

    Totally out of topic but I'd suggest to use a list for your map instead, for more convenience:
    Map = 1113111
        # 1111111
        # 1111111
        # 1112111
    
    So the scene I would convert this entry into my scenario struct an then render it.

    I am using the following code to get those values:
        int i = 1;
        orxSTRING scenario;
        orxConfig_SelectSection("Map");
        do{
            snprintf(constantName, sizeof(constantName), "%s%d", MAP_CONSTANT_NAME, i);
            scenario = orxConfig_GetString(constantName);
            orxLOG("%s - %s
    ", constantName, scenario);
            if (*scenario == ''){
                orxLOG("Nothing more to print.
    ");
            }
            else{
                orxLOG("Read the string: '%s'.
    ", scenario);
            }
            i++;
        }while(i < 5);
    

    (The i < 5 is just a temporary expression so I can test it)

    For some reason the string always come back empty... Any ideas why?
    According to the docs, the getString function should receive a orxSTRING variable, I am passing a stack located char pointer, could this be the problem?

    EDIT:

    Curiously enough, changing the "orxSTRING scenario;" to char*, makes it work o.O

    Mmh, I'm actually surprised this compiles without complaining. Which compiler / version of orx do you use?

    orxConfig_GetString() returns a const orxSTRING.
    Beside that, assuming your constantName buffer is big enough and that your MAP_CONSTANT_NAME contains the right case-sensitive prefix, I don't see anything wrong with your code.

    Also you should use the PushSection/PopSection instead of SelectSection, otherwise some other code might not end up using the section they were expecting. :)

    Lastly, if you use a list, no need to printf-ing the key anymore and you can query the number of elements in the list with orxConfig_GetListCounter, and orxConfig_GetListString to get the desired element.

    Let us know if the const solved the issue or if you still have some troubles there!
  • edited April 2012
    @ sonicbhoc:

    Well, being static wouldn't really make a difference, I can change the content of a static variable, as long as its address is returned to me by a function where its context is declared.

    @ iarwain:

    I am using Code::blocks for windows 7 64 bits.
    It compiles and works with char*, it gives me a warning though. I changed it to const orxSTRING and it works too, with no warnings.
    About the list, well, a bit too late now, got it working already yesterday, had to used a few void* tough B)


    Thanks a lot for the replies guys.
  • edited April 2012
    Knolan wrote:
    @ sonicbhoc:

    Well, being static wouldn't really make a difference, I can change the content of a static variable, as long as its address is returned to me by a function where its context is declared.

    @ iarwain:

    I am using Code::blocks for windows 7 64 bits.

    Did you compile orx in 64bit or are you using a 32bit version? I know the 64bit linux version works just fine but I don't think anybody used a 64bit windows version yet, so just curious. :)
    It compiles and works with char*, it gives me a warning though. I changed it to const orxSTRING and it works too, with no warnings.
    About the list, well, a bit too late now, got it working already yesterday, had to used a few void* tough B)

    Heh no worries, I forgot to write an example in my last post anyway, here it is, maybe it'll be helpful for someone else! :)
    orxConfig_PushSection("MyMap");
    
    for(orxS32 i = 0; i < orxConfig_GetListCounter("Map"); i++)
    {
      const orxSTRING zMapLine;
    
      zMapLine = orxConfig_GetListString("Map", i);
    
      orxLOG("MapLine %ld: %s", i, zMapLine);
    }
    
    orxConfig_PopSection();
    
    Thanks a lot for the replies guys.

    You're welcome! Don't hesitate if you have any other questions. :)
  • edited April 2012
    I believe it is the 32 bits one, not sure, just downloaded the orx-dev-mingw-1.3rc0 package and set the paths to the compiler =)
  • edited April 2012
    Knolan wrote:
    @ sonicbhoc:

    Well, being static wouldn't really make a difference, I can change the content of a static variable, as long as its address is returned to me by a function where its context is declared.
    Oops, I meant to say const (for some reason I get the two key-words mixed up in my head, but not in code...)

    I'm probably wrong about that anyway...
  • edited April 2012
    @sonicbhoc: no you're right, the const is important

    @Knolan: Yep, that would be a 32bit version. Really outdated if you ask me, the current SVN one is much more advanced. :)
Sign In or Register to comment.