Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Settings parser  (Read 36641 times)

0 Members and 3 Guests are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Settings parser
« on: October 16, 2012, 07:46:15 am »
Hello!
I really like the simplicity of the settings parser on the wiki. (https://github.com/SFML/SFML/wiki/Source%3A-Settings-Parser) I would like to use it in a project and I couldn't find a license notice so I was wondering under what license the code is.
« Last Edit: July 30, 2014, 11:41:29 am by Foaly »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Settings parser
« Reply #1 on: October 16, 2012, 07:58:58 am »
People always forget to include a license when they post code on the wiki. So try to contact the author if he left an e-mail address, otherwise just use it freely... that's most likely the intended usage when there is no explicit license (for this wiki, I mean).
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Settings parser
« Reply #2 on: October 16, 2012, 09:28:07 am »
Alright. I'll try.
Would it maybe be a good idea to make all the codes on the wiki under public domain by default? Meaning if nothing else is specified you can use them for whatever you want? This could prevent license issue in the future or clarify things if the author is unavailable.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Settings parser
« Reply #3 on: October 16, 2012, 09:31:38 am »
I guess I could do that, yes.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Settings parser
« Reply #4 on: October 16, 2012, 09:41:03 am »
Great to hear. I think this will be very helpful. Just make a little note on the main page saying that if nothing else is specified the codes are under public domain or something like that.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Settings parser
« Reply #5 on: January 05, 2014, 07:02:03 pm »
Hello everybody!
I have been using the SettingsParser class from the wiki quiet a lot, especially for small projects (for settings or to keep some data between application runs).
I have modified/extended/improved the class a lot over time. The functionallity pretty much stayed the same, but I removed memory leaks, improved the error handeling, removed code duplication, changed the way files are loaded and lots of other stuff. I found some time today to clean it up and upload the code.
I invite everybody to take a look at it and give me some feedback.
I hope find this class as useful as I did.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Re: Settings parser
« Reply #6 on: July 27, 2014, 10:36:43 am »
This is indeed useful, thanks for updating it.

I have however noticed that when I 'set' a value in the file and later the destructor calls write(), it adds a line to the file. These tend to build up after a while.
{much better code}

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Settings parser
« Reply #7 on: July 28, 2014, 10:41:41 am »
I am glad you find it helpful! :)
I noticed that bug as well. I did a quick check and the write loop looks fine. There are no empty lines being written, but still one empty line appears in the file. It might be something releated to line endings, but sadly I don't really have time to investigate right now...
What system are you on?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Settings parser
« Reply #8 on: July 28, 2014, 10:56:00 am »
This may be caused by the reading loop:

    while(!in.eof())
    {
        std::getline(in, line);
        if(line.size() > 0 && line[0] != '#')
        {
             ...
        }
        else
        {
            param = line;
            value = "";
        }
        m_data.push_back(make_pair(param, value));
        m_size++;
    }

!eof() is not a valid stop condition for reading file content. My guess is that you have one unwanted iteration, and end up adding an empty pair every time you read the file. The reason is that eof() will return true only after you try to read past the end of the file. But if you are at the end, not after the end, it will still return false. In fact this function is only meant to be used after the stream has switched to invalid state, to query the reason.

You can google it to find more explanations and more correct ways of stopping read loops (quickly: use while (std::getline(...)) and everything should be fine).
« Last Edit: July 28, 2014, 10:58:26 am by Laurent »
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Settings parser
« Reply #9 on: July 28, 2014, 07:46:53 pm »
Ah that makes sense!
And indeed it fixed the problem. It seems like this was a part from the old code, that I forgot to refactor. Now that I look at it using !in.eof() makes no sense at all. I changed it to std::getline(...).
I wrote a little update for the class (fixed a complier warning and did some other stuff). It's on the wikipage :)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Settings parser
« Reply #10 on: July 28, 2014, 07:54:58 pm »
This looks nice and useful.  :)
Although for my own work I prefer libconfig.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Settings parser
« Reply #11 on: July 29, 2014, 01:35:02 pm »
Thanks for the link! Looks like an interesting library for projects that are a bit bigger. I will definitly check it out!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Settings parser
« Reply #12 on: July 29, 2014, 02:27:48 pm »
Could you update the link in the initial post? :)

By the way, why have you added const to every parameter? This does not improve const-correctness, it just adds information that is useless to the caller and therefore makes the function interface more difficult to understand. See here and here for detailed explanation.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: Settings parser
« Reply #13 on: July 29, 2014, 03:33:10 pm »
By the way, why have you added const to every parameter? This does not improve const-correctness, it just adds information that is useless to the caller and therefore makes the function interface more difficult to understand.

It's probably worth clarifying that this applies to the copied second args (const bool value, const char value etc.), not the string references.
« Last Edit: July 29, 2014, 03:38:32 pm by select_this »
Follow me on Twitter, why don'tcha? @select_this

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Settings parser
« Reply #14 on: July 29, 2014, 05:55:24 pm »
By the way, why have you added const to every parameter? This does not improve const-correctness, it just adds information that is useless to the caller and therefore makes the function interface more difficult to understand.

I agree that it's useless as part of the interface, but it can sometimes be useful to do it in the implementation.
Like so:
void someFunc(int i);

void someFunc(const int i)
{
... do stuff involving 'i' ...
}
If I don't intend to modify 'i' then I want the compiler to help me and tell me if I do, so I want 'i' to be 'const'. But of course it is pointless for users of the function since I take 'i' by value, so the declaration in the header should of course omit the 'const'.
« Last Edit: July 29, 2014, 06:28:05 pm by Jesper Juhl »

 

anything