X-Git-Url: http://git.pippins.net/embedvideo/.git/static/git-logo.png?a=blobdiff_plain;f=mls%2FwindowsGUI%2FMLSFileTrimmer%2FCSVTrimmer.cs;fp=mls%2FwindowsGUI%2FMLSFileTrimmer%2FCSVTrimmer.cs;h=9b39874ddda26b0229a475a2e611478b895a6528;hb=fd3afb11f083a1d2610997ee85c550ff67586314;hp=0000000000000000000000000000000000000000;hpb=5b6a77efbd41b615baf86ca2222e3b1c08fe106f;p=eq%2F.git diff --git a/mls/windowsGUI/MLSFileTrimmer/CSVTrimmer.cs b/mls/windowsGUI/MLSFileTrimmer/CSVTrimmer.cs new file mode 100644 index 0000000..9b39874 --- /dev/null +++ b/mls/windowsGUI/MLSFileTrimmer/CSVTrimmer.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +namespace MLSFileTrimmer +{ + public class CSVTrimmer + { + private String filename = String.Empty; + private Hashtable mlsFields = new Hashtable(); + + public CSVTrimmer(ThirdCouncelorMLSFilesMLSFile xmlData) + { + this.filename = xmlData.Name; + + foreach (String s in xmlData.MLSField) + { + this.mlsFields.Add(s, -1); + } + } + + public void ExtractData(StreamReader readFile, StreamWriter outFile) + { + string line; + string[] row; + + // get headers and create mapping + if ((line = readFile.ReadLine()) != null) + { + row = line.Split('"'); + for (int x = 0; x < row.Length; x++) + { + if (!(row[x].Equals(",")) && !(row[x].Equals(""))) + { + if (this.mlsFields.ContainsKey(row[x]) || this.mlsFields.ContainsKey("*")) + { + this.mlsFields[row[x]] = x; + } + } + } + } + + // write headers in new file + ICollection headers = this.mlsFields.Keys; + StringBuilder sb = new StringBuilder(); + foreach (string s in headers) + { + int index = Convert.ToInt32(this.mlsFields[s]); + if (index >= 0) + { + sb.Append("\""); + sb.Append(s); + sb.Append("\","); + } + } + //sb.Remove(sb.Length - 1, 1); // remove trailing comma + outFile.WriteLine(sb.ToString()); + + // only write specified fields in new file + while ((line = readFile.ReadLine()) != null) + { + sb.Remove(0, sb.Length); + row = line.Split('"'); + foreach (string s in headers) + { + int index = Convert.ToInt32(this.mlsFields[s]); + if (index >= 0) + { + sb.Append("\""); + sb.Append(row[index]); + sb.Append("\","); + } + } + //sb.Remove(sb.Length - 1, 1); // remove trailing comma + outFile.WriteLine(sb.ToString()); + } + } + } +}