4 // Copyright (C) 2004 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 using System
.Collections
;
32 namespace Beagle
.Util
{
36 private UriFu () { }
// class is static
38 static public Uri
PathToFileUri (string path
)
40 return PathToFileUri (path
, null);
43 static public Uri
PathToFileUri (string path
, string fragment
)
45 return new Uri (PathToFileUriString (path
, fragment
), true);
48 static public string PathToFileUriString (string path
)
50 return PathToFileUriString (path
, null);
53 static public string PathToFileUriString (string path
, string fragment
)
55 string str
= String
.Concat (Uri
.UriSchemeFile
,
57 StringFu
.HexEscape (Path
.GetFullPath (path
)));
63 static public Uri
EscapedStringToUri (string path
)
65 // Our current hackery attempts to serialize Uri strings in
66 // escaped and constructable form, so we don't require any
67 // extra processing on deserialization right now.
68 return new Uri (path
, true);
71 // UriBuilder is a piece of shit so we have to do this ourselves.
72 static public string UriToEscapedString (Uri uri
)
74 StringBuilder builder
= new StringBuilder ();
76 builder
.Append (uri
.Scheme
);
78 if (uri
.ToString ().IndexOf (Uri
.SchemeDelimiter
) == uri
.Scheme
.Length
)
79 builder
.Append (Uri
.SchemeDelimiter
);
83 if (uri
.Host
!= String
.Empty
) {
84 if (uri
.UserInfo
!= String
.Empty
)
85 builder
.Append (uri
.UserInfo
+ "@");
87 builder
.Append (uri
.Host
);
90 if (! uri
.IsDefaultPort
)
91 builder
.Append (":" + uri
.Port
);
93 // Both PathAndQuery and Fragment are escaped for us
94 builder
.Append (uri
.PathAndQuery
);
95 builder
.Append (uri
.Fragment
);
97 return builder
.ToString ();
100 //////////////////////////////////
102 static public bool Equals (Uri uri1
, Uri uri2
)
104 return uri1
.ToString () == uri2
.ToString ();
107 static public int Compare (Uri uri1
, Uri uri2
)
109 return String
.Compare (uri1
.ToString (), uri2
.ToString ());
112 //////////////////////////////////
114 public class Comparer
: IComparer
116 public int Compare(object uri1
, object uri2
)
118 return String
.Compare(uri1
.ToString(), uri2
.ToString());
122 public class Hasher
: IHashCodeProvider
124 public int GetHashCode(object o
)
126 return o
.ToString().GetHashCode();
130 static Comparer the_comparer
= new Comparer ();
131 static Hasher the_hasher
= new Hasher ();
133 // Returns a hash table that does the right thing when
135 static public Hashtable
NewHashtable ()
137 return new Hashtable (the_hasher
, the_comparer
);