[runtime] Switch getenv to use heap memory
[mono.git] / mcs / class / System / System.Net.Mail / MailAddress.cs
1 //
2 // System.Net.Mail.MailAddress.cs
3 //
4 // Author:
5 //      Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Text;
32
33 namespace System.Net.Mail {
34         public class MailAddress 
35         {
36                 #region Fields
37
38                 string address;
39                 string displayName;
40                 string host;
41                 string user;
42                 string to_string;
43                 //Encoding displayNameEncoding;
44
45                 #endregion // Fields
46
47                 #region Constructors
48
49                 public MailAddress (string address) : this (address, null)
50                 {
51                 }
52
53                 public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
54                 {
55                 }
56
57                 [MonoTODO ("We don't do anything with displayNameEncoding")]
58                 public MailAddress (string address, string displayName, Encoding displayNameEncoding)
59                 {
60                         if (address == null)
61                                 throw new ArgumentNullException ("address");
62                         if (address.Length == 0)
63                                 throw new ArgumentException ("address");
64
65                         if (displayName != null)
66                                 this.displayName = displayName.Trim ();
67                         ParseAddress (address);
68                 }
69
70                 void ParseAddress (string address)
71                 {
72                         // 1. Quotes for display name
73                         address = address.Trim ();
74                         int idx = address.IndexOf ('"');
75                         if (idx != -1) {
76                                 if (idx != 0 || address.Length == 1)
77                                         throw CreateFormatException ();
78
79                                 int closing = address.LastIndexOf ('"');
80                                 if (closing == idx)
81                                         throw CreateFormatException ();
82
83                                 if (this.displayName == null)
84                                         this.displayName = address.Substring (idx + 1, closing - idx - 1).Trim ();
85                                 address = address.Substring (closing + 1).Trim ();
86                         }
87
88                         // 2. <email>
89                         idx = address.IndexOf ('<');
90                         if (idx >= 0) {
91                                 if (this.displayName == null)
92                                         this.displayName = address.Substring (0, idx).Trim ();
93                                 if (address.Length - 1 == idx)
94                                         throw CreateFormatException ();
95
96                                 int end = address.IndexOf ('>', idx + 1);
97                                 if (end == -1)
98                                         throw CreateFormatException ();
99
100                                 address = address.Substring (idx + 1, end - idx - 1).Trim ();
101                         }
102                         this.address = address;
103                         // 3. email
104                         idx = address.IndexOf ('@');
105                         if (idx <= 0)
106                                 throw CreateFormatException ();
107                         if (idx != address.LastIndexOf ('@'))
108                                 throw CreateFormatException ();
109
110                         this.user = address.Substring (0, idx).Trim ();
111                         if (user.Length == 0)
112                                 throw CreateFormatException ();
113                         this.host = address.Substring (idx + 1).Trim ();
114                         if (host.Length == 0)
115                                 throw CreateFormatException ();
116                 }
117
118                 #endregion // Constructors
119
120 #region Properties
121
122                 public string Address {
123                         get { return address; }
124                 }
125
126                 public string DisplayName {
127                         get {
128                                 if (displayName == null)
129                                         return string.Empty;
130                                 return displayName;
131                         }
132                 }
133
134                 public string Host {
135                         get { return host; }
136                 }
137
138                 public string User {
139                         get { return user; }
140                 }
141
142 #endregion // Properties
143
144 #region Methods
145                 
146                 public override bool Equals (object obj)
147                 {
148                         if (obj == null)
149                                 return false;
150
151                         return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
152                 }
153
154                 public override int GetHashCode ()
155                 {
156                         return ToString ().GetHashCode ();
157                 }
158
159                 public override string ToString ()
160                 {
161                         if (to_string != null)
162                                 return to_string;
163
164                         if (!String.IsNullOrEmpty (displayName))
165                                 to_string = String.Format ("\"{0}\" <{1}>", DisplayName, Address);
166                         else
167                                 to_string = address;
168
169                         return to_string;
170                 }
171
172                 private static FormatException CreateFormatException () {
173                         return new FormatException ("The specified string is not in the "
174                                 + "form required for an e-mail address.");
175                 }
176
177 #endregion // Methods
178         }
179 }
180