2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / Filters / HtmlAgilityPack / tools.cs
blobedaa02eb584a1843d0a0a934fe04a615db595250
1 // HtmlAgilityPack V1.0
3 /*
4 Copyright (C) 2003 Simon Mourier <simonm@microsoft.com>
5 All rights reserved.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. The name of the author may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 using System;
31 using System.Diagnostics;
32 using System.IO;
34 namespace HtmlAgilityPack
36 internal struct IOLibrary
38 internal static void MakeWritable(string path)
40 if (!File.Exists(path))
41 return;
42 File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
45 internal static void CopyAlways(string source, string target)
47 if (!File.Exists(source))
48 return;
49 Directory.CreateDirectory(Path.GetDirectoryName(target));
50 MakeWritable(target);
51 File.Copy(source, target, true);
55 internal struct HtmlLibrary
57 [Conditional("DEBUG")]
58 internal static void GetVersion(ref string version)
60 System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(1, true);
61 version = sf.GetMethod().DeclaringType.Assembly.GetName().Version.ToString();
64 [Conditional("DEBUG")]
65 [Conditional("TRACE")]
66 internal static void Trace(object Value)
68 // category is the method
69 string name = null;
70 GetCurrentMethodName(2, ref name);
71 System.Diagnostics.Trace.WriteLine(Value, name);
74 [Conditional("DEBUG")]
75 [Conditional("TRACE")]
76 internal static void TraceStackFrame(int steps)
78 string name = null;
79 GetCurrentMethodName(2, ref name);
80 string trace = "";
81 for(int i=1;i<steps;i++)
83 System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(i, true);
84 trace += sf.ToString();
86 System.Diagnostics.Trace.WriteLine(trace, name);
87 System.Diagnostics.Trace.WriteLine("");
90 [Conditional("DEBUG")]
91 internal static void GetCurrentMethodName(ref string name)
93 name = null;
94 GetCurrentMethodName(2, ref name);
97 [Conditional("DEBUG")]
98 internal static void GetCurrentMethodName(int skipframe, ref string name)
100 StackFrame sf = new StackFrame(skipframe, true);
101 name = sf.GetMethod().DeclaringType.Name + "." + sf.GetMethod().Name;
106 internal class HtmlCmdLine
108 static internal bool Help;
110 static HtmlCmdLine()
112 Help = false;
113 ParseArgs();
116 internal static string GetOption(string name, string def)
118 string p = def;
119 string[] args = Environment.GetCommandLineArgs();
120 for (int i=1;i<args.Length;i++)
122 GetStringArg(args[i], name, ref p);
124 return p;
127 internal static string GetOption(int index, string def)
129 string p = def;
130 string[] args = Environment.GetCommandLineArgs();
131 int j = 0;
132 for (int i=1;i<args.Length;i++)
134 if (GetStringArg(args[i], ref p))
136 if (index==j)
137 return p;
138 else
139 p = def;
140 j++;
143 return p;
146 internal static bool GetOption(string name, bool def)
148 bool p = def;
149 string[] args = Environment.GetCommandLineArgs();
150 for (int i=1;i<args.Length;i++)
152 GetBoolArg(args[i], name, ref p);
154 return p;
157 internal static int GetOption(string name, int def)
159 int p = def;
160 string[] args = Environment.GetCommandLineArgs();
161 for (int i=1;i<args.Length;i++)
163 GetIntArg(args[i], name, ref p);
165 return p;
168 private static void ParseArgs()
170 string[] args = Environment.GetCommandLineArgs();
171 for (int i=1;i<args.Length;i++)
173 // help
174 GetBoolArg(args[i], "?", ref Help);
175 GetBoolArg(args[i], "h", ref Help);
176 GetBoolArg(args[i], "help", ref Help);
180 private static bool GetStringArg(string Arg, ref string ArgValue)
182 if (('/'==Arg[0]) || ('-'==Arg[0]))
183 return false;
184 ArgValue = Arg;
185 return true;
188 private static void GetStringArg(string Arg, string Name, ref string ArgValue)
190 if (Arg.Length<(Name.Length+3)) // -name:x is 3 more than name
191 return;
192 if (('/'!=Arg[0]) && ('-'!=Arg[0])) // not a param
193 return;
194 if (Arg.Substring(1, Name.Length).ToLower()==Name.ToLower())
195 ArgValue = Arg.Substring(Name.Length+2, Arg.Length-Name.Length-2);
198 private static void GetBoolArg(string Arg, string Name, ref bool ArgValue)
200 if (Arg.Length<(Name.Length+1)) // -name is 1 more than name
201 return;
202 if (('/'!=Arg[0]) && ('-'!=Arg[0])) // not a param
203 return;
204 if (Arg.Substring(1, Name.Length).ToLower()==Name.ToLower())
205 ArgValue = true;
208 private static void GetIntArg(string Arg, string Name, ref int ArgValue)
210 if (Arg.Length<(Name.Length+3)) // -name:12 is 3 more than name
211 return;
212 if (('/'!=Arg[0]) && ('-'!=Arg[0])) // not a param
213 return;
214 if (Arg.Substring(1, Name.Length).ToLower()==Name.ToLower())
218 ArgValue = Convert.ToInt32(Arg.Substring(Name.Length+2, Arg.Length-Name.Length-2));
220 catch
228 internal class HtmlConsoleListener: System.Diagnostics.TraceListener
230 public override void WriteLine(string Message)
232 Write(Message + "\n");
235 public override void Write(string Message)
237 Write(Message, "");
240 public override void Write(string Message, string Category)
242 Console.Write("T:" + Category + ": " + Message);
245 public override void WriteLine(string Message, string Category)
247 Write(Message + "\n", Category);