2009-08-26 Chris Toshok <toshok@ximian.com>
[moon.git] / src / asf / asf.cs
blobff94832a8c8f901e10ff7e8c5c56379a0071f5b0
1 using System;
2 using System.IO;
3 using System.Collections;
4 using System.Collections.Generic;
6 class asf
8 class Field {
9 public string name;
10 public string type;
11 public int get_size ()
13 switch (type) {
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;
19 default:
20 throw new NotImplementedException ("Don't know the size of " + type + "!");
23 public string get_format ()
25 switch (type) {
26 case "asf_byte":
27 case "asf_word":
28 case "asf_dword":
29 return "%u";
30 case "asf_qword":
31 return "%llu";
32 case "asf_guid":
33 case "const char*":
34 return "%s";
35 default:
36 return "%p";
39 public string get_access (string prefix)
42 switch (type) {
43 case "asf_guid":
44 return "asf_guid_tostring (&" + prefix + name + ")";
45 case "asf_byte":
46 case "asf_word":
47 case "asf_dword":
48 return "(asf_dword) " + prefix + name;
49 //return "%i";
50 case "asf_qword":
51 //return "%lld";
52 case "const char*":
53 //return "%s";
54 default:
55 return prefix + name;
60 static int Main (string [] args)
62 try {
63 return main (args);
64 } catch (Exception ex) {
65 Console.WriteLine (ex.Message);
66 Console.WriteLine (ex.StackTrace);
67 return 1;
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 (@"/*
87 * asf-generated.h:
89 * Contact:
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.
96 ");
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 (@"/*
137 * asf-generated.cpp:
139 * Contact:
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")
163 continue;
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")
205 continue;
206 if (File.ReadAllText ("asf-structures.h").Contains ("void " + str + "_dump ("))
207 continue;
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) {
222 int size = 16 + 8;
223 try {
224 foreach (Field field in ReadFields (str)) {
225 size += field.get_size ();
227 } catch {
228 continue;
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 ("");
250 return 0;
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));
260 return result;
263 static List<Field> ReadFields (string structure)
265 List<Field> result = new List<Field> ();
266 bool in_struct = false;
267 int line_number = 0;
268 //bool is_obj = false;
269 foreach (string l in ReadAllLines ("asf-structures.h")) {
270 string line = l;
271 line = line.Trim ();
272 line_number++;
274 if (line.Contains ("//"))
275 line = line.Substring (0, line.IndexOf ("//"));
277 line = line.Trim ();
279 if (!in_struct) {
280 if (line.EndsWith (";"))
281 continue;
283 if (!line.StartsWith ("struct " + structure + " "))
284 continue;
286 in_struct = true;
288 if (line.Contains (" : public asf_object")) {
289 Field field;
290 field = new Field ();
291 field.type = "asf_guid";
292 field.name = "id";
293 result.Add (field);
294 field = new Field ();
295 field.type = "asf_qword";
296 field.name = "size";
297 result.Add (field);
299 } else {
300 if (line == string.Empty || line == "}" || line == "};")
301 break;
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];
309 result.Add (field);
310 } else {
311 Console.WriteLine ("Weird line in asf-structures.h: {0} '{1}', '{2}'", line_number, l, line);
315 return result;
318 static List<string> ReadStructs (bool all)
320 List<string> result = new List<string> ();
322 foreach (string l in ReadAllLines ("asf-structures.h")) {
323 string line = l;
324 line = line.Trim ();
326 if (line.EndsWith (";"))
327 continue;
329 if (!line.StartsWith ("struct "))
330 continue;
332 if (!(all || line.EndsWith (" : public asf_object {")))
333 continue;
335 result.Add (line.Substring (7, line.IndexOf (' ', 8) - 7));
337 return result;
340 static List<string> ReadClasses ()
342 List<string> result = new List<string> ();
343 foreach (string l in ReadAllLines ("asf.h")) {
344 string line = l;
345 line = line.Trim ();
346 if (!(line.StartsWith ("class ") && !line.EndsWith (";")))
347 continue;
349 result.Add (line.Substring (6, line.IndexOf (' ', 8) - 6));
351 return result;