/******************************************************************************
Copyright (C) 2005 Matteo Lucarelli 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long GetPrivateProfileString(char* lpAppName,char* lpKeyName,char* lpDefault,char* lpReturnedString,long nSize,char* lpFileName)
{
	FILE* fp;
	int rightApp=0;
	char line[1024];
	char *c1,*c2;
	*lpReturnedString=0;
	
	fp=fopen(lpFileName,"r");
	if (!fp) return 0;
	
	while (fgets(line,1024,fp)){
		
		// truncate line after comment char (;)
		if (*line==';') continue;
		for (c1=line;c1<(line+strlen(line));c1++) if (*(c1)==';') *(c1)=0;
		
		// check for apps
		if ((*line=='[')&&(c2=strchr(line,']'))){
			c1=line+1;
			*c2=0;
			if (!strcasecmp(c1,lpAppName)) rightApp=1;
			else rightApp=0;
			continue;
		}
		
		// check for key
		if (rightApp==1){
			c1=line;
			if (c2=strchr(c1,'=')){
				*c2=0;
				if (!strcasecmp(c1,lpKeyName)){
					c2++;
					if (c2!=0){
						// exclude blanks and CR/LF at value end
						c1=c2+strlen(c2)-1;
						while ((*c1==' ')||(*c1=='\t')||(*c1=='\n')||(*c1=='\r')) c1--;
						*(c1+1)=0;
						// copy value and return
						snprintf(lpReturnedString,nSize,c2);
						break;
					}
				}
			}
		}
	}
	fclose(fp);
	return strlen(lpReturnedString);
}

unsigned int GetPrivateProfileInt(char* lpAppName,char* lpKeyName,int nDefault,char* lpFileName)
{
	char val[1024];
	if (GetPrivateProfileString(lpAppName,lpKeyName,"",val,1024,lpFileName)==0) return nDefault;
	else return atoi(val);
}