/wbeminfo/CIMTool.cs
C# | 572 lines | 560 code | 7 blank | 5 comment | 11 complexity | 3d1a199f0fa01dfecf90092163940c09 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Diagnostics;
-
- namespace CIMDICT
- {
- public class CIMString
- {
- private string text = "";
- private int pos = 0;
-
- public static string format(string desc)
- {
- desc = desc.Trim();
- if (desc.Length > 0) if (desc[0] == '"') desc = desc.Substring(1);
- if (desc.Length > 0) if (desc[desc.Length - 1] == '"') desc = desc.Substring(0, desc.Length - 1);
- desc = desc.Replace("\"\"", "");
- desc = desc.Replace("\"\n\"", "\n");
- desc = desc.Replace("\\'", "'");
- desc = desc.Replace("\\\"", "\"");
- desc = desc.Replace("\\n", "\n");
- return desc;
- }
-
- public CIMString(string line)
- {
- this.pos = 0;
- this.text = line;
- }
-
- public CIMString(string[] lines)
- {
- this.pos = 0;
- this.text = "";
- foreach (string line in lines)
- {
- //cut off all space
- string ln = line.Trim();
- //skip comment
- if (ln.IndexOf("//") == 0) continue;
- //reformat text
- if (this.text == "")
- {
- this.text = ln;
- }
- else
- {
- this.text = this.text +"\n"+ ln;
- }
- }
- }
-
- //Find the first occur of CHR in STR
- private static int find( string STR, char CHR, int startPos )
- {
- return findmatch(STR, '\0', CHR, startPos);
- }
-
- //Find the matched endSEP of startSEP
- private static int findmatch(string STR, char startSEP, char endSEP, int startPos)
- {
- bool inQuot = false;
- bool inTran = false;
- int deepth = 0;
- for (int i = startPos; i < STR.Length; i++)
- {
- if (inTran)
- {
- inTran = false;
- continue;
- }
-
- if (STR[i] == '"')
- {
- inQuot = !inQuot;
- }
- if (STR[i] == '\\')
- {
- inTran = true;
- }
-
- if (inQuot)
- {
- continue;
- }
-
- if (STR[i] == startSEP) deepth++;
- if (STR[i] == endSEP)
- {
- if (deepth == 0)
- {
- return i;
- }
- else
- {
- deepth--;
- }
- }
- }
- return -1;
- }
-
- public string readBlock(char startSEP, char endSEP)
- {
- int startCnt = find(this.text, startSEP, this.pos);
- if (startCnt < 0) return "";
- startCnt++;
-
- int endCnt = findmatch(this.text, startSEP, endSEP, startCnt);
- if (endCnt < 0) return "";
-
- string ret = "";
- if (endCnt > 0)
- {
- ret = this.text.Substring(startCnt, endCnt - startCnt);
- this.pos = endCnt + 1;
- }
- return ret;
- }
-
- //Example: Description ( "definition of SMX_AutoStart")
- public string readLabeledBlock(string leadingSign, char startSEP, char endSEP)
- {
- int startCnt = this.pos;
- while (startCnt >= 0)
- {
- startCnt = this.text.IndexOf(leadingSign, startCnt);
- if (startCnt < 0) break;
- startCnt += leadingSign.Length;
- string subtext = this.text.Substring(startCnt).Trim();
- if (subtext.IndexOf(startSEP) == 0) break;
- }
- if (startCnt < 0) return "";
-
- this.pos = startCnt;
- return this.readBlock(startSEP, endSEP);
- }
-
- //Example: class SMX_AutoStart {....}
- public string readUnkownBlock(char startSEP, char endSEP, out string name)
- {
- name = "";
- int startCnt = find(this.text, startSEP, this.pos);
- if (startCnt < 0) return "";
- name = this.text.Substring(this.pos, startCnt - this.pos);
- this.pos = startCnt;
- return this.readBlock(startSEP, endSEP);
- }
-
- public string readTill(char endSEP)
- {
- int startCnt = this.pos;
- int endCnt = find(this.text, endSEP, startCnt);
- if (endCnt < 0) return "";
- string subtext = this.text.Substring(startCnt, endCnt - startCnt);
- this.pos = endCnt + 1;
-
- return subtext;
- }
-
- }
-
- public class CIMProp
- {
- public string desc = "";
- public string fullName = "";
- public string name = "";
- public string nameLower = "";
- public string param = "";
- public bool isFunc = true;
-
- protected void setName(string nm)
- {
- this.name = nm;
- this.nameLower = nm.ToLower();
- }
-
- public bool equalTo(string pn)
- {
- return this.nameLower.Equals(pn);
- }
- public bool equalTo(CIMProp prop)
- {
- return this.nameLower.Equals(prop.nameLower);
- }
- public void print()
- {
- Console.WriteLine(" - {0}", this.name);
- }
-
- public CIMProp(string propDef, string propText)
- {
- this.param = (new CIMString(propDef)).readUnkownBlock('(', ')', out this.fullName);
- if (this.fullName == "")
- {
- this.isFunc = false;
- this.fullName = propDef;
- }
-
- string[] parts;
- if (this.fullName.IndexOf('=') >= 0)
- {
- string[] parts1 = this.fullName.Split('=');
- parts = parts1[0].Trim().Split(' ');
- }
- else
- {
- parts = this.fullName.Split(' ');
- }
- int nameIdx = parts.Length - 1;
- string propName = "";
- while (propName.Length <= 1 && nameIdx >= 0 )
- {
- propName = parts[nameIdx].Trim();
- nameIdx--;
- }
- propName = propName.Replace("[", "");
- propName = propName.Replace("]", "");
- propName = propName.Trim();
- this.setName(propName);
-
- this.desc = CIMString.format( (new CIMString(propText)).readLabeledBlock("Description", '(', ')') );
- string vlm = (new CIMString(propText)).readLabeledBlock("ValueMap", '{', '}');
- string vls = (new CIMString(propText)).readLabeledBlock("Values", '{', '}');
-
- if ((vls != "") && (vlm != ""))
- {
- vls = vls.Replace("\"", "");
- vlm = vlm.Replace("\"", "");
- string[] vlsa = vls.Split(',');
- string[] vlma = vlm.Split(',');
- if (vlsa.Length == vlma.Length)
- {
- this.desc += "\n\n-------Value Maps--------";
- int i = 0;
- foreach (string nm in vlsa)
- {
- this.desc = this.desc + "\n" + vlsa[i] + " = " + vlma[i];
- i++;
- }
- }
- }
- }
- }
-
- public class CIMClass
- {
- public string name = "";
- public string nameLower = "";
- public string baseName = "";
- public string baseNameLower = "";
-
- public string desc = "";
- public string ver = "";
-
- public Collection<CIMProp> props = new Collection<CIMProp>();
- public CIMClass baseClass = null;
-
- public string fileName = "";
-
- protected bool hasLoaded = false;
-
- public void setName(string nm)
- {
- this.name = nm;
- this.nameLower = nm.ToLower();
- }
-
- public void setBaseName(string nm)
- {
- this.baseName = nm;
- this.baseNameLower = nm.ToLower();
- }
-
- public bool inheritFrom(string baseName)
- {
- return this.baseNameLower.Equals(baseName);
- }
- public bool inheritFrom(CIMClass baseCim)
- {
- return this.baseNameLower.Equals(baseCim.nameLower);
- }
- public bool equalTo(string compName)
- {
- return this.nameLower.Equals(compName);
- }
- public bool equalTo(CIMClass cim)
- {
- return this.nameLower.Equals(cim.nameLower);
- }
- public bool isValid()
- {
- return (!string.IsNullOrEmpty(this.name));
- }
- public bool isLoaded()
- {
- return this.hasLoaded;
- }
- public bool nameContains(string key)
- {
- if (this.nameLower.IndexOf(key) >= 0)
- {
- return true;
- }
- return false;
- }
-
- //Instant load initialize
- public CIMClass(string fn)
- {
- this.fileName = fn;
- this.hasLoaded = false;
- }
-
- public void load()
- {
- string classdef = "";
-
- CIMString all = new CIMString(File.ReadAllLines(this.fileName));
- CIMString header = new CIMString(all.readBlock('[', ']'));
- CIMString body = new CIMString(all.readUnkownBlock('{', '}', out classdef));
-
- //Version ( "1.0.0" )
- this.ver = CIMString.format(header.readLabeledBlock("Version", '(', ')'));
- //Description ( "definition of SMX_AutoStartFCHBA")
- this.desc = CIMString.format(header.readLabeledBlock("Description", '(', ')'));
-
- //class SMX_AutoStartFCHBA : SMX_AutoStart
- Match classDefMatch = Regex.Match(classdef, @"^\s*?class\s*?(.*?)\s*?:\s*?(.*?)$");
- if (classDefMatch.Success)
- {
- this.setName(classDefMatch.Result("$1").ToString().Trim());
- this.setBaseName(classDefMatch.Result("$2").ToString().Trim());
- }
- else
- {
- // class CIM_DAPort {
- classDefMatch = Regex.Match(classdef, @"^\s*?class\s*?(.*?)$");
- if (classDefMatch.Success)
- {
- this.setName(classDefMatch.Result("$1").ToString().Trim());
- this.setBaseName("");
- }
- }
-
- while (true)
- {
- string bodytext = body.readBlock('[', ']');
- string propdef = body.readTill(';');
- if (propdef == "") break;
-
- this.props.Add(new CIMProp(propdef, bodytext));
- }
-
- this.hasLoaded = true;
- }
-
- public void checkLoadFull()
- {
- if (!this.isLoaded())
- {
- this.load();
- }
- if (this.baseClass != null)
- {
- this.baseClass.checkLoadFull();
- }
- }
-
- public void print()
- {
- Console.WriteLine("{0} : {1}", this.name, this.baseName);
-
- if (!this.isLoaded()) this.load();
-
- foreach ( CIMProp prop in this.props)
- {
- prop.print();
- }
-
- if (this.baseClass != null)
- {
- this.baseClass.print();
- }
- }
-
- public CIMProp getProp(string name)
- {
- foreach (CIMProp prop in this.props)
- {
- if (prop.equalTo(name))
- {
- return prop;
- }
- }
- return null;
- }
- }
-
- public class CIMRepo
- {
- public CIMRepo()
- {
- this.init();
- }
-
- public CIMRepo(string fn)
- {
- this.init(fn);
- }
-
- protected const char SEPERATOR = '|';
-
- protected List<string> MOFFiles = new List<string>();
- protected List<CIMClass> CIMClasses = new List<CIMClass>();
-
- protected List<string> getAllCIMFiles(string path)
- {
- if (path == "")
- {
- string appName = Process.GetCurrentProcess().MainModule.FileName;
- path = Path.GetDirectoryName(appName);
- }
- List<string> result = Directory.GetFiles(path, "*.mof").ToList();
- List<string> dirs = Directory.GetDirectories(path).ToList();
- foreach (string dir in dirs)
- {
- foreach (string mofile in getAllCIMFiles(dir))
- {
- result.Add(mofile);
- }
- }
- return result;
- }
-
- protected bool addClass(CIMClass cim)
- {
- if (!cim.isValid()) return false;
- foreach (CIMClass existCim in this.CIMClasses)
- {
- if (cim.equalTo(existCim)) return false;
- }
- this.CIMClasses.Add(cim);
- return true;
- }
-
- protected void linkBase()
- {
- foreach (CIMClass cim in this.CIMClasses)
- {
- if (!cim.isValid()) continue;
- if (string.IsNullOrEmpty(cim.baseName)) continue;
- foreach (CIMClass existCim in this.CIMClasses)
- {
- if (cim.inheritFrom(existCim))
- {
- cim.baseClass = existCim;
- break;
- }
- }
- }
- }
-
- public CIMClass getClass(string name)
- {
- string searchName = name.ToLower();
- foreach (CIMClass cim in this.CIMClasses)
- {
- if (cim.equalTo(searchName))
- {
- cim.checkLoadFull();
- return cim;
- }
- }
- return null;
- }
-
- protected List<CIMClass> getAllClasses()
- {
- return this.CIMClasses;
- }
-
- protected void init()
- {
- this.MOFFiles = this.getAllCIMFiles("");
-
- //Read all MOF files
- //int i = 0;
- foreach (string filename in this.MOFFiles)
- {
- //Console.WriteLine("# " + i++);
- CIMClass cim = new CIMClass(filename);
- cim.load();
- this.addClass( cim );
- }
- this.CIMClasses.Sort(delegate(CIMClass c1, CIMClass c2) { return c1.name.CompareTo(c2.name); });
- this.linkBase();
-
- //end init
- }
-
- //Init list from a cache file
- protected void init(string listfn)
- {
- foreach (string ln in File.ReadAllLines(listfn))
- {
- string[] parts = ln.Split(SEPERATOR);
- if (parts.Length != 3) continue;
- CIMClass cim = new CIMClass(parts[2]);
- cim.setName(parts[0]);
- cim.setBaseName(parts[1]);
- this.CIMClasses.Add(cim);
- }
- this.CIMClasses.Sort(delegate(CIMClass c1, CIMClass c2) { return c1.name.CompareTo(c2.name); });
- this.linkBase();
- }
-
- public Collection<CIMClass> query( string keywords )
- {
- Collection<CIMClass> ret = new Collection<CIMClass>();
- if (string.IsNullOrEmpty(keywords)) return ret;
-
- //Parse keywords
- string[] keys = keywords.Split(' ');
- for (int i = 0; i < keys.Length; i++)
- {
- keys[i] = keys[i].Trim().ToLower();
- }
-
- //Found all qualified result
- foreach (CIMClass cim in this.CIMClasses)
- {
- bool unmatchedKey = false;
- foreach (string key in keys)
- {
- if (string.IsNullOrEmpty(key)) continue;
- if (!cim.nameContains(key))
- {
- unmatchedKey = true;
- break;
- }
- }
- if (!unmatchedKey)
- {
- cim.checkLoadFull();
- ret.Add(cim);
- }
- }
-
- return ret;
- }
-
- public void genFileList( string listfn )
- {
- this.init();
- FileStream fs = new FileStream(listfn, FileMode.Create);
- StreamWriter sw = new StreamWriter(fs);
- foreach (CIMClass cim in this.getAllClasses())
- {
- sw.WriteLine(cim.name + SEPERATOR + cim.baseName + SEPERATOR + cim.fileName);
- }
- sw.Close();
- fs.Close();
- }
-
- }
- }