#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "../xmlparse.h"
#include "../global.h"
#include "../survey.h"
#include "shared_survey_string.h"
#include "shared_survey_util.h"
#include "shared_survey_info_list_parser.h"
#include "shared_lock.h"
#include "shared_cs_util.h"

char big_buffer[1000];
/*static SURVEY_INFO*
initialize_survey_info(SURVEY_INFO *survey_info)
{
      survey_info->title = '\0';
      survey_info->state = '\0';
      survey_info->create_time = '\0';
      survey_info->finish_time = '\0';
      survey_info->release_time = '\0';
      survey_info->close_time = '\0';
      survey_info->author_id = '\0';
      survey_info->author_name = '\0';
      survey_info->next =0;
      return survey_info;
}
*/


static void startElement(void *userData, const char *name, const char **atts)
{
    SURVEY_INFO_LIST *list = userData;
    int i;

    if(strcmp(name, SURVEY_TAG) == 0)
    {
       if(!list->head)
       {
	  list->head = (SURVEY_INFO*)malloc(sizeof(SURVEY_INFO));
	  list->head = initialize_survey_info(list->head);
	  list->current = list->head;
       }
       else
       {
	   list->current->next=(SURVEY_INFO*)malloc(sizeof(SURVEY_INFO));
	   list->current->next = initialize_survey_info(list->current->next);
	   list->current = list->current->next;

       }
    }
    if(strcmp(name,SURVEY_TAG ) == 0)
    {
       for(i = 0; atts[i]; i = i+2)
       {
	  if(strcmp(atts[i], SURVEY_ID_ATTR) == 0)
            list->current->id = malloc_str(atts[i +1]);
       }
    }
    else if(strcmp(name, SURVEY_AUTHOR_TAG) == 0)
    {
      for(i = 0; atts[i]; i = i+2)
      {
	if(strcmp(atts[i], SURVEY_ID_ATTR) == 0)
	   list->current->author_id = malloc_str(atts[i+1]);
      }
    }
    memset(big_buffer, 0, strlen(big_buffer));
}

static void endElement(void *userData, const char *name)
{
    SURVEY_INFO_LIST *list = userData;
    if(strcmp(name, SURVEY_TITLE_TAG) ==0)
       list->current->title = malloc_str(big_buffer);
    else if(strcmp(name, SURVEY_STATE_TAG) ==0)
       list->current->state = malloc_str(big_buffer);
    else if(strcmp(name, SURVEY_CREATE_TIME_TAG) ==0)
       list->current->create_time = malloc_str(big_buffer);
    else if(strcmp(name, SURVEY_FINISH_TIME_TAG)==0)
       list->current->finish_time = malloc_str(big_buffer);
    else if(strcmp(name, SURVEY_RELEASE_TIME_TAG)==0)
       list->current->release_time = malloc_str(big_buffer);
    else if(strcmp(name, SURVEY_CLOSE_TIME_TAG)==0)
       list->current->close_time = malloc_str(big_buffer);
    else if(strcmp(name, SURVEY_AUTHOR_NAME_TAG)==0)
       list->current->author_name = malloc_str(big_buffer);
    else if(strcmp(name, SURVEY_PATH_TAG)==0)
    {
       strtrim(big_buffer);
       list->current->survey_domain = malloc_str(big_buffer);
    }
    memset(big_buffer, 0, strlen(big_buffer));
}



static void startCharacter(void *userData, const char *text, int len)
{
	strncat(big_buffer, text, len);
}





void free_survey_info_list(SURVEY_INFO_LIST *list)
{
  SURVEY_INFO *ptr;
  list->current = list->head;
 
  while(list->current)
  {

     ptr = list->current;
     if(ptr->id)
       free(ptr->id);
     if(ptr->title)
       free(ptr->title);
     if(ptr->state)
       free(ptr->state);
     if(ptr->create_time)
       free(ptr->create_time);
     if(ptr->finish_time)
       free(ptr->finish_time);
     if(ptr->release_time)
       free(ptr->release_time);
     if(ptr->close_time)
       free(ptr->close_time);
     if(ptr->author_id)
       free(ptr->author_id);
     if(ptr->author_name)
       free(ptr->author_name);
     if(ptr->survey_domain)
       free(ptr->survey_domain);

     list->current = list->current->next;
     free(ptr);
  }
  free(list);
}



void *
survey_info_parser(char *filename)
{
   char *buf, *error_buf;
   SURVEY_INFO_LIST *list;
   FILE *fp;
   struct stat file_stat;
   XML_Parser parser;

    /*initilze list */
    list = (SURVEY_INFO_LIST *)malloc(sizeof(SURVEY_INFO_LIST));
    list->head = 0;
    list->current = 0;

    if(stat(filename, &file_stat))
	 return list;
    buf = (char*)malloc(file_stat.st_size);
    if(!buf)
      cs_critical_error( ERR_MALLOC_FAILED, "");

    fp = fopen(filename, "r");
    if(!fp)
       return list;
    get_shared_lock(fileno(fp), 1);
    if(fread(buf, file_stat.st_size, 1, fp) != 1)
       cs_critical_error(ERR_FREAD_FAILED,""); 
    release_lock(fileno(fp));
    fclose(fp);
    parser = XML_ParserCreate(NULL);
    XML_SetUserData(parser, list);
    XML_SetElementHandler(parser, startElement, endElement);
    XML_SetCharacterDataHandler(parser, startCharacter);
    if(!XML_Parse(parser, buf, file_stat.st_size, 1))
    {
      error_buf = malloc_str(XML_ErrorString(XML_GetErrorCode(parser)));
      cs_critical_error(  ERR_MALLOC_FAILED, "");
      free(error_buf);
    }
    XML_ParserFree(parser);
    free(buf);
    return list;

}

      




