Last active
August 29, 2015 14:16
-
-
Save oexenhave/dfa949a49a7ef9a49733 to your computer and use it in GitHub Desktop.
CsvReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
namespace TimeLog.IO | |
{ | |
public class CsvReader | |
{ | |
public bool HasHeaders { get; private set; } | |
public int LineIndex { get; private set; } | |
public FileInfo CsvFile { get; private set; } | |
public string[] Lines { get; private set; } | |
public string[] CurrentLine { get; private set; } | |
public string[] Columns { get; private set; } | |
public char Splitter { get; set; } | |
public CsvReader(string csvFilePath) | |
: this(csvFilePath, true) | |
{ | |
} | |
public CsvReader(string csvFilePath, bool hasHeaders) | |
: this(csvFilePath, hasHeaders, Encoding.GetEncoding(1252)) | |
{ | |
} | |
public CsvReader(string csvFilePath, bool hasHeaders, Encoding encoding) | |
{ | |
Splitter = ','; | |
HasHeaders = hasHeaders; | |
CsvFile = new FileInfo(csvFilePath); | |
Reset(); | |
if (!CsvFile.Exists) | |
{ | |
throw new ArgumentException("CSV file path not found (" + csvFilePath + ")"); | |
} | |
if (CsvFile.Length == 0) | |
{ | |
throw new ArgumentException("CSV file empty (" + csvFilePath + ")"); | |
} | |
Lines = File.ReadAllLines(CsvFile.FullName, encoding); | |
if (hasHeaders) | |
{ | |
Columns = Split(Lines[0]); | |
} | |
} | |
public string[] Split(string input) | |
{ | |
bool hasQuote = false; | |
var cells = new List<string>(); | |
var currentCell = string.Empty; | |
foreach (char character in input) | |
{ | |
if (character == '"') | |
{ | |
hasQuote = !hasQuote; | |
} | |
else if (character == Splitter) | |
{ | |
if (hasQuote) | |
{ | |
currentCell += character; | |
continue; | |
} | |
cells.Add(currentCell); | |
currentCell = string.Empty; | |
} | |
else | |
{ | |
currentCell += character; | |
} | |
} | |
if (!string.IsNullOrEmpty(currentCell)) | |
{ | |
cells.Add(currentCell); | |
} | |
return cells.ToArray(); | |
} | |
public bool Read() | |
{ | |
if (LineIndex >= Lines.Length) | |
{ | |
return false; | |
} | |
CurrentLine = Split(Lines[LineIndex]); | |
LineIndex++; | |
return true; | |
} | |
public string GetString(int columnIndex) | |
{ | |
if (columnIndex > Columns.Length) | |
{ | |
throw new ArgumentException("Index out of bounds (tried: " + columnIndex + " length: " + Columns.Length); | |
} | |
return CurrentLine[columnIndex]; | |
} | |
public string GetString(string columnName) | |
{ | |
return GetString(GetColumnIndex(columnName)); | |
} | |
public int GetInteger(int columnIndex) | |
{ | |
if (columnIndex > Columns.Length) | |
{ | |
throw new ArgumentException("Index out of bounds (tried: " + columnIndex + " length: " + Columns.Length); | |
} | |
return int.Parse(CurrentLine[columnIndex]); | |
} | |
public int GetInteger(string columnName) | |
{ | |
return GetInteger(GetColumnIndex(columnName)); | |
} | |
public bool GetBoolean(int columnIndex) | |
{ | |
if (columnIndex > Columns.Length) | |
{ | |
throw new ArgumentException("Index out of bounds (tried: " + columnIndex + " length: " + Columns.Length); | |
} | |
bool result; | |
if (bool.TryParse(CurrentLine[columnIndex], out result)) | |
{ | |
return result; | |
} | |
if (CurrentLine[columnIndex] == "1") | |
{ | |
return true; | |
} | |
if (CurrentLine[columnIndex] == "0") | |
{ | |
return false; | |
} | |
throw new Exception("String format cannot be converted to boolean (" + CurrentLine[columnIndex] + ")"); | |
} | |
public bool GetBoolean(string columnName) | |
{ | |
return GetBoolean(GetColumnIndex(columnName)); | |
} | |
public Guid GetGuid(int columnIndex) | |
{ | |
if (columnIndex > Columns.Length) | |
{ | |
throw new ArgumentException("Index out of bounds (tried: " + columnIndex + " length: " + Columns.Length); | |
} | |
return Guid.Parse(CurrentLine[columnIndex]); | |
} | |
public Guid GetGuid(string columnName) | |
{ | |
return GetGuid(GetColumnIndex(columnName)); | |
} | |
public int GetColumnIndex(string columnName) | |
{ | |
for (int i = 0; i < Columns.Length; i++) | |
{ | |
if (Columns[i].ToUpper() == columnName.ToUpper()) | |
{ | |
return i; | |
} | |
} | |
throw new ArgumentException("Column name not found (" + columnName + ")"); | |
} | |
public void Reset() | |
{ | |
LineIndex = HasHeaders ? 1 : 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment