SIL.FieldWorks.UnicodeCharEditor.PUAInstaller.InsertCharacters C# (CSharp) Method

InsertCharacters() private method

Inserts the given PUADefinitions (any Unicode character) into the UnicodeData.txt file. This accounts for all the cases of inserting into the "first/last" blocks. That is, it will split the blocks into two or move the first and last tags to allow a codepoint to be inserted correctly. Also, this accounts for Hexadecimal strings that are within the unicode range, not just four digit unicode files. Assumptions made about the format The codepoints are in order There first last block will always have no space between the word first and the following ">" No other data entries contain the word first followed by a ">" There will always be a "last" on the line directly after a "first".
Pseudocode for inserting lines: if the unicodePoint is a first tag Get first and last uncodePoint range Stick into array all the xmlPoints that fit within the uncodePoint range Look at the next xmlPoint if there are any call WriteCodepointBlock subroutine else if the unicodePoint is greater than the last point but less than or equal to "the xmlPoint" insert the missing line or replace the line look at the next xmlPoint else do nothing except write the line
private InsertCharacters ( IPuaCharacter puaDefinitions, string originalOverrides, string customOverrides ) : void
puaDefinitions IPuaCharacter An array of PUADefinitions to insert into UnicodeDataOverrides.txt.
originalOverrides string original to merge into
customOverrides string where to write output
return void
		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();
					}
				}
			}
		}