Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1734 updated copyright year in all files of the DataImporter branch

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Text;
27using System.Xml;
28using System.Windows.Forms;
29using HeuristicLab.DataImporter.Data;
30using HeuristicLab.DataImporter.Data.Model;
31using HeuristicLab.DataImporter.Data.CommandBase;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat {
35  [StorableClass]
36  public class Hl2ImporterCommand : DataSetCommandBase {
37    private ColumnGroup newColumnGroup;
38
39    private Hl2ImporterCommand()
40      : base(null) {
41    }
42
43    public Hl2ImporterCommand(DataSet dataSet)
44      : base(dataSet) {
45    }
46
47    public override string Description {
48      get { return "Import HL2 file"; }
49    }
50
51    [Storable]
52    private string fileName;
53    public string FileName {
54      get { return fileName; }
55      set { this.fileName = value; }
56    }
57
58    public override void Execute() {
59      base.Execute();
60      newColumnGroup = new ColumnGroup(fileName.Substring(fileName.LastIndexOf('\\') + 1));
61      StreamReader reader = new StreamReader(fileName);
62      string firstLine = reader.ReadLine();
63      string[] variableNames = new string[0];
64
65      //skip meta information && extract variable/column names
66      while (firstLine.Trim()[0] == Hl2Exporter.METATAG[0]) {
67        if (firstLine.Contains(Hl2Exporter.VARIABLENAMES)) {
68          firstLine = firstLine.Replace(Hl2Exporter.METATAG + Hl2Exporter.VARIABLENAMES + "=", "");
69          variableNames = firstLine.Split(new char[] { ';' });
70        }
71        firstLine = reader.ReadLine();
72      }
73      string[] values = firstLine.Split(new char[] { ';' });
74      for (int i = 0; i < values.Length; i++) {
75        if (i < variableNames.Length)
76          newColumnGroup.AddColumn(new StringColumn(variableNames[i]));
77        else
78          newColumnGroup.AddColumn(new StringColumn("Column " + i));
79      }
80
81      while (!reader.EndOfStream) {
82        newColumnGroup.AddRow(reader.ReadLine().Split(new char[] { ';' }));
83      }
84      reader.Close();
85      DataSet.AddColumnGroup(newColumnGroup);
86      DataSet.FireChanged();
87    }
88
89    public override void UndoExecute() {
90      base.UndoExecute();
91      DataSet.RemoveColumnGroup(newColumnGroup);
92      newColumnGroup = null;
93      DataSet.FireChanged();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.