wasCSharpSQLite – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * EncodingCmd.java --
3 *
4 * Copyright (c) 2001 Bruce A. Johnson
5 *
6 * See the file "license.terms" for information on usage and
7 * redistribution of this file, and for a DISCLAIMER OF ALL
8 * WARRANTIES.
9 *
10 * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
11 *
12 * RCS @(#) $Id: EncodingCmd.java,v 1.2 2002/04/12 15:32:44 mdejong Exp $
13 *
14 */
15 using System;
16 using System.Text;
17 using System.Collections;
18 using System.IO;
19  
20 namespace tcl.lang
21 {
22  
23 /// <summary> This class implements the built-in "encoding" command in Tcl.</summary>
24  
25 class EncodingCmd : Command
26 {
27 // FIXME: Make sure this is a global property and not a per-interp
28 // property!
29 internal static string systemTclEncoding = "utf-8";
30 internal static Encoding systemJavaEncoding = UTF8Encoding.UTF8;
31  
32 internal static string[] tclNames = new string[] { "utf-8", "unicode", "ascii", "utf-7" };
33  
34 internal static readonly Encoding[] encodings = new Encoding[] { UTF8Encoding.UTF8, UnicodeEncoding.Unicode, ASCIIEncoding.Unicode, UTF7Encoding.UTF7 };
35  
36 internal static int[] bytesPerChar = new int[] { 1, 2, 1, 1 };
37  
38 private static readonly string[] validCmds = new string[] { "convertfrom", "convertto", "names", "system" };
39  
40 internal const int OPT_CONVERTFROM = 0;
41 internal const int OPT_CONVERTTO = 1;
42 internal const int OPT_NAMES = 2;
43 internal const int OPT_SYSTEM = 3;
44  
45 /// <summary> This procedure is invoked to process the "encoding" Tcl command.
46 /// See the user documentation for details on what it does.
47 ///
48 /// </summary>
49 /// <param name="interp">the current interpreter.
50 /// </param>
51 /// <param name="argv">command arguments.
52 /// </param>
53  
54 public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv )
55 {
56 if ( argv.Length < 2 )
57 {
58 throw new TclNumArgsException( interp, 1, argv, "option ?arg ...?" );
59 }
60  
61 int index = TclIndex.get( interp, argv[1], validCmds, "option", 0 );
62  
63 switch ( index )
64 {
65  
66 case OPT_CONVERTTO:
67 case OPT_CONVERTFROM:
68 {
69 string tclEncoding;
70 Encoding javaEncoding;
71 TclObject data;
72  
73 if ( argv.Length == 3 )
74 {
75 tclEncoding = systemTclEncoding;
76 data = argv[2];
77 }
78 else if ( argv.Length == 4 )
79 {
80  
81 tclEncoding = argv[2].ToString();
82 data = argv[3];
83 }
84 else
85 {
86 throw new TclNumArgsException( interp, 2, argv, "?encoding? data" );
87 }
88  
89 javaEncoding = getJavaName( tclEncoding );
90  
91 if ( (System.Object)javaEncoding == null )
92 {
93 throw new TclException( interp, "unknown encoding \"" + tclEncoding + "\"" );
94 }
95  
96 try
97 {
98 if ( index == OPT_CONVERTFROM )
99 {
100 // Treat the string as binary data
101 byte[] bytes = TclByteArray.getBytes( interp, data );
102  
103 // ATK
104 interp.setResult( System.Text.Encoding.UTF8.GetString( bytes, 0, bytes.Length) );
105 }
106 else
107 {
108 // Store the result as binary data
109  
110  
111 // ATK byte[] bytes = data.ToString().getBytes(javaEncoding);
112 byte[] bytes = System.Text.Encoding.UTF8.GetBytes( data.ToString() );
113 interp.setResult( TclByteArray.newInstance( bytes ) );
114 }
115 }
116 catch ( IOException ex )
117 {
118 throw new TclRuntimeError( "Encoding.cmdProc() error: " + "unsupported java encoding \"" + javaEncoding + "\"" );
119 }
120  
121 break;
122 }
123  
124 case OPT_NAMES:
125 {
126 if ( argv.Length > 2 )
127 {
128 throw new TclNumArgsException( interp, 2, argv, null );
129 }
130  
131 TclObject list = TclList.newInstance();
132 for ( int i = 0; i < tclNames.Length; i++ )
133 {
134 TclList.append( interp, list, TclString.newInstance( tclNames[i] ) );
135 }
136 interp.setResult( list );
137 break;
138 }
139  
140 case OPT_SYSTEM:
141 {
142 if ( argv.Length > 3 )
143 throw new TclNumArgsException( interp, 2, argv, "?encoding?" );
144  
145 if ( argv.Length == 2 )
146 {
147 interp.setResult( systemTclEncoding );
148 }
149 else
150 {
151  
152 string tclEncoding = argv[2].ToString();
153 Encoding javaEncoding = getJavaName( tclEncoding );
154  
155 if ( javaEncoding == null )
156 {
157 throw new TclException( interp, "unknown encoding \"" + tclEncoding + "\"" );
158 }
159  
160 systemTclEncoding = tclEncoding;
161 systemJavaEncoding = javaEncoding;
162 }
163  
164 break;
165 }
166  
167 default:
168 {
169 throw new TclRuntimeError( "Encoding.cmdProc() error: " + "incorrect index returned from TclIndex.get()" );
170 }
171  
172 }
173 return TCL.CompletionCode.RETURN;
174 }
175  
176 internal static int getBytesPerChar( Encoding encoding )
177 {
178 return encoding.GetMaxByteCount( 1 );
179 }
180  
181 internal static System.Text.Encoding getJavaName( string name )
182 {
183 for ( int x = 0; x < EncodingCmd.tclNames.Length; x++ )
184 {
185 if ( EncodingCmd.tclNames[x] == name )
186 return EncodingCmd.encodings[x];
187 }
188 return null;
189 }
190  
191 internal static string getTclName( Encoding encoding )
192 {
193 for ( int x = 0; x < EncodingCmd.encodings.Length; x++ )
194 {
195 if ( EncodingCmd.encodings[x].EncodingName == encoding.EncodingName )
196 return EncodingCmd.tclNames[x];
197 }
198 return null;
199 }
200 }
201 }