/*
ECE4894 Project
Yannick Fortin, Murat Guler, and Jian Liu
A really horrible parser substitute since the antlr generated parser
was dificult to adapt to CE... this one isn't very smart, but it works.
*/
#ifndef _UNICODE
#define _UNICODE
#endif
#include "stdafx.h"
#include "resource.h"
#include <stdlib.h>
#include <tchar.h>
//#include <windows.h>
//#include <iostream>
//#include <stdio.h>
//#include <string.h>
#include "gui.h"
#define ANSI_CHAR 1
#ifdef _UNICODE
#define UNICODE_CHAR 2
#else
#define UNICODE_CHAR 1 //make this thing compatible with non-unicode for testing purposes
#endif
HANDLE sourceHandle, destHandle;
DWORD BytesWritten;
DWORD BytesRead;
TCHAR currentChar[1]; //our one-character buffer
int skipTag()
{
int keepTag = 0;
//check for A HREF start
if(!ReadFile(sourceHandle, currentChar, ANSI_CHAR, &BytesRead, NULL))
{
//std::cout << "Error reading char in skipTag\n";
//exit(1);
return 0;
}
if( (currentChar[0] == 'a') || (currentChar[0] == 'A') )
{
//might have an HREF, read another to check for blank
if(!ReadFile(sourceHandle, currentChar, ANSI_CHAR, &BytesRead, NULL))
{
//std::cout << "Error reading char in skipTag\n";
//exit(1);
return 0;
}
//found a blank, might be HREF, throw it in
if(currentChar[0] == ' ')
{
keepTag = 1;
if(!WriteFile(destHandle, currentChar, UNICODE_CHAR, &BytesWritten, NULL))
{
//std::cout << "error writing to destination file\n";
//exit(1);
return 0;
}
}
}
while (currentChar[0] != '>')
{
if(!ReadFile(sourceHandle, currentChar, ANSI_CHAR, &BytesRead, NULL))
{
//std::cout << "Error reading char in skipTag\n";
//exit(1);
return 0;
}
//write it out if we're keeping the tag in HREF cases
if(keepTag)
{
if(!WriteFile(destHandle, currentChar, UNICODE_CHAR, &BytesWritten, NULL))
{
//std::cout << "error writing to destination file\n";
//exit(1);
return 0;
}
}
}
return 1;
} //end skipTag()
int parse(LPTSTR sourceFile)
{
//open the file to "parse"
sourceHandle = CreateFile(sourceFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(sourceHandle == INVALID_HANDLE_VALUE)
{
//std::cout << "Error opening source\n";
//exit(1);
return 0;
}
//create the destination for the parsed data
destHandle = CreateFile(TEXT("output.txt"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(destHandle == INVALID_HANDLE_VALUE)
{
//std::cout << "Error creating destination\n";
//exit(1);
return 0;
}
do
{
if(!ReadFile(sourceHandle, currentChar, ANSI_CHAR, &BytesRead, NULL))
{
//std::cout << "Error reading char in parse\n";
//exit(1);
return 0;
}
else
{
// two cases:
// currentChar == "<"
if(currentChar[0] == '<')
{
if(!skipTag())
{
return 0;
}
}
// currentChar != "<"
else
{
if(!WriteFile(destHandle, currentChar, UNICODE_CHAR, &BytesWritten, NULL))
{
//std::cout << "error writing to destination file\n";
//exit(1);
return 0;
}
}
}
} while(BytesRead != 0);
//done, close the handles
if(!CloseHandle(sourceHandle))
{
//std::cout << "Error closing sourceHandle\n";
//exit(1);
return 0;
}
if(!CloseHandle(destHandle))
{
//std::cout << "Error closing destHandle\n";
//exit(1);
return 0;
}
return 1;
//std::cout << "All done... \n";
}//end parse()