private void UpdateUCDFile(List<IUcdCharacter> addToUCD, List<IUcdCharacter> removeFromUCD)
{
string ucdFilenameWithoutPath;
// Get the file name we want to modify.
if (addToUCD.Count == 0)
{
if (removeFromUCD.Count == 0)
// If we aren't supposed to change anything, we are done.
return;
ucdFilenameWithoutPath = (removeFromUCD[0]).FileName;
}
else
ucdFilenameWithoutPath = (addToUCD[0]).FileName;
// Create a temporary file to write to and backup the original
var unidataDir = Path.Combine(Path.Combine(IcuDir, "data"), "unidata");
var ucdFilename = Path.Combine(unidataDir, ucdFilenameWithoutPath);
var ucdFileTemp = CreateTempFile(ucdFilename);
// Add the temp file to a list of files to be deleted when we are done.
AddTempFile(ucdFileTemp);
//All the streams necessary to read and write for the Bidi text file
LogFile.AddVerboseLine("StreamReader on <" + ucdFilename + ">");
using (var reader = new StreamReader(ucdFilename, Encoding.ASCII))
{
//These 2 streams are used to allow us to pass through the file twice w/out writing to the hard disk
using (var stringWriter = new StringWriter())
{
//Writes out the final file (to a temp file that will be copied upon success)
using (var writer = new StreamWriter(ucdFileTemp, false, Encoding.ASCII))
{
//Does our first pass through of the file, removing necessary lines
ModifyUCDFile(reader, stringWriter, removeFromUCD, false);
using (var stringReader = new StringReader(stringWriter.ToString()))
{
// Does the second pass through the file, adding necessary lines
ModifyUCDFile(stringReader, writer, addToUCD, true);
// write file
writer.Flush();
}
}
}
}
// If we get this far without an exception, copy the file over the original
FileCopyWithLogging(ucdFileTemp, ucdFilename, true);
}