1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Reflection; 7 8 namespace Lextm.SharpSnmpLib.Mib 9 { 10 public interface IMibResolver 11 { Resolve(string moduleName)12 IModule Resolve(string moduleName); 13 } 14 15 public class FileSystemMibResolver : IMibResolver 16 { 17 private string _path; 18 private bool _recursive; 19 FileSystemMibResolver(string path, bool recursive)20 public FileSystemMibResolver(string path, bool recursive) 21 { 22 _path = path; 23 _recursive = recursive; 24 } 25 26 #region IMibResolver Member 27 Resolve(string moduleName)28 public IModule Resolve(string moduleName) 29 { 30 if (Directory.Exists(_path)) 31 { 32 string[] matchedFiles = Directory.GetFiles( 33 _path, 34 "*", 35 (_recursive) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); 36 37 if ((matchedFiles != null) && (matchedFiles.Length >= 1)) 38 { 39 foreach (string matchedFile in matchedFiles) 40 { 41 if (Path.GetFileNameWithoutExtension(matchedFile.ToLowerInvariant()) == moduleName.ToLowerInvariant()) 42 { 43 try 44 { 45 MibDocument md = new MibDocument (matchedFile); 46 if (md.Modules.Count > 0) 47 { 48 return md.Modules [0]; 49 } 50 } catch 51 { 52 } 53 } 54 } 55 } 56 } 57 58 return null; 59 } 60 61 #endregion 62 63 } 64 65 // earlier code for search of versioned MIBs: 66 // 67 //private const string Pattern = "-V[0-9]+$"; 68 //public static bool AllDependentsAvailable(MibModule module, IDictionary<string, MibModule> modules) 69 //{ 70 // foreach (string dependent in module.Dependents) 71 // { 72 // if (!DependentFound(dependent, modules)) 73 // { 74 // return false; 75 // } 76 // } 77 78 // return true; 79 //} 80 81 //private static bool DependentFound(string dependent, IDictionary<string, MibModule> modules) 82 //{ 83 // if (!Regex.IsMatch(dependent, Pattern)) 84 // { 85 // return modules.ContainsKey(dependent); 86 // } 87 88 // if (modules.ContainsKey(dependent)) 89 // { 90 // return true; 91 // } 92 93 // string dependentNonVersion = Regex.Replace(dependent, Pattern, string.Empty); 94 // return modules.ContainsKey(dependentNonVersion); 95 //} 96 97 } 98