Exemple #1
0
 public void GenerateCode(CGenerator generator)
 {
     if (!String.IsNullOrWhiteSpace(this.Type))
     {
         generator.OutputStream.Write(this.ToString().Trim());
     }
 }
Exemple #2
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (this.Type != null)
			{
				generator.IndentLine(level);

				if (this.IsStatic)
				{
					generator.OutputStream.Write("static ");
				}

				// declare the variable
				this.Type.GenerateCode(generator);

				if (!String.IsNullOrWhiteSpace(this.InitialValue))
				{
					// add initialization value
					generator.OutputStream.Write(" = ");
					generator.WriteMultilineString(this.InitialValue, level);
				}

				generator.OutputStream.Write(";");
				generator.WriteNewLine();
			}
		}
Exemple #3
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.Value))
			{
				generator.IndentLine(level);
				if (this.IsDefault)
				{
					generator.OutputStream.Write("default:");
				}
				else
				{
					generator.OutputStream.Write(String.Format("case {0}:", this.Value));
				}
				generator.WriteNewLine();
				generator.IndentLine(level + 1);
				generator.OutputStream.Write("{");
				generator.WriteNewLine();

				base.GenerateCode(level + 1, generator);

				generator.IndentLine(level + 1);
				generator.OutputStream.Write("}");
				generator.WriteNewLine();

				generator.IndentLine(level + 1);
				generator.OutputStream.Write("break;");
				generator.WriteNewLine();
			}
		}
Exemple #4
0
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (this.Type != null)
            {
                generator.IndentLine(level);

                if (this.IsStatic)
                {
                    generator.OutputStream.Write("static ");
                }

                // declare the variable
                this.Type.GenerateCode(generator);

                if (!String.IsNullOrWhiteSpace(this.InitialValue))
                {
                    // add initialization value
                    generator.OutputStream.Write(" = ");
                    generator.WriteMultilineString(this.InitialValue, level);
                }

                generator.OutputStream.Write(";");
                generator.WriteNewLine();
            }
        }
Exemple #5
0
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (!String.IsNullOrWhiteSpace(this.Value))
            {
                generator.IndentLine(level);
                if (this.IsDefault)
                {
                    generator.OutputStream.Write("default:");
                }
                else
                {
                    generator.OutputStream.Write(String.Format("case {0}:", this.Value));
                }
                generator.WriteNewLine();
                generator.IndentLine(level + 1);
                generator.OutputStream.Write("{");
                generator.WriteNewLine();

                base.GenerateCode(level + 1, generator);

                generator.IndentLine(level + 1);
                generator.OutputStream.Write("}");
                generator.WriteNewLine();

                generator.IndentLine(level + 1);
                generator.OutputStream.Write("break;");
                generator.WriteNewLine();
            }
        }
Exemple #6
0
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (!String.IsNullOrWhiteSpace(this.SwitchVar))
            {
                generator.IndentLine(level);
                generator.OutputStream.Write(String.Format("switch ({0})", this.SwitchVar));
                generator.WriteNewLine();
                generator.IndentLine(level);
                generator.OutputStream.Write("{");
                generator.WriteNewLine();

                SwitchCase defaultCase = null;                 // generate 'default' always as last case
                foreach (SwitchCase switchCase in this.switches)
                {
                    if (switchCase.IsDefault)
                    {
                        defaultCase = switchCase;
                    }
                    else
                    {
                        switchCase.GenerateCode(level + 1, generator);
                    }
                }
                if (defaultCase != null)
                {
                    defaultCase.GenerateCode(level + 1, generator);
                }

                generator.IndentLine(level);
                generator.OutputStream.Write("}");
                generator.WriteNewLine();
            }
        }
Exemple #7
0
		public void GenerateCode(CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.Type))
			{
				generator.OutputStream.Write(this.ToString().Trim());
			}
		}
Exemple #8
0
		public void Save(CGenerator generator)
		{
			if (generator == null)
			{
				throw new ArgumentNullException("generator");
			}

			this.GenerateCode(0, generator);
		}
Exemple #9
0
        public void Save(CGenerator generator)
        {
            if (generator == null)
            {
                throw new ArgumentNullException("generator");
            }

            this.GenerateCode(0, generator);
        }
Exemple #10
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.Condition))
			{
				generator.OutputStream.Write("#if " + this.Condition);
				generator.WriteNewLine();

				base.GenerateCode(level, generator);

				generator.OutputStream.Write("#endif /* " + this.Condition + " */");
				generator.WriteNewLine();
			}
		}
Exemple #11
0
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (!String.IsNullOrWhiteSpace(this.Condition))
            {
                generator.OutputStream.Write("#if " + this.Condition);
                generator.WriteNewLine();

                base.GenerateCode(level, generator);

                generator.OutputStream.Write("#endif /* " + this.Condition + " */");
                generator.WriteNewLine();
            }
        }
Exemple #12
0
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (!String.IsNullOrWhiteSpace(this.File))
            {
                // includes are never indented
                if (this.IsLocal)
                {
                    generator.OutputStream.Write("#include \"" + this.File + "\"");
                }
                else
                {
                    generator.OutputStream.Write("#include <" + this.File + ">");
                }

                generator.WriteNewLine();
            }
        }
Exemple #13
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.File))
			{
				// includes are never indented
				if (this.IsLocal)
				{
					generator.OutputStream.Write("#include \"" + this.File + "\"");
				}
				else
				{
					generator.OutputStream.Write("#include <" + this.File + ">");
				}

				generator.WriteNewLine();			
			}
		}
Exemple #14
0
		public void Save(CGenerator cGenerator)
		{
			CFile cFile = new CFile();

			cFile.AddElement(new Comment("Generated by LwipMibCompiler"));
			cFile.AddElement(EmptyLine.SingleLine);
			
			string headerDefine = cGenerator.FileName;
			headerDefine = new Regex("[^a-zA-Z0-9]").Replace(headerDefine, "_");
			headerDefine = headerDefine.ToUpperInvariant();

			CodeContainerBase e = cFile.AddElement(new PP_Ifdef(headerDefine, inverted: true)) as CodeContainerBase;
			e.AddElement(new PP_Macro(headerDefine, headerDefine));
			e.AddElement(EmptyLine.SingleLine);

			e.AddElement(new PP_Include(LwipDefs.Incl_SnmpOpts));
			e = e.AddElement(new PP_If(LwipDefs.Opt_SnmpEnabled)) as CodeContainerBase;
			e.AddElement(EmptyLine.SingleLine);

			CodeContainerBase cplusplusopen = e.AddElement(new PP_Ifdef("__cplusplus")) as CodeContainerBase;
			cplusplusopen.AddElement(new Code("extern \"C\" {"));
			e.AddElement(EmptyLine.SingleLine);

			if (this.includes.Count > 0)
			{
				e.AddElements(this.includes);
				e.AddElement(EmptyLine.SingleLine);
			}

			if (this.defines.Count > 0)
			{
				e.AddElements(this.defines);
				e.AddElement(EmptyLine.SingleLine);
			}
			
			e.AddElements(this.functionDeclarations, EmptyLine.SingleLine);
			e.AddElements(this.variableDeclarations, EmptyLine.SingleLine);

			e.AddElement(EmptyLine.SingleLine);
			CodeContainerBase cplusplusclose = e.AddElement(new PP_Ifdef("__cplusplus")) as CodeContainerBase;
			cplusplusclose.AddElement(new Code("}"));

			e.AddElement(EmptyLine.SingleLine);
			cFile.Save(cGenerator);
		}
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (!String.IsNullOrWhiteSpace(this.Condition))
            {
                generator.IndentLine(level);
                generator.OutputStream.Write(String.Format("else if ({0})", this.Condition));
                generator.WriteNewLine();
                generator.IndentLine(level);
                generator.OutputStream.Write("{");
                generator.WriteNewLine();

                base.GenerateCode(level, generator);

                generator.IndentLine(level);
                generator.OutputStream.Write("}");
                generator.WriteNewLine();
            }
        }
Exemple #16
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.Condition))
			{
				generator.IndentLine(level);
				generator.OutputStream.Write(String.Format("else if ({0})", this.Condition));
				generator.WriteNewLine();
				generator.IndentLine(level);
				generator.OutputStream.Write("{");
				generator.WriteNewLine();

				base.GenerateCode(level, generator);

				generator.IndentLine(level);
				generator.OutputStream.Write("}");
				generator.WriteNewLine();
			}
		}
Exemple #17
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.Macro))
			{
				if (this.Inverted)
				{
					generator.OutputStream.Write("#ifndef " + this.Macro);
				}
				else
				{
					generator.OutputStream.Write("#ifdef " + this.Macro);
				}
				generator.WriteNewLine();

				base.GenerateCode(level, generator);

				generator.OutputStream.Write("#endif /* " + this.Macro + " */");
				generator.WriteNewLine();
			}
		}
Exemple #18
0
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (!String.IsNullOrWhiteSpace(this.Macro))
            {
                if (this.Inverted)
                {
                    generator.OutputStream.Write("#ifndef " + this.Macro);
                }
                else
                {
                    generator.OutputStream.Write("#ifdef " + this.Macro);
                }
                generator.WriteNewLine();

                base.GenerateCode(level, generator);

                generator.OutputStream.Write("#endif /* " + this.Macro + " */");
                generator.WriteNewLine();
            }
        }
        public override void GenerateCode(int level, CGenerator generator)
        {
            generator.IndentLine(level);

            if (this.IsStatic)
            {
                generator.OutputStream.Write("static ");
            }

            this.returnType.GenerateCode(generator);
            generator.OutputStream.Write(" " + this.Name + "(");

            if (this.Parameter.Count > 0)
            {
                for (int i = 0; i < this.parameter.Count; i++)
                {
                    this.parameter[i].GenerateCode(generator);

                    if (i < (this.parameter.Count - 1))
                    {
                        generator.OutputStream.Write(", ");
                    }
                }
            }
            else
            {
                generator.OutputStream.Write("void");
            }

            generator.OutputStream.Write(")");
            generator.WriteNewLine();
            generator.IndentLine(level);
            generator.OutputStream.Write("{");
            generator.WriteNewLine();

            base.GenerateCode(level, generator);

            generator.IndentLine(level);
            generator.OutputStream.Write("}");
            generator.WriteNewLine();
        }
Exemple #20
0
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (this.increaseLevel)
            {
                level++;
            }

            if (this.declarations.Count > 0)
            {
                foreach (CodeElement element in this.declarations)
                {
                    element.GenerateCode(level, generator);
                }

                EmptyLine.SingleLine.GenerateCode(level, generator);
            }

            foreach (CodeElement element in this.innerElements)
            {
                element.GenerateCode(level, generator);
            }
        }
        public override void GenerateCode(int level, CGenerator generator)
        {
            if (!String.IsNullOrWhiteSpace(this.Condition))
            {
                generator.IndentLine(level);
                generator.OutputStream.Write(String.Format("if ({0})", this.Condition));
                generator.WriteNewLine();
                generator.IndentLine(level);
                generator.OutputStream.Write("{");
                generator.WriteNewLine();

                base.GenerateCode(level, generator);

                generator.IndentLine(level);
                generator.OutputStream.Write("}");
                generator.WriteNewLine();

                foreach (ElseIf elif in this.elseIf)
                {
                    elif.GenerateCode(level, generator);
                }

                if (this.else_.InnerElements.Count > 0)
                {
                    generator.IndentLine(level);
                    generator.OutputStream.Write("else");
                    generator.WriteNewLine();
                    generator.IndentLine(level);
                    generator.OutputStream.Write("{");
                    generator.WriteNewLine();

                    this.else_.GenerateCode(level, generator);

                    generator.IndentLine(level);
                    generator.OutputStream.Write("}");
                    generator.WriteNewLine();
                }
            }
        }
Exemple #22
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			generator.IndentLine(level);

			if (this.IsExtern)
			{
				generator.OutputStream.Write("extern ");
			}

			if (this.IsStatic)
			{
				generator.OutputStream.Write("static ");
			}

			this.returnType.GenerateCode(generator);
			generator.OutputStream.Write(" " + this.Name + "(");

			if (this.Parameter.Count > 0)
			{
				for (int i = 0; i < this.parameter.Count; i++)
				{
					this.parameter[i].GenerateCode(generator);

					if (i < (this.parameter.Count - 1))
					{
						generator.OutputStream.Write(", ");
					}
				}
			}
			else
			{
				generator.OutputStream.Write("void");
			}

			generator.OutputStream.Write(");");
			generator.WriteNewLine();
		}
Exemple #23
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.Condition))
			{
				generator.IndentLine(level);
				generator.OutputStream.Write(String.Format("if ({0})", this.Condition));
				generator.WriteNewLine();
				generator.IndentLine(level);
				generator.OutputStream.Write("{");
				generator.WriteNewLine();

				base.GenerateCode(level, generator);

				generator.IndentLine(level);
				generator.OutputStream.Write("}");
				generator.WriteNewLine();

				foreach (ElseIf elif in this.elseIf)
				{
					elif.GenerateCode(level, generator);
				}

				if (this.else_.InnerElements.Count > 0)
				{
					generator.IndentLine(level);
					generator.OutputStream.Write("else");
					generator.WriteNewLine();
					generator.IndentLine(level);
					generator.OutputStream.Write("{");
					generator.WriteNewLine();

					this.else_.GenerateCode(level, generator);

					generator.IndentLine(level);
					generator.OutputStream.Write("}");
					generator.WriteNewLine();
				}
			}
		}
Exemple #24
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (this.increaseLevel)
				level++;

			if (this.declarations.Count > 0)
			{
				foreach (CodeElement element in this.declarations)
				{
					element.GenerateCode(level, generator);
				}

				EmptyLine.SingleLine.GenerateCode(level, generator);
			}

			foreach (CodeElement element in this.innerElements)
			{
				element.GenerateCode(level, generator);
			}
		}
Exemple #25
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();

				// 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");
		}
Exemple #26
0
		public override void GenerateCode(int level, CGenerator generator)
		{
			if (!String.IsNullOrWhiteSpace(this.SwitchVar))
			{
				generator.IndentLine(level);
				generator.OutputStream.Write(String.Format("switch ({0})", this.SwitchVar));
				generator.WriteNewLine();
				generator.IndentLine(level);
				generator.OutputStream.Write("{");
				generator.WriteNewLine();

				SwitchCase defaultCase = null; // generate 'default' always as last case
				foreach (SwitchCase switchCase in this.switches)
				{
					if (switchCase.IsDefault)
					{
						defaultCase = switchCase;
					}
					else
					{
						switchCase.GenerateCode(level + 1, generator);
					}
				}
				if (defaultCase != null)
				{
					defaultCase.GenerateCode(level + 1, generator);
				}

				generator.IndentLine(level);
				generator.OutputStream.Write("}");
				generator.WriteNewLine();
			}
		}
Exemple #27
0
		public void Save(CGenerator cGenerator)
		{
			CFile cFile = new CFile();

			cFile.AddElement(new Comment("Generated by LwipMibCompiler"));
			cFile.AddElement(EmptyLine.SingleLine);

			cFile.AddElement(new PP_Include(LwipDefs.Incl_SnmpOpts));
			CodeContainerBase e = cFile.AddElement(new PP_If(LwipDefs.Opt_SnmpEnabled)) as CodeContainerBase;
			e.AddElement(EmptyLine.SingleLine);

			// include header file
			string file = cGenerator.FileName;
			if (!String.IsNullOrWhiteSpace(file))
			{
				string ext = System.IO.Path.GetExtension(file);

				string headerFile = !String.IsNullOrEmpty(ext) ? file.Substring(0, file.Length - ext.Length) : file;
				headerFile += ".h";

				e.AddElement(new PP_Include(headerFile));								
			}

			// include common snmp files
			e.AddElement(new PP_Include("lwip/apps/snmp.h"));
			e.AddElement(new PP_Include("lwip/apps/snmp_core.h"));
			e.AddElement(new PP_Include("lwip/apps/snmp_scalar.h"));
			e.AddElement(new PP_Include("lwip/apps/snmp_table.h"));

			if (this.includes.Count > 0)
			{
				e.AddElement(EmptyLine.SingleLine);
				e.AddElements(this.includes);
			}

			if (this.defines.Count > 0)
			{
				e.AddElement(EmptyLine.SingleLine);
				e.AddElements(this.defines);
			}

			if (this.declarations.Count > 0)
			{
				e.AddElement(EmptyLine.TwoLines);
				e.AddElements(this.declarations);
			}

			if (this.implementation.Count > 0)
			{
				e.AddElement(EmptyLine.TwoLines);
				e.AddElements(this.implementation);
			}

			if (this.preservedCode.Count > 0)
			{
				e.AddElement(EmptyLine.TwoLines);
				e.AddElement(new Comment(PreservedSectionHeader));
				e.AddElement(EmptyLine.SingleLine);
				e.AddElements(this.preservedCode);
			}

			cFile.Save(cGenerator);
		}