private void InsertCharacters(IPuaCharacter[] puaDefinitions, string originalOverrides, string customOverrides)
{
// Open the file for reading and writing
LogFile.AddVerboseLine("StreamReader on <" + originalOverrides + ">");
using (var reader = new StreamReader(originalOverrides, Encoding.ASCII))
{
reader.Peek(); // force autodetection of encoding.
using (var writer = new StreamWriter(customOverrides, false, Encoding.ASCII))
{
try
{
// Insert the PUA via their codepoints
string line;
var lastCode = 0;
// Start looking at the first codepoint
var codeIndex = 0;
var newCode = Convert.ToInt32(puaDefinitions[codeIndex].CodePoint, 16);
// Used to find the type for casting ArrayLists to IPuaCharacter[]
//var factory = new PuaCharacterFactory();
//var puaCharForType = factory.Create("");
//var puaCharType = puaCharForType.GetType();
//While there is a line to be read in the file
while ((line = reader.ReadLine()) != null)
{
// skip entirely blank lines
if (line.Length <= 0)
continue;
if (line.StartsWith("Code") || line.StartsWith("block")) // header line or special instruction
{
writer.WriteLine(line);
continue;
}
//Grab codepoint
var strFileCode = line.Substring(0, line.IndexOf(';')).Trim(); // current code in file
var fileCode = Convert.ToInt32(strFileCode, 16);
// If the new codepoint is greater than the last one processed in the file, but
// less than or equal to the current codepoint in the file.
if (newCode > lastCode && newCode <= fileCode)
{
while (newCode <= fileCode)
{
LogCodepoint(puaDefinitions[codeIndex].CodePoint);
// Replace the line with the new PuaDefinition
writer.WriteLine("{0} #{1}", puaDefinitions[codeIndex], m_comment);
lastCode = newCode;
// Look for the next PUA codepoint that we wish to insert, we are done
// with this one If we are all done, push through the rest of the file.
if (++codeIndex >= puaDefinitions.Length)
{
// Write out the original top of the section if it hasn't been replaced.
if (fileCode != lastCode)
{
writer.WriteLine(line);
}
while ((line = reader.ReadLine()) != null)
writer.WriteLine(line);
break;
}
newCode = Convert.ToInt32(puaDefinitions[codeIndex].CodePoint, 16);
}
if (codeIndex >= puaDefinitions.Length)
break;
// Write out the original top of the section if it hasn't been replaced.
if (fileCode != lastCode)
{
writer.WriteLine(line);
}
}
//if it's not a first tag and the codepoints don't match
else
{
writer.WriteLine(line);
}
lastCode = fileCode;
}
// Output any codepoints after the old end
while (codeIndex < puaDefinitions.Length)
{
LogCodepoint(puaDefinitions[codeIndex].CodePoint);
// Add a line with the new PuaDefinition
writer.WriteLine("{0} #{1}", puaDefinitions[codeIndex], m_comment);
codeIndex++;
}
}
finally
{
writer.Flush();
writer.Close();
reader.Close();
}
}
}
}