//Called by btnSelectFileToLoad_Click
//splits up the filename from C:\folder\folder\2134.htm into several parts
//filename = 2134.htm
//filename without the ext. = 2134
//creates a directory on the server and copies all the images into it
//Opens an output file for writing of the entire study in html format
//opens and arraylist which html is loaded into
//transfers arraylist to string array
//searchs through string array to find <h1> tag when found calls writeH1file with args
// the string array
// the start position
// the ending position
// the current section
// the current line as a string
// the name of the file minus the ext
//when finished closes the fileStream for the full study
private void htmlFileCreater()
{
FileStream strm = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Define the regular expressions
Regex h1_open = new Regex("<h1>");
Regex h2_open = new Regex("<h2>");
Regex table_open = new Regex("<table");
Regex h3_open = new Regex("<h3>");
Regex close_body = new Regex("</body>");
//Define the matches
Match h1Open = h1_open.Match("NOMATCH");
Match h2Open = h2_open.Match("NOMATCH");
Match h3Open = h3_open.Match("NOMATCH");
Match tableOpen = table_open.Match("NOMATCH");
Match closeBody = close_body.Match("NOMATCH");
//Take the file stream and create a stream reader
StreamReader rdr = new StreamReader(strm);
//Set up a couple of string variables to hold some information we want
string currentLine;
string fileNameMinusDirInfo;
string fileNameMinusExtension;
//Split up the filename we passed in which is actually the filename as returned from the open file
//dialog (which also contians the path) - we only wnat the last part, the real filename
//and not the part containing any directory information
string[] currentFileInfo = fileName.Split(new char[] { '\\' });
//Stick that information into a string so it is easier to know what it is
// fileNameMinusDirInfo - lots esier than (currentFileInfo[currentFileInfo.Length - 1]) :)
fileNameMinusDirInfo = currentFileInfo[currentFileInfo.Length - 1];
//now from that file name get the part that doesn't have the extenxtion
string[] filenameParts = fileNameMinusDirInfo.Split(new char[] { '.' });
fileNameMinusExtension = filenameParts[0];
//open the output file using the fileNameMinusDirInfo variable we got
FileStream strmOut = new FileStream("W:\\" + fileNameMinusExtension.Substring(0,2) + "xx\\" + fileNameMinusDirInfo, FileMode.Create);
//Now open the output stream for writing
StreamWriter fullStudyFileWriter = new StreamWriter(strmOut);
//we are going to load the file into and arraylist and then after that transfer it
//to a standard string[] array
ArrayList htmlFileArrayList = new ArrayList();
//we don't declare the array till later cause we need to get the size AFTER we load the
//the arrayList.
int currentPosition = 0;
int h1startPosition = 0;
int h1endPosition = 0;
int sectionCounter = 0;
//before anything else try and create the directory and copy over the gif's for the charts
//- if that fails bail out...
try{
Directory.CreateDirectory("W:\\" + fileNameMinusExtension.Substring(0, 2) + "xx\\" + fileNameMinusExtension);
string dirString = "G:\\tfgfile\\INDSTUD\\" + fileNameMinusExtension.Substring(0, 2) + "xx\\" + fileNameMinusExtension;
//MessageBox.Show(dirString);
Directory.SetCurrentDirectory(dirString);
//MessageBox.Show(Directory.GetCurrentDirectory());
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
foreach (FileInfo file in dir.GetFiles())
{
//MessageBox.Show(file.Name);
if (file.Extension.EndsWith("gif") || file.Extension.EndsWith("jpg"))
{
string inFileName = dirString + "\\" + file.Name;
string outFileName = "W:\\" + fileNameMinusExtension.Substring(0, 2) + "xx\\" + fileNameMinusExtension + "\\" + file.Name;
int i;
FileStream fin;
FileStream fout;
fin = new FileStream(inFileName, FileMode.Open);
fout = new FileStream(outFileName, FileMode.Create);
try
{
do
{
i = fin.ReadByte();
if (i != -1) fout.WriteByte((byte)i);
} while (i != -1);
}
catch (IOException exc)
{
Console.WriteLine(exc.Message + "File Error");
}
fin.Close();
fout.Close();
}
}
//return;
}
catch(Exception ex)
{
MessageBox.Show("I couldn't create the directory or copy all of the images: " + fileNameMinusExtension + "\nCheck your 'W' drive and try agian.\nExiting\n" + ex.Message);
return;
}
//stuff the file into an array so we can process it.
while (!rdr.EndOfStream)
{
currentLine = rdr.ReadLine();
fullStudyFileWriter.WriteLine(currentLine);
htmlFileArrayList.Add(currentLine);
}
string[] data = new string[htmlFileArrayList.Count];
htmlFileArrayList.CopyTo(data);
for (int i = 0; i < data.Length; i++)
{
currentLine = data[i];
h1Open = h1_open.Match(currentLine);
closeBody = close_body.Match(currentLine);
if (h1Open.Success || closeBody.Success)
{
//MessageBox.Show(currentLine);
h1startPosition = h1endPosition;
h1endPosition = i;
if (h1startPosition != 0)
{
//get the title of the section and determin the number.
//in the future the sectionNumberDecider will decide ALL the section numbers
//but for most of them they are still undefined so we just move sequentially
//unless it need to be a 19 or 20
if (sectionNumberDecider(data[h1startPosition]) == 19)
{
sectionCounter = 19;
}
if (sectionNumberDecider(data[h1startPosition]) == 20)
{
sectionCounter = 20;
}
//MessageBox.Show("Section: " + sectionCounter + " Start: " + h1startPosition + " End: " + h1endPosition + " " + currentLine);
writeH1file(data, h1startPosition, h1endPosition, sectionCounter, data[h1startPosition], fileNameMinusExtension);
sectionCounter++;
}
}
//The worker is commented out for the moment as it wasn't working!
//worker.ReportProgress(currentPosition);
currentPosition++;
}
strm.Close();
strmOut.Close();
} //end of htmlFileCreator