1 // HtmlAgilityPack V1.0
4 Copyright (C) 2003 Simon Mourier <simonm@microsoft.com>
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
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.
31 using System
.Diagnostics
;
34 namespace HtmlAgilityPack
36 internal struct IOLibrary
38 internal static void MakeWritable(string path
)
40 if (!File
.Exists(path
))
42 File
.SetAttributes(path
, File
.GetAttributes(path
) & ~FileAttributes
.ReadOnly
);
45 internal static void CopyAlways(string source
, string target
)
47 if (!File
.Exists(source
))
49 Directory
.CreateDirectory(Path
.GetDirectoryName(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
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
)
79 GetCurrentMethodName(2, ref name
);
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
)
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
;
116 internal static string GetOption(string name
, string def
)
119 string[] args
= Environment
.GetCommandLineArgs();
120 for (int i
=1;i
<args
.Length
;i
++)
122 GetStringArg(args
[i
], name
, ref p
);
127 internal static string GetOption(int index
, string def
)
130 string[] args
= Environment
.GetCommandLineArgs();
132 for (int i
=1;i
<args
.Length
;i
++)
134 if (GetStringArg(args
[i
], ref p
))
146 internal static bool GetOption(string name
, bool def
)
149 string[] args
= Environment
.GetCommandLineArgs();
150 for (int i
=1;i
<args
.Length
;i
++)
152 GetBoolArg(args
[i
], name
, ref p
);
157 internal static int GetOption(string name
, int def
)
160 string[] args
= Environment
.GetCommandLineArgs();
161 for (int i
=1;i
<args
.Length
;i
++)
163 GetIntArg(args
[i
], name
, ref p
);
168 private static void ParseArgs()
170 string[] args
= Environment
.GetCommandLineArgs();
171 for (int i
=1;i
<args
.Length
;i
++)
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]))
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
192 if (('/'!=Arg
[0]) && ('-'!=Arg
[0])) // not a param
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
202 if (('/'!=Arg
[0]) && ('-'!=Arg
[0])) // not a param
204 if (Arg
.Substring(1, Name
.Length
).ToLower()==Name
.ToLower())
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
212 if (('/'!=Arg
[0]) && ('-'!=Arg
[0])) // not a param
214 if (Arg
.Substring(1, Name
.Length
).ToLower()==Name
.ToLower())
218 ArgValue
= Convert
.ToInt32(Arg
.Substring(Name
.Length
+2, Arg
.Length
-Name
.Length
-2));
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
)
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
);