#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.IO; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HEAL.Attic; namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat { [StorableType("A13F1017-2798-475A-BAFE-211B2921B926")] public class Hl2ImporterCommand : DataSetCommandBase { private ColumnGroup newColumnGroup; [StorableConstructor] protected Hl2ImporterCommand(StorableConstructorFlag _) : base(_) { } public Hl2ImporterCommand(DataSet dataSet) : base(dataSet) { } public override string Description { get { return "Import HL2 file"; } } [Storable] private string fileName; public string FileName { get { return fileName; } set { this.fileName = value; } } public override void Execute() { base.Execute(); newColumnGroup = new ColumnGroup(fileName.Substring(fileName.LastIndexOf('\\') + 1)); StreamReader reader = new StreamReader(fileName); string firstLine = reader.ReadLine(); string[] variableNames = new string[0]; //skip meta information && extract variable/column names while (firstLine.Trim()[0] == Hl2Exporter.METATAG[0]) { if (firstLine.Contains(Hl2Exporter.VARIABLENAMES)) { firstLine = firstLine.Replace(Hl2Exporter.METATAG + Hl2Exporter.VARIABLENAMES + "=", ""); variableNames = firstLine.Split(new char[] { ';' }); } firstLine = reader.ReadLine(); } string[] values = firstLine.Split(new char[] { ';' }); for (int i = 0; i < values.Length; i++) { if (i < variableNames.Length) newColumnGroup.AddColumn(new StringColumn(variableNames[i])); else newColumnGroup.AddColumn(new StringColumn("Column " + i)); } while (!reader.EndOfStream) { newColumnGroup.AddRow(reader.ReadLine().Split(new char[] { ';' })); } reader.Close(); DataSet.AddColumnGroup(newColumnGroup); DataSet.FireChanged(); } public override void UndoExecute() { base.UndoExecute(); DataSet.RemoveColumnGroup(newColumnGroup); newColumnGroup = null; DataSet.FireChanged(); } } }