/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ namespace OpenTraffic.Model.Importer { using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using OpenTraffic.Model; public abstract class Contram { protected delegate void Command( string line, TrafficModel m, Dictionary d, string commandName); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public abstract string GetName(); [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public abstract string GetExtensions(); protected Dictionary commands { get; private set; } protected Contram() { commands = new Dictionary(); } private static Dictionary GetDictionary(string line) { Dictionary d = new Dictionary(); line = line.Substring(1); String[] cols = SplitLine(line); for (int i = 0; i < cols.Length; i++) { string col = cols[i]; if (d.ContainsKey(col)) { col += "_2"; } d.Add(col, i); } return d; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "2")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")] protected static string GetString(string nombre, Dictionary cols, String[] values) { return values[cols[nombre]]; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "float")] protected static float GetFloat(string nombre, Dictionary cols, String[] values) { return GetFloat(cols[nombre], values); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "float")] protected static float GetFloat(int index, String[] values) { NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat; return float.Parse(values[index], nfi); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "2")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")] protected static int GetInt(string nombre, Dictionary cols, String[] values) { NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat; return int.Parse(values[cols[nombre]], nfi); } protected static String[] SplitLine(string line) { if (line != null) { return line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); } return new String[0]; } protected void LoadFile(FileInfo file, OpenTraffic.Model.TrafficModel m, bool include) { if (file != null) { StreamReader sr = null; try { sr = new StreamReader(file.FullName, Encoding.GetEncoding(28591)); //WESTER EUROPE string line; if (!include) { line = sr.ReadLine(); if ((line == null) || ((!line.Equals("CONTRAM 7 NETWORK")) && (!line.Equals("CONTRAM 8 NETWORK")))) { throw new FileLoadException("Not a contram version 7 or 8 file"); } } Command command = null; Dictionary d = null; Boolean skip = false; string commandName = ""; while ((line = sr.ReadLine()) != null) { if (line.Trim().Length == 0) { skip = false; command = null; d = null; } else { if (!skip) { if (command == null) { if (commands.ContainsKey(line.Trim())) { commandName = line; command = commands[line.Trim()]; skip = false; } else { skip = true; } } else { if (command != null) { bool skipLine = false; // Console.WriteLine(line); if (line[0] == '*') { skipLine = true; if (d == null) d = GetDictionary(line); } if (line.StartsWith("INCLUDE")) { string filename = line.Substring("INCLUDE".Length).Trim(); filename = filename.Replace("\"", ""); this.LoadFile(new FileInfo(file.DirectoryName + "\\" + filename), m, true); skipLine = true; } if (!skipLine) { command(line, m, d, commandName); try { } catch (Exception) { throw new FileLoadException("Error in file " + file.FullName + "on line:" + line); } } } } } } } } finally { if (sr != null) { sr.Close(); } } } } } }