using System; using System.Collections; namespace Sog { public class TabTextFile { private string FileName; private string[] Titles; private string[] Types; private ArrayList Body; public int CurrentLine { get; private set; } public TabTextFile(string strFileName, string strContent) { FileName = strFileName; string[] content = strContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); //头2行分别是字段名字和类型 Titles = content[0].Split(new char[] { '\t' }); Types = content[1].Split(new char[] { '\t' }); Body = new ArrayList(); for (int i = 2; i < content.Length; i++) { string[] line = content[i].Split(new char[] { '\t' }); Body.Add(line); } Body.TrimToSize(); Begin(); } public void Begin() { CurrentLine = -1; } public bool Next() { CurrentLine++; if (CurrentLine >= Body.Count) return false; return true; } public void SetCurrentLine(int currentLine) { CurrentLine = currentLine; } public int TotalLine() { return Body.Count; } public T Get(string strColName) { int nColIndex = Array.IndexOf(Titles, strColName); if (nColIndex < 0) { TraceLog.Error("TabFile: read data error, file:{0}, colName:{1}", FileName, strColName); return default(T); } return this.Get(nColIndex); } public T Get(int nColIndex) { string strValue = this.getValueString(nColIndex); Type t = typeof(T); try { return (T)Convert.ChangeType(strValue, t); } catch (Exception) { TraceLog.Error("TabFile: {0} Wrong Format: {1}:{2}", FileName, nColIndex, CurrentLine); } return default(T); } private string getValueString(int nColIndex) { string[] line = (string[])Body[CurrentLine]; if (nColIndex < 0 || nColIndex >= line.Length) { TraceLog.Error("TabFile:" + FileName + " Wrong ColIndex: " + nColIndex + " At Line: " + CurrentLine); return null; } string value = line[nColIndex]; return value.Replace("\\n","\n"); //return line[nColIndex]; } public string GetString(string strColName) { int nColIndex = Array.IndexOf(Titles, strColName); return getValueString(nColIndex); } public int GetInt32(string strColName) { return GetInt32(Array.IndexOf(Titles, strColName)); } public int GetInt32(int nColIndex) { try { return Convert.ToInt32(getValueString(nColIndex)); } catch (Exception) { TraceLog.Error("TabFile: " + FileName + "Wrong Format: " + nColIndex + " : " + CurrentLine); return 0; } } public uint GetUInt32(string strColName) { return GetUInt32(Array.IndexOf(Titles, strColName)); } public uint GetUInt32(int nColIndex) { try { return Convert.ToUInt32(getValueString(nColIndex)); } catch (Exception) { TraceLog.Error("TabFile: " + FileName + "Wrong Format: " + nColIndex + " : " + CurrentLine); return 0; } } public double GetDouble(string strColName) { return GetDouble(Array.IndexOf(Titles, strColName)); } public double GetDouble(int nColIndex) { try { return Convert.ToDouble(getValueString(nColIndex)); } catch (Exception) { TraceLog.Error("TabFile: " + FileName + "Wrong Format: " + nColIndex + " : " + CurrentLine); return 0; } } public float GetFloat(string strColName) { return (float)GetDouble(strColName); } public float GetFloat(int nColIndex) { return (float)GetDouble(nColIndex); } } }