Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. public IEnumerable<DsnReference> GetDsnReferences(string directory)
  2.         {
  3.             var references = new LinkedList<DsnReference>();
  4.             var cfqueryRegex = new Regex(@"<cfquery(.*?)>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
  5.             var attributeRegex = new Regex(@"(\w+)\s*=\s*[""'](.*?)[""']", RegexOptions.Compiled | RegexOptions.Multiline);
  6.  
  7.             VisitFiles(
  8.                 directory, "",
  9.                 (abs, relative) =>
  10.                 {
  11.                     if (!relative.EndsWith(".cfm") && !relative.EndsWith(".cfc"))
  12.                         return;
  13.  
  14.                     string contents;
  15.                     using (var sr = new StreamReader(abs))
  16.                         contents = sr.ReadToEnd();
  17.  
  18.                     var matches = cfqueryRegex.Matches(contents);
  19.                     foreach (Match match in matches)
  20.                     {
  21.                         var attributes = (
  22.                             from attributeMatch in
  23.                             attributeRegex.Matches(match.Result("$1")).OfType<Match>()
  24.                             select new
  25.                             {
  26.                                 Key = attributeMatch.Result("$1").ToLower(),
  27.                                 Value = attributeMatch.Result("$2").ToLower()
  28.                             }).ToDictionary(k => k.Key, v => v.Value);
  29.  
  30.                         references.AddLast(new DsnReference
  31.                         {
  32.                             Dsn = attributes.ContainsKey("datasource") ? attributes["datasource"] : null,
  33.                             Username = attributes.ContainsKey("username") ? attributes["username"] : null,
  34.                             Password = attributes.ContainsKey("password") ? attributes["password"] : null,
  35.                             RelativePath = relative
  36.                         });
  37.                     }
  38.                 });
  39.  
  40.             return references;
  41.         }
  42.  
  43.         private static void VisitFiles(string path, string relative, Action<string, string> visit)
  44.         {
  45.             var files = Directory.GetFiles(path);
  46.             var directories = Directory.GetDirectories(path).Where(d => !d.EndsWith("\\.svn"));
  47.  
  48.             foreach (var di in directories.Select(dir => new DirectoryInfo(dir)))
  49.             {
  50.                 VisitFiles(di.FullName, relative + "\\" + di.Name, visit);
  51.             }
  52.  
  53.             foreach (var fi in files.Select(file => new FileInfo(file)))
  54.             {
  55.                 visit(fi.FullName, relative + "\\" + fi.Name);
  56.             }
  57.         }
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement