#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <ctype.h>
#include <string.h>

#define     SINGLE_QUOTE    '\''
#define     DOUBLE_QUOTE    '"'

char        *pszConfigFile = "WIN.INI";
static char szResult[0x100];

char *Trim( char *pszStr )
{
    int i;

    for( i = strlen(pszStr)-1; i >= 0; i--)
        if( isspace(pszStr[i]) )
            pszStr[i] = 0;
        else
            break;

    for( i = 0; pszStr[i] != 0; i++ )
        if( !isspace(pszStr[i]) )
            break;
    if( pszStr[i] == 0 ) return pszStr;
    memmove(pszStr, &pszStr[i], strlen(&pszStr[i]) + 1);

    return pszStr;
}

static
char *StripComment( char *pszStmt )
{
    char    *p;
    char    literal;

    literal = 0;
    for( p=pszStmt; *p != 0; p++ )
        if( literal != 0 ) {
            if( *p == literal )
                literal = 0;
        } else {
            if( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE) {
                literal = *p;
                continue;
            }
            if( *p == ';' ) {
                *p = 0;
                break;
            }
        }
    return pszStmt;
}

char *StripQuotes( char *pszStr )
{
    char    *p;
    char    literal;

    literal = 0;
    for( p=pszStr; *p != 0; ) {
        if( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
            if( literal == 0 ) {
                literal = *p;
                memmove(p, p+1, strlen(p+1)+1);
                continue;
            } else
            if( *p == literal ) {
                memmove(p, p+1, strlen(p+1)+1);
                literal = 0;
                continue;
            }
        }
        p++;
    }
    return pszStr;
}

int GetPrivateProfileString(
    char *pszGroup,
    char *pszItem,
    char *pszDefault,
    char *pszResult,
    int  nSize,
    char *pszFileName)
{
    FILE *pIniFile;
    char szResult[0x100];
    char szGroup[0x100];
    char szItem[0x100];

    if( pszFileName == NULL || pszFileName[0] == 0)
        pszFileName = pszConfigFile;
    if( access(pszFileName, 04) != 0 ) {
        _searchenv(pszFileName, "PATH", szResult);
        if( szResult[0] == 0) strcpy(szResult, pszFileName);
    } else
        strcpy(szResult, pszFileName);
    if( (pIniFile = fopen( szResult, "rt" )) == NULL ) goto FINISH;

    strncpy(szGroup, pszGroup, sizeof(szGroup)-1);
    szGroup[sizeof(szGroup)] = 0;
    Trim(szGroup);
    while( 1 ) {
        int len;

    if( fgets( szResult, sizeof(szResult), pIniFile ) == NULL ) goto FINISH;
        if( (len = strlen(Trim(StripComment(szResult)))) < 2 ) continue;
        if( szResult[0]     != '[' ) continue;
        if( szResult[len-1] != ']' ) continue;
        memmove(szResult, &szResult[1], len-2);
        szResult[len-2] = 0;
        Trim( szResult );
        if( strnicmp( szGroup, szResult, strlen(szGroup)) == 0 ) break;
    }
    strncpy(szItem, pszItem, sizeof(szItem)-1);
    szItem[sizeof(szItem)] = 0;
    Trim(szItem);
    while( 1 ) {
        int  len;
        char *rc;

    if( fgets( szResult, sizeof(szResult), pIniFile ) == NULL ) goto FINISH;
        if( (len = strlen(Trim(StripComment(szResult)))) == 0 ) continue;
        if( szResult[0] == '[') goto FINISH; // start of other section reached
        rc = strchr( szResult, '=');
        if( rc != NULL ) len = rc - szResult;
        strncpy(szGroup, szResult, len);
        szGroup[len] = 0;
        Trim( szGroup );
        if( stricmp( szGroup, szItem ) == 0 ) {
            memmove(szResult, &szResult[len+1], strlen(&szResult[len+1])+1);
            Trim(szResult);
            break;
        }
    }
    fclose( pIniFile );
    strncpy(pszResult, szResult, nSize-1);
    pszResult[nSize-1] = 0;
    return strlen( pszResult );
FINISH:
    if( pIniFile != NULL ) fclose( pIniFile );
    strncpy(pszResult, pszDefault, nSize-1);
    pszResult[nSize-1] = 0;
    return strlen( pszResult );
}

int GetPrivateProfileInt(
    char *pszGroup,
    char *pszItem,
    int  nDefault,
    char *pszFileName)
{
    GetPrivateProfileString(pszGroup, pszItem, "", szResult, sizeof(szResult), pszFileName);
    if( szResult[0] == 0 )
        return nDefault;
    return atoi(szResult);
}