public OptionList(GCSVTable csv, Func<IData, Options, string> getDefault, Func<string, Options> getList, Utilities.VariableBin var, Func<IData, bool> selector) { Var = var; Options = new List<Option>(); foreach (var line in csv) { if (!selector(line)) continue; var o = new Option(); o.Parent = this; o.Caption = line["caption"]; o.ID = line["id"]; o.Active = line["active"].ToBool(); var l = getList(line["list"]); o.Values = l.ToList(); o.Default = getDefault(line, o.Values); o.Initialize(); if (!var.Str.ContainsKey(o.ID)) var.Str[o.ID] = o.Default; if (var.Str.ContainsKey(o.ID)) o.ComboBox.SelectedIndex = o.Values.FindIndex(p => p.Key == var.Str[o.ID]); Options.Add(o); } }
public OptionList(GCSVTable csv, IGCSVCollection csvs, Utilities.VariableBin var, Func<IData,bool> selector) { Var = var; Options = new List<Option>(); foreach (var line in csv) { if (!selector(line)) continue; var o = new Option(); o.Parent = this; o.Caption = line["caption"]; o.ID = line["id"]; o.Active = line["active"].ToBool(); o.Default = line["default"]; var l = csvs[line["list"]]; o.Values = l.Where(m => !m.ContainsKey("active") || m["active"].ToBool()).Select(m => new KeyValuePair<string, string>(m[line["value"]], m[line["text"]])).ToList(); o.Initialize(); if (!var.Str.ContainsKey(o.ID) || !o.Active) // set defaults if variable is not set or option is not active var.Str[o.ID] = o.Default; if (var.Str.ContainsKey(o.ID)) o.ComboBox.SelectedIndex = o.Values.FindIndex(p => p.Key == var.Str[o.ID]); Options.Add(o); } }
void WriteToStream(StreamWriter writer, GCSVTable gcsv) { WriteHeaderLine(writer, gcsv); foreach (var line in gcsv) { writer.WriteLine(line.ToString(m_delimiter)); } }
private void StartGCSV() { m_headerLine = m_headerLine.Where(key => key != string.Empty).ToArray(); m_header = GCSVMain.CreateHeader(m_headerLine); m_gcsv = new GCSVTable(m_name, m_header); m_started = true; m_headerLine = null; m_name = null; }
public GCSVTable ReadGCSVFromLines(List<string[]> lines, ref int position) { m_gcsv = null; m_name = null; m_headerLine = null; m_started = false; for (; position < lines.Count; position++) { string[] line = lines[position]; // If this is the start of a gcsv if (line[0][0] == GCSVMain.InitialCharacter) { // If we had already started a gcsv, this must be the next one, // so we return the one we were filling up. if (m_started == true) return m_gcsv; m_name = line[0].Substring(1); // If the gcsv header is defined on the same line as the name // the line will have multiple delimited values and the second // value, which is the first field name in the header, will // not be empty. if (line.Length > 1 && !string.IsNullOrEmpty(line[1])) { m_headerLine = new string[line.Length - 1]; Array.Copy(line, 1, m_headerLine, 0, line.Length - 1); StartGCSV(); } continue; } // If we've started the gcsv, we can add this line to it. if (m_started) { // If the line is not long enough, create a new line // with empty strings to pad the end. if (line.Length != m_header.Length) { string[] newLine = new string[m_header.Length]; Array.Copy(line, newLine, Math.Min(newLine.Length, line.Length)); for (int i = line.Length; i < newLine.Length; i++) { newLine[i] = string.Empty; } line = newLine; } m_gcsv.Add(new GCSVLine(m_header, line)); } else { // If we've read the name of the gcsv, but not the header // then this line is the header. if (m_name != null) { m_headerLine = line; StartGCSV(); } } } GCSVTable result = m_gcsv; m_gcsv = null; return result; }
public static void WriteToFile(string path, GCSVTable gcsv) { WriteToFile(path, new[]{gcsv}); }
void WriteHeaderLine(StreamWriter writer, GCSVTable gcsv) { writer.WriteLine(GCSVMain.InitialCharacter + gcsv.Name + Environment.NewLine + gcsv.Header.Keys.Implode(m_delimiter)); }