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 |
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using CCodeGeneration;
|
---|
35 | using System;
|
---|
36 | using System.IO;
|
---|
37 |
|
---|
38 | namespace LwipSnmpCodeGeneration
|
---|
39 | {
|
---|
40 | public class MibCFile
|
---|
41 | {
|
---|
42 | #region Fields
|
---|
43 |
|
---|
44 | private const string PreservedSectionMarker = "LWIP MIB generator - preserved section begin";
|
---|
45 | private const string PreservedSectionHeader =
|
---|
46 | "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" +
|
---|
47 | PreservedSectionMarker + "\n" +
|
---|
48 | "Code below is preserved on regeneration. Remove these comment lines to regenerate code.\n" +
|
---|
49 | "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
|
---|
50 |
|
---|
51 | private readonly List<CodeElement> includes = new List<CodeElement>();
|
---|
52 | private readonly List<CodeElement> defines = new List<CodeElement>();
|
---|
53 | private readonly List<CodeElement> declarations = new List<CodeElement>();
|
---|
54 | private readonly List<CodeElement> implementation = new List<CodeElement>();
|
---|
55 | private readonly List<CodeElement> preservedCode = new List<CodeElement>();
|
---|
56 |
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | public MibCFile()
|
---|
60 | {
|
---|
61 | }
|
---|
62 |
|
---|
63 | #region Accessors
|
---|
64 |
|
---|
65 | public List<CodeElement> Includes
|
---|
66 | {
|
---|
67 | get { return this.includes; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public List<CodeElement> Defines
|
---|
71 | {
|
---|
72 | get { return this.defines; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public List<CodeElement> Declarations
|
---|
76 | {
|
---|
77 | get { return this.declarations; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public List<CodeElement> Implementation
|
---|
81 | {
|
---|
82 | get { return this.implementation; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public List<CodeElement> PreservedCode
|
---|
86 | {
|
---|
87 | get { return this.preservedCode; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | #region Methods
|
---|
93 |
|
---|
94 | public void Save(CGenerator cGenerator)
|
---|
95 | {
|
---|
96 | CFile cFile = new CFile();
|
---|
97 |
|
---|
98 | cFile.AddElement(new Comment("Generated by LwipMibCompiler"));
|
---|
99 | cFile.AddElement(EmptyLine.SingleLine);
|
---|
100 |
|
---|
101 | cFile.AddElement(new PP_Include(LwipDefs.Incl_SnmpOpts));
|
---|
102 | CodeContainerBase e = cFile.AddElement(new PP_If(LwipDefs.Opt_SnmpEnabled)) as CodeContainerBase;
|
---|
103 | e.AddElement(EmptyLine.SingleLine);
|
---|
104 |
|
---|
105 | // include header file
|
---|
106 | string file = cGenerator.FileName;
|
---|
107 | if (!String.IsNullOrWhiteSpace(file))
|
---|
108 | {
|
---|
109 | string ext = System.IO.Path.GetExtension(file);
|
---|
110 |
|
---|
111 | string headerFile = !String.IsNullOrEmpty(ext) ? file.Substring(0, file.Length - ext.Length) : file;
|
---|
112 | headerFile += ".h";
|
---|
113 |
|
---|
114 | e.AddElement(new PP_Include(headerFile));
|
---|
115 | }
|
---|
116 |
|
---|
117 | // include common snmp files
|
---|
118 | e.AddElement(new PP_Include("lwip/apps/snmp.h"));
|
---|
119 | e.AddElement(new PP_Include("lwip/apps/snmp_core.h"));
|
---|
120 | e.AddElement(new PP_Include("lwip/apps/snmp_scalar.h"));
|
---|
121 | e.AddElement(new PP_Include("lwip/apps/snmp_table.h"));
|
---|
122 |
|
---|
123 | if (this.includes.Count > 0)
|
---|
124 | {
|
---|
125 | e.AddElement(EmptyLine.SingleLine);
|
---|
126 | e.AddElements(this.includes);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (this.defines.Count > 0)
|
---|
130 | {
|
---|
131 | e.AddElement(EmptyLine.SingleLine);
|
---|
132 | e.AddElements(this.defines);
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (this.declarations.Count > 0)
|
---|
136 | {
|
---|
137 | e.AddElement(EmptyLine.TwoLines);
|
---|
138 | e.AddElements(this.declarations);
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (this.implementation.Count > 0)
|
---|
142 | {
|
---|
143 | e.AddElement(EmptyLine.TwoLines);
|
---|
144 | e.AddElements(this.implementation);
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (this.preservedCode.Count > 0)
|
---|
148 | {
|
---|
149 | e.AddElement(EmptyLine.TwoLines);
|
---|
150 | e.AddElement(new Comment(PreservedSectionHeader));
|
---|
151 | e.AddElement(EmptyLine.SingleLine);
|
---|
152 | e.AddElements(this.preservedCode);
|
---|
153 | }
|
---|
154 |
|
---|
155 | cFile.Save(cGenerator);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public static string GetPreservedCode(string file)
|
---|
159 | {
|
---|
160 | if (File.Exists(file))
|
---|
161 | {
|
---|
162 | using (StreamReader fileStream = new StreamReader(file))
|
---|
163 | {
|
---|
164 | while (!fileStream.EndOfStream)
|
---|
165 | {
|
---|
166 | string line = fileStream.ReadLine();
|
---|
167 | if (line == PreservedSectionMarker)
|
---|
168 | {
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (!fileStream.EndOfStream)
|
---|
174 | {
|
---|
175 | // skip the rest of the comment + spacer line
|
---|
176 | fileStream.ReadLine(); // "Code below is preserved...
|
---|
177 | fileStream.ReadLine(); // "+++++++++++++++++++++++...
|
---|
178 | fileStream.ReadLine(); // */
|
---|
179 | fileStream.ReadLine(); //
|
---|
180 |
|
---|
181 | string preservedCode = fileStream.ReadToEnd();
|
---|
182 |
|
---|
183 | int lastEndif = preservedCode.LastIndexOf("#endif", StringComparison.Ordinal);
|
---|
184 | preservedCode = preservedCode.Remove(lastEndif);
|
---|
185 |
|
---|
186 | return preservedCode;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | return null;
|
---|
192 | }
|
---|
193 |
|
---|
194 | #endregion
|
---|
195 | }
|
---|
196 | }
|
---|