Simple class to hold config key/value pairs
    private List <MagellanConfigPair> ReadConfig()
    {
        /**
         * Look for settings in ini.json if it exists
         * and the library DLL exists
         */
        #if NEWTONSOFT_JSON
        List <MagellanConfigPair> json_ports = JsonConfig();
        if (json_ports.Count > 0)
        {
            return(json_ports);
        }
        #endif

        string my_location = this.GetMyPath();
        char   sep         = Path.DirectorySeparatorChar;
        if (!File.Exists(my_location + sep + "ports.conf"))
        {
            throw new Exception("No configuration found!");
        }
        StreamReader fp = new StreamReader(my_location + sep + "ports.conf");
        List <MagellanConfigPair> conf = new List <MagellanConfigPair>();
        HashSet <string>          hs   = new HashSet <string>();
        string line;
        while ((line = fp.ReadLine()) != null)
        {
            line = line.TrimStart(null);
            if (line == "" || line[0] == '#')
            {
                continue;
            }
            string[] pieces = line.Split(null);
            if (pieces.Length != 2)
            {
                Console.WriteLine("Warning: malformed port.conf line: " + line);
                Console.WriteLine("Format: <port_string> <handler_class_name>");
            }
            else if (hs.Contains(pieces[0]))
            {
                Console.WriteLine("Warning: device already has a module attached.");
                Console.WriteLine("Line will be ignored: " + line);
            }
            else
            {
                var pair = new MagellanConfigPair();
                pair.port   = pieces[0];
                pair.module = pieces[1];
                conf.Add(pair);
                hs.Add(pieces[0]);
            }
        }

        return(conf);
    }
Exemple #2
0
    private List<MagellanConfigPair> ReadConfig()
    {
        /**
         * Look for settings in ini.json if it exists
         * and the library DLL exists
         */
        #if NEWTONSOFT_JSON
        List<MagellanConfigPair> json_ports = JsonConfig();
        if (json_ports.Count > 0) {
            return json_ports;
        }
        #endif

        string my_location = AppDomain.CurrentDomain.BaseDirectory;
        char sep = Path.DirectorySeparatorChar;
        StreamReader fp = new StreamReader(my_location + sep + "ports.conf");
        List<MagellanConfigPair> conf = new List<MagellanConfigPair>();
        HashSet<string> hs = new HashSet<string>();
        string line;
        while( (line = fp.ReadLine()) != null) {
            line = line.TrimStart(null);
            if (line == "" || line[0] == '#') continue;
            string[] pieces = line.Split(null);
            if (pieces.Length != 2) {
                Console.WriteLine("Warning: malformed port.conf line: "+line);
                Console.WriteLine("Format: <port_string> <handler_class_name>");
            } else if (hs.Contains(pieces[0])) {
                Console.WriteLine("Warning: device already has a module attached.");
                Console.WriteLine("Line will be ignored: "+line);
            } else {
	        var pair = new MagellanConfigPair();
                pair.port = pieces[0];
                pair.module = pieces[1];
                conf.Add(pair);
                hs.Add(pieces[0]);
            }
        }    

        return conf;
    }
Exemple #3
0
    private List<MagellanConfigPair> JsonConfig()
    {
        string my_location = AppDomain.CurrentDomain.BaseDirectory;
        char sep = Path.DirectorySeparatorChar;
        string ini_file = my_location + sep + ".." + sep + ".." + sep + ".." + sep + "ini.json";
        List<MagellanConfigPair> conf = new List<MagellanConfigPair>();
        if (!File.Exists(ini_file)) {
            return conf;
        }

        try {
            string ini_json = File.ReadAllText(ini_file);
            JObject o = JObject.Parse(ini_json);
            foreach (var port in o["NewMagellanPorts"]) {
                if (port["port"] == null) {
                    Console.WriteLine("Missing the \"port\" setting. JSON:");
                    Console.WriteLine(port);
                } else if (port["module"] == null) {
                    Console.WriteLine("Missing the \"module\" setting. JSON:");
                    Console.WriteLine(port);
                } else {
		    var pair = new MagellanConfigPair();
		    pair.port = (string)port["port"];
		    pair.module = (string)port["module"];
		    conf.Add(pair);
                }
            }
        } catch (NullReferenceException) {
            // probably means now NewMagellanPorts key in ini.json
            // not a fatal problem
        } catch (Exception ex) {
            // unexpected exception
            Console.WriteLine(ex);
        }

        return conf;
    }