Looking for source to read/write INI files?
Author |
Message |
Jt #1 / 34
|
 Looking for source to read/write INI files?
I would like to find if possilble source code on example of reading a Windoze INI file. I this that I need to write, is going to be compiled to run in a Win32 console and Unix teminal console modes. Any help on this? Thanks, Jt
|
Wed, 23 Feb 2005 07:20:42 GMT |
|
 |
those who know me have no need of my nam #2 / 34
|
 Looking for source to read/write INI files?
in comp.lang.c i read: Quote: >I would like to find if possilble source code on example of reading a >Windoze INI file. I this that I need to write, is going to be compiled to >run in a Win32 console and Unix teminal console modes.
of course it's possible, what makes you think it wouldn't be? just fgets a line at a time and fill in a structure, using a simple state machine. -- bringing you boring signatures for 17 years
|
Wed, 23 Feb 2005 10:08:26 GMT |
|
 |
Jack Klei #3 / 34
|
 Looking for source to read/write INI files?
comp.lang.c: Quote: > I would like to find if possilble source code on example of reading a > Windoze INI file. I this that I need to write, is going to be compiled to > run in a Win32 console and Unix teminal console modes. > Any help on this? > Thanks, > Jt
And your question about the C language is? As far as C is concerned, you read Windows INI files by opening them with fopen(), using one or more of the various file input functions prototyped in <stdio.h>, and finally using fclose(). If you want to know about Windows specific file formats, try a Windows specific group, or http://www.wotsit.org. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Wed, 23 Feb 2005 12:10:52 GMT |
|
 |
John Laur #4 / 34
|
 Looking for source to read/write INI files?
Obviously he did not phrase that the best, but I think it's clear that he wasn't asking if it was possible to write it from scratch. He wants to know if it's possible to find source code for a library or example program that will do that. As it sounds like this will run on a Unix teminal console, then a windows group probably is not the best, as I think windows has built-in functions for this sort of thing that will not be available on Unix... That said... I do remember coming accross public domain libraries for doing this. This isn't the one I remember, but doing a simple web search I came up with the following (amond others) that should work: http://www.muquit.com/muquit/software/libmcfg/libmcfg.html
Quote: > in comp.lang.c i read: > >I would like to find if possilble source code on example of reading a > >Windoze INI file. I this that I need to write, is going to be compiled to > >run in a Win32 console and Unix teminal console modes. > of course it's possible, what makes you think it wouldn't be? > just fgets a line at a time and fill in a structure, using a simple state > machine. > -- > bringing you boring signatures for 17 years
|
Thu, 24 Feb 2005 01:57:45 GMT |
|
 |
Jt #5 / 34
|
 Looking for source to read/write INI files?
Reading an INI is not the problem really. Its writing back to the INI file. Finding the right Section and finding the right key and update it, is my problem. Samples, that I've found are more trouble to fix than write it from scratch.
Quote: > Obviously he did not phrase that the best, but I think it's clear that he > wasn't asking if it was possible to write it from scratch. > He wants to know if it's possible to find source code for a library or > example program that will do that. As it sounds like this will run on a > Unix teminal console, then a windows group probably is not the best, as I > think windows has built-in functions for this sort of thing that will not be > available on Unix... > That said... I do remember coming accross public domain libraries for doing > this. > This isn't the one I remember, but doing a simple web search I came up with > the following (amond others) that should work: > http://www.muquit.com/muquit/software/libmcfg/libmcfg.html
> > in comp.lang.c i read: > > >I would like to find if possilble source code on example of reading a > > >Windoze INI file. I this that I need to write, is going to be compiled to > > >run in a Win32 console and Unix teminal console modes. > > of course it's possible, what makes you think it wouldn't be? > > just fgets a line at a time and fill in a structure, using a simple state > > machine. > > -- > > bringing you boring signatures for 17 years
|
Thu, 24 Feb 2005 11:51:32 GMT |
|
 |
Mark McIntyr #6 / 34
|
 Looking for source to read/write INI files?
On Sun, 08 Sep 2002 03:51:32 GMT, in comp.lang.c , "Jt" Quote:
>Reading an INI is not the problem really. Its writing back to the INI file. >Finding the right Section and finding the right key and update it, is my >problem.
Read in the whole file to an array of char arrays. Write it all out when you need to. <ot> Windows ini files are an inherently broken way of storing config info mind you - use the registry. On a networked PC, potentially shared by several users, you end up with dozens of megs of unwanted inis cluttering up your disk, being copied up with your profile, being copied down again etc etc etc. </ot> -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Fri, 25 Feb 2005 06:32:30 GMT |
|
 |
Richard Heathfiel #7 / 34
|
 Looking for source to read/write INI files?
Quote:
> On Sun, 08 Sep 2002 03:51:32 GMT, in comp.lang.c , "Jt"
> >Reading an INI is not the problem really. Its writing back to the INI file. > >Finding the right Section and finding the right key and update it, is my > >problem. > Read in the whole file to an array of char arrays. Write it all out > when you need to. > <ot> > Windows ini files are an inherently broken way of storing config info > mind you - use the registry.
The registry is an inherently non-portable way to store config info mind you - use ini files. :-) Quote: > On a networked PC, potentially shared by > several users, you end up with dozens of megs of unwanted inis > cluttering up your disk, being copied up with your profile, being > copied down again etc etc etc.
That's a file management issue, which (as you rightly point out) is OT here. Such problems are, of course, solvable without resorting to non-portable solutions such as "the registry". --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Fri, 25 Feb 2005 06:47:38 GMT |
|
 |
Mark McIntyr #8 / 34
|
 Looking for source to read/write INI files?
On Sun, 08 Sep 2002 23:47:38 +0100, in comp.lang.c , Richard Quote:
>> On Sun, 08 Sep 2002 03:51:32 GMT, in comp.lang.c , "Jt"
>> >Reading an INI is not the problem really. Its writing back to the INI file. >> >Finding the right Section and finding the right key and update it, is my >> >problem. >> Read in the whole file to an array of char arrays. Write it all out >> when you need to. >> <ot> >> Windows ini files are an inherently broken way of storing config info >> mind you - use the registry. >The registry is an inherently non-portable way to store config info mind >you - use ini files. :-)
I _did_ say OT. Quote: >> On a networked PC, potentially shared by >> several users, you end up with dozens of megs of unwanted inis >> cluttering up your disk, being copied up with your profile, being >> copied down again etc etc etc. >That's a file management issue, which (as you rightly point out) is OT >here. Such problems are, of course, solvable without resorting to >non-portable solutions such as "the registry".
Only by using a database. Which is of course..... (OTOH if you _do_ have a workable solution to this problem which works for an NT4 network consisting of multiple domains globally distributed and with circa 77000 employees using a zillion different apps, I have a possible customer for you....) -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Fri, 25 Feb 2005 06:54:43 GMT |
|
 |
those who know me have no need of my nam #9 / 34
|
 Looking for source to read/write INI files?
in comp.lang.c i read: Quote: ><ot> >Windows ini files are an inherently broken way of storing config info >mind you - use the registry. On a networked PC, potentially shared by >several users, you end up with dozens of megs of unwanted inis >cluttering up your disk, being copied up with your profile, being >copied down again etc etc etc. ></ot>
it's a good thing that this off-topic opinion wasn't really posted, otherwise some people would disagree and then we'd probably have a protracted off-topic discussion in this group. -- bringing you boring signatures for 17 years
|
Fri, 25 Feb 2005 09:39:31 GMT |
|
 |
PubPo #10 / 34
|
 Looking for source to read/write INI files?
Quote:
> I would like to find if possilble source code on example of reading a > Windoze INI file. I this that I need to write, is going to be compiled to > run in a Win32 console and Unix teminal console modes.
Here is some old public domain stuff that I got years ago. I don't think it's 100% identical to Windoze, but it's tolerably close for most things. ***ini.c /* ** This .ini management code is placed into the Public Domain ** by Carey {*filter*}worth on May 26, 1998. */ #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <ctype.h> #include <limits.h> #include <time.h> #include <stdarg.h> #include "pi.h" /* ** Expect the following two macros to be defined: ** The standard C macro FILENAME_MAX might only be large enough for ** just the filename, and not any path.<disgust> I need to allow ** enough for paths, too. #define MAX_FILENAME 128 #define LINE_INPUT_MAX 256 */ void StripSpaces(char *string) /* ** Strip leading and trailing spaces. */ {char *trailing; while ((*string) && ((*string==' ') || (*string=='\t'))) memmove(string,string+1,strlen(string)); trailing=string+strlen(string); while ((*string) && ((*trailing==' ') || (*trailing=='\t'))) *trailing-- = '\0'; Quote: }
static void StripLF(char *Str) { if (Str==NULL) return; while (Str[0]) {int x; x=strlen(Str)-1; if (Str[x]=='\n') Str[x]='\0'; else if (Str[x]=='\r') Str[x]='\0'; else return; } Quote: }
static int ReadLine(FILE *fp, char *line) /* ** Read a line of input, removing '\n'. With error checking. */ {char *cp; memset(line,'\0',LINE_INPUT_MAX); cp=fgets(line,LINE_INPUT_MAX-2,fp); if (cp==NULL) {*line='\0';return FALSE;} if (feof(fp)!=0) {*line='\0';return FALSE;} if (ferror(fp)!=0) {*line='\0';return FALSE;} StripLF(line); return TRUE; Quote: }
static int StrEqIC(char *s1,char *s2) // Compare strings, ignoring case. { if ((s1==NULL) || (s2==NULL)) return FALSE; while (tolower(*s1)==tolower(*s2)) {if (*s1=='\0') return TRUE; s1++;s2++; } return FALSE; Quote: }
static char * StrAdd(char *str1,char *str2) /* ** Combine two strings. Str1 points to the heap. If Str1 is a NULL ** then that means an error previously occured and to return NULL. */ {char *ptr; if (str1==NULL) return NULL; ptr=(char*)realloc(str1,strlen(str1)+strlen(str2)+1); if (ptr==NULL) {free(str1);return NULL;} strcat(ptr,str2); return ptr; Quote: }
static void ParseLine(char *line,char *var, char *data) /* ** This routine divides the line into two parts. The variable, and ** the 'string'. If the line is a comment, the 'data' variable will ** contain the line and 'var' will be empty. */ {char *str; StripSpaces(line); *data='\0'; *var='\0'; if ((line[0]==';') || (line[0]=='%') || (line[0]=='#')) { strcpy(data,line); return; } strcpy(var,line); str=strpbrk(var," =\t"); if (str!=NULL) { strcpy(data,str); *str='\0'; if ((*data==' ') || (*data=='\t')) StripSpaces(data); if (*data=='=') {*data=' ';StripSpaces(data);} } Quote: }
int FileExists(char *FileName) /* ** Just return whether a file exists or not. */ {FILE *f; f=fopen(FileName,"rb"); if (f==NULL) return FALSE; fclose(f); return TRUE; Quote: }
int CreateTempFileName(char *TempFileName) /* ** Create a unique temporary filename in the current working directory. ** ** TempFileName must point to a char space of at least FILENAME_MAX+1 chars. ** ** We also make sure we can actually open the file, in addition ** to it being a unique, unused filename. ** ** We have to create our own temporary filename, because the library ** version only has to be able to do a few, which may not be enough, ** since we totally rewrite the file for every update. */ {int x; FILE *TempFile=NULL; int tries=0; strcpy(TempFileName,"tempfile.tmp"); do { srand((unsigned int)time(NULL)+tries); for (x=4;x<8;x++) TempFileName[x]=(char)('0'+(rand() % 10)); if (!FileExists(TempFileName)) TempFile=fopen(TempFileName,"w"); tries++; } while ((TempFile==NULL) && (tries<100)); if (TempFile==NULL) return FALSE; fclose(TempFile); return TRUE; Quote: }
static int UpdateCfgStr(char *FileName,char *SectionName,char *VarWanted, char *NewData) /* ** This will update a variable in a specific section in your .ini file. ** It will do so safely by copying it to a new file, and when finished, ** will delete the old one and rename the new one to the correct name. ** If any fatal error occurs, it will return a FALSE to indicate failure ** and TRUE to indicate success. I generally don't care why it failed, ** just knowing that it failed is usually enough. */ { FILE *CfgFile,*NewCfgFile; char SectionWanted[LINE_INPUT_MAX]; char line[LINE_INPUT_MAX]; char var[LINE_INPUT_MAX]; char data[LINE_INPUT_MAX]; char TempFileName[MAX_FILENAME+1]; int Status=TRUE; int Updated=FALSE; int InSection=FALSE; int BlankLines=0; if (!CreateTempFileName(TempFileName)) { DumpDebug("Unable to create a temporary filename to update %s\n",FileName); return FALSE; } NewCfgFile=fopen(TempFileName,"w"); if (NewCfgFile==NULL) { DumpDebug("Unable to open a temporary file to update %s\n",TempFileName); return FALSE; } sprintf(SectionWanted,"[%s]",SectionName); CfgFile=fopen(FileName,"r"); if (CfgFile) { while (ReadLine(CfgFile,line)) { if (StrEqIC(line,SectionWanted)) InSection=TRUE; else if (InSection && (line[0]=='[')) {/* leaving our section */ InSection=FALSE; if (!Updated) /* Variable wasn't found, we have to add it */ { fprintf(NewCfgFile,"%s = %s\n",VarWanted,NewData); while (BlankLines) {fprintf(NewCfgFile,"\n");BlankLines--;} Updated=TRUE; } } if (line[0]=='\0') BlankLines++; else { while (BlankLines) {fprintf(NewCfgFile,"\n");BlankLines--;} ParseLine(line,var,data); if (InSection && StrEqIC(var,VarWanted) && !Updated) { fprintf(NewCfgFile,"%s = %s\n",var,NewData); Updated=TRUE; } else fprintf(NewCfgFile,"%s\n",line); } } } /* ** Our section may not have even been there (or there wasn't already ** a config file) in which case we have to add both the variable and ** the section itself. */ if (!Updated) { /* We may have hit EOF while still in our section. */ /* If so, we don't need to add the section header. */ if (!InSection) { if (BlankLines!=0) fprintf(NewCfgFile,"\n"); fprintf(NewCfgFile,"%s\n",SectionWanted); } fprintf(NewCfgFile,"%s = %s\n",VarWanted,NewData); } fprintf(NewCfgFile,"\n"); if (CfgFile && ferror(CfgFile)) Status=FALSE; if (ferror(NewCfgFile)) Status=FALSE; if (CfgFile) fclose(CfgFile); fclose(NewCfgFile); if (!Status) remove(TempFileName); else {/*if (remove(FileName)) return FALSE;*/ remove(FileName); if (rename(TempFileName,FileName)) return FALSE; } return Status; Quote: }
static int FindCfgLine(char *FileName,char *SectionName, char *VarName, char *Data) /* ** Find a VarName within SectionName in file FileName. ** Return TRUE on success, else FALSE */ { FILE *CfgFile; char line[LINE_INPUT_MAX]; char SectionWanted[LINE_INPUT_MAX]; char var[LINE_INPUT_MAX]; int InSection=FALSE; CfgFile=fopen(FileName,"r"); if (CfgFile==NULL) { DumpDebug("File %s not found.\n",FileName); return FALSE; } sprintf(SectionWanted,"[%s]",SectionName); while (ReadLine(CfgFile,line)) { if (StrEqIC(line,SectionWanted)) InSection=TRUE; else if (InSection && (line[0]=='[')) InSection=FALSE; if (InSection) { ParseLine(line,var,Data); if (StrEqIC(VarName,var)) { fclose(CfgFile); return TRUE; } } } fclose(CfgFile); *Data='\0'; return FALSE; Quote: }
unsigned int ReadCfgItem(char *FileName,char *SectionName, char *VarName, void *DataPtr, enum CfgVarTypes DataType, unsigned int MaxItems) /* ** Reads item(s) from a .ini / .cfg file. ** Returns 0 on failure, else the number of items read. ** If the items read don't match the items wanted (MAxItems), then ** something may be wrong and the variable may be only partially ** initialized. */ {int ItemsRead=0; char Str[LINE_INPUT_MAX]; char *Token; if (MaxItems==0) return 0; /* Got to have something to read */ if (!FindCfgLine(FileName, SectionName, VarName, Str)) { DumpDebug("File=%s Sect=%s Var=%s Data= **NOT FOUND**\n", FileName,SectionName,VarName); return 0; } /* ** Found the line we wanted, now convert the string to ** the data format we want. */ if (DataType==Cfg_String) { if (MaxItems==0) return 0; if (strlen(Str) >= MaxItems) Str[MaxItems-1]='\0'; strcpy((char*)DataPtr,Str); DumpDebug("File=%s Sect=%s Var=%s Data=%s\n", FileName,SectionName,VarName,DataPtr); return MaxItems; } Token=strtok(Str," ,\t"); while ( (ItemsRead < MaxItems) && (Token != NULL)) {char *EndChar; DumpDebug("Token=%s\n",Token); switch (DataType) { case Cfg_Integer: {long int x; x=strtol(Token,&EndChar,10); if (tolower(*EndChar)=='k') x*=1024; if (tolower(*EndChar)=='m') x*=1048576; if (tolower(*EndChar)=='g') x*=(1024*1048576); if (x < INT_MIN) x=INT_MIN; if (x > INT_MAX) x=INT_MAX; *((int*)DataPtr)=(int)x; DumpDebug("File=%s Sect=%s Var=%s Data=%d\n", FileName,SectionName,VarName,*((int*)DataPtr)); DataPtr=((char*)DataPtr)+sizeof(int); break; } case Cfg_UInteger: {unsigned long int x; x=strtoul(Token,&EndChar,10); if (tolower(*EndChar)=='k') x*=1024; if (tolower(*EndChar)=='m') x*=1048576; if (tolower(*EndChar)=='g') x*=(1024*1048576);
... read more »
|
Fri, 25 Feb 2005 13:37:38 GMT |
|
 |
Richard Heathfiel #11 / 34
|
 Looking for source to read/write INI files?
Quote:
<snip> > (OTOH if you _do_ have a workable solution to this problem which works > for an NT4 network consisting of multiple domains globally distributed > and with circa 77000 employees using a zillion different apps, I have > a possible customer for you....)
As it happens, I *do* have a workable solution to this problem but, as it involves ditching NT, I don't suppose the customer would want to hear about it. :-) --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Fri, 25 Feb 2005 18:16:15 GMT |
|
 |
#12 / 34
|
 Looking for source to read/write INI files?
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
E. Gibbo #13 / 34
|
 Looking for source to read/write INI files?
Quote:
><snip> >> (OTOH if you _do_ have a workable solution to this problem which works >> for an NT4 network consisting of multiple domains globally distributed >> and with circa 77000 employees using a zillion different apps, I have >> a possible customer for you....) >As it happens, I *do* have a workable solution to this problem but, as >it involves ditching NT, I don't suppose the customer would want to hear >about it. :-)
To the contrary, Mr. Heathfield. If you really did have a *solution* to the problem, and it involved ditching NT (presumably for some free OS like Linux?), the world would beat a path to your door. More and more firms are becoming interested in the idea of freeing themselves from Microsoft's ever-tightening noose of license restrictions, "compulsory upgrades", etc.. The stumbling block, of course, generally turns out to be that most of the "zillion" apps in question are actually just MS Office, Access, PowerPoint, or VisualWhateverOfTheDay. And surprise, surprise, Microsoft seems awfully reluctant to do anything that might enable people to port these apps away from the one platform that they control, "Wintel". Customers *want* to hear that they can do without Microsoft. And contrary to popular FUD, differences in the UI, window managers, etc., are *not* actually keeping most people from using Microsoft alternatives. When people see the latest X Windows environments, they typically say "wow, how come Windows doesn't have that". Remote display and administration of <N> desktops from one central location? It's the stuff of dreams in the Wintel world, the cutting edge. With the right third-party software, maybe you can do that, but most users haven't even grasped that it is possible, yet. Secretaries are not actually stupid, as it turns out: give them a consistent and sensible enviroment (especially one that is *reliable* for a change), and give them a reasonable time to learn it, and they are generally quite adaptable. What customers *don't* want to hear is how they can do without Microsoft by hiring a team of Unix programmers to write shell scripts, how they can easily use{*filter*}instead of Word, as long as they are willing to accept a completely new and counterintuitive definition of the term "easy", etc.. When there is a Unix platform that seamlessly runs Office, or that seamlessly accepts and produces all the Office file formats even, it's all over for Bill's little monopoly. He knows that, and he is certainly no fool. It'll happen eventually, but the biggest corporation in the world will be making sure it happens as slowly and painfully as possible. By that time, Bill will have safely moved his eggs into the "control of multimedia content and formats" basket. --Ben --
|
Sat, 26 Feb 2005 00:53:35 GMT |
|
 |
#14 / 34
|
 Looking for source to read/write INI files?
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Mark McIntyr #15 / 34
|
 Looking for source to read/write INI files?
On Mon, 09 Sep 2002 11:16:15 +0100, in comp.lang.c , Richard Quote:
><snip> >> (OTOH if you _do_ have a workable solution to this problem which works >> for an NT4 network consisting of multiple domains globally distributed >> and with circa 77000 employees using a zillion different apps, I have >> a possible customer for you....) >As it happens, I *do* have a workable solution to this problem but, as >it involves ditching NT, I don't suppose the customer would want to hear >about it. :-)
Grin, nope not a chance, retooling around 100K desktops and migrating a few hundred million bucks worth of in-house apps might be cost-ineffective. ps I hope its in C.... -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Sat, 26 Feb 2005 05:42:57 GMT |
|
|
Page 1 of 3
|
[ 34 post ] |
|
Go to page:
[1]
[2] [3] |
|