protected void ReadFile(IFileContainer file) { var raw_lines = file.ReadAllLines(); _lines = new List <item_type>(); if (_titles == null) { _titles = new string[0]; // If set in constructor. Don't overwrite it here. } foreach (var raw_line in raw_lines) { string comment; TeaboxDataLineType type; string[] data; var atts = typeof(item_type).GetCustomAttributes(typeof(TeaboxDataNoComments), true); // ToDo Test ParseLine(raw_line, (atts.Length == 1 && atts[0].GetType() == typeof(TeaboxDataNoComments)), out comment, out type, out data); if (type == TeaboxDataLineType.Titles && _titles.Length == 0) { _titles = new string[data.Length]; Array.Copy(data, _titles, data.Length); } var new_item = new item_type(); TeaboxDataLine.SetData(new_item, data); TeaboxDataLine.SetTitles(new_item, _titles); TeaboxDataLine.SetLineType(new_item, ref type); TeaboxDataLine.SetComment(new_item, ref comment); TeaboxDataLine.SetPropertiesFromData <item_type>(new_item); _lines.Add(new_item); } }
public static void SetTitles(TeaboxDataLine line, IList <string> titles) { if (titles.IsReadOnly) { line._titles = new List <string>(titles); } else { line._titles = titles; } }
public static void SetData(TeaboxDataLine line, IList <string> data) { if (data.IsReadOnly) { line._data = new List <string>(data); } else { line._data = data; } }
// ToDo Write tests for this public static void SetPropertiesFromData <line_type>(TeaboxDataLine line) where line_type : TeaboxDataLine { foreach (var prop in typeof(line_type).GetProperties()) { var atts = prop.GetCustomAttributes(typeof(TeaboxDataAttribute), true); if (atts.Length == 1 && atts[0].GetType() == typeof(TeaboxDataAttribute)) { if (prop.PropertyType == typeof(string)) { prop.SetValue(line, TeaboxDataLine.GetData(line, prop.Name)); } else if (prop.PropertyType == typeof(int)) { int v = 0; int.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v); prop.SetValue(line, v); } else if (prop.PropertyType == typeof(DateTime)) { DateTime v = DateTime.MinValue; DateTime.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v); prop.SetValue(line, v); } else if (prop.PropertyType == typeof(bool)) { bool v = false; bool.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v); prop.SetValue(line, v); } else if (prop.PropertyType == typeof(double)) { double v = 0; double.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v); prop.SetValue(line, v); } else if (prop.PropertyType == typeof(decimal)) // ToDo Test { decimal v = 0; decimal.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v); prop.SetValue(line, v); } else { throw new Exception("Property type not supported."); } } } }
// ToDo Write tests for this public static void SetDataFromProperties <line_type>(TeaboxDataLine line) where line_type : TeaboxDataLine { foreach (var prop in typeof(line_type).GetProperties()) { var atts = prop.GetCustomAttributes(typeof(TeaboxDataAttribute), true); if (atts.Length == 1 && atts[0].GetType() == typeof(TeaboxDataAttribute)) { var value_object = prop.GetValue(line); var value_string = ""; if (value_object != null) { value_string = value_object.ToString(); } TeaboxDataLine.SetData(line, prop.Name, value_string); } } }
// ToDo Possible to remove _unique_instance_id ? public static Guid GetUniqueInstanceId(TeaboxDataLine line) { return(line._unique_instance_id); }
public static void SetComment(TeaboxDataLine line, ref string comment) { line._comment = comment; }
public static void SetLineType(TeaboxDataLine line, ref TeaboxDataLineType type) { line._type = type; }
public static void SetData(TeaboxDataLine line, int index, string value) { line[index] = value; }
public static void SetData(TeaboxDataLine line, string column_name, string value) { line[column_name] = value; }
public static IList <string> GetData(TeaboxDataLine line) { return(line._data); }
public static IList <string> GetTitles(TeaboxDataLine line) { return(line._titles); }
public static string GetComment(TeaboxDataLine line) { return(line._comment); }
public static string GetData(TeaboxDataLine line, string column_name, string default_value) { return(line[column_name, default_value]);; }
public static string GetData(TeaboxDataLine line, int index, string default_value) { return(line[index, default_value]);; }
public static string GetData(TeaboxDataLine line, string column_name) { return(line[column_name]); }
public static string GetData(TeaboxDataLine line, int index) { return(line[index]); }
protected void WriteFile(IFileContainer file) { //if (_titles == null) // _titles = new string[0]; // If set in constructor. Don't overwrite it here. var raw_lines = new List <string>(); foreach (var line in _lines) { string comment = TeaboxDataLine.GetComment(line); var type = TeaboxDataLine.GetLineType(line); string raw_line = ""; if (type == TeaboxDataLineType.Titles) { raw_line += (TitleRowIdentifier + string.Join(DataDelimiter, TeaboxDataLine.GetData(line))); } else if (type == TeaboxDataLineType.Data) { TeaboxDataLine.SetDataFromProperties <item_type>(line); string line_data = ""; if (_titles == null || _titles.Length == 0) { var parts = TeaboxDataLine.GetData(line); for (int i = 0; i < parts.Count; i++) { if (i > 0) { line_data += DataDelimiter; } line_data += parts[i]; } } else { for (int i = 0; i < _titles.Length; i++) { if (i > 0) { line_data += DataDelimiter; } line_data += TeaboxDataLine.GetData(line, _titles[i]); } } raw_line += line_data; } if (!string.IsNullOrEmpty(comment)) { if (raw_line != "") { raw_line += " "; } raw_line += (CommentIdentifier + comment); } raw_lines.Add(raw_line); } file.WriteAllLines(raw_lines); }
public static TeaboxDataLineType GetLineType(TeaboxDataLine line) { return(line._type); }