C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 29th, 2012, 09:18 AM
Yashas Yashas is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 Yashas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old September 29th, 2012, 09:44 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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!

Reply With Quote
  #3  
Old September 29th, 2012, 09:45 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
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);
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #4  
Old September 29th, 2012, 11:11 AM
Yashas Yashas is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 Yashas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old September 29th, 2012, 01:47 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
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

Reply With Quote
  #6  
Old October 13th, 2012, 11:26 PM
Yashas Yashas is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 Yashas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

Reply With Quote
  #7  
Old October 14th, 2012, 02:26 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 38 m 37 sec
Reputation Power: 1800
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.

Reply With Quote
  #8  
Old October 14th, 2012, 03:16 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 38 m 37 sec
Reputation Power: 1800
Quote:
Originally Posted by Yashas
I have come so far, but I have trouble.
  1. Post the code in code tags.
  2. Tell us exactly what "trouble" you are having so we don't have to guess.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > INI Praser

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap