-   Programming (
-   -   Settings (

kalleanka 05-12-2006 01:38 PM


I need to make a file with settings and I wonder if there is a standard way or a lib, class that handels this in c? Or should I just do it the old fashion way open read write close file.


burntfuse 05-12-2006 03:07 PM

I don't know of any standard C functions, it's not hard to do something using fprintf and fscanf. Here's how I did it in an IDE I'm writing (remove the 'g' from the beginning of gfree, gint, gchar, etc. if you're not using GTK). I think you should get the general idea. ;)


gchar *path = g_build_filename(g_getenv("HOME"), ".asmide", NULL); //get the path to the config file

FILE *configfile = fopen(path, "r"); //and try to open the file

if (configfile == NULL) { //if it doesn't exist,

gtk_window_set_default_size(window, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
PangoFontDescription *font = pango_font_description_from_string(DEFAULT_FONT);
gtk_widget_modify_font(text, font);
pango_font_description_free(font); //and use default settings
*color_label = g_strdup(DEFAULT_COLOR_LABEL);
*color_comment = g_strdup(DEFAULT_COLOR_COMMENT);
*color_directive = g_strdup(DEFAULT_COLOR_DIRECTIVE);
*tabwidth = DEFAULT_TAB_WIDTH;
*output_type = g_strdup(DEFAULT_OUTPUT_TYPE);
*usesyncolor = TRUE;

} else {

gchar rawinput[100], fontname[50];
gint window_width = 0, window_height = 0;
*usesyncolor = TRUE;
*tabwidth = 0;

while (!feof(configfile) && fscanf(configfile, "%99[^\n]\n", &rawinput) != 0) { //otherwise, read a line from the file

if (g_str_has_prefix(rawinput, "WindowWidth=")) //parse recognized properties
sscanf(rawinput + 12, "%d", &window_width);
else if (g_str_has_prefix(rawinput, "WindowHeight="))
sscanf(rawinput + 13, "%d", &window_height);
else if (g_str_has_prefix(rawinput, "Font="))
sscanf(rawinput + 5, "%49[^\n]\n", &fontname); //TODO: add error checking?
else if (g_str_has_prefix(rawinput, "ColorLabel="))
*color_label = g_strndup(rawinput + 11, strlen(rawinput) - 11);
else if (g_str_has_prefix(rawinput, "ColorComment="))
*color_comment = g_strndup(rawinput + 13, strlen(rawinput) - 13);
else if (g_str_has_prefix(rawinput, "ColorDirective="))
*color_directive = g_strndup(rawinput + 15, strlen(rawinput) - 15);
else if (g_str_has_prefix(rawinput, "OutputType="))
*output_type = g_strndup(rawinput + 11, strlen(rawinput) - 11);
else if (g_str_has_prefix(rawinput, "SyntaxColoring="))
sscanf(rawinput + 15, "%d", usesyncolor);
else if (g_str_has_prefix(rawinput, "TabWidth="))
sscanf(rawinput + 9, "%d", tabwidth);
fprintf(stderr, "asmide: Unrecognized property %s in config file\n", rawinput); //if it's an invalid property, show an error
} //continue until the end of the file


if (window_width <= 0) //do some sanity checks for properties
window_width = DEFAULT_WINDOW_WIDTH;
if (window_height <= 0)
window_height = DEFAULT_WINDOW_HEIGHT;

if (*color_label == NULL)
*color_label = g_strdup(DEFAULT_COLOR_LABEL);
if (*color_comment == NULL)
*color_comment = g_strdup(DEFAULT_COLOR_COMMENT);
if (*color_directive == NULL)
*color_directive = g_strdup(DEFAULT_COLOR_DIRECTIVE);

if (*output_type == NULL)
*output_type = g_strdup(DEFAULT_OUTPUT_TYPE);

if (*tabwidth <= 0)
*tabwidth = DEFAULT_TAB_WIDTH;

gtk_window_set_default_size(window, window_width, window_height); //then set the properties
PangoFontDescription *font = pango_font_description_from_string(fontname);
gtk_widget_modify_font(text, font);



gchar *path = g_build_filename(g_getenv("HOME"), ".asmide", NULL); //get the path to the config file

FILE *configfile = fopen(path, "w"); //and try to open the file

if (configfile == NULL) { //if it couldn't be opened,

GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "Can't open configuration file\n%s to write settings!", path);
gtk_message_dialog_format_secondary_text(dialog, "Your permissions must be really\nscrewed up if you don't have write\naccess to your home directory.");
gtk_dialog_run(dialog); //show a warning dialog

} else { //otherwise,

gint window_width, window_height;
gchar *fontname;
gtk_window_get_size(window, &window_width, &window_height); //get the properties
fontname = pango_font_description_to_string((gtk_widget_get_style(text))->font_desc);
fprintf(configfile, "WindowWidth=%d\nWindowHeight=%d\nFont=%s\nColorLabel=%s\nColorComment=%s\nColorDirective=%s\nOutput Type=%s\nSyntaxColoring=%d\nTabWidth=%d\n", window_width, window_height, fontname, color_label, color_comment, color_directive, output_type, usesyncolor, tabwidth); //and write them to the file


kalleanka 05-13-2006 05:07 AM

Thanks. I will do some thing like you then. But it would be handy with a settings object and call a set- and get-method.

ioerror 05-15-2006 07:05 AM

There is no standard way to do this in C (it's fairly low-level after all). There are probably libraries that can do this, but I can't think of any offhand. However, Freeciv has a set of functions to set/get parameters in its config file (they use a Windows style ini file). You might be able to adapt that, or parts of it at least.

alred 05-15-2006 11:56 AM

you can try this reference if you wanted to , adapting or rework them to your needs , they do resember ini files to me ::

//but the trend now could probably be xml though ...


burntfuse 05-16-2006 11:07 AM

Yeah, you could link to libxml or something (although it'll be more complex and produce larger config files).

kalleanka 05-26-2006 06:17 AM

xml sounds the way to go even if I wrote it already. Maybe I do it next time. I like standards so better adapt myself.

By the way stringhandeling sucks in c. I had a lot of truble with it knowing pascal and java.

All times are GMT -5. The time now is 05:13 AM.