* HebrewCalendar.cs: Patch by Juan C. Olivares
[mono.git] / mcs / class / corlib / System.Globalization / TextInfo.cs
1 //
2 // System.Globalization.TextInfo.cs
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
6 //      Duncan Mak (duncan@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc.
9 //
10 // TODO:
11 //   Missing the various code page mappings.
12 //   Missing the OnDeserialization implementation.
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35 using System;
36 using System.Globalization;
37 using System.Runtime.Serialization;
38 using System.Runtime.InteropServices;
39
40 namespace System.Globalization {
41
42         [Serializable]
43         public class TextInfo: IDeserializationCallback
44         {
45                 
46                 [StructLayout (LayoutKind.Sequential)]
47                 struct Data {
48                         public int ansi;
49                         public int ebcdic;
50                         public int mac;
51                         public int oem;
52                         public byte list_sep;
53                 }
54                 
55                 int m_win32LangID;
56                 int m_nDataItem;
57                 bool m_useUserOverride;
58
59                 [NonSerialized]
60                 readonly CultureInfo ci;
61                 
62                 [NonSerialized]
63                 readonly Data data;
64
65                 internal unsafe TextInfo (CultureInfo ci, int lcid, void* data)
66                 {
67                         this.m_win32LangID = lcid;
68                         this.ci = ci;
69                         if (data != null)
70                                 this.data = *(Data*) data;
71                         else {
72                                 this.data = new Data ();
73                                 this.data.list_sep = (byte) '.';
74                         }
75                 }
76
77                 public virtual int ANSICodePage
78                 {
79                         get {
80                                 return data.ansi;
81                         }
82                 }
83
84                 public virtual int EBCDICCodePage
85                 {
86                         get {
87                                 return data.ebcdic;
88                         }
89                 }
90
91                 public virtual string ListSeparator 
92                 {
93                         get {
94                                 
95                                 return ((char) data.list_sep).ToString ();
96                         }
97                 }
98
99                 public virtual int MacCodePage
100                 {
101                         get {
102                                 return data.mac;
103                         }
104                 }
105
106                 public virtual int OEMCodePage
107                 {
108                         get {
109                                 return data.oem;
110                         }
111                 }
112
113                 public override bool Equals (object obj)
114                 {
115                         if (obj == null)
116                                 return false;
117                         TextInfo other = obj as TextInfo;
118                         if (other == null)
119                                 return false;
120                         if (other.m_win32LangID != m_win32LangID)
121                                 return false;
122                         if (other.ci != ci)
123                                 return false;
124                         return true;
125                 }
126
127                 public override int GetHashCode()
128                 {
129                         return (m_win32LangID);
130                 }
131
132                 public virtual char ToLower(char c)
133                 {
134                         return Char.ToLower (c);
135                 }
136                 
137                 public virtual string ToLower(string str)
138                 {
139                         if(str==null) 
140                                 throw new ArgumentNullException("string is null");
141
142                         return str.ToLower (ci);
143                 }
144                 
145                 public override string ToString()
146                 {
147                         return "TextInfo - " + m_win32LangID;
148                 }
149
150                 public string ToTitleCase (string str)
151                 {
152                         if(str == null)
153                                 throw new ArgumentNullException("string is null");
154                         
155                         Text.StringBuilder s = new Text.StringBuilder ();
156                         bool space_seen = true;
157                                 
158                         for (int i = 0; i < str.Length; i ++){
159                                 char c = str [i];
160                                 if (Char.IsLetter (c)){
161                                         if (space_seen)
162                                                 s.Append (Char.ToUpper (c, ci));
163                                         else
164                                                 s.Append (Char.ToLower (c, ci));
165                                         space_seen = false;
166                                 } else {
167                                         s.Append (c);
168                                         if (Char.IsWhiteSpace (c))
169                                                 space_seen = true;
170                                 }
171                         }
172
173                         return s.ToString ();
174                 }
175
176                 public virtual char ToUpper (char c)
177                 {
178                         return Char.ToUpper (c, ci);
179                 }
180
181                 public virtual string ToUpper (string str)
182                 {
183                         if(str==null)
184                                 throw new ArgumentNullException("string is null");
185                         
186                         return str.ToUpper (ci);
187                 }
188
189                 /* IDeserialization interface */
190                 [MonoTODO]
191                 void IDeserializationCallback.OnDeserialization(object sender)
192                 {
193                 }
194         }
195 }