LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-12-2006, 01:38 PM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Rep: Reputation: 38
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
 
Old 05-12-2006, 03:07 PM   #2
burntfuse
Member
 
Registered: Sep 2005
Location: Laurel, MD, USA
Distribution: Slackware 10.1, FC5
Posts: 164

Rep: Reputation: 30
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);
 
Old 05-13-2006, 05:07 AM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Original Poster
Rep: Reputation: 38
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.
 
Old 05-15-2006, 07:05 AM   #4
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
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.
 
Old 05-15-2006, 11:56 AM   #5
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
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 ...


.
 
Old 05-16-2006, 11:07 AM   #6
burntfuse
Member
 
Registered: Sep 2005
Location: Laurel, MD, USA
Distribution: Slackware 10.1, FC5
Posts: 164

Rep: Reputation: 30
Yeah, you could link to libxml or something (although it'll be more complex and produce larger config files).
 
Old 05-26-2006, 06:17 AM   #7
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Original Poster
Rep: Reputation: 38
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.
 
  


Reply

Tags
file, settings


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"nvidia-settings" OC settings and SuSE 10 NetRAVEN5000 Linux - Hardware 4 01-12-2007 06:23 PM
User settings Thanotos Slackware 6 01-10-2006 06:58 PM
LANG settings r_213 Linux - Software 4 01-13-2005 04:26 AM
Mandrake 10.0 Gnome/KDE Taskbar icon settings...are there settings? LiquidRezin Linux - Newbie 2 03-22-2004 10:27 AM
Changing US keyboard settings to Spain (SP) settings or Mexico. zLinuxz Linux - General 4 12-01-2002 01:49 AM


All times are GMT -5. The time now is 07:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration