The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
INI Praser
Discuss INI Praser in the C Programming forum on Dev Shed. INI Praser C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 29th, 2012, 09:18 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 59 m 50 sec
Reputation Power: 0
|
|
|
INI Praser
Hi,
I am new to standard libs of C.I used to program hardware where these libs dont come in any use.
I was trying to develop a simple ini praser.
My INI Syntax is simple:
[HEADER]
ITEM=VALUE
ITEM2=VALUE
I want a function that would return the VALUE of an item in he spefic section or header.
Somewhat lyk this
char * text = GetItemValue(char * FileName,char * section,char * item);
Thanks for ur consideration
|

September 29th, 2012, 09:44 AM
|
|
|
The keywords to search for are ini, parser, and C.
If you want to write your own function, I'd pass a FILE * pointing to an opened stream rather than a char * pointing to a filename. Passing a pointer o a filename, you'd need to open (and close) the file everytime you call the function.
Also, I'd rather pass in the destination address rather than return a pointer to the result. The return value could then indicate success or some error ("section not exist", "key not exist", "value too long for destination length", ...)
Code:
int GetItemValue(char *dst, size_t dstlen, FILE *inihandle, const char *section, const char *item);
Happy Coding!
|

September 29th, 2012, 09:45 AM
|
 |
Contributed User
|
|
|
|
Well the usual way to read a text file is (no error checks for brevity)
Code:
FILE *fp = fopen("file.txt","r");
char buff[BUFSIZ];
while ( fgets(buff,BUFSIZ,fp) != NULL ) {
// do something with each line
}
fclose(fp);
|

September 29th, 2012, 11:11 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 59 m 50 sec
Reputation Power: 0
|
|
|
Reply
I find difficult to start using these libs.However, I will try developing the code and paste the code here if I go successful.
Thanks & Regards
|

September 29th, 2012, 01:47 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
If you're using Windows, why write your own when there is a function already provided by the Windows API to do this. See GetPrivateProfileString() for more details.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

October 13th, 2012, 11:26 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 59 m 50 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever If you're using Windows, why write your own when there is a function already provided by the Windows API to do this. |
I am running windows but VC++ or so,I am using a Game Development SOftware having the language syntax as C and also few libs of C.
================================
I have come so far, but I have trouble.
char * GetItemValue(char * filename,char * section,char * item)
{
char value[1024];
char sectionstr[256];
char itemstr[256];
char tmp;
char * errormsg;
int i;
FILE * inifile;
if((inifile=fopen(filename,"r")) == NULL)
{
errormsg = str_cat(str_create("Failed to open "),str_create(filename));
WriteINILog("GetItemValue",errormsg);
return -1;
}
//File Found
goto find;
find:
while(!feof(inifile))
{
find_section:
if((getc(inifile)) == '[')
{
i = 0;
while(1)
{
tmp = getc(inifile);
if(tmp == ']')
{
if(str_cmp(section,sectionstr))
{
i = 0;
while(1)
{
tmp = getc(inifile);
if(tmp == '=')
{
if(str_cmp(item,itemstr))
{
WriteINILog("GetItemValue","Requested Item Found");
while(1)
{
i = 0;
tmp = getc(inifile);
if(tmp == ';') { return value; }
if(feof(inifile)) { WriteINILog("GetItemValue","End Of File while refering a value"); return -3; }
value[i] = tmp;
i++;
wait(1);
}
}
}
if(tmp == '[') { WriteINILog("GetItemValue","Item not found in the given section/Non-terminating Item"); return -2; }
if(feof(inifile)){ WriteINILog("GetItemValue","Item not found in the given section/Non-terminating Item"); return -2; }
itemstr[i] =tmp;
i++;
wait(1);
}
}
else
{
i = 0;
goto find_section;
}
}
if(tmp == '\n') { continue; }
if(feof(inifile)){ WriteINILog("GetItemValue","Section opened is never closed"); return -1; }
sectionstr[i] = tmp;
i++;
wait(1);
}
}
wait(1);
}
WriteINILog("GetItemValue","Required section not found or no Sections EXIST:End of File(EOF)");
}
My INI File
[TestSectionMain]
TestItem=12345;
TestItem2=12345;
[TestSection]
TestItem=123;
|

October 14th, 2012, 02:26 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by Yashas I am running windows but VC++ or so,I am using a Game Development SOftware having the language syntax as C and also few libs of C. | None of which precludes you from using GetPrivateProfileString(). Why do you think it does?
Either way, source code to do this is commonly available; why waste your time developing yet another? For example.
Last edited by clifford : October 14th, 2012 at 03:14 AM.
|

October 14th, 2012, 03:16 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by Yashas
I have come so far, but I have trouble.
| - Post the code in code tags.
- Tell us exactly what "trouble" you are having so we don't have to guess.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|