tools.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // HtmlAgilityPack V1.0 - Simon Mourier <[email protected]>
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. namespace HtmlAgilityPack
  6. {
  7. internal struct IOLibrary
  8. {
  9. internal static void MakeWritable(string path)
  10. {
  11. if (!File.Exists(path))
  12. return;
  13. File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
  14. }
  15. internal static void CopyAlways(string source, string target)
  16. {
  17. if (!File.Exists(source))
  18. return;
  19. Directory.CreateDirectory(Path.GetDirectoryName(target));
  20. MakeWritable(target);
  21. File.Copy(source, target, true);
  22. }
  23. }
  24. #if TARGET_JVM1
  25. internal struct HtmlLibrary
  26. {
  27. [Conditional("DEBUG")]
  28. internal static void GetVersion(out string version)
  29. {
  30. System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(1, true);
  31. version = sf.GetMethod().DeclaringType.Assembly.GetName().Version.ToString();
  32. }
  33. [Conditional("DEBUG")]
  34. [Conditional("TRACE")]
  35. internal static void Trace(object Value)
  36. {
  37. // category is the method
  38. string name = null;
  39. GetCurrentMethodName(2, out name);
  40. System.Diagnostics.Trace.WriteLine(Value, name);
  41. }
  42. [Conditional("DEBUG")]
  43. [Conditional("TRACE")]
  44. internal static void TraceStackFrame(int steps)
  45. {
  46. string name = null;
  47. GetCurrentMethodName(2, out name);
  48. string trace = "";
  49. for(int i=1;i<steps;i++)
  50. {
  51. System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(i, true);
  52. trace += sf.ToString();
  53. }
  54. System.Diagnostics.Trace.WriteLine(trace, name);
  55. System.Diagnostics.Trace.WriteLine("");
  56. }
  57. [Conditional("DEBUG")]
  58. internal static void GetCurrentMethodName(out string name)
  59. {
  60. name = null;
  61. GetCurrentMethodName(2, out name);
  62. }
  63. [Conditional("DEBUG")]
  64. internal static void GetCurrentMethodName(int skipframe, out string name)
  65. {
  66. StackFrame sf = new StackFrame(skipframe, true);
  67. name = sf.GetMethod().DeclaringType.Name + "." + sf.GetMethod().Name;
  68. }
  69. }
  70. #endif
  71. internal class HtmlCmdLine
  72. {
  73. static internal bool Help;
  74. static HtmlCmdLine()
  75. {
  76. Help = false;
  77. ParseArgs();
  78. }
  79. internal static string GetOption(string name, string def)
  80. {
  81. string p = def;
  82. string[] args = Environment.GetCommandLineArgs();
  83. for (int i=1;i<args.Length;i++)
  84. {
  85. GetStringArg(args[i], name, ref p);
  86. }
  87. return p;
  88. }
  89. internal static string GetOption(int index, string def)
  90. {
  91. string p = def;
  92. string[] args = Environment.GetCommandLineArgs();
  93. int j = 0;
  94. for (int i=1;i<args.Length;i++)
  95. {
  96. if (GetStringArg(args[i], ref p))
  97. {
  98. if (index==j)
  99. return p;
  100. else
  101. p = def;
  102. j++;
  103. }
  104. }
  105. return p;
  106. }
  107. internal static bool GetOption(string name, bool def)
  108. {
  109. bool p = def;
  110. string[] args = Environment.GetCommandLineArgs();
  111. for (int i=1;i<args.Length;i++)
  112. {
  113. GetBoolArg(args[i], name, ref p);
  114. }
  115. return p;
  116. }
  117. internal static int GetOption(string name, int def)
  118. {
  119. int p = def;
  120. string[] args = Environment.GetCommandLineArgs();
  121. for (int i=1;i<args.Length;i++)
  122. {
  123. GetIntArg(args[i], name, ref p);
  124. }
  125. return p;
  126. }
  127. private static void ParseArgs()
  128. {
  129. string[] args = Environment.GetCommandLineArgs();
  130. for (int i=1;i<args.Length;i++)
  131. {
  132. // help
  133. GetBoolArg(args[i], "?", ref Help);
  134. GetBoolArg(args[i], "h", ref Help);
  135. GetBoolArg(args[i], "help", ref Help);
  136. }
  137. }
  138. private static bool GetStringArg(string Arg, ref string ArgValue)
  139. {
  140. if (('/'==Arg[0]) || ('-'==Arg[0]))
  141. return false;
  142. ArgValue = Arg;
  143. return true;
  144. }
  145. private static void GetStringArg(string Arg, string Name, ref string ArgValue)
  146. {
  147. if (Arg.Length<(Name.Length+3)) // -name:x is 3 more than name
  148. return;
  149. if (('/'!=Arg[0]) && ('-'!=Arg[0])) // not a param
  150. return;
  151. if (Arg.Substring(1, Name.Length).ToLower()==Name.ToLower())
  152. ArgValue = Arg.Substring(Name.Length+2, Arg.Length-Name.Length-2);
  153. }
  154. private static void GetBoolArg(string Arg, string Name, ref bool ArgValue)
  155. {
  156. if (Arg.Length<(Name.Length+1)) // -name is 1 more than name
  157. return;
  158. if (('/'!=Arg[0]) && ('-'!=Arg[0])) // not a param
  159. return;
  160. if (Arg.Substring(1, Name.Length).ToLower()==Name.ToLower())
  161. ArgValue = true;
  162. }
  163. private static void GetIntArg(string Arg, string Name, ref int ArgValue)
  164. {
  165. if (Arg.Length<(Name.Length+3)) // -name:12 is 3 more than name
  166. return;
  167. if (('/'!=Arg[0]) && ('-'!=Arg[0])) // not a param
  168. return;
  169. if (Arg.Substring(1, Name.Length).ToLower()==Name.ToLower())
  170. {
  171. try
  172. {
  173. ArgValue = Convert.ToInt32(Arg.Substring(Name.Length+2, Arg.Length-Name.Length-2));
  174. }
  175. catch
  176. {
  177. }
  178. }
  179. }
  180. }
  181. internal class HtmlConsoleListener: System.Diagnostics.TraceListener
  182. {
  183. public override void WriteLine(string Message)
  184. {
  185. Write(Message + "\n");
  186. }
  187. public override void Write(string Message)
  188. {
  189. Write(Message, "");
  190. }
  191. public override void Write(string Message, string Category)
  192. {
  193. Console.Write("T:" + Category + ": " + Message);
  194. }
  195. public override void WriteLine(string Message, string Category)
  196. {
  197. Write(Message + "\n", Category);
  198. }
  199. }
  200. }