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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
public class CSVWriter
{
/// <summary>
/// Gets or sets the outfile.
/// </summary>
/// <value>The file to write.</value>
public string Outfile { get; set; }
/// <summary>
/// Gets or sets the delimiter.
/// </summary>
/// <value>The delimiter.</value>
/// <remarks>Standard: ;</remarks>
public string Delimiter { get; set; }
/// <summary>
/// Gets or sets the write lines to file.
/// </summary>
/// <value>The write lines to file.</value>
/// <remarks>Every X files are written to the file.
/// Standard: 100 lines
/// </remarks>
public int WriteLinesToFile { get; set; }
/// <summary>
/// Gets or sets the cache.
/// </summary>
/// <value>The cache.</value>
private List<string> Cache { get; set; }
/// <summary>
/// Gets or sets the line counter.
/// </summary>
/// <value>The line counter.</value>
/// <remarks>Implemented for performance reason (cheaper to increase a counter than to count all elements of a list)</remarks>
private int LineCounter { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="CSVWriter"/> class.
/// </summary>
/// <param name="Outfile">The outfile.</param>
public CSVWriter(string Outfile)
{
this.Outfile = Outfile;
this.Delimiter = ";";
this.WriteLinesToFile = 100;
Cache = new List<string>();
}
/// <summary>
/// Initializes a new instance of the <see cref="CSVWriter"/> class.
/// </summary>
/// <param name="Outfile">The outfile.</param>
/// <param name="Delimiter">The delimiter.</param>
public CSVWriter(string Outfile, string Delimiter)
{
this.Outfile = Outfile;
this.Delimiter = Delimiter;
this.WriteLinesToFile = 100;
Cache = new List<string>();
}
/// <summary>
/// Writes the specified value.
/// </summary>
/// <param name="value">The value.</param>
public void Write(params object[] value)
{
Cache.Add(ConvertToCSVString(value));
}
/// <summary>
/// Writes the line.
/// </summary>
/// <param name="value">The value.</param>
public void WriteLine(params object[] value)
{
Cache.Add(ConvertToCSVString(value));
LineCounter++;
if (LineCounter == WriteLinesToFile)
SaveFile();
}
/// <summary>
/// Saves the file.
/// </summary>
private void SaveFile()
{
StreamWriter writer = new StreamWriter(Outfile, true);
for (int i = 0; i < WriteLinesToFile; i++)
{
writer.WriteLine(Cache[i]);
}
writer.Close();
Cache.Clear();
}
/// <summary>
/// Converts to CSV string.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
private string ConvertToCSVString(params object[] value)
{
// StringBuilder implementation
// faster string concatenation than using "+" etc.
StringBuilder StringToWrite = new StringBuilder();
// for-loops are _very_ cheaper (=faster) than foreach loops
for (int i = 0; i < value.Length; i++)
{
StringToWrite.Append(Convert.ToString(value[i]) + Delimiter);
}
string result = StringToWrite.ToString();
// Cut the last added delimiter
if (result.EndsWith(Delimiter))
result = result.TrimEnd(Delimiter.ToCharArray());
return result;
}
/// <summary>
/// Flushes this instance.
/// </summary>
/// <remarks>Writes the complete cache to the file</remarks>
public void Flush()
{
int BackupWriteLinesToFile = WriteLinesToFile;
WriteLinesToFile = Cache.Count;
SaveFile();
WriteLinesToFile = BackupWriteLinesToFile;
}
}
|