2002-01-14 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System.Text / Encoding.cs
1 // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Text.Encoding.cs
4 //
5 // Author:
6 //   Sean MacIsaac (macisaac@ximian.com)
7 //   Dietmar Maurer (dietmar@ximian.com)
8 //
9 // (C) Ximian, Inc.  http://www.ximian.com
10 //
11
12 using System.Runtime.CompilerServices;
13
14 namespace System.Text {
15
16         public abstract class Encoding {
17                 private static ASCIIEncoding ascii_encoding;
18                 private static UnicodeEncoding big_endian_unicode;
19                 private static UnicodeEncoding unicode_encoding;
20                 private static UTF7Encoding utf7_encoding;
21                 private static UTF8Encoding utf8_encoding;
22
23                 private int codepage;
24    
25                 protected string body_name;
26                 protected string encoding_name;
27                 protected string header_name;
28                 protected string web_name;
29
30                 protected bool is_browser_display = false;
31                 protected bool is_browser_save = false;
32                 protected bool is_mail_news_display = false;
33                 protected bool is_mail_news_save = false;
34                 
35                 private Encoder default_encoder = null;
36                 private Decoder default_decoder = null;
37                 
38                 // used for iconv 
39                 private string  iconv_name;
40                 private bool    big_endian;
41                 private Encoder iconv_encoder = null;
42                 private Decoder iconv_decoder = null;
43
44                 protected Encoding()
45                 {
46                 }
47
48                 protected Encoding (int codepage)
49                 {
50                         this.codepage = codepage;
51                 }
52
53                 internal protected Encoding (string name, bool big_endian)
54                 {
55                         this.iconv_name = name;
56                         this.big_endian = big_endian;
57                         
58                         iconv_decoder = new IConvDecoder (iconv_name, big_endian);
59                         iconv_encoder = new IConvEncoder (iconv_name, big_endian);
60                 }
61
62                 public static Encoding Unicode {
63                         get {
64                                 if (unicode_encoding == null) {
65                                         unicode_encoding = new UnicodeEncoding();
66                                 }
67                                 return unicode_encoding;
68                         }
69                 }
70
71                 public static Encoding UTF7 {
72                         get {
73                                 if (utf7_encoding == null) {
74                                         utf7_encoding = new UTF7Encoding();
75                                 }
76                                 return utf7_encoding;
77                         }
78                 }
79
80                 public static Encoding UTF8 {
81                         get {
82                                 if (utf8_encoding == null) {
83                                         utf8_encoding = new UTF8Encoding();
84                                 }
85                                 return utf8_encoding;
86                         }
87                 }
88
89                 public static Encoding ASCII {
90                         get {
91                                 if (ascii_encoding == null)
92                                         ascii_encoding = new ASCIIEncoding ();
93                                 return ascii_encoding;
94                         }
95                 }
96
97                 public static Encoding BigEndianUnicode {
98                         get {
99                                 if (big_endian_unicode == null)
100                                         big_endian_unicode = new UnicodeEncoding (true, true);
101                                 return big_endian_unicode;
102                         }
103                 }
104
105                 public virtual string BodyName {
106                         get {
107                                 return body_name;
108                         }
109                 }
110
111                 public virtual int CodePage {
112                         get {
113                                 return codepage;
114                         }
115                 }
116
117                 public static Encoding Default {
118                         get {
119                                 return ASCII;
120                         }
121                 }
122
123                 public virtual string EncodingName {
124                         get {
125                                 return encoding_name;
126                         }
127                 }
128
129                 public virtual string HeaderName {
130                         get {
131                                 return header_name;
132                         }
133                 }
134
135                 public virtual bool IsBrowserDisplay {
136                         get {
137                                 return is_browser_display;
138                         }
139                 }
140
141                 public virtual bool IsBrowserSave {
142                         get {
143                                 return is_browser_save;
144                         }
145                 }
146
147                 public virtual bool IsMailNewsDisplay {
148                         get {
149                                 return is_mail_news_display;
150                         }
151                 }
152
153                 public virtual bool IsMailNewsSave {
154                         get {
155                                 return is_mail_news_save;
156                         }
157                 }
158
159                 public virtual string WebName {
160                         get {
161                                 return web_name;
162                         }
163                 }
164
165                 [MonoTODO]
166                 public virtual int WindowsCodePage {
167                         get {
168                                 // FIXME
169                                 return 0;
170                         }
171                 }
172
173                 public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes)
174                 {
175                         return dstEncoding.GetBytes (srcEncoding.GetChars (bytes));
176                 }
177
178                 public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding,
179                                              byte[] bytes, int index, int count)
180                 {
181                         return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count));
182                 }
183
184                 public override bool Equals (object value)
185                 {
186                         if (!(value is Encoding))
187                                 return false;
188
189                         Encoding e = (Encoding) value;
190
191                         if (e.codepage != codepage)
192                                 return false;
193
194                         if (e.body_name != body_name)
195                                 return false;
196
197                         if (e.encoding_name != encoding_name)
198                                 return false;
199
200                         if (e.header_name != header_name)
201                                 return false;
202                         
203                         return true;
204                 }
205
206                 public virtual int GetByteCount (char[] chars)
207                 {
208                         return GetByteCount (chars, 0, chars.Length);
209                 }
210
211                 public virtual int GetByteCount (string s)
212                 {
213                         char [] chars = s.ToCharArray ();
214                         
215                         return GetByteCount (chars, 0, chars.Length);
216                 }
217
218                 public virtual int GetByteCount (char[] chars, int index, int count)
219                 {
220                         return iconv_encoder.GetByteCount (chars, index, count, false);
221                 }
222
223                 public virtual byte[] GetBytes(char[] chars)
224                 {
225                         return GetBytes (chars, 0, chars.Length);
226                 }
227
228                 public virtual byte[] GetBytes(string s)
229                 {
230                         char [] chars = s.ToCharArray (); 
231                         return GetBytes (chars, 0, chars.Length);
232                 }
233
234                 public virtual byte[] GetBytes(char[] chars, int index, int count)
235                 {
236                         int bc = GetMaxByteCount (count - index);
237                         byte [] bytes = new byte [bc];
238                         
239                         int len = GetBytes (chars, index, count, bytes, 0);
240                         byte [] res = new byte [len];
241
242                         Array.Copy (bytes, res, len);
243                         
244                         return res;
245                 }
246
247                 public int GetBytes (char[] chars, int charIndex, int charCount,
248                                      byte[] bytes, int byteIndex)
249                 {
250                         return iconv_encoder.GetBytes (chars, charIndex, charCount, bytes, byteIndex, true);
251                 }
252
253                 public virtual int GetBytes(string s, int charIndex, int charCount,
254                                             byte[] bytes, int byteIndex)
255                 {
256                         return GetBytes (s.ToCharArray (), charIndex, charCount, bytes, byteIndex);
257                 }
258
259                 public virtual int GetCharCount (byte[] bytes)
260                 {
261                         return GetCharCount (bytes, 0, bytes.Length);
262                 }
263
264                 public virtual int GetCharCount (byte[] bytes, int index, int count)
265                 {
266                         return iconv_decoder.GetCharCount (bytes, index, count);
267                 }
268
269                 public virtual char[] GetChars (byte[] bytes)
270                 {
271                         return GetChars (bytes, 0, bytes.Length);
272                 }
273
274                 public virtual char[] GetChars (byte[] bytes, int index, int count)
275                 {
276                         int cc = GetMaxCharCount (count - index);
277                         char [] chars = new char [cc];
278
279                         int len = GetChars (bytes, index, count, chars, 0);
280                         char [] res = new char [len];
281                         
282                         Array.Copy (chars, res, len);
283
284                         return res;
285                 }
286
287                 public virtual int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
288                 {
289                         return iconv_decoder.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
290                 }
291
292                 public virtual Decoder GetDecoder()
293                 {
294                         if (iconv_name != null)
295                                 return new IConvDecoder (iconv_name, big_endian);
296                         
297                         if (default_decoder == null)
298                                 default_decoder = new DefaultDecoder (this);
299                         
300                         return default_decoder;
301                 }
302
303                 public virtual Encoder GetEncoder() 
304                 {
305                         if (iconv_name != null)
306                                 return new IConvEncoder (iconv_name, big_endian);
307
308                         if (default_encoder == null)
309                                 default_encoder = new DefaultEncoder (this);
310                         
311                         return default_encoder;
312                 }
313
314                 [MonoTODO]
315                 public virtual Encoding GetEncoding (int codepage)
316                 {
317                         // FIXME
318                         return null;
319                 }
320                 
321                 [MonoTODO]
322                 public virtual Encoding GetEncoding (string name)
323                 {
324                         // FIXME
325                         return null;
326                 }
327
328                 [MonoTODO]
329                 public override int GetHashCode()
330                 {
331                         // FIXME
332                         return 0;
333                 }
334
335                 public abstract int GetMaxByteCount (int charCount);
336
337                 public abstract int GetMaxCharCount (int byteCount);
338
339                 [MonoTODO]
340                 public virtual byte[] GetPreamble()
341                 {
342                         // FIXME
343                         return null;
344                 }
345
346                 public virtual string GetString(byte[] bytes)
347                 {
348                         return GetString (bytes, 0, bytes.Length);
349                 }
350
351                 public virtual string GetString(byte[] bytes, int index, int count)
352                 {
353                         char [] chars = GetChars (bytes, index, count);
354
355                         return chars.ToString ();
356                 }
357
358                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
359                 internal extern static IntPtr IConvNewEncoder (string name, bool big_endian);
360
361                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
362                 internal extern static IntPtr IConvNewDecoder (string name, bool big_endian);
363
364                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
365                 internal extern static void IConvReset (IntPtr converter);
366
367                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
368                 internal extern static int IConvGetByteCount (IntPtr converter, char[] chars,
369                                                               int index, int count);
370
371                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
372                 internal extern static int IConvGetBytes (IntPtr converter, char[] chars, int charIndex,
373                                                           int charCount, byte[] bytes, int byteIndex);
374                 
375                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
376                 internal extern static int IConvGetCharCount (IntPtr converter, byte[] bytes,
377                                                               int index, int count);
378
379                 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
380                 internal extern static int IConvGetChars (IntPtr converter, byte[] bytes, int byteIndex,
381                                                           int byteCount, char[] chars, int charIndex);
382         }
383 }