//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / pf
//
//File: fileparser.cpp
//
//Version: $Revision: 1.1 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/10/05 07:09:49 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Juhana Räsänen
//
//Description:
//      See corresponding header file
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 
//
//

#include "parser.h"
#include <ctype.h>
#include <string.h>	// strerror
#include <errno.h>	// errno
#include "pf/debug.h"

pfParser :: pfParser(const char *filename_)
    : _inputFile(0)
{
    _inputFile = fopen(filename_, "r");
    if (_inputFile == 0)
    {
        debugString("pfParser: Could not open file", filename_);
        debugString("fopen", strerror(errno));
    }
    return;
}


pfParser :: ~pfParser(void)
{
    if (_inputFile != 0)
    {
        fclose(_inputFile);
    }
    return;
}


int pfParser :: nextKeyword(
    char *keyword_,
    char *parameter_,
    const char *findThis_)
{
    int numChars = 0;
    keyword_[0] = '\0';
    parameter_[0] = '\0';

    if (_inputFile != 0)
    {
        char line[256];

        while (!feof(_inputFile))
        {
            eatWhite();
            fgets(line, 255, _inputFile);
            numChars = strlen(line);
            if ((line[numChars - 1] != '\n') && (numChars == 255))
            {
                debugUser("pfParser: Line too long!");
                numChars = 0;
                break;
            }
            else if ((line[0] != '#') && (numChars > 0))
            {
                line[numChars - 1] = '\0';
                char *keyword = line;
                char *parameter = findNextWord(line);
                if (parameter[0] != '\0')
                {
                    parameter--;
                    parameter[0] = '\0';
                    parameter++;
                }
                if ((findThis_ == 0) || (strcasecmp(keyword, findThis_) == 0))
                {
                    strcpy(keyword_, keyword);
                    strcpy(parameter_, parameter);
                    break;
                }
            }
        }
    }
    else
    {
        debugUser("pfParser: Can't read (no file)");
    }
    return numChars;
}


void pfParser :: rewind(void)
{
    if (_inputFile != 0)
    {
        ::rewind(_inputFile);
    }
    else
    {
        debugUser("pfParser: Can't rewind (no file)");
    }
    return;
}

void pfParser :: eatWhite(void)
{
    char c = 0;
    do
    {
        c = fgetc(_inputFile);
        if (isspace(c) == 0)
        {
            ungetc(c, _inputFile);
            c = EOF; // To terminate the loop
            break;
        }
    } while (c != EOF);
    return;
}

char *pfParser :: findNextWord(char *buffer_)
{
    while ((isspace(buffer_[0]) == 0) && (buffer_[0] != '\0'))
    {
        buffer_++;
    }
    while ((isspace(buffer_[0]) != 0)  && (buffer_[0] != '\0'))
    {
        buffer_++;
    }
    return buffer_;
}

