コード例 #1
0
        private void OpenMib(string file)
        {
            try
            {
                MibDocument md = new MibDocument(file);
                MibTypesResolver.ResolveTypes(md.Modules[0]);

                this.treeMib.Nodes.Clear();
                this.listviewNodeDetails.Items.Clear();

                MibTree mt = new MibTree(md.Modules[0] as MibModule);
                foreach (MibTreeNode mibTreeNode in mt.Root)
                {
                    AddNode(mibTreeNode, this.treeMib.Nodes);

                    foreach (TreeNode node in this.treeMib.Nodes)
                    {
                        node.Expand();
                    }
                }
            }
            catch
            {
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("lwIP MIB Compiler");
            Console.WriteLine("");

            // check args
            if ((args.Length < 2) || String.IsNullOrWhiteSpace(args[0]) || String.IsNullOrWhiteSpace(args[1]))
            {
                PrintUsage();
                return;
            }

            string mibFile = args[0];

            if (!File.Exists(mibFile))
            {
                Console.WriteLine(String.Format("Unable to find file '{0}'!", mibFile));
            }

            string destFile = args[1];
            string destHeaderFile;

            if (Directory.Exists(destFile))
            {
                // only directory passed -> create dest filename from mib filename
                string mibFileName = Path.GetFileNameWithoutExtension(mibFile).ToLowerInvariant();
                destFile = Path.Combine(destFile, mibFileName + ".c");
            }

            string destFileExt = Path.GetExtension(destFile);

            if (!String.IsNullOrEmpty(destFileExt))
            {
                destHeaderFile = destFile.Substring(0, destFile.Length - destFileExt.Length);
            }
            else
            {
                destHeaderFile = destFile;
            }
            destHeaderFile += ".h";

            for (int i = 2; i < args.Length; i++)
            {
                if (!String.IsNullOrWhiteSpace(args[i]) && Directory.Exists(args[i]))
                {
                    MibTypesResolver.RegisterResolver(new FileSystemMibResolver(args[i], true));
                }
            }


            // read and resolve MIB
            Console.WriteLine(" Reading MIB file...");

            MibDocument md = new MibDocument(mibFile);

            MibTypesResolver.ResolveTypes(md.Modules[0]);
            MibTree mt = new MibTree(md.Modules[0] as MibModule);

            if (mt.Root.Count == 0)
            {
                Console.WriteLine("No root element found inside MIB!");
                return;
            }

            MibCFile      generatedFile       = new MibCFile();
            MibHeaderFile generatedHeaderFile = new MibHeaderFile();

            foreach (MibTreeNode mibTreeNode in mt.Root)
            {
                // create LWIP object tree from MIB structure
                Console.WriteLine(" Creating lwIP object tree " + mibTreeNode.Entity.Name);

                SnmpMib snmpMib = new SnmpMib();
                snmpMib.Oid     = mibTreeNode.Entity.Value;
                snmpMib.BaseOid = MibTypesResolver.ResolveOid(mibTreeNode.Entity).GetOidValues();
                snmpMib.Name    = mibTreeNode.Entity.Name;

                ProcessMibTreeNode(mibTreeNode, snmpMib);

                // let the tree transform itself depending on node structure
                snmpMib.Analyze();

                if (snmpMib.ChildNodes.Count != 0)
                {
                    // generate code from LWIP object tree
                    Console.WriteLine(" Generating code " + snmpMib.Name);
                    snmpMib.Generate(generatedFile, generatedHeaderFile);
                }
            }

            string preservedCode = MibCFile.GetPreservedCode(destFile);

            if (!string.IsNullOrEmpty(preservedCode))
            {
                generatedFile.PreservedCode.Add(new PlainText(preservedCode));
            }
            else
            {
                generatedFile.PreservedCode.AddRange(generatedFile.Implementation);
            }
            generatedFile.Implementation.Clear();


            using (StreamWriter fileWriter = new StreamWriter(destHeaderFile))
            {
                CGenerator cGenerator = new CGenerator(fileWriter, destHeaderFile, 3, " ", Environment.NewLine);
                generatedHeaderFile.Save(cGenerator);
            }
            using (StreamWriter fileWriter = new StreamWriter(destFile))
            {
                CGenerator cGenerator = new CGenerator(fileWriter, destFile, 3, " ", Environment.NewLine);
                generatedFile.Save(cGenerator);
            }

            Console.WriteLine(" Done");
        }