// HTML_ColorerPage.cpp: implementation of the HTML_ColorerPage class.
//
//////////////////////////////////////////////////////////////////////
#include "../HTML/HTML_all.h"
#include "HTML_ColorerPage.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
HTML_ColorerPage::HTML_ColorerPage()
{
isHTML = false;
readLinksFrom("colorer_links.cfg");
}
HTML_ColorerPage::~HTML_ColorerPage()
{
freeKeyedValueArray(&instLinks);
freeKeyedValueArray(&dirLinks);
}
HTML_ColorerPage*
HTML_ColorerPage::setInput(char* x)
{
if(strlen(x) > 65536)
{
x[65536] = '\0';
error("Input code truncated to 64 kbytes");
}
input = x;
return(this);
}
HTML_ColorerPage*
HTML_ColorerPage::setCsspath(char* x)
{
if(strlen(x) > 256)
{
x[256] = '\0';
error("Style sheet path truncated to 256 bytes");
}
csspath = x; //write to global variable
return(this);
}
HTML_ColorerPage*
HTML_ColorerPage::setTabs(int x)
{
fillTabs = x; //write to global variable
return(this);
}
HTML_ColorerPage*
HTML_ColorerPage::setLinks(char* x)
{
if(x == NULL)
{
linkspath = NULL; //global
return(this);
}
if(strlen(x) > 20480)
{
x[20480] = '\0';
error("Links truncated to 20 kbytes");
}
links = x;
freeKeyedValueArray(&instLinks); //global
freeKeyedValueArray(&dirLinks); //global
//dummy name, just to make colorer.c think links are used
linkspath = "links.cfg"; //global
// parse links to global variables
if(readLinksString(links.c_str()) != 0)
{
freeKeyedValueArray(&instLinks); //global
freeKeyedValueArray(&dirLinks); //global
linkspath = NULL; //global
return(this);
}
return(this);
}
HTML_ColorerPage*
HTML_ColorerPage::setIsHTML(bool x)
{
isHTML = x;
return(this);
}
const char*
HTML_ColorerPage::getInput()
{
return(input.c_str());
}
const char*
HTML_ColorerPage::getCsspath()
{
return(csspath); //global
}
const int
HTML_ColorerPage::getTabs()
{
return(fillTabs); //global
}
const char*
HTML_ColorerPage::getLinks()
{
return(links.c_str());
}
const bool
HTML_ColorerPage::getIsHTML()
{
return(isHTML);
}
HTML_ColorerPage*
HTML_ColorerPage::generatePage()
{
//create general things
doc->setTitle("MPASM/SASM/SXKEY Code HTML Colorer");
doc->getHead()
->writeRaw("");
doc->newTag("H1")
->write("MPASM/SASM/SXKEY Code HTML Colorer");
form = doc->newForm();
form
->setAction("/cgi-bin/colorer.exe")
->setMethod("post");
// ->setMethod("get");
table = form->newTable();
cell = table->newRow()->newCell();
cell->setAlign("center");
table = cell->newTable();
table
->setWidth("30%")
->setCellspacing("3")
->setCellpadding("3")
->setBgcolor("cornsilk");
errorcell = table->newRow()->newCell();
errorcell->setColspan("2");
return(this);
}
HTML_ColorerPage*
HTML_ColorerPage::error(char* x)
{
errorList.append("\n");
errorList.append(x);
return(this);
}
HTML_ColorerPage*
HTML_ColorerPage::printErrors()
{
if(errorList.size() != 0)
{
errorcell
->setBgcolor("lightyellow")
->newFont()->setColor("red")->bold()
->newTag("PRE")->write(errorList.c_str());
}
return(this);
}
char
HTML_ColorerPage::readLinksString(const char* links)
{
int state = 0;
int i = 0;
keyedValueArray* arr;
char key[256];
char value[256];
char cssclass[256];
char result = 0;
char ch;
int pos = 0;
int size = strlen(links);
while(pos <= size)
{
// states:
// 0 - looking for ->Class
// 1 - reading Class
// 2 - looking for key
// 3 - reading key
// 4 - looking for value
// 5 - reading value
//
if(pos < size)
{
ch = links[pos];
}
else
{
ch = ' ';
}
switch(state)
{
case 0: //looking for .class
if(ch == '.')
{
state = 1;
i = 0;
}
else if(!isspace(ch))
{
error("Links rejected> Class name should start from dot '.'");
return(1);
}
break;
case 1: //reading class name
if(isalpha(ch))
{
cssclass[i++] = ch;
}
else if(isspace(ch))
{
cssclass[i] = '\0';
i = 0;
if(_stricmp(cssclass, "Inst") == 0)
{
arr = &instLinks;
}
else if(_stricmp(cssclass, "Dir") == 0)
{
arr = &dirLinks;
}
else
{
error("Links rejected> Can't recognize .class name");
return(1);
}
state = 2;
}
else
{
error("Links rejected> Class name should be separated by a whitespace");
return(1);
}
break;
case 2: //looking for key
if(isalpha(ch) || ch == '$' || ch == '#')
{
key[i++] = ch;
state = 3;
}
else if(i == 0 && ch == '.')
{
state = 1;
}
else if(!isspace(ch))
{
error("Links rejected> A keyword should start from a letter, $, or #");
return(1);
}
break;
case 3: //reading key
if(isalpha(ch)
|| ch == ','
|| ch == '-'
|| ch == '+'
|| ch == '/'
|| ch == '>'
|| ch == '<'
|| ch == '!'
|| ch == '#'
|| ch == '_'
/*|| ch == ';' //> <
|| ch == '&'*/)
{
if(i <= 250 && ch == '>')
{
key[i++] = '&';
key[i++] = 'g';
key[i++] = 't';
key[i++] = ';';
}
else if(i <= 250 && ch == '<')
{
key[i++] = '&';
key[i++] = 'l';
key[i++] = 't';
key[i++] = ';';
}
else
{
key[i++] = ch;
}
}
else if(ch == ':')
{
key[i] = '\0';
i = 0;
state = 4;
}
else if(!isspace(ch))
{
error("Links rejected> A keyword should be separated by a colon");
return(1);
}
break;
case 4: //looking for a value
if(!isspace(ch))
{
value[i++] = ch;
state = 5;
}
break;
case 5: //reading a value
if(!isspace(ch))
{
value[i++] = ch;
}
else
{
value[i] = '\0';
i = 0;
state = 2;
//add key-value
addKeyAndValue(arr, key, value);
}
break;
default:
break;
}
if(i >= 255)
{
error("Links rejected> String can be maximum 256 characters long");
return(1);
}
pos++;
}
return(0);
}
int
HTML_ColorerPage::readLinksFrom(char* linkspath)
{
FILE* fl;
char ch[2] = " ";
if(linkspath != NULL)
{
fl = fopen(linkspath, "r");
if(fl == NULL)
{
return(1);
}
linkspath = "dummy"; //links exist
links = "";
while(fread(ch, 1, 1, fl) == 1)
{
links.append(ch);
}
fclose(fl);
}
return(0);
}