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 new Uri (PathToFileUriString (path
), true);
43 static public string PathToFileUriString (string path
)
45 return Uri
.UriSchemeFile
+ Uri
.SchemeDelimiter
46 + StringFu
.HexEscape (Path
.GetFullPath (path
));
49 static public Uri
EscapedStringToUri (string path
)
51 // Our current hackery attempts to serialize Uri strings in
52 // escaped and constructable form, so we don't require any
53 // extra processing on deserialization right now.
54 return new Uri (path
, true);
57 // UriBuilder is a piece of shit so we have to do this ourselves.
58 static public string UriToEscapedString (Uri uri
)
60 StringBuilder builder
= new StringBuilder ();
62 builder
.Append (uri
.Scheme
);
64 if (uri
.ToString ().IndexOf (Uri
.SchemeDelimiter
) == uri
.Scheme
.Length
)
65 builder
.Append (Uri
.SchemeDelimiter
);
69 if (uri
.Host
!= String
.Empty
) {
70 if (uri
.UserInfo
!= String
.Empty
)
71 builder
.Append (uri
.UserInfo
+ "@");
73 builder
.Append (uri
.Host
);
76 if (! uri
.IsDefaultPort
)
77 builder
.Append (":" + uri
.Port
);
79 // Both PathAndQuery and Fragment are escaped for us
80 builder
.Append (uri
.PathAndQuery
);
81 builder
.Append (uri
.Fragment
);
83 return builder
.ToString ();
86 //////////////////////////////////
88 static public bool Equals (Uri uri1
, Uri uri2
)
90 return uri1
.ToString () == uri2
.ToString ();
93 static public int Compare (Uri uri1
, Uri uri2
)
95 return String
.Compare (uri1
.ToString (), uri2
.ToString ());
98 //////////////////////////////////
100 public class Comparer
: IComparer
102 public int Compare(object uri1
, object uri2
)
104 return String
.Compare(uri1
.ToString(), uri2
.ToString());
108 public class Hasher
: IHashCodeProvider
110 public int GetHashCode(object o
)
112 return o
.ToString().GetHashCode();
116 static Comparer the_comparer
= new Comparer ();
117 static Hasher the_hasher
= new Hasher ();
119 // Returns a hash table that does the right thing when
121 static public Hashtable
NewHashtable ()
123 return new Hashtable (the_hasher
, the_comparer
);