~ubuntu-branches/ubuntu/karmic/mono-addins/karmic

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins.Database/Util.cs

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2007-07-14 12:07:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070714120748-2elczfsjlrdsrpms
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Util.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.Collections;
 
32
using System.IO;
 
33
using System.Reflection;
 
34
using Mono.Addins.Description;
 
35
using Mono.Addins.Serialization;
 
36
 
 
37
namespace Mono.Addins.Database
 
38
{
 
39
        internal class Util
 
40
        {
 
41
                public static bool IsWindows {
 
42
                        get { return Path.DirectorySeparatorChar == '\\'; }
 
43
                }
 
44
                        
 
45
                public static void CheckWrittableFloder (string path)
 
46
                {
 
47
                        string testFile = null;
 
48
                        int n = 0;
 
49
                        do {
 
50
                                testFile = Path.Combine (path, new Random ().Next ().ToString ());
 
51
                                n++;
 
52
                        } while (File.Exists (testFile) && n < 100);
 
53
                        if (n == 100)
 
54
                                throw new InvalidOperationException ("Could not create file in directory: " + path);
 
55
                        
 
56
                        StreamWriter w = new StreamWriter (testFile);
 
57
                        w.Close ();
 
58
                        File.Delete (testFile);
 
59
                }
 
60
                
 
61
                public static void AddDependencies (AddinDescription desc, AddinScanResult scanResult)
 
62
                {
 
63
                        // Not implemented in AddinScanResult to avoid making AddinDescription remotable
 
64
                        foreach (ModuleDescription mod in desc.AllModules) {
 
65
                                foreach (Dependency dep in mod.Dependencies) {
 
66
                                        AddinDependency adep = dep as AddinDependency;
 
67
                                        if (adep == null) continue;
 
68
                                        string depid = Addin.GetFullId (desc.Namespace, adep.AddinId, adep.Version);
 
69
                                        scanResult.AddAddinToUpdateRelations (depid);
 
70
                                }
 
71
                        }
 
72
                }
 
73
                
 
74
                public static Assembly LoadAssemblyForReflection (string fileName)
 
75
                {
 
76
/*                      if (!gotLoadMethod) {
 
77
                                reflectionOnlyLoadFrom = typeof(Assembly).GetMethod ("ReflectionOnlyLoadFrom");
 
78
                                gotLoadMethod = true;
 
79
                                LoadAssemblyForReflection (typeof(Util).Assembly.Location);
 
80
                        }
 
81
                        
 
82
                        if (reflectionOnlyLoadFrom != null)
 
83
                                return (Assembly) reflectionOnlyLoadFrom.Invoke (null, new string [] { fileName });
 
84
                        else
 
85
*/                              return Assembly.LoadFile (fileName);
 
86
                }
 
87
                
 
88
                // Works like Path.GetFullPath, but it does not require the path to exist
 
89
                public static string GetFullPath (string path)
 
90
                {
 
91
                        if (path == null)
 
92
                                throw new ArgumentNullException ("path");
 
93
                                
 
94
                        if (!Path.IsPathRooted (path))
 
95
                                path = Path.Combine (Environment.CurrentDirectory, path);
 
96
                        
 
97
                        string root = Path.GetPathRoot (path);
 
98
                        path = path.Substring (root.Length);
 
99
                        
 
100
                        string[] parts = path.Split (Path.DirectorySeparatorChar);
 
101
                        string[] newParts = new string [parts.Length];
 
102
                        int i = 0;
 
103
                        for (int n=0; n<parts.Length; n++) {
 
104
                                string p = parts [n];
 
105
                                if (p == null || p.Length == 0 || p == ".")
 
106
                                        continue;
 
107
                                if (p == "..") {
 
108
                                        if (i > 0)
 
109
                                                i--;
 
110
                                } else {
 
111
                                        newParts [i++] = p;
 
112
                                }
 
113
                        }
 
114
                        return root + string.Join (new string (Path.DirectorySeparatorChar, 1), newParts, 0, i);
 
115
                }
 
116
                
 
117
                public static int GetStringHashCode (string s)
 
118
                {
 
119
                        int h = 0;
 
120
                        int n = 0;
 
121
                        for (; n < s.Length - 1; n+=2) {
 
122
                                h = (h << 5) - h + s[n];
 
123
                                h = (h << 5) - h + s[n+1];
 
124
                        }
 
125
                        if (n < s.Length)
 
126
                                h = (h << 5) - h + s[n];
 
127
                        return h;
 
128
                }
 
129
                
 
130
                public static string GetGacPath (string fullName)
 
131
                {
 
132
                        string gacDir = typeof(Uri).Assembly.Location;
 
133
                        gacDir = Path.GetDirectoryName (gacDir);
 
134
                        gacDir = Path.GetDirectoryName (gacDir);
 
135
                        gacDir = Path.GetDirectoryName (gacDir);
 
136
                        
 
137
                        string[] parts = fullName.Split (',');
 
138
                        if (parts.Length != 4) return null;
 
139
                        string name = parts[0].Trim ();
 
140
                        
 
141
                        int i = parts[1].IndexOf ('=');
 
142
                        string version = i != -1 ? parts[1].Substring (i+1).Trim () : parts[1].Trim ();
 
143
                        
 
144
                        i = parts[2].IndexOf ('=');
 
145
                        string culture = i != -1 ? parts[2].Substring (i+1).Trim () : parts[2].Trim ();
 
146
                        if (culture == "neutral") culture = "";
 
147
                        
 
148
                        i = parts[3].IndexOf ('=');
 
149
                        string token = i != -1 ? parts[3].Substring (i+1).Trim () : parts[3].Trim ();
 
150
                        
 
151
                        string file = Path.Combine (gacDir, name);
 
152
                        file = Path.Combine (file, version + "_" + culture + "_" + token);
 
153
                        return file;
 
154
                }
 
155
        }
 
156
}