/// <summary> /// Merges 2 2da objects, saving the results in a 2da file. The method expects that /// the source 2da will have at least enough rows to be contiguous with the merge /// 2da (the source 2da should be padded by calling Pad() if necessary). If the /// source and merge 2da's share some rows, the merge 2da rows will overwrite the /// source 2da rows. /// </summary> /// <param name="source">The source 2da</param> /// <param name="merge">The merge 2da</param> /// <param name="outFile">The name of the output 2da</param> public static void Merge2da(_2DA source, _2DA merge, string outFile) { using(StreamWriter writer = new StreamWriter(outFile, false)) { // Write the 2da header. writer.WriteLine(headerString); writer.WriteLine(); writer.WriteLine(source.Heading); // Make the column sizes in the source and 2da files to be the largest // of each file, to make the columns have the correct width. for (int i = 0; i < source.colSizes.Length; i++) { source.colSizes[i] = System.Math.Max(source.colSizes[i], merge.colSizes[i]); merge.colSizes[i] = source.colSizes[i]; } // output all of the source strings before our merge. for (int i = 0; i < merge.Offset; i++) { string s = source[i]; writer.WriteLine(s); } // Test all of the rows that the merge is about to overwrite to make sure they // are really empty. If any are not then generate a warning message for those // rows. int end = System.Math.Min(source.rows.Count, merge.Offset + merge.rows.Count); for (int i = merge.Offset; i < end; i++) if (!source.IsEmpty(i)) { //CMain.Warning("Overwriting non-empty row {0} in {1}", i, source.FileName); } // output all of the merge strings. for (int i = 0; i < merge.rows.Count; i++) { string s = merge[i]; writer.WriteLine(s); } // output any remaining source strings, in case the merge is in the middle. for (int i = merge.Offset + merge.rows.Count; i < source.rows.Count; i++) { string s = source[i]; writer.WriteLine(s); } writer.Flush(); writer.Close(); } }