3 using System
.Collections
;
4 using System
.Collections
.Generic
;
11 public int get_size ()
14 case "asf_byte": return 1;
15 case "asf_word": return 2;
16 case "asf_dword": return 4;
17 case "asf_qword": return 8;
18 case "asf_guid": return 16;
20 throw new NotImplementedException ("Don't know the size of " + type
+ "!");
23 public string get_format ()
39 public string get_access (string prefix
)
44 return "asf_guid_tostring (&" + prefix
+ name
+ ")";
48 return "(asf_dword) " + prefix
+ name
;
60 static int Main (string [] args
)
64 } catch (Exception ex
) {
65 Console
.WriteLine (ex
.Message
);
66 Console
.WriteLine (ex
.StackTrace
);
71 static int main (string [] args
)
73 List
<string> structs
= ReadStructs (true);
74 List
<string> objects
= ReadStructs (false);
75 List
<string> classes
= ReadClasses ();
77 objects
.Add ("asf_object");
79 List
<string> debug_structs
= new List
<string> (objects
);
80 //debug_structs.Add ("WAVEFORMATEXTENSIBLE");
81 debug_structs
.Add ("WAVEFORMATEX");
82 debug_structs
.Add ("BITMAPINFOHEADER");
83 debug_structs
.Add ("asf_video_stream_data");
85 using (StreamWriter writer
= new StreamWriter ("asf-generated.h")) {
86 writer
.WriteLine (@"/*
90 * Moonlight List (moonlight-list@lists.ximian.com)
92 * Copyright 2007 Novell, Inc. (http://www.novell.com)
94 * See the LICENSE file included with the distribution for details.
97 writer
.WriteLine ("#ifndef _ASF_MOONLIGHT_GENERATED_H_");
98 writer
.WriteLine ("#define _ASF_MOONLIGHT_GENERATED_H_");
100 writer
.WriteLine ("");
102 foreach (string str
in structs
) {
103 writer
.WriteLine ("struct " + str
+ ";");
105 writer
.WriteLine ("");
107 foreach (string str
in classes
) {
108 writer
.WriteLine ("class " + str
+ ";");
110 writer
.WriteLine ("");
112 writer
.WriteLine (" /* Validation functions */ ");
113 writer
.WriteLine ("bool asf_object_validate_exact (const asf_object* obj, ASFParser* parser);");
114 foreach (string str
in structs
) {
115 writer
.WriteLine ("bool " + str
+ "_validate (const " + str
+ "* obj, ASFParser* parser);");
117 writer
.WriteLine ("");
119 writer
.WriteLine ("/* Debug functions */ ");
121 foreach (string str
in debug_structs
) {
122 if (!File
.ReadAllText ("asf-structures.h").Contains ("void " + str
+ "_dump (")) {
123 writer
.WriteLine ("void " + str
+ "_dump (const " + str
+ "* obj);");
126 writer
.WriteLine ("");
128 writer
.WriteLine ("void print_sizes ();");
129 writer
.WriteLine ("void asf_object_dump_exact (const asf_object* obj);");
130 writer
.WriteLine ("");
131 writer
.WriteLine ("#endif");
132 writer
.WriteLine ("");
135 using (StreamWriter writer
= new StreamWriter ("asf-generated.cpp")) {
136 writer
.WriteLine (@"/*
140 * Moonlight List (moonlight-list@lists.ximian.com)
142 * Copyright 2008 Novell, Inc. (http://www.novell.com)
144 * See the LICENSE file included with the distribution for details.
147 writer
.WriteLine ("#include <config.h>");
148 writer
.WriteLine ("#include \"asf.h\"");
150 writer
.WriteLine ("");
151 writer
.WriteLine ("void print_sizes () {");
152 foreach (string str
in objects
) {
153 writer
.WriteLine ("\tprintf (\"sizeof ({0}) = %i.\\n\", sizeof ({0}));", str
);
155 writer
.WriteLine ("}");
156 writer
.WriteLine ("");
158 writer
.WriteLine ("bool asf_object_validate_exact (const asf_object* obj, ASFParser* parser)");
159 writer
.WriteLine ("{");
160 writer
.WriteLine ("\tswitch (asf_get_guid_type (&obj->id)) {");
161 foreach (string str
in objects
) {
162 if (str
== "asf_object")
164 writer
.WriteLine ("\tcase {0}:", str
.ToUpper ());
165 writer
.WriteLine ("\t\treturn {0}_validate (({0}*) obj, parser);", str
);
167 // Special case a bit
168 writer
.WriteLine ("\tcase ASF_NONE:");
169 writer
.WriteLine ("\tcase ASF_LANGUAGE_LIST:");
170 writer
.WriteLine ("\tcase ASF_METADATA:");
171 writer
.WriteLine ("\tcase ASF_PADDING:");
172 writer
.WriteLine ("\tcase ASF_ADVANCED_MUTUAL_EXCLUSION:");
173 writer
.WriteLine ("\tcase ASF_STREAM_PRIORITIZATION:");
174 writer
.WriteLine ("\tcase ASF_INDEX_PARAMETERS:");
175 writer
.WriteLine ("\t\treturn true; // Do nothing, we don't use these objects at all, so there's no need to validate either.");
176 writer
.WriteLine ("\tdefault:");
177 writer
.WriteLine ("#if DEBUG");
178 writer
.WriteLine ("\t\tprintf (\"ASF warning: No validation implemented for %s.\\n\", asf_guid_get_name (&obj->id));");
179 writer
.WriteLine ("#endif");
180 writer
.WriteLine ("\t\treturn true;");
181 writer
.WriteLine ("\t}");
182 writer
.WriteLine ("}");
183 writer
.WriteLine ("");
185 writer
.WriteLine ("/* Debug functions */ ");
186 foreach (string str
in debug_structs
) {
187 if (!File
.ReadAllText ("asf-structures.cpp").Contains ("void " + str
+ "_dump (")) {
188 writer
.WriteLine ("void " + str
+ "_dump (const " + str
+ "* obj)");
189 writer
.WriteLine ("{");
190 writer
.WriteLine ("\tASF_DUMP (\"" + str
.ToUpper () + "\\n\");");
191 foreach (Field field
in ReadFields (str
)) {
192 writer
.WriteLine ("\tASF_DUMP (\"\\t" + field
.name
+ " = " + field
.get_format () + "\\n\", " + field
.get_access ("obj->") + ");");
194 writer
.WriteLine ("}");
195 writer
.WriteLine ("");
198 writer
.WriteLine ("");
200 writer
.WriteLine ("void asf_object_dump_exact (const asf_object* obj)");
201 writer
.WriteLine ("{");
202 writer
.WriteLine ("\tswitch (asf_get_guid_type (&obj->id)) {");
203 foreach (string str
in objects
) {
204 if (str
== "asf_object")
206 if (File
.ReadAllText ("asf-structures.h").Contains ("void " + str
+ "_dump ("))
208 writer
.WriteLine ("\tcase {0}:", str
.ToUpper ());
209 writer
.WriteLine ("\t\t{0}_dump (({0}*) obj); break;", str
);
211 writer
.WriteLine ("\tdefault:");
212 writer
.WriteLine ("\t\tasf_object_dump (obj); break;");
213 writer
.WriteLine ("\t}");
214 writer
.WriteLine ("}");
215 writer
.WriteLine ("");
217 writer
.WriteLine ("/* ");
218 writer
.WriteLine (" Some almost read to use copy-and-paste code.");
219 writer
.WriteLine ("");
220 writer
.WriteLine ("");
221 foreach (string str
in objects
) {
224 foreach (Field field
in ReadFields (str
)) {
225 size
+= field
.get_size ();
230 writer
.WriteLine ("bool " + str
+ "_validate (const " + str
+ "* obj, ASFParser* parser)");
231 writer
.WriteLine ("{");
232 writer
.WriteLine ("\tif (!(asf_guid_validate (&obj->id, &" + str
.Replace ("asf_", "asf_guids_") + ", parser))) {");
233 writer
.WriteLine ("\t\treturn false;");
234 writer
.WriteLine ("\t}");
235 writer
.WriteLine ("\t// FIXME: Verify that this size is correct.");
236 writer
.WriteLine ("\tif (obj->size < " + size
.ToString () + ") {");
237 writer
.WriteLine ("\t\tparser->AddError (g_strdup_printf (\"Invalid size (expected >= " + size
.ToString () + ", got %llu).\", obj->size));");
238 writer
.WriteLine ("\t\treturn false;");
239 writer
.WriteLine ("\t}");
240 writer
.WriteLine ("\t// TODO: More verifications?");
241 writer
.WriteLine ("\treturn true;");
242 writer
.WriteLine ("}");
243 writer
.WriteLine ("");
245 writer
.WriteLine ("");
246 writer
.WriteLine ("*/");
248 writer
.WriteLine ("");
253 static List
<string> ReadAllLines (string pattern
)
255 List
<string> result
= new List
<string> ();
256 foreach (string file
in Directory
.GetFiles (pattern
)) {
257 if (!file
.Contains ("asf-generated."))
258 result
.AddRange (File
.ReadAllLines (file
));
263 static List
<Field
> ReadFields (string structure
)
265 List
<Field
> result
= new List
<Field
> ();
266 bool in_struct
= false;
268 //bool is_obj = false;
269 foreach (string l
in ReadAllLines ("asf-structures.h")) {
274 if (line
.Contains ("//"))
275 line
= line
.Substring (0, line
.IndexOf ("//"));
280 if (line
.EndsWith (";"))
283 if (!line
.StartsWith ("struct " + structure
+ " "))
288 if (line
.Contains (" : public asf_object")) {
290 field
= new Field ();
291 field
.type
= "asf_guid";
294 field
= new Field ();
295 field
.type
= "asf_qword";
300 if (line
== string.Empty
|| line
== "}" || line
== "};")
303 line
= line
.TrimEnd (';');
304 string [] vars
= line
.Split (' ');
305 if (vars
.Length
== 2) {
306 Field field
= new Field ();
307 field
.type
= vars
[0];
308 field
.name
= vars
[1];
311 Console
.WriteLine ("Weird line in asf-structures.h: {0} '{1}', '{2}'", line_number
, l
, line
);
318 static List
<string> ReadStructs (bool all
)
320 List
<string> result
= new List
<string> ();
322 foreach (string l
in ReadAllLines ("asf-structures.h")) {
326 if (line
.EndsWith (";"))
329 if (!line
.StartsWith ("struct "))
332 if (!(all
|| line
.EndsWith (" : public asf_object {")))
335 result
.Add (line
.Substring (7, line
.IndexOf (' ', 8) - 7));
340 static List
<string> ReadClasses ()
342 List
<string> result
= new List
<string> ();
343 foreach (string l
in ReadAllLines ("asf.h")) {
346 if (!(line
.StartsWith ("class ") && !line
.EndsWith (";")))
349 result
.Add (line
.Substring (6, line
.IndexOf (' ', 8) - 6));