#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "../xmlparse.h"
#include "../global.h"
#include "shared_survey_string.h"
#include "shared_survey_record_html_parser.h"
#include "shared_lock.h"
#include "shared_cs_util.h"

char big_buffer[500];

static void 
StartElement(void *userData, const char *name, const char **atts) 
{
    RECORD_LIST *list = userData;
    int i = 0;

    if(strcmp(name, "record") == 0)
    {
       if(!list->head)
       {
	  list->head = (RECORD*)malloc(sizeof(RECORD));
	  list->current = list->head;
       }
       else
       {
          list->current->next = (RECORD*)malloc(sizeof(RECORD));
	  list->current = list->current->next;
       }
       list->current->time = malloc_str(atts[i+1]);
       list->current->course_list = 0;
       list->current->next = 0;
       list->current->head = 0;
	  
    }
    else if(strcmp(name, "result") == 0)
    {
	if(!list->current->head)
	{
	   list->current->head = (RESULT*)malloc(sizeof(RESULT));
	   list->current->current = list->current->head;
	}
	else
	{
	   list->current->current->next = (RESULT*)malloc(sizeof(RESULT));
	   list->current->current = list->current->current->next;
	}
        for(i = 0; atts[i]; i = i+2)
	{
          if(strcmp(atts[i], "name") == 0)
            list->current->current->name = malloc_str(atts[i+1]);
	  else if(strcmp(atts[i], "value") == 0)
	    list->current->current->value = malloc_str(atts[i + 1]);
	}
	list->current->current->next = 0;
    }
    memset(big_buffer, 0, strlen(big_buffer));
}





static void
EndElement(void *userData, const char *name)
{

    RECORD_LIST *list = userData;
    if(strcmp(name, "course_list") ==0)
	list->current->course_list = malloc_str(big_buffer);
    else if(strcmp(name, "expect_takers") ==0)
	list->expect_takers = atoi(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_record_html_list(RECORD_LIST *list)
{
    RESULT  *ptr;
    RECORD *rec_ptr;

    list->current = list->head;

    while(list->current)
    {
        rec_ptr = list->current;
	
	if(rec_ptr->time)
		free(rec_ptr->time);
	if(rec_ptr->course_list)
		free(rec_ptr->course_list);

	if(rec_ptr->head)
	{
	   rec_ptr->current = rec_ptr->head;
	   while(rec_ptr->current)
	   {
	       ptr = rec_ptr->current;
	       if(ptr->name)
		   free(ptr->name);
	       if(ptr->value)
		   free(ptr->value);
	       rec_ptr->current = rec_ptr->current->next;
	       free(ptr);
	   }
	}
	list->current = list->current->next;
	free(rec_ptr);
    }
   if(list)
      free(list);
}
    

	



void *
record_html_parser(char *filename)
{
    char *buf, *error_buf;
    RECORD_LIST *list;   /* shared_survey_record_html_parser.h */
    FILE *fp;
    struct stat file_stat;
    XML_Parser parser;
    
    /*initialize list */
    list = (RECORD_LIST *)malloc(sizeof(RECORD_LIST));
    list->head = 0;
    list->current = 0;
    list->expect_takers = 0; 


    if(stat(filename, &file_stat))
       cs_critical_error(ERR_STAT_FAILED, filename);
       
    buf = (char *)malloc(file_stat.st_size);
    if(!buf)
	cs_critical_error( ERR_MALLOC_FAILED, "in record_html_parser()");
    
    fp = fopen(filename, "r");
    if(!fp)
	cs_critical_error(ERR_FOPEN_READ_FAILED, filename);

    get_shared_lock(fileno(fp), 1);    /* shared_lock.c */
    if(fread(buf, file_stat.st_size, 1, fp) != 1)
	cs_critical_error(ERR_FREAD_FAILED, filename);
    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_XML_PARSER,  error_buf);
       free(error_buf);
    }
    XML_ParserFree(parser);
    free(buf);
    return list;
    
}
     

