using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Windows.Forms; using HeuristicLab.DataImporter.Data; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat { [StorableClass] public class Hl2ImporterCommand : DataSetCommandBase { private ColumnGroup newColumnGroup; private Hl2ImporterCommand() : base(null) { } 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(); } } }