Repository navigation
Expand file tree
/
Copy pathJbaCcForm.cs
More file actions
220 lines (175 loc) · 7.88 KB
/
Copy pathJbaCcForm.cs
File metadata and controls
220 lines (175 loc) · 7.88 KB
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
215
216
217
218
219
220
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JBACodeTest
{
public partial class JbaCcForm : Form
{
DbManager _dbManager;
public JbaCcForm(DbManager dbManager)
{
_dbManager = dbManager;
InitializeComponent();
buttonImport.Enabled = false;
ClearStatus();
AddStatusLine("App started. Please select local DB file and input .pre file then click Go");
}
private void AddStatusLine(string line)
{
textBoxStatus.Text += ("\r\n" + line);
}
private void ClearStatus()
{
textBoxStatus.Text = "";
}
private void buttonBrowseDb_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
openFileDialog.RestoreDirectory = true;
openFileDialog.Title = "Select database";
openFileDialog.DefaultExt = "mdf";
openFileDialog.Filter = "SQL Server data file (*.mdf)|*.mdf";
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBoxDbPath.Text = openFileDialog.FileName;
}
}
private void buttonChooseInputFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
openFileDialog.RestoreDirectory = true;
openFileDialog.Title = "Select input file";
openFileDialog.DefaultExt = "pre";
openFileDialog.Filter = "pre files (*.pre)|*.pre|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBoxInputFileName.Text = openFileDialog.FileName;
}
}
private void buttonImport_Click(object sender, EventArgs e)
{
AddStatusLine("Parsing file " + textBoxInputFileName.Text);
List<RainfallEntry> entries = ParseFile(textBoxInputFileName.Text);
AddStatusLine("Parse complete, returned " + entries.Count.ToString() + " entries");
AddStatusLine("Inserting into DB, please wait...");
var result = _dbManager.InsertData(textBoxDbPath.Text, entries);
AddStatusLine("Finished import: " + result);
}
private List<RainfallEntry> ParseFile(string inputFile)
{
List<RainfallEntry> entries = new List<RainfallEntry>();
const Int32 BufferSize = 128;
using (var fileStream = File.OpenRead(inputFile))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
{
int startYear = 0, endYear = 0;
String line;
int lineNum = 1;
int lineNumInBlock = -1;
int xVal = -1, yVal = -1;
while ((line = streamReader.ReadLine()) != null)
{
// Process line
if (lineNum < 5)
{
// skip these header lines
}
else if (lineNum == 5)
{
// regex find the "Years" block
var yearsBlock = Regex.Match(line.ToLowerInvariant(), "\\[years=.*?\\]"); // NB toLower
if (!yearsBlock.Success)
throw new Exception("Couldn't find Years in line 5 of header");
var years2 = Regex.Match(yearsBlock.Value, @"\d{4}\s*-\s*\d{4}"); // not sure if this regex use is overkill but using in an attempt to second guess variations in the file format, e.g. whitespace
if (!years2.Success)
throw new Exception("Couldn't find xxxx-yyyy in years block");
var yrs = years2.Value.Split(new char[] { '-' });
if (!Int32.TryParse(yrs[0], out startYear))
throw new Exception("YEARS parse error (start)");
if (!Int32.TryParse(yrs[1], out endYear))
throw new Exception("YEARS parse error (end)");
}
else
{
var gridrefBlock = Regex.Match(line.ToLowerInvariant(), "grid-ref=.*"); // NB toLower
if (gridrefBlock.Success) /// new block start
{
var eqpos = gridrefBlock.Value.IndexOf('=');
var commapos = gridrefBlock.Value.IndexOf(',');
var xStr = gridrefBlock.Value.Substring(eqpos + 1, commapos - eqpos - 1);
var yStr = gridrefBlock.Value.Substring(commapos + 1);
if (!Int32.TryParse(xStr, out xVal))
throw new Exception("x parse error");
if (!Int32.TryParse(yStr, out yVal))
throw new Exception("y parse error");
lineNumInBlock = 0;
}
else
{
// assume this is a line of 12 ints
var monthlyVals = ParseRainfallLine(line);
for (int m = 0; m < monthlyVals.Length; ++m)
{
int year = startYear + lineNumInBlock;
RainfallEntry e = new RainfallEntry() { _x = xVal, _y = yVal, _date = new DateTime(year, m+1, 1), _amount = monthlyVals[m] };
entries.Add(e);
}
lineNumInBlock++;
}
}
lineNum++;
}
}
return entries;
}
private int[] ParseRainfallLine(string line) // (Note: I initially assumed this is a line of 12 ints separated by spaces or tabs, but turns out it's just 12 x 5 character blocks!)
{
var vals = new int[12];
if (line.Length != 60)
throw new Exception("Unexpected line length");
for (int i = 0; i < 12; ++i)
{
var mstr = line.Substring(i * 5, 5);
if (!Int32.TryParse(mstr, out vals[i]))
throw new Exception("monthly value parse error");
}
return vals;
}
private void buttonTestDb_Click(object sender, EventArgs e)
{
AddStatusLine(_dbManager.Report(textBoxDbPath.Text, 115, 263));
}
private void textBoxInputFileName_TextChanged(object sender, EventArgs e)
{
UpdateButtons();
}
private void UpdateButtons()
{
bool enable = false;
if (textBoxInputFileName.Text != "" && File.Exists(textBoxInputFileName.Text))
{
if (textBoxDbPath.Text != "" && File.Exists(textBoxDbPath.Text))
{
enable = true;
}
}
buttonImport.Enabled = enable;
}
private void textBoxDbPath_TextChanged(object sender, EventArgs e)
{
UpdateButtons();
}
}
}