PageRenderTime 55ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/wbeminfo/CIMTool.cs

http://seautils.googlecode.com/
C# | 572 lines | 560 code | 7 blank | 5 comment | 11 complexity | 3d1a199f0fa01dfecf90092163940c09 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.IO;
  8. using System.Diagnostics;
  9. namespace CIMDICT
  10. {
  11. public class CIMString
  12. {
  13. private string text = "";
  14. private int pos = 0;
  15. public static string format(string desc)
  16. {
  17. desc = desc.Trim();
  18. if (desc.Length > 0) if (desc[0] == '"') desc = desc.Substring(1);
  19. if (desc.Length > 0) if (desc[desc.Length - 1] == '"') desc = desc.Substring(0, desc.Length - 1);
  20. desc = desc.Replace("\"\"", "");
  21. desc = desc.Replace("\"\n\"", "\n");
  22. desc = desc.Replace("\\'", "'");
  23. desc = desc.Replace("\\\"", "\"");
  24. desc = desc.Replace("\\n", "\n");
  25. return desc;
  26. }
  27. public CIMString(string line)
  28. {
  29. this.pos = 0;
  30. this.text = line;
  31. }
  32. public CIMString(string[] lines)
  33. {
  34. this.pos = 0;
  35. this.text = "";
  36. foreach (string line in lines)
  37. {
  38. //cut off all space
  39. string ln = line.Trim();
  40. //skip comment
  41. if (ln.IndexOf("//") == 0) continue;
  42. //reformat text
  43. if (this.text == "")
  44. {
  45. this.text = ln;
  46. }
  47. else
  48. {
  49. this.text = this.text +"\n"+ ln;
  50. }
  51. }
  52. }
  53. //Find the first occur of CHR in STR
  54. private static int find( string STR, char CHR, int startPos )
  55. {
  56. return findmatch(STR, '\0', CHR, startPos);
  57. }
  58. //Find the matched endSEP of startSEP
  59. private static int findmatch(string STR, char startSEP, char endSEP, int startPos)
  60. {
  61. bool inQuot = false;
  62. bool inTran = false;
  63. int deepth = 0;
  64. for (int i = startPos; i < STR.Length; i++)
  65. {
  66. if (inTran)
  67. {
  68. inTran = false;
  69. continue;
  70. }
  71. if (STR[i] == '"')
  72. {
  73. inQuot = !inQuot;
  74. }
  75. if (STR[i] == '\\')
  76. {
  77. inTran = true;
  78. }
  79. if (inQuot)
  80. {
  81. continue;
  82. }
  83. if (STR[i] == startSEP) deepth++;
  84. if (STR[i] == endSEP)
  85. {
  86. if (deepth == 0)
  87. {
  88. return i;
  89. }
  90. else
  91. {
  92. deepth--;
  93. }
  94. }
  95. }
  96. return -1;
  97. }
  98. public string readBlock(char startSEP, char endSEP)
  99. {
  100. int startCnt = find(this.text, startSEP, this.pos);
  101. if (startCnt < 0) return "";
  102. startCnt++;
  103. int endCnt = findmatch(this.text, startSEP, endSEP, startCnt);
  104. if (endCnt < 0) return "";
  105. string ret = "";
  106. if (endCnt > 0)
  107. {
  108. ret = this.text.Substring(startCnt, endCnt - startCnt);
  109. this.pos = endCnt + 1;
  110. }
  111. return ret;
  112. }
  113. //Example: Description ( "definition of SMX_AutoStart")
  114. public string readLabeledBlock(string leadingSign, char startSEP, char endSEP)
  115. {
  116. int startCnt = this.pos;
  117. while (startCnt >= 0)
  118. {
  119. startCnt = this.text.IndexOf(leadingSign, startCnt);
  120. if (startCnt < 0) break;
  121. startCnt += leadingSign.Length;
  122. string subtext = this.text.Substring(startCnt).Trim();
  123. if (subtext.IndexOf(startSEP) == 0) break;
  124. }
  125. if (startCnt < 0) return "";
  126. this.pos = startCnt;
  127. return this.readBlock(startSEP, endSEP);
  128. }
  129. //Example: class SMX_AutoStart {....}
  130. public string readUnkownBlock(char startSEP, char endSEP, out string name)
  131. {
  132. name = "";
  133. int startCnt = find(this.text, startSEP, this.pos);
  134. if (startCnt < 0) return "";
  135. name = this.text.Substring(this.pos, startCnt - this.pos);
  136. this.pos = startCnt;
  137. return this.readBlock(startSEP, endSEP);
  138. }
  139. public string readTill(char endSEP)
  140. {
  141. int startCnt = this.pos;
  142. int endCnt = find(this.text, endSEP, startCnt);
  143. if (endCnt < 0) return "";
  144. string subtext = this.text.Substring(startCnt, endCnt - startCnt);
  145. this.pos = endCnt + 1;
  146. return subtext;
  147. }
  148. }
  149. public class CIMProp
  150. {
  151. public string desc = "";
  152. public string fullName = "";
  153. public string name = "";
  154. public string nameLower = "";
  155. public string param = "";
  156. public bool isFunc = true;
  157. protected void setName(string nm)
  158. {
  159. this.name = nm;
  160. this.nameLower = nm.ToLower();
  161. }
  162. public bool equalTo(string pn)
  163. {
  164. return this.nameLower.Equals(pn);
  165. }
  166. public bool equalTo(CIMProp prop)
  167. {
  168. return this.nameLower.Equals(prop.nameLower);
  169. }
  170. public void print()
  171. {
  172. Console.WriteLine(" - {0}", this.name);
  173. }
  174. public CIMProp(string propDef, string propText)
  175. {
  176. this.param = (new CIMString(propDef)).readUnkownBlock('(', ')', out this.fullName);
  177. if (this.fullName == "")
  178. {
  179. this.isFunc = false;
  180. this.fullName = propDef;
  181. }
  182. string[] parts;
  183. if (this.fullName.IndexOf('=') >= 0)
  184. {
  185. string[] parts1 = this.fullName.Split('=');
  186. parts = parts1[0].Trim().Split(' ');
  187. }
  188. else
  189. {
  190. parts = this.fullName.Split(' ');
  191. }
  192. int nameIdx = parts.Length - 1;
  193. string propName = "";
  194. while (propName.Length <= 1 && nameIdx >= 0 )
  195. {
  196. propName = parts[nameIdx].Trim();
  197. nameIdx--;
  198. }
  199. propName = propName.Replace("[", "");
  200. propName = propName.Replace("]", "");
  201. propName = propName.Trim();
  202. this.setName(propName);
  203. this.desc = CIMString.format( (new CIMString(propText)).readLabeledBlock("Description", '(', ')') );
  204. string vlm = (new CIMString(propText)).readLabeledBlock("ValueMap", '{', '}');
  205. string vls = (new CIMString(propText)).readLabeledBlock("Values", '{', '}');
  206. if ((vls != "") && (vlm != ""))
  207. {
  208. vls = vls.Replace("\"", "");
  209. vlm = vlm.Replace("\"", "");
  210. string[] vlsa = vls.Split(',');
  211. string[] vlma = vlm.Split(',');
  212. if (vlsa.Length == vlma.Length)
  213. {
  214. this.desc += "\n\n-------Value Maps--------";
  215. int i = 0;
  216. foreach (string nm in vlsa)
  217. {
  218. this.desc = this.desc + "\n" + vlsa[i] + " = " + vlma[i];
  219. i++;
  220. }
  221. }
  222. }
  223. }
  224. }
  225. public class CIMClass
  226. {
  227. public string name = "";
  228. public string nameLower = "";
  229. public string baseName = "";
  230. public string baseNameLower = "";
  231. public string desc = "";
  232. public string ver = "";
  233. public Collection<CIMProp> props = new Collection<CIMProp>();
  234. public CIMClass baseClass = null;
  235. public string fileName = "";
  236. protected bool hasLoaded = false;
  237. public void setName(string nm)
  238. {
  239. this.name = nm;
  240. this.nameLower = nm.ToLower();
  241. }
  242. public void setBaseName(string nm)
  243. {
  244. this.baseName = nm;
  245. this.baseNameLower = nm.ToLower();
  246. }
  247. public bool inheritFrom(string baseName)
  248. {
  249. return this.baseNameLower.Equals(baseName);
  250. }
  251. public bool inheritFrom(CIMClass baseCim)
  252. {
  253. return this.baseNameLower.Equals(baseCim.nameLower);
  254. }
  255. public bool equalTo(string compName)
  256. {
  257. return this.nameLower.Equals(compName);
  258. }
  259. public bool equalTo(CIMClass cim)
  260. {
  261. return this.nameLower.Equals(cim.nameLower);
  262. }
  263. public bool isValid()
  264. {
  265. return (!string.IsNullOrEmpty(this.name));
  266. }
  267. public bool isLoaded()
  268. {
  269. return this.hasLoaded;
  270. }
  271. public bool nameContains(string key)
  272. {
  273. if (this.nameLower.IndexOf(key) >= 0)
  274. {
  275. return true;
  276. }
  277. return false;
  278. }
  279. //Instant load initialize
  280. public CIMClass(string fn)
  281. {
  282. this.fileName = fn;
  283. this.hasLoaded = false;
  284. }
  285. public void load()
  286. {
  287. string classdef = "";
  288. CIMString all = new CIMString(File.ReadAllLines(this.fileName));
  289. CIMString header = new CIMString(all.readBlock('[', ']'));
  290. CIMString body = new CIMString(all.readUnkownBlock('{', '}', out classdef));
  291. //Version ( "1.0.0" )
  292. this.ver = CIMString.format(header.readLabeledBlock("Version", '(', ')'));
  293. //Description ( "definition of SMX_AutoStartFCHBA")
  294. this.desc = CIMString.format(header.readLabeledBlock("Description", '(', ')'));
  295. //class SMX_AutoStartFCHBA : SMX_AutoStart
  296. Match classDefMatch = Regex.Match(classdef, @"^\s*?class\s*?(.*?)\s*?:\s*?(.*?)$");
  297. if (classDefMatch.Success)
  298. {
  299. this.setName(classDefMatch.Result("$1").ToString().Trim());
  300. this.setBaseName(classDefMatch.Result("$2").ToString().Trim());
  301. }
  302. else
  303. {
  304. // class CIM_DAPort {
  305. classDefMatch = Regex.Match(classdef, @"^\s*?class\s*?(.*?)$");
  306. if (classDefMatch.Success)
  307. {
  308. this.setName(classDefMatch.Result("$1").ToString().Trim());
  309. this.setBaseName("");
  310. }
  311. }
  312. while (true)
  313. {
  314. string bodytext = body.readBlock('[', ']');
  315. string propdef = body.readTill(';');
  316. if (propdef == "") break;
  317. this.props.Add(new CIMProp(propdef, bodytext));
  318. }
  319. this.hasLoaded = true;
  320. }
  321. public void checkLoadFull()
  322. {
  323. if (!this.isLoaded())
  324. {
  325. this.load();
  326. }
  327. if (this.baseClass != null)
  328. {
  329. this.baseClass.checkLoadFull();
  330. }
  331. }
  332. public void print()
  333. {
  334. Console.WriteLine("{0} : {1}", this.name, this.baseName);
  335. if (!this.isLoaded()) this.load();
  336. foreach ( CIMProp prop in this.props)
  337. {
  338. prop.print();
  339. }
  340. if (this.baseClass != null)
  341. {
  342. this.baseClass.print();
  343. }
  344. }
  345. public CIMProp getProp(string name)
  346. {
  347. foreach (CIMProp prop in this.props)
  348. {
  349. if (prop.equalTo(name))
  350. {
  351. return prop;
  352. }
  353. }
  354. return null;
  355. }
  356. }
  357. public class CIMRepo
  358. {
  359. public CIMRepo()
  360. {
  361. this.init();
  362. }
  363. public CIMRepo(string fn)
  364. {
  365. this.init(fn);
  366. }
  367. protected const char SEPERATOR = '|';
  368. protected List<string> MOFFiles = new List<string>();
  369. protected List<CIMClass> CIMClasses = new List<CIMClass>();
  370. protected List<string> getAllCIMFiles(string path)
  371. {
  372. if (path == "")
  373. {
  374. string appName = Process.GetCurrentProcess().MainModule.FileName;
  375. path = Path.GetDirectoryName(appName);
  376. }
  377. List<string> result = Directory.GetFiles(path, "*.mof").ToList();
  378. List<string> dirs = Directory.GetDirectories(path).ToList();
  379. foreach (string dir in dirs)
  380. {
  381. foreach (string mofile in getAllCIMFiles(dir))
  382. {
  383. result.Add(mofile);
  384. }
  385. }
  386. return result;
  387. }
  388. protected bool addClass(CIMClass cim)
  389. {
  390. if (!cim.isValid()) return false;
  391. foreach (CIMClass existCim in this.CIMClasses)
  392. {
  393. if (cim.equalTo(existCim)) return false;
  394. }
  395. this.CIMClasses.Add(cim);
  396. return true;
  397. }
  398. protected void linkBase()
  399. {
  400. foreach (CIMClass cim in this.CIMClasses)
  401. {
  402. if (!cim.isValid()) continue;
  403. if (string.IsNullOrEmpty(cim.baseName)) continue;
  404. foreach (CIMClass existCim in this.CIMClasses)
  405. {
  406. if (cim.inheritFrom(existCim))
  407. {
  408. cim.baseClass = existCim;
  409. break;
  410. }
  411. }
  412. }
  413. }
  414. public CIMClass getClass(string name)
  415. {
  416. string searchName = name.ToLower();
  417. foreach (CIMClass cim in this.CIMClasses)
  418. {
  419. if (cim.equalTo(searchName))
  420. {
  421. cim.checkLoadFull();
  422. return cim;
  423. }
  424. }
  425. return null;
  426. }
  427. protected List<CIMClass> getAllClasses()
  428. {
  429. return this.CIMClasses;
  430. }
  431. protected void init()
  432. {
  433. this.MOFFiles = this.getAllCIMFiles("");
  434. //Read all MOF files
  435. //int i = 0;
  436. foreach (string filename in this.MOFFiles)
  437. {
  438. //Console.WriteLine("# " + i++);
  439. CIMClass cim = new CIMClass(filename);
  440. cim.load();
  441. this.addClass( cim );
  442. }
  443. this.CIMClasses.Sort(delegate(CIMClass c1, CIMClass c2) { return c1.name.CompareTo(c2.name); });
  444. this.linkBase();
  445. //end init
  446. }
  447. //Init list from a cache file
  448. protected void init(string listfn)
  449. {
  450. foreach (string ln in File.ReadAllLines(listfn))
  451. {
  452. string[] parts = ln.Split(SEPERATOR);
  453. if (parts.Length != 3) continue;
  454. CIMClass cim = new CIMClass(parts[2]);
  455. cim.setName(parts[0]);
  456. cim.setBaseName(parts[1]);
  457. this.CIMClasses.Add(cim);
  458. }
  459. this.CIMClasses.Sort(delegate(CIMClass c1, CIMClass c2) { return c1.name.CompareTo(c2.name); });
  460. this.linkBase();
  461. }
  462. public Collection<CIMClass> query( string keywords )
  463. {
  464. Collection<CIMClass> ret = new Collection<CIMClass>();
  465. if (string.IsNullOrEmpty(keywords)) return ret;
  466. //Parse keywords
  467. string[] keys = keywords.Split(' ');
  468. for (int i = 0; i < keys.Length; i++)
  469. {
  470. keys[i] = keys[i].Trim().ToLower();
  471. }
  472. //Found all qualified result
  473. foreach (CIMClass cim in this.CIMClasses)
  474. {
  475. bool unmatchedKey = false;
  476. foreach (string key in keys)
  477. {
  478. if (string.IsNullOrEmpty(key)) continue;
  479. if (!cim.nameContains(key))
  480. {
  481. unmatchedKey = true;
  482. break;
  483. }
  484. }
  485. if (!unmatchedKey)
  486. {
  487. cim.checkLoadFull();
  488. ret.Add(cim);
  489. }
  490. }
  491. return ret;
  492. }
  493. public void genFileList( string listfn )
  494. {
  495. this.init();
  496. FileStream fs = new FileStream(listfn, FileMode.Create);
  497. StreamWriter sw = new StreamWriter(fs);
  498. foreach (CIMClass cim in this.getAllClasses())
  499. {
  500. sw.WriteLine(cim.name + SEPERATOR + cim.baseName + SEPERATOR + cim.fileName);
  501. }
  502. sw.Close();
  503. fs.Close();
  504. }
  505. }
  506. }