1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Xml;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using HeuristicLab.DataImporter.Data;
|
---|
9 | using HeuristicLab.DataImporter.Data.Model;
|
---|
10 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
11 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat {
|
---|
14 | [StorableClass]
|
---|
15 | public class Hl2ImporterCommand : DataSetCommandBase {
|
---|
16 | private ColumnGroup newColumnGroup;
|
---|
17 |
|
---|
18 | private Hl2ImporterCommand()
|
---|
19 | : base(null) {
|
---|
20 | }
|
---|
21 |
|
---|
22 | public Hl2ImporterCommand(DataSet dataSet)
|
---|
23 | : base(dataSet) {
|
---|
24 | }
|
---|
25 |
|
---|
26 | public override string Description {
|
---|
27 | get { return "Import HL2 file"; }
|
---|
28 | }
|
---|
29 |
|
---|
30 | [Storable]
|
---|
31 | private string fileName;
|
---|
32 | public string FileName {
|
---|
33 | get { return fileName; }
|
---|
34 | set { this.fileName = value; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public override void Execute() {
|
---|
38 | base.Execute();
|
---|
39 | newColumnGroup = new ColumnGroup(fileName.Substring(fileName.LastIndexOf('\\') + 1));
|
---|
40 | StreamReader reader = new StreamReader(fileName);
|
---|
41 | string firstLine = reader.ReadLine();
|
---|
42 | string[] variableNames = new string[0];
|
---|
43 |
|
---|
44 | //skip meta information && extract variable/column names
|
---|
45 | while (firstLine.Trim()[0] == Hl2Exporter.METATAG[0]) {
|
---|
46 | if (firstLine.Contains(Hl2Exporter.VARIABLENAMES)) {
|
---|
47 | firstLine = firstLine.Replace(Hl2Exporter.METATAG + Hl2Exporter.VARIABLENAMES + "=", "");
|
---|
48 | variableNames = firstLine.Split(new char[] { ';' });
|
---|
49 | }
|
---|
50 | firstLine = reader.ReadLine();
|
---|
51 | }
|
---|
52 | string[] values = firstLine.Split(new char[] { ';' });
|
---|
53 | for (int i = 0; i < values.Length; i++) {
|
---|
54 | if (i < variableNames.Length)
|
---|
55 | newColumnGroup.AddColumn(new StringColumn(variableNames[i]));
|
---|
56 | else
|
---|
57 | newColumnGroup.AddColumn(new StringColumn("Column " + i));
|
---|
58 | }
|
---|
59 |
|
---|
60 | while (!reader.EndOfStream) {
|
---|
61 | newColumnGroup.AddRow(reader.ReadLine().Split(new char[] { ';' }));
|
---|
62 | }
|
---|
63 | reader.Close();
|
---|
64 | DataSet.AddColumnGroup(newColumnGroup);
|
---|
65 | DataSet.FireChanged();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override void UndoExecute() {
|
---|
69 | base.UndoExecute();
|
---|
70 | DataSet.RemoveColumnGroup(newColumnGroup);
|
---|
71 | newColumnGroup = null;
|
---|
72 | DataSet.FireChanged();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|