ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I actually wrote a little app that did this exact thing. Here's an exerpt:
Code:
char **drives = NULL;
char *destination = NULL;
char **folders = NULL;
char **extensions = NULL;
char **exclusions = NULL;
int ndrives = 0;
int nfolders = 0;
int nextensions = 0;
int nexclusions = 0;
int change_section(char *str)
{
if(!strcmpi(str, "[Drives]"))
return 1;
if(!strcmpi(str, "[Destination]"))
return 2;
if(!strcmpi(str, "[Folders]"))
return 3;
if(!strcmpi(str, "[Extensions]"))
return 4;
if(!strcmpi(str, "[Exclusions]"))
return 5;
return 0;
}
void load_ini(void)
{
FILE *fp;
char buf[4096], *b, **dest;
int line = 0, section = 0;
if(!(fp = fopen("settings.ini", "r")))
{
fprintf(stderr, "Couldn't open \"settings.ini\" for reading!\n");
exit(1);
}
while(fgets(buf, sizeof(buf), fp))
{
if(feof(fp))
break;
line++;
b = &buf[strlen(buf)-1];
while(*buf && (!isprint(*b) || isspace(*b)))
*b-- = '\0';
b = buf;
while(isspace(*b))
b++;
if(!*b || *b == '#')
continue;
if(*b == '[')
{
section = change_section(b);
continue;
}
switch(section)
{
case 1:
if(ndrives)
drives = realloc(drives, sizeof(char *)*(ndrives+1));
else
drives = malloc(sizeof(char *));
dest = &drives[ndrives++];
break;
case 2:
if(destination)
free(destination);
dest = &destination;
break;
case 3:
if(nfolders)
folders = realloc(folders, sizeof(char *)*(nfolders+1));
else
folders = malloc(sizeof(char *));
dest = &folders[nfolders++];
break;
case 4:
if(nextensions)
extensions = realloc(extensions, sizeof(char *)*(nextensions+1));
else
extensions = malloc(sizeof(char *));
dest = &extensions[nextensions++];
break;
case 5:
if(nexclusions)
exclusions = realloc(exclusions, sizeof(char *)*(nexclusions+1));
else
exclusions = malloc(sizeof(char *));
dest = &exclusions[nexclusions++];
break;
default:
fprintf(stderr,
"Line %d: Found setting outside of section header.\n", line);
fclose(fp);
exit(1);
}
*dest = malloc(strlen(b)+1);
strcpy(*dest, b);
}
if(!destination)
{
fprintf(stderr, "No destination drive was specified.\n");
exit(1);
}
fclose(fp);
}
Then you can simply call load_ini() and viola. Here's an example of the ini file that this program uses:
Code:
# Lines beginning with a hashmark (#) are considered to be comments and
# are ignored. Blank lines are also ignored.
# --- Configuration Begins ---
# List of drives to search on the PC. Drives that don't exist will simply
# be ignored. Do NOT include the trailing blackslash (\)
[Drives]
C:
# D:
# E:
# The folder to copy all the found files to. If the folder doesn't exist
# when the program starts, it will automatically be created. If
# multiple folders are listed, ONLY the last one will be used. Do NOT
# include the trailing backslash (\)
[Destination]
C:\User
If you are doing it in Windows, why not use the Win32 APIs for reading ini files. The APIs you will want to look at are the [Get/Write]PrivateProfile* functions.
To be honest, didn't know anything about the windoze API's...
I'm not looking to get into Windoze programming - thought I could use the knowledge that I have so far to do it as i'm still learning C on Linux and don't wanna confuse it ny taking months to learn windoze API's etc, let alone keep my PC in windoze when i don't have to
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.