-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileForFilter.cs
214 lines (202 loc) · 6.89 KB
/
FileForFilter.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using Microsoft.Win32;
using System.Windows;
namespace Filters
{
public class FileForFilter : INotifyPropertyChanged
{
private String sourceFileName;
private Boolean isValid;
private List<String> columnNames;
private List<List<String>> spreadsheet;
private List<List<String>> filteredSpreadsheet;
private Int32 columnsCount;
public String SourceFileName
{
get
{
return this.sourceFileName;
}
set
{
this.sourceFileName = value;
OnPropertyChanged("SourceFileName");
}
}
public Boolean IsValid
{
get
{
return this.isValid;
}
set
{
this.isValid = value;
OnPropertyChanged("IsValid");
}
}
public List<String> ColumnNames
{
get
{
return this.columnNames;
}
set
{
this.columnNames = value;
OnPropertyChanged("ColumnNames");
}
}
public List<List<String>> Spreadsheet
{
get
{
return this.spreadsheet;
}
set
{
this.spreadsheet = value;
OnPropertyChanged("SpreadSheet");
}
}
public List<List<String>> FilteredSpreadsheet
{
get
{
return this.filteredSpreadsheet;
}
set
{
this.filteredSpreadsheet = value;
OnPropertyChanged("FilteredSpreadsheet");
}
}
public Int32 ColumnsCount
{
get
{
return this.columnsCount;
}
set
{
this.columnsCount = value;
OnPropertyChanged("ColumnsCount");
}
}
public FileForFilter()
{
this.SourceFileName = String.Empty;
this.IsValid = false;
this.ColumnNames = new List<String>();
this.Spreadsheet = new List<List<String>>();
this.FilteredSpreadsheet = new List<List<String>>();
this.ColumnsCount = 0;
}
public void OpenFile()
{
OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "(*.csv)|*.csv";
ofd.Title = "Open .csv file...";
if (false == ofd.ShowDialog())
{
return;
}
this.SourceFileName = ofd.FileName;
this.ColumnNames = new List<String>();
this.Spreadsheet = new List<List<String>>();
this.FilteredSpreadsheet = new List<List<String>>();
this.isValid = false;
String fullText = String.Empty;
using (Stream fStream = new FileStream(ofd.FileName,
FileMode.Open,
FileAccess.Read,
FileShare.None))
{
using (StreamReader sr = new StreamReader(fStream))
{
fullText = sr.ReadToEnd();
}
}
using (StringReader sr = new StringReader(fullText))
{
String line = sr.ReadLine();
this.ColumnNames = line.Split(',').OfType<String>().ToList();
this.ColumnsCount = this.ColumnNames.Count;
this.Spreadsheet.Add(this.ColumnNames);
while (null != (line = sr.ReadLine()))
{
List<String> row = line.Split(',').OfType<String>().ToList();
if (row.Count != this.columnsCount)
{
this.isValid = false;
MessageBox.Show("Something wrong with this file.\n" +
"Different cells counts per row.\n" +
"Choose another one.");
return;
}
this.Spreadsheet.Add(row);
}
this.isValid = true;
this.FilteredSpreadsheet = this.Spreadsheet;
}
}
public void FilterText(Filter filter)
{
List<List<String>> newFilteredSpreadSheet = new List<List<String>>();
newFilteredSpreadSheet.Add(this.Spreadsheet[0]);
for (Int32 row_id = 1; row_id < this.Spreadsheet.Count; ++row_id)
{
List<String> row = this.Spreadsheet[row_id];
Boolean validRow = true;
foreach (Subfilter sf in filter.Subfilters)
{
Int32 feature = sf.Feature;
Double cellVal=0, minVal, maxVal;
Double.TryParse(row[feature].Replace('.', ','), out cellVal);
Double.TryParse(sf.FromAsString.Replace('.', ','), out minVal);
Double.TryParse(sf.ToAsString.Replace('.', ','), out maxVal);
if ((cellVal < minVal) || (cellVal > maxVal)) validRow = false;
}
if (validRow) newFilteredSpreadSheet.Add(row);
}
this.FilteredSpreadsheet = newFilteredSpreadSheet;
}
public void ExportFilteredCSV()
{
SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = "(*.csv)|*.csv";
sfd.Title = "Save filtered file...";
if (false == sfd.ShowDialog())
{
return;
}
using (Stream fStream = new FileStream(sfd.FileName,
FileMode.Create,
FileAccess.Write,
FileShare.None))
{
using (StreamWriter sr = new StreamWriter(fStream))
{
foreach (List<String> row in this.FilteredSpreadsheet)
{
sr.WriteLine(String.Join(",", row.ToArray<String>()));
}
}
}
MessageBox.Show("Filtered file is saved to\n" + sfd.FileName);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(String propertyName)
{
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}