2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / Charset.cs
1 /*
2  *  Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *     The contents of this file are subject to the Initial 
5  *     Developer's Public License Version 1.0 (the "License"); 
6  *     you may not use this file except in compliance with the 
7  *     License. You may obtain a copy of the License at 
8  *     http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *     Software distributed under the License is distributed on 
11  *     an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *     express or implied.  See the License for the specific 
13  *     language governing rights and limitations under the License.
14  * 
15  *  Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16  *  All Rights Reserved.
17  */
18
19 using System;
20 using System.Text;
21
22 namespace FirebirdSql.Data.Common
23 {
24         internal sealed class Charset
25         {
26                 #region Static Fields
27
28                 private static CharsetCollection supportedCharsets;
29
30                 #endregion
31
32                 #region Static Properties
33
34                 public static CharsetCollection SupportedCharsets
35                 {
36                         get
37                         {
38                                 if (Charset.supportedCharsets == null)
39                                 {
40                                         Charset.InitializeSupportedCharsets();
41                                 }
42
43                                 return Charset.supportedCharsets;
44                         }
45                 }
46
47                 public static Charset DefaultCharset
48                 {
49                         get { return Charset.SupportedCharsets[0]; }
50                 }
51
52                 #endregion
53
54                 #region Fields
55
56                 private int             id;
57                 private int             bytesPerCharacter;
58                 private string  name;
59                 private string  systemName;
60
61                 #endregion
62
63                 #region Properties
64
65                 public int ID
66                 {
67                         get { return this.id; }
68                 }
69
70                 public string Name
71                 {
72                         get { return this.name; }
73                 }
74
75                 public int BytesPerCharacter
76                 {
77                         get { return this.bytesPerCharacter; }
78                 }
79
80                 #endregion
81
82                 #region Constructors
83
84                 public Charset(
85                         int id, string name, int bytesPerCharacter, string systemName)
86                 {
87                         this.id                                 = id;
88                         this.name                               = name;
89                         this.bytesPerCharacter  = bytesPerCharacter;
90                         this.systemName                 = systemName;
91                 }
92
93                 #endregion
94
95                 #region Methods
96
97                 public byte[] GetBytes(string s)
98                 {
99                         return this.GetEncoding().GetBytes(s);
100                 }
101
102                 public int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
103                 {
104                         return this.GetEncoding().GetBytes(s, charIndex, charCount, bytes, byteIndex);
105                 }
106
107                 public string GetString(byte[] buffer)
108                 {
109                         return this.GetString(buffer, 0, buffer.Length);
110                 }
111
112                 public string GetString(byte[] buffer, int index, int count)
113                 {
114                         return this.GetEncoding().GetString(buffer, index, count);
115                 }
116
117                 #endregion
118
119                 #region Private Methods
120
121                 private Encoding GetEncoding()
122                 {
123                         switch (this.systemName)
124                         {
125                                 case "NONE":
126                                         return Encoding.Default;
127
128                                 case "OCTETS":
129                                         return new BinaryEncoding();
130
131                                 default:
132                                         return Encoding.GetEncoding(this.systemName);
133                         }
134                 }
135
136                 #endregion
137
138                 #region Static Methods
139
140                 public static void InitializeSupportedCharsets()
141                 {
142                         Charset.supportedCharsets = new CharsetCollection();
143
144                         // NONE
145                         Charset.Add(0, "NONE", 1, "NONE");
146                         // OCTETS
147                         Charset.Add(1, "OCTETS", 1, "OCTETS");
148                         // American Standard Code for Information Interchange   
149                         Charset.Add(2, "ASCII", 1, "ascii");
150                         // Eight-bit Unicode Transformation Format
151                         Charset.Add(3, "UNICODE_FSS", 3, "UTF-8");
152                         // Shift-JIS, Japanese
153                         Charset.Add(5, "SJIS_0208", 2, "shift_jis");
154                         // JIS X 0201, 0208, 0212, EUC encoding, Japanese
155                         Charset.Add(6, "EUCJ_0208", 2, "euc-jp");
156                         // Windows Japanese     
157                         Charset.Add(7, "ISO2022-JP", 2, "iso-2022-jp");
158                         // MS-DOS United States, Australia, New Zealand, South Africa   
159                         Charset.Add(10, "DOS437", 1, "IBM437");
160                         // MS-DOS Latin-1                               
161                         Charset.Add(11, "DOS850", 1, "ibm850");
162                         // MS-DOS Nordic        
163                         Charset.Add(12, "DOS865", 1, "IBM865");
164                         // MS-DOS Portuguese    
165                         Charset.Add(13, "DOS860", 1, "IBM860");
166                         // MS-DOS Canadian French       
167                         Charset.Add(14, "DOS863", 1, "IBM863");
168                         // ISO 8859-1, Latin alphabet No. 1
169                         Charset.Add(21, "ISO8859_1", 1, "iso-8859-1");
170                         // ISO 8859-2, Latin alphabet No. 2
171                         Charset.Add(22, "ISO8859_2", 1, "iso-8859-2");
172                         // Windows Korean       
173                         Charset.Add(44, "KSC_5601", 2, "ks_c_5601-1987");
174                         // MS-DOS Icelandic     
175                         Charset.Add(47, "DOS861", 1, "ibm861");
176                         // Windows Eastern European
177                         Charset.Add(51, "WIN1250", 1, "windows-1250");
178                         // Windows Cyrillic
179                         Charset.Add(52, "WIN1251", 1, "windows-1251");
180                         // Windows Latin-1
181                         Charset.Add(53, "WIN1252", 1, "windows-1252");
182                         // Windows Greek
183                         Charset.Add(54, "WIN1253", 1, "windows-1253");
184                         // Windows Turkish
185                         Charset.Add(55, "WIN1254", 1, "windows-1254");
186                         // Big5, Traditional Chinese
187                         Charset.Add(56, "BIG_5", 2, "big5");
188                         // GB2312, EUC encoding, Simplified Chinese     
189                         Charset.Add(57, "GB_2312", 2, "gb2312");
190                         // Windows Hebrew
191                         Charset.Add(58, "WIN1255", 1, "windows-1255");
192                         // Windows Arabic       
193                         Charset.Add(59, "WIN1256", 1, "windows-1256");
194                         // Windows Baltic       
195                         Charset.Add(60, "WIN1257", 1, "windows-1257");
196                 }
197
198                 private static void Add(
199                         int id, string charset, int bytesPerCharacter, string systemName)
200                 {
201                         Charset.SupportedCharsets.Add(
202                                         id,
203                                         charset,
204                                         bytesPerCharacter,
205                                         systemName);
206                 }
207
208                 #endregion
209         }
210 }