/*--------------------------------------------------------------------------
	Configuration file management.
	The configuration file has one entry:
	name = value 
	per line
	


	Mark Koennecke,    October 1996

	Copyright:

	Labor fuer Neutronenstreuung
	Paul Scherrer Institut
	CH-5423 Villigen-PSI


        The authors hereby grant permission to use, copy, modify, distribute,
        and license this software and its documentation for any purpose, provided
        that existing copyright notices are retained in all copies and that this
        notice is included verbatim in any distributions. No written agreement,
        license, or royalty fee is required for any of the authorized uses.
        Modifications to this software may be copyrighted by their authors
        and need not follow the licensing terms described here, provided that
        the new terms are clearly indicated on the first page of each file where
        they apply.

        IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
        FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
        ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
        DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
        POSSIBILITY OF SUCH DAMAGE.

        THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
        IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
        NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
        MODIFICATIONS.
----------------------------------------------------------------------------*/
#include "fortify.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include "ifile.h"

/*====================== Locals ============================================*/

  static IPair *CreateNewEntry(char *name, char *val, IPair *pN)
  {
     IPair *pRes;
     
     pRes = (IPair *)malloc(sizeof(IPair));
     if(!pRes)
         return NULL;
     if(name)
        pRes->name = strdup(name);
     if(val)
     	pRes->value = strdup(val);
     pRes->pNext = pN;
     return pRes;
  }
/*--------------------------------------------------------------------------*/
 static void RemoveWhiteSpace(char *pText)
 {
    int i, ii, i3;
    char *pPtr;
      
    assert(pText);
    
    /* find start */
    i = 0;
    while(isspace(pText[i]))
    {
      i++;
    }     
    
    /* find end */
    ii = strlen(pText);
    ii--;
    while( (isspace(pText[ii])) || (pText[ii] == '\n') )
    {
      ii--;
    }    
    
    /* copy it */
    pPtr = pText;
    for(i3 = i; i3 < (ii+1); i3++)
    {
      *pPtr = pText[i3];
      pPtr++;
    }
    *pPtr = '\0';
 }
/*===========================================================================*/
  IPair *IFReadConfigFile(FILE *fd)
  {
     IPair *pList = NULL;
     char pBueffel[256];
     char pName[132];
     char pValue[132];
     char *pPos;
     int iLen;
     
     assert(fd);
     
     while(!feof(fd))
     {
        fgets(pBueffel,255,fd);
        if(feof(fd))
          continue;

        pPos = strchr(pBueffel,'=');
        if(!pPos)
           continue;
        if(pBueffel[0] == '#')
           continue;
        
        iLen = pPos - pBueffel;
        strncpy(pName,pBueffel,iLen);
        pName[iLen] = '\0';
        strcpy(pValue,(pPos+1));
        RemoveWhiteSpace(pName);
        RemoveWhiteSpace(pValue);
        pList = CreateNewEntry(pName,pValue,pList);
        if(!pList)
           return NULL;
     }
     return pList;
  }
/*--------------------------------------------------------------------------*/
  char *IFindOption(IPair *pList, char *name)
  {
     IPair *pCurrent;
  
     if(!pList)
     {
       return NULL;
     }
     
     pCurrent = pList;
     while(pCurrent)
     {
        if(strcmp(name,pCurrent->name) == 0)
           return pCurrent->value;
        pCurrent = pCurrent->pNext;   
     }
     return NULL;
  }  
/*--------------------------------------------------------------------------*/
  IPair *IFAddOption(IPair *pList,char *name, char *value)
  {
  	return CreateNewEntry(name,value,pList);
  }
/*-------------------------------------------------------------------------*/

 int IFSaveOptions(IPair *pList,FILE *fd)
 {
    IPair *pCurrent;
    
    assert(fd);
    assert(pList);
    
    pCurrent = pList;
    while(pCurrent)
    {
       fprintf(fd,"%s = %s \n", pCurrent->name, pCurrent->value);
       pCurrent = pCurrent->pNext;
    }
    return 1;
 }
/*--------------------------------------------------------------------------*/

  void IFDeleteOptions(IPair *pList)
  {
    IPair *pCurrent, *pTemp;
    
    pCurrent = pList;
    while(pCurrent)
    {
      pTemp = pCurrent->pNext;
      if(pCurrent->name)
      {
         free(pCurrent->name);
      }
      if(pCurrent->value)
      {
         free(pCurrent->value);
      }
      free(pCurrent);
      pCurrent = pTemp;
    }
  }
/*===========================================================================
	Testcode: Define MAIN to activate it. Needs a file test.txt
	as input
*/

#ifdef MAIN
  int main(int argc, char *argv[])
  {
     FILE *fd;
     IPair *pList = NULL;
     char *pPos = NULL;
     
     fd = fopen("test.txt","r");
     if(!fd)
        exit(2);
     
        
     pList = IFReadConfigFile(fd);
     if(!pList)
       exit(2);
     fclose(fd);
       
     pPos = IFindOption(pList,"Gabi");
     if(pPos)
             puts(pPos);
     
     fd = fopen("lala.txt","w");
     IFSaveOptions(pList,fd);
     IFDeleteOptions(pList);     
     
  }
#endif

