Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Backend.Hl2ImporterFormat/Hl2ImporterCommand.cs @ 6133

Last change on this file since 6133 was 6133, checked in by gkronber, 13 years ago

#1471: imported generic parts of DataImporter from private code base

File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Xml;
7using System.Windows.Forms;
8using HeuristicLab.DataImporter.Data;
9using HeuristicLab.DataImporter.Data.Model;
10using HeuristicLab.DataImporter.Data.CommandBase;
11using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
12
13namespace 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}
Note: See TracBrowser for help on using the repository browser.