Can't modify config file
Emily Brace

Hello!

I have some code to check if there's a config file and if there isn't, it will create one, simple stuff. I can then read from the file just fine, but when I try to set values and then save, the save fails.

Here's some of my code.
I first load the config file in the beginning of my code

// Outside of main()
ALLEGRO_CONFIG* config;
// Inside of main()
config = al_load_config_file("config.ini");
// This works just fine
value = al_get_config_value(config, "blah" "blah");
// But this doesn't :c
al_set_config_value(config, "blah", "blah", "blah");
// This returns false and the file isn't changed
al_save_config_file("config.ini", config);

Not sure what I'm doing wrong.
Thanks! :D

Edgar Reynaldo

Hey Emily,

Here are a few things to try.

1. Is the file read-only? Save would fail in that case.

2. Have you compiled in debug mode or release mode with debugging info and checked allegro.log for clues?

3. Have you tried saving as a different file?

4. How do you know al_set_config_value has failed? Have you checked it with al_get_config_value?

5. What version of Allegro 5 are you using? If it's a bug, it may have been fixed by now.

Emily Brace

1.
No, it can create the file just fine.

2.
Tried compiling in debug mode. Where can I find allegro.log?

3.
Yes, and that also fails.

4.
Sorry that I was unclear. Meant that al_save_config_file failed. If I try to check the value before and after, the value changes.

5.
Running Allegro 5.2.1.1

Edgar Reynaldo

al_save_config_file only fails in a couple of cases. One, where the files fails to be created, to be saved, or to be closed. Two, where there is an error in the file stream.

allegro.log will be created in the same folder as your program if the version of allegro you linked to has debugging logs enabled.

Your best bet is to debug this yourself. Set a breakpoint in config.c:433, and then check the return values of retsave and retclose and see if there was an error. If the breakpoint isn't triggered, then the file couldn't be created. If retsave is false, then set a breakpoint in al_save_config_file_f and step through the function checking return values.

I can give you a short tutorial on debugging with gdb if you need help.

Can you provide a small test case that fails for you? And provide the config file and source file you're using? That way I can test it out on my machine and see if it works for me or not.

Emily Brace

I tried recompiling Allegro with WANT_RELEASE_LOGGING, but no log is being created.
I'll see what I can figure out...

Just to be clear, I'm trying to update an already created config file. A workaround could be to remove the file and create it again if it's needed...

EDIT: Tried removing the file and write it again like this and it removes the file, but doesn't write it again

ifstream fileCheck("config.ini");
if (fileCheck.good())
{
  fileCheck.close();
  remove("engine.ini");

  ALLEGRO_CONFIG* config = al_create_config();
  al_set_config_value(config, "Blah", "Bloh", "Bleh");
  al_set_config_value(config, "Blah", "Bleh", "Bloh");

  al_save_config_file("engine.ini", config);
}

SiegeLord

You're calling this after al_init, right?

Emily Brace

Yes, I can create it just fine, just not modify it afterwards.
I'm using various other Allegro stuff for graphics etc. so I know it's working.
Dunno if PhysFS is messing stuff up though.

EDIT: Yup, after initializing PhysFS, you can't save config files, since PhysFS doesn't support writing to zip files. Not sure if bug or not though.

SiegeLord

You can probably configure PhysFS to be able to save outside the zip file if you mount a real directory. Alternatively, you could call al_set_standard_file_interface() before saving your config file and then restoring it with al_set_physfs_file_interface().

Edgar Reynaldo

As an aside, I think 7-zip has an API that lets you write to archive files. If we wrote a file system driver for 7-zip, what you're trying to do could be done. And 7-zip supports MANY different formats.

Emily Brace

Well, that would also be cool, but the point was never to write to the zip file itself. The issue was just that it tried to do that and that was why it failed.

Edgar Reynaldo

That's why it's always best to try and provide a minimum, complete, verifiable example (MCVE like Stack Overflow likes to call it), so others can try to reproduce it and so we can see everything that's going on. I would never have guessed you were using PhysFS from what you showed us. ;)

Emily Brace

Good point, although, I wouldn't have thought of including that code either way xd

Elias

I wonder if we should provide a function al_set_physfs_file_interface_read_only. But then when you try and read your config file it still would try to read from the .zip - I guess it has to be the user's responsibility to set the file interface they want to use.

Emily Brace

Or some version of the function that excludes config files :p

Thread #616549. Printed from Allegro.cc