1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System.Collections.Generic;
24: using System.Text;
25: using System.Text.RegularExpressions;
26: #endregion
27:
28: namespace Utilities.FileFormats.Delimited
29: {
30: /// <summary>
31: /// Individual row within a delimited file
32: /// </summary>
33: public class Row
34: {
35: #region Constructors
36:
37: /// <summary>
38: /// Constructor
39: /// </summary>
40: public Row()
41: {
42: }
43:
44: /// <summary>
45: /// Constructor
46: /// </summary>
47: /// <param name="Content">Content of the row</param>
48: /// <param name="Delimiter">Delimiter to parse the individual cells</param>
49: public Row(string Content, string Delimiter)
50: {
51: Regex TempSplitter = new Regex(string.Format("(?<Value>\"(?:[^\"]|\"\")*\"|[^{0}\r\n]*?)(?<Delimiter>{0}|\r\n|\n|$)", Delimiter));
52: MatchCollection Matches = TempSplitter.Matches(Content);
53: bool Finished = false;
54: foreach (Match Match in Matches)
55: {
56: if (!Finished)
57: {
58: Cells.Add(new Cell(Match.Groups["Value"].Value));
59: }
60: Finished = string.IsNullOrEmpty(Match.Groups["Delimiter"].Value) || Match.Groups["Delimiter"].Value == "\r\n" || Match.Groups["Delimiter"].Value == "\n";
61: }
62: }
63:
64: #endregion
65:
66: #region Public Properties
67:
68: private List<Cell> _Cells = new List<Cell>();
69:
70: /// <summary>
71: /// Cells within the row
72: /// </summary>
73: public List<Cell> Cells
74: {
75: get { return _Cells; }
76: set { _Cells = value; }
77: }
78:
79: /// <summary>
80: /// Returns a cell within the row
81: /// </summary>
82: /// <param name="Position">The position of the cell</param>
83: /// <returns>The specified cell</returns>
84: public Cell this[int Position]
85: {
86: get { return _Cells[Position]; }
87: set { _Cells[Position] = value; }
88: }
89:
90: /// <summary>
91: /// Number of cells within the row
92: /// </summary>
93: public int NumberOfCells
94: {
95: get { return Cells.Count; }
96: }
97:
98: #endregion
99:
100: #region Public Overridden Functions
101:
102: /// <summary>
103: /// To string function
104: /// </summary>
105: /// <returns>The content of the row in string form</returns>
106: public override string ToString()
107: {
108: StringBuilder Builder = new StringBuilder();
109: string Seperator = "";
110: foreach (Cell CurrentCell in Cells)
111: {
112: Builder.Append(Seperator).Append(CurrentCell);
113: Seperator = ",";
114: }
115: Builder.Append(System.Environment.NewLine);
116: return Builder.ToString();
117: }
118:
119: #endregion
120: }
121: }