wasCSharpSQLite – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections;
3 using System.Diagnostics;
4 using System.Text;
5  
6 using sqlite_int64 = System.Int64;
7 using i32 = System.Int32;
8 using i64 = System.Int64;
9 using u32 = System.UInt32;
10  
11 namespace tcl.lang
12 {
13 #if TCLSH
14 using lang;
15 using Tcl_Channel = Channel;
16 using Tcl_DString = TclString;
17 using Tcl_Interp = Interp;
18 using Tcl_Obj = TclObject;
19 using Tcl_WideInt = System.Int64;
20  
21 public partial class TCL
22 {
23  
24 // -- Conversion from TCL to tclsharp coding
25 // Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
26 public static void Tcl_AppendElement( Interp interp, StringBuilder toAppend )
27 {
28 interp.appendElement( toAppend.ToString() );
29 }
30  
31 public static void Tcl_AppendElement( Interp interp, string toAppend )
32 {
33 interp.appendElement( toAppend );
34 }
35  
36 public static void Tcl_AppendResult( Interp interp, params object[] tos )
37 {
38 if ( tos != null )
39 {
40 StringBuilder result = new StringBuilder( 100 );
41 for ( int i = 0; i < tos.Length && tos[i] != null; i++ )
42 result.Append( tos[i].ToString() );
43 interp.appendElement( result.ToString() );
44 }
45 }
46  
47 public static void Tcl_AppendResult( Interp interp, params string[] strings )
48 {
49 if ( strings != null )
50 {
51 StringBuilder result = new StringBuilder( 100 );
52 for ( int i = 0; i < strings.Length && strings[i] != null && strings[i] != ""; i++ )
53 result.Append( strings[i] );
54 interp.appendElement( result.ToString() );
55 }
56 }
57  
58 public static void Tcl_BackgroundError( Interp interp )
59 {
60 interp.setErrorCode( TclInteger.newInstance( TCL_ERROR ) );
61 interp.addErrorInfo( "Background Error" );
62 }
63  
64 public static void Tcl_CreateCommand( Interp interp, string cmdName, Interp.dxObjCmdProc ObjCmdProc, object ClientData, Interp.dxCmdDeleteProc DbDeleteCmd )
65 {
66 interp.createObjCommand( cmdName, ObjCmdProc, ClientData, DbDeleteCmd );
67 }
68  
69 public static void Tcl_CreateObjCommand( Interp interp, string cmdName, Interp.dxObjCmdProc ObjCmdProc, object ClientData, Interp.dxCmdDeleteProc DbDeleteCmd )
70 {
71 interp.createObjCommand( cmdName, ObjCmdProc, ClientData, DbDeleteCmd );
72 }
73  
74  
75 public static bool Tcl_CreateCommandPointer( Interp interp, StringBuilder command, object clientData )
76 {
77 try
78 {
79 interp.createObjCommand( command.ToString(), null, clientData, null );
80 return false;
81 }
82 catch
83 {
84 return true;
85 }
86 }
87  
88 public static bool Tcl_CreateCommandPointer( Interp interp, string command, object clientData )
89 {
90 try
91 {
92 interp.createObjCommand( command, null, clientData, null );
93 return false;
94 }
95 catch
96 {
97 return true;
98 }
99 }
100  
101 public static void Tcl_DecrRefCount( ref TclObject to )
102 {
103 to.release();
104 if ( to.internalRep == null )
105 to = null;
106 }
107  
108 public static int Tcl_DeleteCommand( Interp interp, string cmdName )
109 {
110 return interp.deleteCommand( cmdName );
111 }
112  
113 public static void Tcl_DStringAppendElement( TclObject str, string append )
114 {
115 TclString.append( str, append );
116 }
117  
118 public static void Tcl_DStringFree( ref TclObject str )
119 {
120 str.release();
121 }
122  
123 public static void Tcl_DStringInit( out TclObject str )
124 {
125 str = TclString.newInstance( "" );
126 str.preserve();
127 }
128  
129 public static int Tcl_DStringLength( TclObject str )
130 {
131 return str.ToString().Length;
132 }
133  
134 public static TclObject Tcl_DuplicateObj( TclObject to )
135 {
136 return to.duplicate();
137 }
138  
139 public static int Tcl_Eval( Interp interp, string s )
140 {
141 try
142 {
143 interp.eval( s );
144 return 0;
145 }
146 catch
147 {
148 return 1;
149 };
150 }
151 public static int Tcl_EvalObjEx( Interp interp, TclObject tobj, int flags )
152 {
153 try
154 {
155 interp.eval( tobj, flags );
156 return 0;
157 }
158 catch ( TclException e )
159 {
160 if ( e.getCompletionCode() == TCL.CompletionCode.RETURN )
161 return TCL_RETURN;
162 else if ( e.getCompletionCode() == TCL.CompletionCode.BREAK || interp.getResult().ToString() == "invoked \"break\" outside of a loop" )
163 return TCL_BREAK;
164 else
165 return TCL_ERROR;
166 };
167 }
168  
169 public static void Tcl_Free( ref TclObject[] to )
170 {
171 if ( to != null )
172 for ( int i = 0; i < to.Length; i++ )
173 while ( to[i] != null && to[i].refCount > 0 )
174 to[i].release();
175 to = null;
176 }
177  
178 public static void Tcl_Free( ref TclObject to )
179 {
180 while ( to.refCount > 0 )
181 to.release();
182 }
183  
184 public static void Tcl_Free<T>( ref T x ) where T : class
185 {
186 x = null;
187 }
188  
189 public static bool Tcl_GetBoolean( Interp interp, TclObject to, out int result )
190 {
191 try
192 {
193 result = ( TclBoolean.get( interp, to ) ? 1 : 0 );
194 return false;
195 }
196 catch
197 {
198 result = 0;
199 return true;
200 }
201 }
202  
203 public static bool Tcl_GetBoolean( Interp interp, TclObject to, out bool result )
204 {
205 try
206 {
207 result = TclBoolean.get( interp, to );
208 return false;
209 }
210 catch
211 {
212 result = false;
213 return true;
214 }
215 }
216  
217 public static bool Tcl_GetBooleanFromObj( Interp interp, TclObject to, out bool result )
218 {
219 try
220 {
221 result = TclBoolean.get( interp, to );
222 return false;
223 }
224 catch
225 {
226 result = false;
227 return true;
228 }
229 }
230  
231 public static bool Tcl_GetCommandInfo( Interp interp, string command, out WrappedCommand value )
232 {
233 try
234 {
235 value = interp.getObjCommand( command );
236 return false;
237 }
238 catch
239 {
240 value = null;
241 return true;
242 }
243 }
244  
245 public static byte[] Tcl_GetByteArrayFromObj( TclObject to, out int n )
246 {
247 n = TclByteArray.getLength( null, to );
248 return Encoding.UTF8.GetBytes( to.ToString() );
249 }
250  
251 public static bool Tcl_GetDouble( Interp interp, TclObject to, out double value )
252 {
253 try
254 {
255 value = TclDouble.get( interp, to );
256 return false;
257 }
258 catch
259 {
260 value = 0;
261 return true;
262 }
263 }
264  
265 public static bool Tcl_GetDoubleFromObj( Interp interp, TclObject to, out double value )
266 {
267 try
268 {
269 if ( to.ToString() == "NaN" )
270 value = Double.NaN;
271 else
272 value = TclDouble.get( interp, to );
273 return false;
274 }
275 catch
276 {
277 value = 0;
278 return true;
279 }
280 }
281  
282 public static bool Tcl_GetIndexFromObj( Interp interp, TclObject to, string[] table, string msg, int flags, out int index )
283 {
284 try
285 {
286 index = TclIndex.get( interp, to, table, msg, flags );
287 return false;
288 }
289 catch
290 {
291 index = 0;
292 return true;
293 }
294 }
295  
296 public static bool Tcl_GetInt( Interp interp, TclObject to, out int value )
297 {
298 try
299 {
300 value = TclInteger.get( interp, to );
301 return false;
302 }
303 catch
304 {
305 value = 0;
306 return true;
307 }
308 }
309  
310 public static bool Tcl_GetInt( Interp interp, TclObject to, out u32 value )
311 {
312 try
313 {
314 value = (u32)TclInteger.get( interp, to );
315 return false;
316 }
317 catch
318 {
319 value = 0;
320 return true;
321 }
322 }
323  
324 public static int Tcl_GetIntFromObj( Interp interp, TclObject to, out int value )
325 {
326 try
327 {
328 value = TclInteger.get( interp, to );
329 return TCL.TCL_OK;
330 }
331 catch
332 {
333 value = 0;
334 return TCL.TCL_ERROR;
335 }
336 }
337  
338 public static bool Tcl_GetLong( Interp interp, TclObject to, out i64 value )
339 {
340 try
341 {
342 value = (i64)TclLong.get( interp, to );
343 return false;
344 }
345 catch
346 {
347 value = 0;
348 return true;
349 }
350 }
351  
352 public static TclObject Tcl_GetObjResult( Interp interp )
353 {
354 TclObject toReturn = interp.getResult();
355 return toReturn;
356 }
357  
358 public static string Tcl_GetString( TclObject to )
359 {
360 return to.ToString();
361 }
362  
363 public static string Tcl_GetStringFromObj( TclObject to, int n )
364 {
365 Debug.Assert( n == 0, "Try calling by ref" );
366 return to.ToString();
367 }
368  
369 public static string Tcl_GetStringFromObj( TclObject to, out int n )
370 {
371 byte[] tb = System.Text.Encoding.UTF8.GetBytes( to.ToString() );
372 string ts = System.Text.Encoding.UTF8.GetString( tb, 0, tb.Length );
373 n = ts.Length;
374 return ts;
375 }
376  
377 public static string Tcl_GetStringResult( Interp interp )
378 {
379 return interp.getResult().ToString();
380 }
381  
382 public static TclObject Tcl_GetVar2Ex( Interp interp, string part1, string part2, VarFlag flags )
383 {
384 try
385 {
386 Var[] result = Var.lookupVar( interp, part1, part2, flags, "read", false, true );
387 if ( result == null )
388 {
389 // lookupVar() returns null only if VarFlag.LEAVE_ERR_MSG is
390 // not part of the flags argument, return null in this case.
391  
392 return null;
393 }
394  
395 Var var = result[0];
396 Var array = result[1];
397 TclObject to = null;
398  
399 if ( var.isVarScalar() && !var.isVarUndefined() )
400 {
401 to = (TclObject)var.value;
402 //if ( to.typePtr != "String" )
403 //{
404 // double D = 0;
405 // if ( !Double.TryParse( to.ToString(), out D ) ) { if ( String.IsNullOrEmpty( to.typePtr ) ) to.typePtr = "string"; }
406 // else if ( to.typePtr == "ByteArray" )
407 // to.typePtr = "bytearray";
408 // else if ( to.ToString().Contains( "." ) )
409 // to.typePtr = "double";
410 // else
411 // to.typePtr = "int";
412 //}
413 return to;
414 }
415 else if ( var.isSQLITE3_Link() )
416 {
417 to = (TclObject)var.sqlite3_get();
418 }
419 else
420 {
421 to = TclList.newInstance();
422 foreach ( string key in ( (Hashtable)array.value ).Keys )
423 {
424 Var s = (Var)( (Hashtable)array.value )[key];
425 if (s.value != null) TclList.append( null, to, TclString.newInstance( s.value.ToString() ) );
426 }
427 }
428 return to;
429 }
430 catch (Exception e)
431 {
432 return null;
433 };
434 }
435  
436 public static TclObject Tcl_GetVar( Interp interp, string part, VarFlag flags )
437 {
438 try
439 {
440 TclObject to = interp.getVar( part, flags );
441 return to;
442 }
443 catch ( Exception e )
444 {
445 return TclObj.newInstance( "" );
446 };
447 }
448  
449  
450 public static TclObject Tcl_GetVarType( Interp interp, string part1, string part2, VarFlag flags )
451 {
452 try
453 {
454 TclObject to = interp.getVar( part1, part2, flags );
455 return to;
456 }
457 catch
458 {
459 return null;
460 };
461 }
462  
463 public static bool Tcl_GetWideIntFromObj( Interp interp, TclObject to, out sqlite_int64 value )
464 {
465 try
466 {
467 if ( to.ToString() == "NaN" )
468 unchecked
469 {
470 value = (long)Double.NaN;
471 }
472 else
473 value = TclLong.get( interp, to );
474 return false;
475 }
476 catch
477 {
478 value = 0;
479 return true;
480 };
481 }
482  
483 public static void Tcl_IncrRefCount( TclObject to )
484 {
485 to.preserve();
486 }
487  
488 public static void Tcl_LinkVar( Interp interp, string name, Object GetSet, VarFlags flags )
489 {
490 Debug.Assert( ( ( flags & VarFlags.SQLITE3_LINK_READ_ONLY ) != 0 ) || GetSet.GetType().Name == "SQLITE3_GETSET" );
491 Var[] linkvar = Var.lookupVar( interp, name, null, VarFlag.GLOBAL_ONLY, "define", true, false );
492 linkvar[0].flags |= VarFlags.SQLITE3_LINK | flags;
493 linkvar[0].sqlite3_get_set = GetSet;
494 linkvar[0].refCount++;
495 }
496  
497 public static bool Tcl_ListObjAppendElement( Interp interp, TclObject to, TclObject elemObj )
498 {
499 try
500 {
501 TclList.append( interp, to, elemObj );
502 return false;
503 }
504 catch
505 {
506 return true;
507 }
508 }
509  
510 public static void Tcl_ListObjIndex( Interp interp, TclObject to, int nItem, out TclObject elmObj )
511 {
512 try
513 {
514 elmObj = TclList.index( interp, to, nItem );
515 }
516 catch
517 {
518 elmObj = null;
519 }
520 }
521  
522 public static bool Tcl_ListObjGetElements( Interp interp, TclObject to, out int nItem, out TclObject[] elmObj )
523 {
524 try
525 {
526 elmObj = TclList.getElements( interp, to );
527 nItem = elmObj.Length;
528 return false;
529 }
530 catch
531 {
532 elmObj =null;
533 nItem = 0;
534 return true;
535 }
536 }
537  
538 public static void Tcl_ListObjLength( Interp interp, TclObject to, out int nArg )
539 {
540 try
541 {
542 nArg = TclList.getLength( interp, to );
543 }
544 catch
545 {
546 nArg = 0;
547 }
548 }
549  
550 public static TclObject Tcl_NewBooleanObj( int value )
551 {
552 return TclBoolean.newInstance( value != 0 );
553 }
554  
555 public static TclObject Tcl_NewByteArrayObj( byte[] value, int bytes )
556 {
557 if ( value == null || value.Length == 0 || bytes == 0 )
558 return TclByteArray.newInstance();
559 else
560 return TclByteArray.newInstance( value, 0, bytes );
561 }
562  
563 public static TclObject Tcl_NewByteArrayObj( string value, int bytes )
564 {
565 if ( value == null || bytes == 0 )
566 return TclByteArray.newInstance();
567 else
568 return TclByteArray.newInstance( System.Text.Encoding.UTF8.GetBytes( value.Substring( 0, bytes ) ) );
569 }
570  
571 public static TclObject Tcl_NewDoubleObj( double value )
572 {
573 return TclDouble.newInstance( value );
574 }
575  
576 public static TclObject Tcl_NewIntObj( int value )
577 {
578 return TclInteger.newInstance( value );
579 }
580  
581 public static TclObject Tcl_NewListObj( int nArg, TclObject[] aArg )
582 {
583 TclObject to = TclList.newInstance();
584 for ( int i = 0; i < nArg; i++ )
585 TclList.append( null, to, aArg[i] );
586 return to;
587 }
588  
589 public static TclObject Tcl_NewObj()
590 {
591 return TclString.newInstance( "" );
592 }
593  
594 public static TclObject Tcl_NewStringObj( byte[] value, int iLength )
595 {
596 if ( iLength > 0 && iLength < value.Length )
597 return TclString.newInstance( Encoding.UTF8.GetString( value, 0, iLength ) );
598 else
599 return TclString.newInstance( Encoding.UTF8.GetString( value, 0, value.Length ) );
600 }
601  
602 public static TclObject Tcl_NewStringObj( string value, int iLength )
603 {
604 if ( value == null )
605 value = "";
606 else
607 value = value.Split( '\0' )[0];
608 if ( iLength <= 0 )
609 iLength = value.Length;
610 return TclString.newInstance( value.Substring( 0, iLength ) );
611 }
612  
613 public static TclObject Tcl_NewWideIntObj( long value )
614 {
615 return TclLong.newInstance( value );
616 }
617  
618 public static bool Tcl_ObjSetVar2( Interp interp, TclObject toName, TclObject part2, TclObject toValue, VarFlag flags )
619 {
620 try
621 {
622 if ( part2 == null )
623 interp.setVar( toName, toValue, flags );
624 else
625 interp.setVar( toName.ToString(), part2.ToString(), toValue.ToString(), flags );
626 return false;
627 }
628 catch
629 {
630 return true;
631 }
632 }
633 public static void Tcl_PkgProvide( Interp interp, string name, string version )
634 {
635 interp.pkgProvide( name, version );
636 }
637  
638 public static void Tcl_ResetResult( Interp interp )
639 {
640 interp.resetResult();
641 }
642  
643 public static void Tcl_SetBooleanObj( TclObject to, int result )
644 {
645 to.stringRep = TclBoolean.newInstance( result != 0 ).ToString();
646 to.preserve();
647 }
648  
649 public static bool Tcl_SetCommandInfo( Interp interp, string command, WrappedCommand value )
650 {
651 try
652 {
653 value = interp.getObjCommand( command );
654 return false;
655 }
656 catch
657 {
658 return true;
659 }
660 }
661  
662 public static void Tcl_SetIntObj( TclObject to, int result
663 )
664 {
665 while ( to.Shared )
666 to.release();
667 TclInteger.set( to, result );
668 to.preserve();
669 }
670  
671 public static void Tcl_SetLongObj( TclObject to, long result )
672 {
673 while ( to.Shared )
674 to.release();
675 TclLong.set( to, result );
676 to.preserve();
677 }
678  
679 public static void Tcl_SetObjResult( Interp interp, TclObject to )
680 {
681 interp.resetResult();
682 interp.setResult( to );
683 }
684  
685 public static void Tcl_SetResult( Interp interp, StringBuilder result, int dummy )
686 {
687 interp.resetResult();
688 interp.setResult( result.ToString() );
689 }
690  
691 public static void Tcl_SetResult( Interp interp, string result, int dummy )
692 {
693 interp.resetResult();
694 interp.setResult( result );
695 }
696  
697 public static void Tcl_SetVar( Interp interp, string part, string value, int flags )
698 {
699 interp.setVar( part, value, (VarFlag)flags );
700 }
701  
702 public static void Tcl_SetVar2( Interp interp, string part1, string part2, string value, int flags )
703 {
704 interp.setVar( part1, part2, value, (VarFlag)flags );
705 }
706  
707 public static void Tcl_SetVar2( Interp interp, string part1, string part2, TclObject value, int flags )
708 {
709 interp.setVar( part1, part2, value, (VarFlag)flags );
710 }
711  
712 public static void Tcl_UnregisterChannel( Interp interp, Channel chan )
713 {
714 TclIO.unregisterChannel( interp, chan );
715 }
716  
717 public static int Tcl_VarEval( Interp interp, string Scriptname, params string[] argv )
718 {
719 try
720 {
721 //Tcl_Obj[] aArg = null;
722 int rc = 0;
723 Tcl_Obj pCmd = Tcl_NewStringObj( Scriptname, -1 );
724 Tcl_IncrRefCount( pCmd );
725 for ( int i = 0; i < argv.Length; i++ )
726 {
727 if ( argv[i] != null && argv[i] != " " )
728 rc = Tcl_ListObjAppendElement( interp, pCmd, Tcl_NewStringObj( argv[i], -1 ) ) ? 1 : 0;
729 if ( rc != 0 )
730 {
731 Tcl_DecrRefCount( ref pCmd );
732 return 1;
733 }
734 }
735 rc = Tcl_EvalObjEx( interp, pCmd, TCL_EVAL_DIRECT );
736 Tcl_DecrRefCount( ref pCmd );
737 return rc == TCL_BREAK ? 1 : 0;
738 }
739 catch
740 {
741 return 1;
742 }
743 }
744  
745 public static void Tcl_WrongNumArgs( Interp interp, int argc, TclObject[] argv, string message )
746 {
747 throw new TclNumArgsException( interp, argc, argv, message == null ? "option ?arg ...?" : message );
748 }
749  
750 public static Interp Tcl_GetSlave( Interp interp, string slaveInterp )
751 {
752 try
753 {
754 return ( (tcl.lang.InterpSlaveCmd)interp.slaveTable[slaveInterp] ).slaveInterp;
755 }
756 catch
757 {
758 return null;
759 }
760 }
761 }
762 #endif
763 }