1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
// gapi-parser.cs - parsing driver application.
//
// Author: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2005 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
namespace GtkSharp.Parsing {
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml;
public class Parser {
[DllImport ("libc")]
static extern int system (string command);
public static int Main (string[] args)
{
if (args.Length < 1) {
Console.WriteLine ("Usage: gapi2-parser <filename>");
return 0;
}
XmlDocument src_doc = new XmlDocument ();
string input_path = null;
string pl_path = null;
if (args.Length > 1) {
pl_path = args[0];
input_path = args[1];
} else {
input_path = args[0];
}
try {
using (Stream stream = File.OpenRead (input_path))
src_doc.Load (stream);
} catch (XmlException e) {
Console.WriteLine ("Couldn't open source file.");
Console.WriteLine (e);
return 1;
}
XmlNode root = src_doc.DocumentElement;
if (root.Name != "gapi-parser-input") {
Console.WriteLine ("Improperly formatted input file: " + input_path);
return 1;
}
foreach (XmlNode apinode in root.ChildNodes) {
if (apinode.Name != "api")
continue;
string outfile = (apinode as XmlElement).GetAttribute ("filename");
string prefile = outfile + ".pre";
if (File.Exists (prefile))
File.Delete (prefile);
foreach (XmlNode libnode in apinode.ChildNodes) {
if (libnode.Name != "library")
continue;
string lib = (libnode as XmlElement).GetAttribute ("name");
foreach (XmlNode nsnode in libnode.ChildNodes) {
if (nsnode.Name != "namespace")
continue;
string ns = (nsnode as XmlElement).GetAttribute ("name");
ArrayList files = new ArrayList ();
Hashtable excludes = new Hashtable ();
foreach (XmlNode srcnode in nsnode.ChildNodes) {
if (!(srcnode is XmlElement))
continue;
XmlElement elem = srcnode as XmlElement;
switch (srcnode.Name) {
case "dir":
string dir = elem.InnerXml;
Console.Write ("<dir {0}> ", dir);
DirectoryInfo di = new DirectoryInfo (dir);
foreach (FileInfo file in di.GetFiles ("*.c"))
files.Add (dir + Path.DirectorySeparatorChar + file.Name);
foreach (FileInfo file in di.GetFiles ("*.h"))
files.Add (dir + Path.DirectorySeparatorChar + file.Name);
break;
case "file":
string incfile = elem.InnerXml;
Console.Write ("<file {0}> ", incfile);
files.Add (incfile);
break;
case "exclude":
string excfile = elem.InnerXml;
Console.Write ("<exclude {0}> ", excfile);
excludes [excfile] = 1;
break;
case "directory":
string dir_path = elem.GetAttribute ("path");
Console.Write ("<directory {0}: excluding ", dir_path);
Hashtable excs = new Hashtable ();
foreach (XmlNode exc_node in srcnode.ChildNodes) {
if (exc_node.Name != "exclude")
continue;
string excfilename = (exc_node as XmlElement).InnerXml;
Console.Write (excfilename + " ");
excs [excfilename] = 1;
}
DirectoryInfo dinfo = new DirectoryInfo (dir_path);
foreach (FileInfo file in dinfo.GetFiles ("*.c")) {
if (excs.Contains (file.Name))
continue;
files.Add (dir_path + Path.DirectorySeparatorChar + file.Name);
}
foreach (FileInfo file in dinfo.GetFiles ("*.h")) {
if (excs.Contains (file.Name))
continue;
files.Add (dir_path + Path.DirectorySeparatorChar + file.Name);
}
Console.Write ("> ");
break;
default:
Console.WriteLine ("Invalid source: " + srcnode.Name);
break;
}
}
Console.WriteLine ();
if (files.Count == 0)
continue;
ArrayList realfiles = new ArrayList ();
foreach (string file in files) {
string trimfile = file.TrimEnd ();
if (excludes.Contains (trimfile))
continue;
realfiles.Add (trimfile);
}
string[] filenames = (string[]) realfiles.ToArray (typeof (string));
string pp_args = String.Join (" ", filenames);
system (pl_path + "gapi_pp.pl " + pp_args + " | " + pl_path + "gapi2xml.pl " + ns + " " + prefile + " " + lib);
}
}
XmlDocument final = new XmlDocument ();
final.Load (prefile);
XmlTextWriter writer = new XmlTextWriter (outfile, null);
writer.Formatting = Formatting.Indented;
final.Save (writer);
File.Delete (prefile);
}
return 0;
}
}
}
|