Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.3 KiB

  1. /*+*******************************************************************************************
  2. Project : StringCheck
  3. File : filescan.c
  4. Summary : This class scans a file for instances of a search string and appends
  5. the filename, line# and matched string to a file.
  6. Classes / Fcns :
  7. Notes / Revisions :
  8. *******************************************************************************************+*/
  9. using System;
  10. using System.IO;
  11. using System.Text.RegularExpressions;
  12. public class FileScan
  13. {
  14. /*---------------------------------------------------------
  15. Constructors
  16. ----------------------------------------------------------*/
  17. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++
  18. Method: public FileScan()
  19. Summary:
  20. Args:
  21. Modifies:
  22. Returns:
  23. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  24. public FileScan()
  25. {
  26. }
  27. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++
  28. Method: public FileScan(string InputFile, string OutputFile, string SearchString)
  29. Summary:
  30. Args:
  31. Modifies:
  32. Returns:
  33. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  34. public FileScan(string InputFile, string OutputFile, string SearchString)
  35. {
  36. Setup(InputFile, OutputFile, SearchString);
  37. }
  38. /*---------------------------------------------------------
  39. Public Methods
  40. ----------------------------------------------------------*/
  41. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++
  42. Method: public void Setup(string InputFile, string OutputFile, string SearchString)
  43. Summary:
  44. Args:
  45. Modifies:
  46. Returns:
  47. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  48. public void Setup(string InputFile, string OutputFile, string SearchString)
  49. {
  50. this.InputFile = InputFile;
  51. this.OutputFile = OutputFile;
  52. this.SearchString = SearchString;
  53. }
  54. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++
  55. Method: public int ScanFile()
  56. Summary:
  57. Args:
  58. Modifies:
  59. Returns:
  60. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  61. public int ScanFile()
  62. {
  63. /*---------------------------------------------------------
  64. To Do: Need error checking
  65. ----------------------------------------------------------*/
  66. StreamWriter OutputStream;
  67. StreamReader InputStream;
  68. String line, OutputString;
  69. int linenum, padding, i ;
  70. Match m;
  71. Regex r;
  72. bool excludeLine;
  73. // Open log file and input file
  74. OutputStream = File.AppendText(OutputFile);
  75. InputStream = File.OpenText(InputFile);
  76. // Build regex expression
  77. // System.ArgumentException
  78. try
  79. {
  80. r = new Regex(SearchString);
  81. }
  82. catch ( System.ArgumentException )
  83. {
  84. Console.WriteLine("FileScan:ScanFile() - ERROR - \"{0}\" is not a valid regex expression", SearchString);
  85. return 0;
  86. }
  87. // Read input file line by line - repeat until end of file
  88. Console.WriteLine("Scanning {0} for {1}", InputFile, SearchString);
  89. linenum = 0;
  90. while ((line=InputStream.ReadLine())!=null)
  91. {
  92. linenum++;
  93. m = r.Match(line);
  94. if (m.Success)
  95. {
  96. // Remove leading spaces
  97. padding = 0;
  98. while ( line[padding] == ' ')
  99. {
  100. padding++;
  101. }
  102. line = line.Substring(padding);
  103. // check exclusions
  104. excludeLine = false;
  105. foreach (string s in FileScan.excludes)
  106. {
  107. if ( -1 != line.IndexOf(s) )
  108. {
  109. excludeLine = true;
  110. break;
  111. }
  112. }
  113. if ( excludeLine == false )
  114. {
  115. // Write to output file in CSV format
  116. //OutputString = "\"" + InputFile + "\"," + linenum + ",\"" + line + "\"";
  117. OutputString = InputFile + "~" + linenum + "~" + line;
  118. OutputStream.WriteLine(OutputString);
  119. }
  120. }
  121. }
  122. // cleanup and return
  123. OutputStream.Flush();
  124. OutputStream.Close();
  125. InputStream.Close();
  126. return 1;
  127. }
  128. /*---------------------------------------------------------
  129. // member vars
  130. ----------------------------------------------------------*/
  131. private string InputFile = "";
  132. private string OutputFile = "out.txt";
  133. private string SearchString = "*";
  134. static string[] excludes = {
  135. "DllImport",
  136. "///",
  137. "StringTable.GetString",
  138. "new Button",
  139. "FillSpec.Parse",
  140. "case \"",
  141. "Debug.Assert",
  142. "Debug.Fail",
  143. "Debug.WriteLine",
  144. "MessageTrace" };
  145. } // class FileScan