PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/TCL/src/commands/LsortCmd.cs

https://bitbucket.org/eumario/csharp-sqlite
C# | 160 lines | 87 code | 31 blank | 42 comment | 9 complexity | 59b80bd03886a13b5225b8a66ea793ba MD5 | raw file
  1. /*
  2. * LsortCmd.java
  3. *
  4. * The file implements the Tcl "lsort" command.
  5. *
  6. * Copyright (c) 1997 Sun Microsystems, Inc.
  7. *
  8. * See the file "license.terms" for information on usage and
  9. * redistribution of this file, and for a DISCLAIMER OF ALL
  10. * WARRANTIES.
  11. *
  12. * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
  13. *
  14. * RCS @(#) $Id: LsortCmd.java,v 1.3 2003/01/09 02:15:39 mdejong Exp $
  15. */
  16. using System;
  17. namespace tcl.lang
  18. {
  19. /*
  20. * This LsortCmd class implements the Command interface for specifying a new
  21. * Tcl command. The Lsort command implements the built-in Tcl command "lsort"
  22. * which is used to sort Tcl lists. See user documentation for more details.
  23. */
  24. class LsortCmd : Command
  25. {
  26. /*
  27. * List of switches that are legal in the lsort command.
  28. */
  29. private static readonly string[] validOpts = new string[] { "-ascii", "-command", "-decreasing", "-dictionary", "-increasing", "-index", "-integer", "-real", "-unique" };
  30. /*
  31. *----------------------------------------------------------------------
  32. *
  33. * cmdProc --
  34. *
  35. * This procedure is invoked as part of the Command interface to
  36. * process the "lsort" Tcl command. See the user documentation for
  37. * details on what it does.
  38. *
  39. * Results:
  40. * A standard Tcl result.
  41. *
  42. * Side effects:
  43. * See the user documentation.
  44. *
  45. *----------------------------------------------------------------------
  46. */
  47. public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv )
  48. {
  49. if ( argv.Length < 2 )
  50. {
  51. throw new TclNumArgsException( interp, 1, argv, "?options? list" );
  52. }
  53. string command = null;
  54. int sortMode = QSort.ASCII;
  55. int sortIndex = -1;
  56. bool sortIncreasing = true;
  57. bool unique = false;
  58. for ( int i = 1; i < argv.Length - 1; i++ )
  59. {
  60. int index = TclIndex.get( interp, argv[i], validOpts, "option", 0 );
  61. switch ( index )
  62. {
  63. case 0:
  64. sortMode = QSort.ASCII;
  65. break;
  66. case 1:
  67. if ( i == argv.Length - 2 )
  68. {
  69. throw new TclException( interp, "\"-command\" option must be" + " followed by comparison command" );
  70. }
  71. sortMode = QSort.COMMAND;
  72. command = argv[i + 1].ToString();
  73. i++;
  74. break;
  75. case 2:
  76. sortIncreasing = false;
  77. break;
  78. case 3:
  79. sortMode = QSort.DICTIONARY;
  80. break;
  81. case 4:
  82. sortIncreasing = true;
  83. break;
  84. case 5:
  85. if ( i == argv.Length - 2 )
  86. {
  87. throw new TclException( interp, "\"-index\" option must be followed by list index" );
  88. }
  89. sortIndex = Util.getIntForIndex( interp, argv[i + 1], -2 );
  90. command = argv[i + 1].ToString();
  91. i++;
  92. break;
  93. case 6:
  94. sortMode = QSort.INTEGER;
  95. break;
  96. case 7:
  97. sortMode = QSort.REAL;
  98. break;
  99. case 8: /* -unique */
  100. unique = true;
  101. break;
  102. }
  103. }
  104. TclObject list = argv[argv.Length - 1];
  105. bool isDuplicate = false;
  106. // If the list object is unshared we can modify it directly. Otherwise
  107. // we create a copy to modify: this is "copy on write".
  108. if ( list.Shared )
  109. {
  110. list = list.duplicate();
  111. isDuplicate = true;
  112. }
  113. try
  114. {
  115. TclList.sort( interp, list, sortMode, sortIndex, sortIncreasing, command, unique );
  116. interp.setResult( list );
  117. }
  118. catch ( TclException e )
  119. {
  120. if ( isDuplicate )
  121. {
  122. list.release();
  123. }
  124. throw;
  125. }
  126. return TCL.CompletionCode.RETURN;
  127. }
  128. } // LsortCmd
  129. }