~peter-stuifzand/do-plugins/vmware

« back to all changes in this revision

Viewing changes to VMWare/src/VMWarePreferencesReader.cs

  • Committer: peter at stuifzand
  • Date: 2010-02-05 09:30:50 UTC
  • Revision ID: peter@stuifzand.eu-20100205093050-vtxf5xwqi47ghku3
Added beginning of VMWare plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.IO;
 
4
using System.Collections.Generic;
 
5
using System.Text.RegularExpressions;
 
6
 
 
7
namespace VMWare {
 
8
 
 
9
        public class VMWarePreferencesReader {
 
10
                public List<string> ReadPreferences() {
 
11
                   return ReadFile(FindPreferencesFile());
 
12
                }
 
13
 
 
14
                private string FindPreferencesFile() {
 
15
                        string home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
 
16
                        return Path.Combine(home, ".vmware/preferences");
 
17
                }
 
18
 
 
19
                private List<string> ReadFile(string filename) {
 
20
                        Dictionary<string, VM> vms = new Dictionary<string, VM>();
 
21
 
 
22
                        using (FileStream fs = new FileStream (filename, FileMode.Open, FileAccess.Read)) {
 
23
                                using (StreamReader reader = new StreamReader (fs)) {
 
24
 
 
25
                                        Regex preferenceLine = new Regex ("^pref\\.ws\\.(\\w+)\\.(\\w+)\\s*=\\s*\"([^\"]+)\"$");
 
26
                                        string s;
 
27
                                        while ((s = reader.ReadLine ()) != null) {
 
28
                                                Match preference = preferenceLine.Match (s);
 
29
                                                if (preference.Groups.Count != 4) continue;
 
30
 
 
31
                                                string obj  = preference.Groups[1].ToString();
 
32
                                                if (!vms.ContainsKey(obj)) {
 
33
                                                        vms[obj] = new VM();
 
34
                                                }
 
35
 
 
36
                                                string prop = preference.Groups[2].ToString();
 
37
                                                string val  = preference.Groups[3].ToString();
 
38
 
 
39
                                                if (prop == "present") {
 
40
                                                        vms[obj].present = val == "TRUE";
 
41
                                                }
 
42
                                                else if (prop == "type") {
 
43
                                                        vms[obj].type = val;
 
44
                                                }
 
45
                                                else if (prop == "path") {
 
46
                                                        vms[obj].path = val;
 
47
                                                }
 
48
                                                else if (prop == "file") {
 
49
                                                        vms[obj].file = val;
 
50
                                                }
 
51
                                                else if (prop == "dest") {
 
52
                                                        vms[obj].dest = val;
 
53
                                                }
 
54
                                        }
 
55
                                }
 
56
                        }
 
57
 
 
58
                        Dictionary<string, int> unique_vms = new Dictionary<string, int>();
 
59
                        foreach (string vm in vms.Keys) {
 
60
                                if (vms[vm].file == null) continue;
 
61
                                if (!vms[vm].present) continue;
 
62
                                unique_vms[ vms[vm].file ] = 1;
 
63
                        }
 
64
 
 
65
                        List<string> vm_list = new List<string> (unique_vms.Keys);
 
66
                        foreach (string vm in unique_vms.Keys) {
 
67
                                vm_list.Add (vm);
 
68
                        }
 
69
                        return vm_list;
 
70
                }
 
71
        }
 
72
 
 
73
        class VM {
 
74
                public bool present;
 
75
                public string type;
 
76
                public string path;
 
77
                public string file;
 
78
                public string dest;
 
79
        }
 
80
 
 
81
}