#include "shared_bb_manifest.h"

char big_buffer[100];

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

    if(strcmp(name, "resource") == 0)
    {
        for(i = 0; atts[i]; i = i+2)
	{
          if(strcmp(atts[i], "baseurl") == 0)
             strncpy(big_buffer, atts[i+1], 100);
	}
    }
}


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

}

static void startCharacter(void *userData, const char *text, int len)
{
}
	
void *
get_bb_image_base(char *filename)
{
    char *buf, *error_buf;
    char *base;
    FILE *fp;
    struct stat file_stat;
    XML_Parser parser;
    
    if(stat(filename, &file_stat))
       cs_critical_error(ERR_FOPEN_READ_FAILED, "");
    buf = (char*)malloc(file_stat.st_size);
    if(!buf)
	cs_critical_error( ERR_MALLOC_FAILED, "");
    
    fp = fopen(filename, "r");
    if(!fp)
	cs_critical_error(ERR_FOPEN_READ_FAILED, "");
    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_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);
    base = malloc_str(big_buffer);
    return base;
    
}
     

