#include <stdio.h>
#include "AttributeParser.h"
#include "Syslog.h"

void AttributeParser::parse(const char *filename, Attributes *attributes,
			    Boolean debug)
{
     char errorBuf[256];
     FILE *fp;
     
     if ((fp = fopen(filename, "r")) == 0) {
	  
	  sprintf(errorBuf, 
		  "Attributes::parse() - Couldn't open %s attributes file \"%s\"\n", 
		  attributes->objectName(), filename);
	  
	  Syslog::write("%s\n", errorBuf);
	  throw Exception(errorBuf);
     }
     

  Boolean error = False;
  parseComplex(fp, attributes, NULL, &error, errorBuf, debug );

  fclose(fp);

  if (error) {
    Syslog::write("%s\n", errorBuf);
//    attributes->printDescriptions();
    throw Exception(errorBuf);
  }

  if (attributes->verify() == -1) {
    // Missing attributes
    sprintf(errorBuf, "Missing %s attributes\n", attributes->objectName());
    Syslog::write("%s", errorBuf);
    throw Exception(errorBuf);
  }
}

void AttributeParser::parse(const char *filename, ItemList *itemList,
			    Boolean debug )
{

     char errorBuf[256];
     Boolean error = False;
     FILE *fp;

     if ((fp = fopen(filename, "r")) == 0) {
	  
	  sprintf(errorBuf, 
		  "Attributes::parse() - Couldn't open %s attributes file \"%s\"\n", 
		  itemList->identifier(), filename);
	  
	  Syslog::write("%s\n", errorBuf);
	  throw Exception(errorBuf);
     }

     dprintf("AttributeParser -- File opened.  Starting parser\n");

     parseComplex(fp, NULL, itemList, &error, errorBuf, debug );
     fclose(fp);

     // Perform attribute checking here
     if( error ) {
	  Syslog::write("%s\n", errorBuf );
	  throw( Exception(errorBuf) );
     }
}

void AttributeParser::parse(const char *filename, 
			    Attributes *attributes,
			    ItemList *itemList,
			    Boolean debug)
{
     char errorBuf[256];
     Boolean error = False;
     FILE *fp;

     dprintf("AttributeParser::parse -- Opening file %s",
	     filename );

     if ((fp = fopen(filename, "r")) == 0) {
	  
	  sprintf(errorBuf, 
		  "Attributes::parse() - Couldn't open %s attributes file \"%s\"\n", 
		  itemList->identifier(), filename);
	  
	  Syslog::write("%s\n", errorBuf);
	  throw Exception(errorBuf);
     }

     dprintf("AttributeParser::parse -- Starting parser");

     parseComplex(fp, attributes, itemList, &error, errorBuf, debug );

     fclose(fp);

     // Perform attribute checking here
     if( error ) {
	  Syslog::write("%s\n", errorBuf );
	  throw( Exception(errorBuf) );
     }

     dprintf("AttributeParser::parse -- Completed load of %s", filename );
}

