Exemplo n.º 1
0
    /// <summary> Compares the order of two items in the array.
    /// 
    /// </summary>
    /// <param name="obj1">first item.
    /// </param>
    /// <param name="obj2">second item.
    /// </param>
    /// <returns> 0 if they are equal, 1 if obj1 > obj2, -1 otherwise.
    /// 
    /// </returns>
    /// <exception cref=""> TclException if an error occurs during sorting.
    /// </exception>
    private int compare( TclObject obj1, TclObject obj2 )
    {

      int index;
      int code = 0;

      if ( sortIndex != -1 )
      {
        // The "-index" option was specified.  Treat each object as a
        // list, extract the requested element from each list, and
        // compare the elements, not the lists.  The special index "end"
        // is signaled here with a negative index (other than -1).

        TclObject obj;
        if ( sortIndex < -1 )
        {
          index = TclList.getLength( sortInterp, obj1 ) - 1;
        }
        else
        {
          index = sortIndex;
        }

        obj = TclList.index( sortInterp, obj1, index );
        if ( obj == null )
        {

          throw new TclException( sortInterp, "element " + index + " missing from sublist \"" + obj1 + "\"" );
        }
        obj1 = obj;

        if ( sortIndex < -1 )
        {
          index = TclList.getLength( sortInterp, obj2 ) - 1;
        }
        else
        {
          index = sortIndex;
        }

        obj = TclList.index( sortInterp, obj2, index );
        if ( obj == null )
        {

          throw new TclException( sortInterp, "element " + index + " missing from sublist \"" + obj2 + "\"" );
        }
        obj2 = obj;
      }

      switch ( sortMode )
      {

        case ASCII:
          // ATK C# CompareTo use option
          // similar to -dictionary but a > A
          code = System.Globalization.CultureInfo.InvariantCulture.CompareInfo.Compare( obj1.ToString(), obj2.ToString(), System.Globalization.CompareOptions.Ordinal );
          // code = obj1.ToString().CompareTo(obj2.ToString());
          break;

        case DICTIONARY:

          code = doDictionary( obj1.ToString(), obj2.ToString() );
          break;

        case INTEGER:
          try
          {
            int int1 = TclInteger.get( sortInterp, obj1 );
            int int2 = TclInteger.get( sortInterp, obj2 );

            if ( int1 > int2 )
            {
              code = 1;
            }
            else if ( int2 > int1 )
            {
              code = -1;
            }
          }
          catch ( TclException e1 )
          {
            sortInterp.addErrorInfo( "\n    (converting list element from string to integer)" );
            throw e1;
          }
          break;

        case REAL:
          try
          {
            double f1 = TclDouble.get( sortInterp, obj1 );
            double f2 = TclDouble.get( sortInterp, obj2 );

            if ( f1 > f2 )
            {
              code = 1;
            }
            else if ( f2 > f1 )
            {
              code = -1;
            }
          }
          catch ( TclException e2 )
          {
            sortInterp.addErrorInfo( "\n    (converting list element from string to real)" );
            throw e2;
          }
          break;

        case COMMAND:
          StringBuilder sbuf = new StringBuilder( sortCommand );

          Util.appendElement( sortInterp, sbuf, obj1.ToString() );

          Util.appendElement( sortInterp, sbuf, obj2.ToString() );
          try
          {
            sortInterp.eval( sbuf.ToString(), 0 );
          }
          catch ( TclException e3 )
          {
            sortInterp.addErrorInfo( "\n    (user-defined comparison command)" );
            throw e3;
          }

          try
          {
            code = TclInteger.get( sortInterp, sortInterp.getResult() );
          }
          catch ( TclException e )
          {
            sortInterp.resetResult();
            TclException e4 = new TclException( sortInterp, "comparison command returned non-numeric result" );
            throw e4;
          }
          break;


        default:

          throw new TclRuntimeError( "Unknown sortMode " + sortMode );

      }

      if ( sortIncreasing )
      {
        return code;
      }
      else
      {
        return -code;
      }
    }
Exemplo n.º 2
0
    internal static void logCommandInfo( Interp interp, char[] script_array, int script_index, int cmdIndex, int length, TclException e )
    // The exception caused by the script 
    // evaluation. 
    {
      string ellipsis;
      string msg;
      int offset;
      int pIndex;

      if ( interp.errAlreadyLogged )
      {
        // Someone else has already logged error information for this
        // command; we shouldn't add anything more.

        return;
      }

      // Compute the line number where the error occurred.
      // Note: The script array must be accessed directly
      // because we want to count from the beginning of
      // the script, not the current index.

      interp.errorLine = 1;

      for ( pIndex = 0; pIndex < cmdIndex; pIndex++ )
      {
        if ( script_array[pIndex] == '\n' )
        {
          interp.errorLine++;
        }
      }


      // Create an error message to add to errorInfo, including up to a
      // maximum number of characters of the command.

      if ( length < 0 )
      {
        //take into account the trailing '\0'
        int script_length = script_array.Length - 1;

        length = script_length - cmdIndex;
      }
      if ( length > 150 )
      {
        offset = 150;
        ellipsis = "...";
      }
      else
      {
        offset = length;
        ellipsis = "";
      }

      msg = new string( script_array, cmdIndex, offset );
      if ( !( interp.errInProgress ) )
      {
        interp.addErrorInfo( "\n    while executing\n\"" + msg + ellipsis + "\"" );
      }
      else
      {
        interp.addErrorInfo( "\n    invoked from within\n\"" + msg + ellipsis + "\"" );
      }
      interp.errAlreadyLogged = false;
      e.errIndex = cmdIndex + offset;
    }
Exemplo n.º 3
0
        /// <summary> Compares the order of two items in the array.
        ///
        /// </summary>
        /// <param name="obj1">first item.
        /// </param>
        /// <param name="obj2">second item.
        /// </param>
        /// <returns> 0 if they are equal, 1 if obj1 > obj2, -1 otherwise.
        ///
        /// </returns>
        /// <exception cref=""> TclException if an error occurs during sorting.
        /// </exception>
        private int compare(TclObject obj1, TclObject obj2)
        {
            int index;
            int code = 0;

            if (sortIndex != -1)
            {
                // The "-index" option was specified.  Treat each object as a
                // list, extract the requested element from each list, and
                // compare the elements, not the lists.  The special index "end"
                // is signaled here with a negative index (other than -1).

                TclObject obj;
                if (sortIndex < -1)
                {
                    index = TclList.getLength(sortInterp, obj1) - 1;
                }
                else
                {
                    index = sortIndex;
                }

                obj = TclList.index(sortInterp, obj1, index);
                if (obj == null)
                {
                    throw new TclException(sortInterp, "element " + index + " missing from sublist \"" + obj1 + "\"");
                }
                obj1 = obj;

                if (sortIndex < -1)
                {
                    index = TclList.getLength(sortInterp, obj2) - 1;
                }
                else
                {
                    index = sortIndex;
                }

                obj = TclList.index(sortInterp, obj2, index);
                if (obj == null)
                {
                    throw new TclException(sortInterp, "element " + index + " missing from sublist \"" + obj2 + "\"");
                }
                obj2 = obj;
            }

            switch (sortMode)
            {
            case ASCII:
                // ATK C# CompareTo use option
                // similar to -dictionary but a > A
                code = System.Globalization.CultureInfo.InvariantCulture.CompareInfo.Compare(obj1.ToString(), obj2.ToString(), System.Globalization.CompareOptions.Ordinal);
                // code = obj1.ToString().CompareTo(obj2.ToString());
                break;

            case DICTIONARY:

                code = doDictionary(obj1.ToString(), obj2.ToString());
                break;

            case INTEGER:
                try
                {
                    int int1 = TclInteger.get(sortInterp, obj1);
                    int int2 = TclInteger.get(sortInterp, obj2);

                    if (int1 > int2)
                    {
                        code = 1;
                    }
                    else if (int2 > int1)
                    {
                        code = -1;
                    }
                }
                catch (TclException e1)
                {
                    sortInterp.addErrorInfo("\n    (converting list element from string to integer)");
                    throw e1;
                }
                break;

            case REAL:
                try
                {
                    double f1 = TclDouble.get(sortInterp, obj1);
                    double f2 = TclDouble.get(sortInterp, obj2);

                    if (f1 > f2)
                    {
                        code = 1;
                    }
                    else if (f2 > f1)
                    {
                        code = -1;
                    }
                }
                catch (TclException e2)
                {
                    sortInterp.addErrorInfo("\n    (converting list element from string to real)");
                    throw e2;
                }
                break;

            case COMMAND:
                System.Text.StringBuilder sbuf = new System.Text.StringBuilder(sortCommand);

                Util.appendElement(sortInterp, sbuf, obj1.ToString());

                Util.appendElement(sortInterp, sbuf, obj2.ToString());
                try
                {
                    sortInterp.eval(sbuf.ToString(), 0);
                }
                catch (TclException e3)
                {
                    sortInterp.addErrorInfo("\n    (user-defined comparison command)");
                    throw e3;
                }

                try
                {
                    code = TclInteger.get(sortInterp, sortInterp.getResult());
                }
                catch (TclException e)
                {
                    sortInterp.resetResult();
                    TclException e4 = new TclException(sortInterp, "comparison command returned non-numeric result");
                    throw e4;
                }
                break;


            default:

                throw new TclRuntimeError("Unknown sortMode " + sortMode);
            }

            if (sortIncreasing)
            {
                return(code);
            }
            else
            {
                return(-code);
            }
        }