MibCFile.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Martin Hentschel <info@cl-soft.de>
  30. *
  31. */
  32. using System.Collections.Generic;
  33. using CCodeGeneration;
  34. using System;
  35. using System.IO;
  36. namespace LwipSnmpCodeGeneration
  37. {
  38. public class MibCFile
  39. {
  40. #region Fields
  41. private const string PreservedSectionMarker = "LWIP MIB generator - preserved section begin";
  42. private const string PreservedSectionHeader =
  43. "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" +
  44. PreservedSectionMarker + "\n" +
  45. "Code below is preserved on regeneration. Remove these comment lines to regenerate code.\n" +
  46. "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
  47. private readonly List<CodeElement> includes = new List<CodeElement>();
  48. private readonly List<CodeElement> defines = new List<CodeElement>();
  49. private readonly List<CodeElement> declarations = new List<CodeElement>();
  50. private readonly List<CodeElement> implementation = new List<CodeElement>();
  51. private readonly List<CodeElement> preservedCode = new List<CodeElement>();
  52. #endregion
  53. public MibCFile()
  54. {
  55. }
  56. #region Accessors
  57. public List<CodeElement> Includes
  58. {
  59. get { return this.includes; }
  60. }
  61. public List<CodeElement> Defines
  62. {
  63. get { return this.defines; }
  64. }
  65. public List<CodeElement> Declarations
  66. {
  67. get { return this.declarations; }
  68. }
  69. public List<CodeElement> Implementation
  70. {
  71. get { return this.implementation; }
  72. }
  73. public List<CodeElement> PreservedCode
  74. {
  75. get { return this.preservedCode; }
  76. }
  77. #endregion
  78. #region Methods
  79. public void Save(CGenerator cGenerator)
  80. {
  81. CFile cFile = new CFile();
  82. cFile.AddElement(new Comment("Generated by LwipMibCompiler"));
  83. cFile.AddElement(EmptyLine.SingleLine);
  84. cFile.AddElement(new PP_Include(LwipDefs.Incl_SnmpOpts));
  85. CodeContainerBase e = cFile.AddElement(new PP_If(LwipDefs.Opt_SnmpEnabled)) as CodeContainerBase;
  86. e.AddElement(EmptyLine.SingleLine);
  87. // include header file
  88. string file = cGenerator.FileName;
  89. if (!String.IsNullOrWhiteSpace(file))
  90. {
  91. string ext = System.IO.Path.GetExtension(file);
  92. string headerFile = !String.IsNullOrEmpty(ext) ? file.Substring(0, file.Length - ext.Length) : file;
  93. headerFile += ".h";
  94. e.AddElement(new PP_Include(headerFile));
  95. }
  96. // include common snmp files
  97. e.AddElement(new PP_Include("lwip/apps/snmp.h"));
  98. e.AddElement(new PP_Include("lwip/apps/snmp_core.h"));
  99. e.AddElement(new PP_Include("lwip/apps/snmp_scalar.h"));
  100. e.AddElement(new PP_Include("lwip/apps/snmp_table.h"));
  101. if (this.includes.Count > 0)
  102. {
  103. e.AddElement(EmptyLine.SingleLine);
  104. e.AddElements(this.includes);
  105. }
  106. if (this.defines.Count > 0)
  107. {
  108. e.AddElement(EmptyLine.SingleLine);
  109. e.AddElements(this.defines);
  110. }
  111. if (this.declarations.Count > 0)
  112. {
  113. e.AddElement(EmptyLine.TwoLines);
  114. e.AddElements(this.declarations);
  115. }
  116. if (this.implementation.Count > 0)
  117. {
  118. e.AddElement(EmptyLine.TwoLines);
  119. e.AddElements(this.implementation);
  120. }
  121. if (this.preservedCode.Count > 0)
  122. {
  123. e.AddElement(EmptyLine.TwoLines);
  124. e.AddElement(new Comment(PreservedSectionHeader));
  125. e.AddElement(EmptyLine.SingleLine);
  126. e.AddElements(this.preservedCode);
  127. }
  128. cFile.Save(cGenerator);
  129. }
  130. public static string GetPreservedCode(string file)
  131. {
  132. if (File.Exists(file))
  133. {
  134. using (StreamReader fileStream = new StreamReader(file))
  135. {
  136. while (!fileStream.EndOfStream)
  137. {
  138. string line = fileStream.ReadLine();
  139. if (line == PreservedSectionMarker)
  140. {
  141. break;
  142. }
  143. }
  144. if (!fileStream.EndOfStream)
  145. {
  146. // skip the rest of the comment + spacer line
  147. fileStream.ReadLine(); // "Code below is preserved...
  148. fileStream.ReadLine(); // "+++++++++++++++++++++++...
  149. fileStream.ReadLine(); // */
  150. fileStream.ReadLine(); //
  151. string preservedCode = fileStream.ReadToEnd();
  152. int lastEndif = preservedCode.LastIndexOf("#endif", StringComparison.Ordinal);
  153. preservedCode = preservedCode.Remove(lastEndif);
  154. return preservedCode;
  155. }
  156. }
  157. }
  158. return null;
  159. }
  160. #endregion
  161. }
  162. }