home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
HTMLXLAT.CMM
< prev
next >
Wrap
Text File
|
1995-12-13
|
5KB
|
195 lines
// HTMLxlat.cmm - CEnvi tool to convert an HTML document into
// ver.1 the same document as produced by CMMCGI
// using the TINYHTML.LIB library.
Instructions()
{
puts("\nHTMLxlat - Translate HTML document into form creatable with CMMCGI\a")
puts("")
puts("USAGE: CEnvi HTMLxlat.cmm <HTMLinput> <cmmOutput>")
puts("WHERE: HTMLinput - Name of existing HTML document")
puts(" cmmOutput - Path for new Cmm document")
puts("")
puts("EXAMPLE: CEnviW HTMLxlat.cmm HANGMAN.HTM HANGMAN.CMM")
puts("")
exit(EXIT_FAILURE);
}
Error(FormatS)
{
va_start(valist,FormatS);
vprintf(FormatS,valist);
va_end(valist);
printf("\a\n");
exit(EXIT_FAILURE);
}
main(argc,argv)
{
if ( 3 != argc )
Instructions();
source = ReadInputFile(argv[1]);
WriteOutputFile(argv[2],source);
return EXIT_SUCCESS;
}
ReadInputFile(FileSpec) // read input into huge buffer
{
if ( !(fp = fopen(FileSpec,"rb")) )
Error("Could not read input file \"%s\"",FileSpec);
fseek(fp,0,SEEK_END);
len = ftell(fp);
fseek(fp,0,SEEK_SET);
fread(ret,len,fp);
fclose(fp);
return ret;
}
WriteOutputFile(FileSpec,src)
{
if ( !(fp = fopen(FileSpec,"wt")) )
Error("Could not create output file \"%s\"",FileSpec);
// always start with the same lines
fprintf(fp,"//%s - CMMCGI output file created by HTMLXLAT\n\n",FileSpec);
fprintf(fp,"#include <tinyhtml.lib>\n\n");
// read in source up to <HTML>
if ( !(html = strstr(src,"<HTML")) )
Error("Couldn't find beginning \"<HTML\" tag in %s.",FileSpec);
// write plain text beginning before the HTML section
fprintf(fp,"CGIOut(\"");
WriteDoubleQuotedText(fp,src,html-src);
fprintf(fp,"\");\n\n");
// start the CGIOut() statement
fprintf(fp,"CGIOut(\n");
// start with this tag, and all that follow
WriteTag(fp,1,html);
// finally end the CGIOut() statement
fprintf(fp,"\n);");
fclose(fp);
}
SkipWhiteSpace(text) // remove whitespace from beginning and end
{
while ( text[0] && strchr(" \r\n\t",text[0]) )
text++;
// remove whitespace from end
len = strlen(text);
while( len && strchr(" \r\n\t",text[len-1]) )
len--;
text[len] = 0;
}
EscapeChars = "\\\r\n\t\'\"\a\b";
EscapeXlat = `\\\r\n\t\'\"\a\b`;
IndentLevel(fp,level)
{
fprintf(fp,"%*s",4*level,"");
}
WriteDoubleQuotedText(fp,src,len) // output each character, in escape-sequence if needed
{
memcpy(s,src,len);
do {
seq = s + strcspn(s,EscapeChars);
fwrite(s,seq-s,fp);
if ( seq[0] ) {
// write out esapce sequence
fprintf(fp,"\\%c",EscapeXlat[1 + 2 * (strchr(EscapeChars,seq[0])-EscapeChars)]);
s = seq + 1;
}
} while ( seq[0] );
}
WriteTextString(fp,text,len)
{
memcpy(s,text,len);
if ( !strchr(s,'`') )
fprintf(fp,"`%s`",s);
else {
fprintf(fp,"\"");
WriteDoubleQuotedText(fp,s,len);
fprintf(fp,"\"");
}
}
WriteTag(fp,level,src)
{
assert( '<' == (s = src)[0] );
s++;
// get the name of this tag
len = strcspn(s," >");
end = s[len];
if ( len == 0 || !strchr(" >",end) )
Error("No tag found at \"%.30s\"",src);
memcpy(TagName,s,len);
IndentLevel(fp,level);
// find the end tag
sprintf(EndTagTemplate,"</%s>",TagName);
EndTag = strstr(s,EndTagTemplate);
// if not end tag and no attributes, just print the text
if ( !EndTag && end == '>' ) {
fprintf(fp,"\"<%s>\"",TagName);
src = s + len + 1 ;
} else {
// this tag has attributes and/or an EndTag
fprintf(fp,"tag(\"%s\"",TagName);
if ( end == '>' ) {
// no attributes
fprintf(fp,EndTag ? ",NULL" : ")" );
s += len + 1;
} else {
// append the attributes
EndBrace = strchr(s+len,'>');
if ( !EndBrace )
Error("No end for tag at \"%.30s\"",src);
fprintf(fp,",");
WriteTextString(fp,s+len+1,EndBrace-(s+len+1));
if ( !EndTag )
fprintf(fp,")");
s = EndBrace + 1;
}
if ( !EndTag ) {
src = s;
} else {
src = EndTag + strlen(EndTagTemplate);
EndTag[0] = 0;
// append each line read
SkipWhiteSpace(s);
while( s[0] ) {
if ( s[0] == '<' ) {
fprintf(fp,",\n");
WriteTag(fp,level+1,s);
} else {
// write plain text
len = strcspn(s,"\r\n<");
memcpy(text,s,len);
s += len;
SkipWhiteSpace(text);
if ( text[0] ) {
fprintf(fp,",\n");
IndentLevel(fp,level+1);
WriteTextString(fp,text,len);
}
}
SkipWhiteSpace(s);
}
fprintf(fp,"\n");
IndentLevel(fp,level);
fprintf(fp,")");
}
}
SkipWhiteSpace(src);
}