![]() |
Settings
Hi,
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. thanks |
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. ;)
/* READING SETTINGS FROM THE FILE */ 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); else 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 fclose(configfile); 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); pango_font_description_free(font); } g_free(path); /* WRITING SETTINGS TO THE FILE */ 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 gtk_widget_destroy(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 g_free(fontname); fclose(configfile); } g_free(path); |
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.
|
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.
|
you can try this reference if you wanted to , adapting or rework them to your needs , they do resember ini files to me ::
http://www.koders.com/c/fidE9BA7C14D...2BE31790B.aspx //but the trend now could probably be xml though ... . |
Yeah, you could link to libxml or something (although it'll be more complex and produce larger config files).
|
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 06:46 PM. |