/*
 * hattrick.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 *
 * Copyright: See COPYING file that comes with this distribution
 */

#include "utils.h"
#include <map>


int cUtils::html2text( std::string &output, std::string input )
{
    std::string html_input = input;

    std::map<std::string, std::string> html_to_replace;
    std::map<std::string, std::string>::iterator el;

    std::string text_output;
    std::string::size_type pos = 0, loc_end = 0;


    html_to_replace[ "<br>" ] = "\n";
    html_to_replace[ "&nbsp;" ] = " ";
    html_to_replace[ "</a>" ] = "";
    html_to_replace[ "/>" ] = "";
    html_to_replace[ "<br" ] = "";

    //HREF-links entfernen: (inklusive playerDetails.asp=098433)
    while( (  html_input.find( "<a", pos ) != std::string::npos ) && pos < html_input.length() )
    {
        pos = html_input.find( "<a", pos );
        loc_end = html_input.find(">", pos );
        html_input.erase( pos, loc_end - pos + 1 );
    }

    //HREF-links entfernen mit Grossbuchstaben <A HREF=
    while( (  html_input.find( "<A", pos ) != std::string::npos ) && pos < html_input.length() )
    {
        pos = html_input.find( "<A", pos );
        loc_end = html_input.find(">", pos );
        html_input.erase( pos, loc_end - pos + 1 );
    }

    //HTML-Code ersetzen:
    for (el = html_to_replace.begin(); el != html_to_replace.end(); ++el)
    {
        pos = 0;
        while( (  html_input.find( el->first, pos ) != std::string::npos ) && pos < html_input.length() )
        {
            pos = html_input.find( el->first, pos );
            html_input.replace( pos, el->first.size(), el->second );
        }
    }

    output = html_input;

    return 0;
}
