Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13042 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.IO;
23using HeuristicLab.DataImporter.Data.CommandBase;
24using HeuristicLab.DataImporter.Data.Model;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat {
28  [StorableClass]
29  public class Hl2ImporterCommand : DataSetCommandBase {
30    private ColumnGroup newColumnGroup;
31
32    [StorableConstructor]
33    protected Hl2ImporterCommand(bool deserializing) : base(deserializing) { }
34
35    public Hl2ImporterCommand(DataSet dataSet)
36      : base(dataSet) {
37    }
38
39    public override string Description {
40      get { return "Import HL2 file"; }
41    }
42
43    [Storable]
44    private string fileName;
45    public string FileName {
46      get { return fileName; }
47      set { this.fileName = value; }
48    }
49
50    public override void Execute() {
51      base.Execute();
52      newColumnGroup = new ColumnGroup(fileName.Substring(fileName.LastIndexOf('\\') + 1));
53      StreamReader reader = new StreamReader(fileName);
54      string firstLine = reader.ReadLine();
55      string[] variableNames = new string[0];
56
57      //skip meta information && extract variable/column names
58      while (firstLine.Trim()[0] == Hl2Exporter.METATAG[0]) {
59        if (firstLine.Contains(Hl2Exporter.VARIABLENAMES)) {
60          firstLine = firstLine.Replace(Hl2Exporter.METATAG + Hl2Exporter.VARIABLENAMES + "=", "");
61          variableNames = firstLine.Split(new char[] { ';' });
62        }
63        firstLine = reader.ReadLine();
64      }
65      string[] values = firstLine.Split(new char[] { ';' });
66      for (int i = 0; i < values.Length; i++) {
67        if (i < variableNames.Length)
68          newColumnGroup.AddColumn(new StringColumn(variableNames[i]));
69        else
70          newColumnGroup.AddColumn(new StringColumn("Column " + i));
71      }
72
73      while (!reader.EndOfStream) {
74        newColumnGroup.AddRow(reader.ReadLine().Split(new char[] { ';' }));
75      }
76      reader.Close();
77      DataSet.AddColumnGroup(newColumnGroup);
78      DataSet.FireChanged();
79    }
80
81    public override void UndoExecute() {
82      base.UndoExecute();
83      DataSet.RemoveColumnGroup(newColumnGroup);
84      newColumnGroup = null;
85      DataSet.FireChanged();
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.