Changeset 549 for trunk


Ignore:
Timestamp:
Dec 7, 2006, 3:45:47 AM (18 years ago)
Author:
dkearney
Message:

added function Rappture::Units::Search::for which takes in a string and searches for and returns units if they exist. the returned units will be valid rappture units

Location:
trunk/src/tcl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tcl/src/RpUnitsTclInterface.cc

    r537 r549  
    2323// EXTERN int Rapptureunits_Init _ANSI_ARGS_((Tcl_Interp * interp));
    2424
    25 static int RpTclUnitsConvert   _ANSI_ARGS_((    ClientData cdata,
     25static int RpTclUnitsConvert    _ANSI_ARGS_((   ClientData cdata,
    2626                                                Tcl_Interp *interp,
    2727                                                int argc,
    2828                                                const char *argv[]    ));
    2929
    30 static int RpTclUnitsDesc      _ANSI_ARGS_((    ClientData cdata,
     30static int RpTclUnitsDesc       _ANSI_ARGS_((   ClientData cdata,
    3131                                                Tcl_Interp *interp,
    3232                                                int argc,
    3333                                                const char *argv[]    ));
    3434
    35 static int RpTclUnitsSysFor   _ANSI_ARGS_((    ClientData cdata,
     35static int RpTclUnitsSysFor     _ANSI_ARGS_((   ClientData cdata,
    3636                                                Tcl_Interp *interp,
    3737                                                int argc,
    3838                                                const char *argv[]    ));
    3939
    40 static int RpTclUnitsSysAll   _ANSI_ARGS_((    ClientData cdata,
     40static int RpTclUnitsSysAll     _ANSI_ARGS_((   ClientData cdata,
     41                                                Tcl_Interp *interp,
     42                                                int argc,
     43                                                const char *argv[]    ));
     44
     45static int RpTclUnitsSearchFor  _ANSI_ARGS_((   ClientData cdata,
    4146                                                Tcl_Interp *interp,
    4247                                                int argc,
     
    6469 * ::Rappture::Units::System::for
    6570 * ::Rappture::Units::System::all
     71 * ::Rappture::Units::Search::for
    6672 */
    6773
     
    8187    Tcl_CreateCommand(interp, "::Rappture::Units::System::all",
    8288        RpTclUnitsSysAll, (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
     89
     90    Tcl_CreateCommand(interp, "::Rappture::Units::Search::for",
     91        RpTclUnitsSearchFor, (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
    8392
    8493    return TCL_OK;
     
    468477
    469478/**********************************************************************/
     479// FUNCTION: RpTclUnitsSearchfor()
     480/// Rappture::Units::Search::for fxn in Tcl, returns string of found units
     481/**
     482 * Returns a list of all units from the given units string that
     483 * were found within the units dictionary. This function takes in a
     484 * string with or without a value. The string at the very least should
     485 * contain the units you are searching for in the dictionary. If the
     486 * string contains a value as well, the value will be ignored. A value
     487 * is considered any numeric sequence as defined by the function
     488 * strtod().
     489 *
     490 * Full function call:
     491 * ::Rappture::Units::Search::for <units>
     492 */
     493
     494int
     495RpTclUnitsSearchFor (  ClientData cdata,
     496                        Tcl_Interp *interp,
     497                        int argc,
     498                        const char *argv[]  )
     499{
     500    std::string unitsName     = ""; // name of the units provided by user
     501    std::string type          = ""; // junk variable that validate() needs
     502    int nextarg               = 1; // start parsing using the '2'th argument
     503    int err                   = 0; // err code for validate
     504    char* endptr              = NULL;
     505
     506    Tcl_ResetResult(interp);
     507
     508    // parse through command line options
     509    if (argc != 2) {
     510        Tcl_AppendResult(interp, "wrong # args: should be \"",
     511                argv[0], " units\"", (char*)NULL);
     512        return TCL_ERROR;
     513    }
     514
     515    // find where the unitsName begins
     516    strtod(argv[nextarg],&endptr);
     517    unitsName = std::string(endptr);
     518
     519    err = RpUnits::validate(unitsName,type);
     520    if (err) {
     521        /*
     522         * according to tcl version, in this case we
     523         * should return an empty string. i happen to disagree.
     524         * the next few lines is what i think the user should see.
     525        Tcl_AppendResult(interp,
     526            "The units named: \"", unitsName.c_str(),
     527            "\" is not a recognized unit for rappture",
     528            (char*)NULL);
     529        return TCL_ERROR;
     530        */
     531        return TCL_OK;
     532    }
     533
     534    Tcl_AppendResult(interp, unitsName.c_str(), (char*)NULL);
     535
     536    return TCL_OK;
     537}
     538
     539/**********************************************************************/
    470540// FUNCTION: list2str()
    471541/// Convert a std::list<std::string> into a comma delimited std::string
  • trunk/src/tcl/tests/units.test

    r545 r549  
    44#   Rappture::Units::System::for
    55#   Rappture::Units::System::all
     6#   Rappture::Units::Search::for
    67#
    78# This file contains a collection of tests for one of the Rappture Tcl
     
    802803} {0 2e-07M2/KVUS}
    803804
     805#--------------------------------------------------------------------
     806# Rappture::Units::Search::for <units>
     807#--------------------------------------------------------------------
     808
     809test search-for-6.0.0.1 {Rappture::Units::Search::for, 300K} {
     810    list [catch {Rappture::Units::Search::for 300K} msg] $msg
     811} {0 K}
     812
     813test search-for-6.0.0.2 {Rappture::Units::Search::for, 300cm2/Vs} {
     814    list [catch {Rappture::Units::Search::for 300cm2/Vs} msg] $msg
     815} {0 cm2/Vs}
     816
     817test search-for-6.0.0.3 {Rappture::Units::Search::for, cm2/Vs} {
     818    list [catch {Rappture::Units::Search::for cm2/Vs} msg] $msg
     819} {0 cm2/Vs}
     820
     821test search-for-6.0.1.1 {Rappture::Units::Search::for, 4notaunit} {
     822    list [catch {Rappture::Units::Search::for 4notaunit} msg] $msg
     823} {0 {}}
     824
     825test search-for-6.0.1.2 {Rappture::Units::Search::for, notaunit} {
     826    list [catch {Rappture::Units::Search::for notaunit} msg] $msg
     827} {0 {}}
     828
     829test search-for-6.0.1.2 {Rappture::Units::Search::for, notaunit} {
     830    list [catch {Rappture::Units::Search::for ^&} msg] $msg
     831} {0 {}}
    804832
    805833::tcltest::cleanupTests
Note: See TracChangeset for help on using the changeset viewer.