/********************************************************************************* *Author: OnClick *Version: 0.0.1 *UnityVersion: 2018.3.11f1 *Date: 2019-09-08 *Description: IFramework *History: 2018.11-- *********************************************************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IFramework.Serialization.DataTable { /// /// 数据行 /// public class DataRow : IDataRow { /// /// 读取一行 /// /// 行String /// 标题行 /// public List ReadLine(string val, List headNames) { List strVals = SpilitRow(val); if (strVals.Count != headNames.Count) throw new Exception("Read Err Count Is different"); List cols = new List(); for (int i = 0; i < headNames.Count; i++) { cols.Add(new DataColumn() { value = strVals[i], headNameForRead = headNames[i] }); } return cols; } /// /// 切割一行 /// /// /// protected virtual List SpilitRow(string val) { var list = val.Split(',').ToList(); if (string.IsNullOrEmpty(list.Last())) { list.RemoveAt(list.Count - 1); } return list; } /// /// 读取标题行 /// /// 行String /// public virtual List ReadHeadLine(string val) { List headNames = val.Split(',').ToList(); if (string.IsNullOrEmpty(headNames.Last())) { headNames.RemoveAt(headNames.Count - 1); } return headNames; } /// /// 写入一行 /// /// 写入的信息 /// public virtual void WriteLine(List cols, StringBuilder builder) { for (int i = 0; i < cols.Count; i++) { var index = i; var c = cols[index]; string val = c.value; val = val.Replace("\"", "\"\""); if (val.Contains(StringConvert.dot) || val.Contains("\"") || val.Contains('\r') || val.Contains('\n')) val = string.Format("\"{0}\"", val); builder.Append(val); builder.Append(StringConvert.dot.ToString()); } } /// /// 写入标题行 /// /// 写入的标题 /// public virtual void WriteHeadLine(List headNames, StringBuilder builder) { for (int i = 0; i < headNames.Count; i++) { var index = i; var val = headNames[index]; val = val.Replace("\"", "\"\""); if (val.Contains(StringConvert.dot) || val.Contains("\"") || val.Contains('\r') || val.Contains('\n')) val = string.Format("\"{0}\"", val); builder.Append(val); builder.Append(StringConvert.dot.ToString()); } } } }