#include <fstream>
using namespace std;

#include <stringconvert.h>
#include <stringserialization.h>

string htmlstring::lessthan = "&lt;";
string htmlstring::greaterthan = "&gt;";
string htmlstring::amp = "&amp;";
string htmlstring::quot = "&quot;";


boolc filestring::serializeInverse(stringc & filename, stringc & str)
{ 
  return deserialize(filename,str); 
}



boolc filestring::serialize(string & str, stringc & filename)
{
  str.clear();
  ifstream file(filename.c_str());
  if (!file)
    return false;

  char c;
  while (file.get(c))
    str.push_back(c);

  //assert(str.empty()==false);// Make sure the file is not empty. This may be unreasonable.

  return true;
}

boolc filestring::deserialize
(
  stringc & filename, 
  stringc & str
)
{
  ofstream file(filename.c_str());
  if (!file)
    return false;

  file << str;

  return true;
}


boolc htmlstring::serialize(string & str, stringc & html )
{
  str=html;
  string& s2(str);

  stringconvert::findandreplace(s2,lessthan,"<");
  stringconvert::findandreplace(s2,greaterthan,">");
  stringconvert::findandreplace(s2,amp,"&");
  stringconvert::findandreplace(s2,quot,"\"");

  return true;
}

boolc htmlstring::serializeInverse(string & html, stringc & str )
{
  html = str; 
  stringconvert::findandreplace(html,"&",amp);
  stringconvert::findandreplace(html,"<",lessthan);
  stringconvert::findandreplace(html,">",greaterthan);
  stringconvert::findandreplace(html,"\"",quot);

  return true;
}


boolc htmlstring::ishtml(stringc & str)
{
  string s2(str);
  string find;

  find=lessthan;

  vector<string> vs;
  vs.push_back(lessthan);
  vs.push_back(greaterthan);
  vs.push_back(amp);
  vs.push_back(quot);

  for (uint i=0; i<vs.size(); ++i)
  {
    find=vs[i];
    if (s2.find(find) != string::npos)
      return true;
  }

  return false;
}



