* roottypes.cs: Rename from tree.cs.
[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 readonly static CharsetCollection supportedCharsets = Charset.InitializeSupportedCharsets();
29
30                 #endregion
31
32                 #region Static Properties
33
34                 public static CharsetCollection SupportedCharsets
35                 {
36                         get { return Charset.supportedCharsets; }
37                 }
38
39                 public static Charset DefaultCharset
40                 {
41                         get { return Charset.SupportedCharsets[0]; }
42                 }
43
44                 #endregion
45
46                 #region Fields
47
48                 private int             id;
49                 private int             bytesPerCharacter;
50                 private string  name;
51                 private string  systemName;
52
53                 #endregion
54
55                 #region Properties
56
57                 public int ID
58                 {
59                         get { return this.id; }
60                 }
61
62                 public string Name
63                 {
64                         get { return this.name; }
65                 }
66
67                 public int BytesPerCharacter
68                 {
69                         get { return this.bytesPerCharacter; }
70                 }
71
72                 #endregion
73
74                 #region Constructors
75
76                 public Charset(
77                         int id, string name, int bytesPerCharacter, string systemName)
78                 {
79                         this.id                                 = id;
80                         this.name                               = name;
81                         this.bytesPerCharacter  = bytesPerCharacter;
82                         this.systemName                 = systemName;
83                 }
84
85                 #endregion
86
87                 #region Methods
88
89                 public byte[] GetBytes(string s)
90                 {
91                         return this.GetEncoding().GetBytes(s);
92                 }
93
94                 public int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
95                 {
96                         return this.GetEncoding().GetBytes(s, charIndex, charCount, bytes, byteIndex);
97                 }
98
99                 public string GetString(byte[] buffer)
100                 {
101                         return this.GetString(buffer, 0, buffer.Length);
102                 }
103
104                 public string GetString(byte[] buffer, int index, int count)
105                 {
106                         return this.GetEncoding().GetString(buffer, index, count);
107                 }
108
109                 #endregion
110
111                 #region Private Methods
112
113                 private Encoding GetEncoding()
114                 {
115                         switch (this.systemName)
116                         {
117                                 case "NONE":
118                                         return Encoding.Default;
119
120                                 case "OCTETS":
121                                         return new BinaryEncoding();
122
123                                 default:
124                                         return Encoding.GetEncoding(this.systemName);
125                         }
126                 }
127
128                 #endregion
129
130                 #region Static Methods
131
132                 public static CharsetCollection InitializeSupportedCharsets()
133                 {
134                         CharsetCollection charsets = new CharsetCollection();
135
136                         // NONE
137                         Charset.Add(charsets, 0, "NONE", 1, "NONE");
138                         // OCTETS
139                         Charset.Add(charsets, 1, "OCTETS", 1, "OCTETS");
140                         // American Standard Code for Information Interchange   
141                         Charset.Add(charsets, 2, "ASCII", 1, "ascii");
142                         // Eight-bit Unicode Transformation Format
143                         Charset.Add(charsets, 3, "UNICODE_FSS", 3, "UTF-8");
144                         // Shift-JIS, Japanese
145                         Charset.Add(charsets, 5, "SJIS_0208", 2, "shift_jis");
146                         // JIS X 0201, 0208, 0212, EUC encoding, Japanese
147                         Charset.Add(charsets, 6, "EUCJ_0208", 2, "euc-jp");
148                         // Windows Japanese     
149                         Charset.Add(charsets, 7, "ISO2022-JP", 2, "iso-2022-jp");
150                         // MS-DOS United States, Australia, New Zealand, South Africa   
151                         Charset.Add(charsets, 10, "DOS437", 1, "IBM437");
152                         // MS-DOS Latin-1                               
153                         Charset.Add(charsets, 11, "DOS850", 1, "ibm850");
154                         // MS-DOS Nordic        
155                         Charset.Add(charsets, 12, "DOS865", 1, "IBM865");
156                         // MS-DOS Portuguese    
157                         Charset.Add(charsets, 13, "DOS860", 1, "IBM860");
158                         // MS-DOS Canadian French       
159                         Charset.Add(charsets, 14, "DOS863", 1, "IBM863");
160                         // ISO 8859-1, Latin alphabet No. 1
161                         Charset.Add(charsets, 21, "ISO8859_1", 1, "iso-8859-1");
162                         // ISO 8859-2, Latin alphabet No. 2
163                         Charset.Add(charsets, 22, "ISO8859_2", 1, "iso-8859-2");
164                         // Windows Korean       
165                         Charset.Add(charsets, 44, "KSC_5601", 2, "ks_c_5601-1987");
166                         // MS-DOS Icelandic     
167                         Charset.Add(charsets, 47, "DOS861", 1, "ibm861");
168                         // Windows Eastern European
169                         Charset.Add(charsets, 51, "WIN1250", 1, "windows-1250");
170                         // Windows Cyrillic
171                         Charset.Add(charsets, 52, "WIN1251", 1, "windows-1251");
172                         // Windows Latin-1
173                         Charset.Add(charsets, 53, "WIN1252", 1, "windows-1252");
174                         // Windows Greek
175                         Charset.Add(charsets, 54, "WIN1253", 1, "windows-1253");
176                         // Windows Turkish
177                         Charset.Add(charsets, 55, "WIN1254", 1, "windows-1254");
178                         // Big5, Traditional Chinese
179                         Charset.Add(charsets, 56, "BIG_5", 2, "big5");
180                         // GB2312, EUC encoding, Simplified Chinese     
181                         Charset.Add(charsets, 57, "GB_2312", 2, "gb2312");
182                         // Windows Hebrew
183                         Charset.Add(charsets, 58, "WIN1255", 1, "windows-1255");
184                         // Windows Arabic       
185                         Charset.Add(charsets, 59, "WIN1256", 1, "windows-1256");
186                         // Windows Baltic       
187                         Charset.Add(charsets, 60, "WIN1257", 1, "windows-1257");
188
189             return charsets;
190                 }
191
192                 private static void Add(
193                         CharsetCollection charsets, int id, string charset, int bytesPerCharacter, string systemName)
194                 {
195                         charsets.Add(
196                                         id,
197                                         charset,
198                                         bytesPerCharacter,
199                                         systemName);
200                 }
201
202                 #endregion
203         }
204 }