-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsvReader.cs
83 lines (73 loc) · 2.04 KB
/
CsvReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class CsvReader
{
Queue<string> mLines;
Dictionary<string, int> mHeaders;
string[] currentLine;
int mLineNumber = 0;
public CsvReader(string file)
{
mHeaders = new Dictionary<string, int>();
mLines = new Queue<string>(File.ReadAllLines(file));
string[] headers = mLines.Dequeue().Split(',');
int idx = 0;
foreach(var header in headers)
{
mHeaders.Add(header, idx++);
}
mLineNumber = 1;
}
public CsvReader(string CsvData, bool Text=false)
{
mLines = new Queue<string>(CsvData.Split('\n'));
mHeaders = new Dictionary<string, int>();
////Debug.Log(mLines.Peek());
string[] headers = mLines.Dequeue().Split(',');
int idx = 0;
////Debug.Log(headers.Length);
for (;idx < headers.Length;)
{
if (idx == headers.Length-1)
{
headers[idx] = headers[idx].Substring(0, headers[idx].Length-1);
}
mHeaders.Add(headers[idx], idx++);
}
mLineNumber = 1;
}
public bool Read()
{
if (mLines.Count > 0)
{
currentLine = mLines.Dequeue().Split(',');
++mLineNumber;
return true;
}
return false;
}
public int GetHeaderIndex(string head)
{
if (!mHeaders.ContainsKey(head))
return -1;
return mHeaders[head];
}
public string GetFieldOrEmpty(string head)
{
// foreach (string key in currentLine)
// {
// ////Debug.Log(key);
// }
//////Debug.Log(head);
//////Debug.Log(mHeaders[head]);
if (!mHeaders.ContainsKey(head))
return "";
return currentLine[mHeaders[head]];
}
public int GetLineNumber()
{
return mLineNumber;
}
}