IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 5057 for trunk/psLib/src/types


Ignore:
Timestamp:
Sep 15, 2005, 11:22:22 AM (21 years ago)
Author:
drobbin
Message:

Minor changes from new SDRS, prototype code for psArguments

Location:
trunk/psLib/src/types
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/types/psBitSet.h

    r4898 r5057  
    1212 *  @author Ross Harman, MHPCC
    1313 *
    14  *  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2005-08-30 01:14:13 $
     14 *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2005-09-15 21:22:22 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    5858;
    5959
    60 
    6160/** Allocate a psBitSet.
    6261 *
     
    9291 */
    9392psBitSet* psBitSetClear(
    94     /* @returned@ */
    9593    psBitSet* bitSet,                  ///< Pointer to psBitSet to be cleared.
    9694    long bit                           ///< Bit to be cleared.
     
    120118 */
    121119psBitSet* psBitSetOp(
    122     /* @returned@ */
    123120    psBitSet* outBitSet,                 ///< Resulting psBitSet from binary operation
    124121    const psBitSet* inBitSet1,           ///< First psBitSet on which to operate
  • trunk/psLib/src/types/psMetadata.c

    r5000 r5057  
    1212 *  @author Ross Harman, MHPCC
    1313 *
    14  *  @version $Revision: 1.82 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2005-09-12 21:36:54 $
     14 *  @version $Revision: 1.83 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2005-09-15 21:22:22 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3939#include "psConstants.h"
    4040#include "psLogMsg.h"
     41#include "psTrace.h"
    4142
    4243/******************************************************************************/
     
    811812
    812813
    813 char *psMetadataLookupString(bool *status,
    814                              const psMetadata *md,
    815                              const char *key)
     814char *psMetadataLookupStr(bool *status,
     815                          const psMetadata *md,
     816                          const char *key)
    816817{
    817818    psMetadataItem *item = psMetadataLookup((psMetadata*)md, key); // The metadata with instruments
     
    893894
    894895
     896// Set verbosity level
     897int psArgumentVerbosity(int *argc, char **argv)
     898{
     899    int logLevel = 2;   // Default log level
     900    int argnum = 0;   // Argument number
     901
     902    // set in order, so that -vvv overrides -vv overrides -v
     903    if ( (argnum = psArgumentGet(*argc, argv, "-v")) ) {
     904        psArgumentRemove(argnum, argc, argv);
     905        logLevel = 3;
     906    }
     907    if ( (argnum = psArgumentGet(*argc, argv, "-vv")) ) {
     908        psArgumentRemove(argnum, argc, argv);
     909        logLevel = 4;
     910    }
     911    if ( (argnum = psArgumentGet(*argc, argv, "-vvv")) ) {
     912        psArgumentRemove(argnum, argc, argv);
     913        logLevel = 5;
     914    }
     915    psLogSetLevel (logLevel);  // XXX: This function should return an error if the log level is invalid
     916
     917    if ( (argnum = psArgumentGet(*argc, argv, "-logfmt")) ) {
     918        if (*argc < argnum + 2) {
     919            psError(PS_ERR_IO, true, "-logfmt switch specified without a format.");
     920        } else {
     921            psArgumentRemove(argnum, argc, argv);
     922            psLogSetFormat(argv[argnum]); // XXX EAM : this function should return an error if the log format is invalid
     923            psArgumentRemove(argnum, argc, argv);
     924        }
     925    }
     926
     927    // Now the trace stuff
     928    // argument format is: -trace (facil) (level)
     929    while ( (argnum = psArgumentGet(*argc, argv, "-trace")) ) {
     930        if ( (*argc < argnum + 3) ) {
     931            psError(PS_ERR_IO, true, "-trace switch specified without facility and level.");
     932        }
     933        psArgumentRemove(argnum, argc, argv);
     934        psTraceSetLevel(argv[argnum], atoi(argv[argnum+1])); // XXX: This function should return an error if the trace level is invalid
     935        psArgumentRemove(argnum, argc, argv);
     936        psArgumentRemove(argnum, argc, argv);
     937    }
     938    if ((argnum = psArgumentGet(*argc, argv, "-trace-levels"))) {
     939        psTracePrintLevels();
     940        exit(2);
     941    }
     942
     943    return logLevel;
     944}
     945
     946// Find the location of the specified argument
     947int psArgumentGet(int argc, char **argv, const char *arg)
     948{
     949    for (int i = 1; i < argc; i++) {
     950        if (!strcmp(argv[i], arg))
     951            return i;
     952    }
     953
     954    return 0;
     955}
     956
     957// Remove the specified argument (by location)
     958bool psArgumentRemove(int argnum, int *argc, char **argv)
     959{
     960    if (argnum > 0) {
     961        (*argc)--;
     962        for (int i = argnum; i < *argc; i++) {
     963            argv[i] = argv[i+1];
     964        }
     965    } else {
     966        return false;
     967    }
     968
     969    return true;
     970}
     971
     972
     973static psMetadataItem *argumentRead(psMetadataItem *item, // Item to read into
     974                                    int argnum, // Argument number
     975                                    int *argc, // Number of arguments in total
     976                                    char **argv) // The arguments
     977
     978{
     979    psMetadataItem *newItem = NULL;
     980    switch(item->type)
     981    {
     982        // Only doing a representative set of types
     983    case PS_META_S32:
     984        newItem = psMetadataItemAlloc(item->name, item->type, item->comment, atoi(argv[argnum]));
     985        psArgumentRemove(argnum, argc, argv);
     986        break;
     987    case PS_META_F32:
     988        newItem = psMetadataItemAlloc(item->name, item->type, item->comment, atof(argv[argnum]));
     989        psArgumentRemove(argnum, argc, argv);
     990        break;
     991    case PS_META_BOOL:
     992        // Turn option on; no optional argument to remove
     993        newItem = psMetadataItemAlloc(item->name, item->type, item->comment, true);
     994        break;
     995        // XXX: Include the other numerical types
     996    case PS_META_STR: {
     997            //psString string = psStringCopy(argv[argnum]); // Get the argument into PS memory management
     998            //psFree(string);
     999            newItem = psMetadataItemAlloc(item->name, item->type, item->comment, argv[argnum]);
     1000            psArgumentRemove(argnum, argc, argv);
     1001        }
     1002        break;
     1003    default:
     1004        psError(PS_ERR_IO, true, "Argument type (%x) is not supported --- argument %s ignored\n",
     1005                item->type, item->name);
     1006        psFree(newItem);
     1007        return NULL;
     1008    }
     1009
     1010    return newItem;
     1011}
     1012
     1013
     1014// XXX: There is a memory leak in the MULTI section.  I think it might have something to do with reference
     1015// counting between lists and MD, in the second section of the code (copy newArgs into arguments), but I'm not
     1016// entirely sure.
     1017bool psArgumentParse(psMetadata *arguments,
     1018                     int *argc,
     1019                     char **argv)
     1020{
     1021    // We need to do a bit of mucking around in order to preserve the arguments metadata until the last
     1022    // minute --- if there is a bad argument, we need to return the old "arguments", since they contain
     1023    // the default values, which we probably want to output in a "help" message (we don't want to print
     1024    // the changed values and have the user think that they are default values).
     1025
     1026    psMetadata *newArgs = psMetadataAlloc(); // Place to read arguments into
     1027    psList *changed = psListAlloc(NULL);// List of keys that have changed
     1028
     1029    for (int i = 1; i < *argc; i++) {
     1030        psTrace(__func__, 7, "Looking at %s\n", argv[i]);
     1031        psMetadataItem *argItem = psMetadataLookup(arguments, argv[i]);
     1032        if (argItem) {
     1033            psArgumentRemove(i, argc, argv); // Remove the switch
     1034            if (argItem->type != PS_META_MULTI) {
     1035                if (argItem->type != PS_META_BOOL && *argc < i + 1) {
     1036                    psError(PS_ERR_IO, true, "Required argument for %s is missing.\n", argItem->name);
     1037                    // XXX: Cleanup before returning
     1038                    psFree(newArgs);
     1039                    return false;
     1040                }
     1041                psMetadataItem *newItem = argumentRead(argItem, i, argc, argv);
     1042                psMetadataAddItem(newArgs, newItem, PS_LIST_TAIL, PS_META_REPLACE);
     1043                psFree(newItem);
     1044            } else {
     1045                // Go through the MULTI
     1046                psList *multi = argItem->data.V; // The list of MULTI psMetadataItems
     1047                if (*argc < i + multi->n) {
     1048                    psError(PS_ERR_IO, true, "Not enough arguments for %s.\n", argItem->name);
     1049                    // Remove the arguments --- they will be ignored
     1050                    for (int j = i; j < *argc; j++) {
     1051                        psArgumentRemove(i, argc, argv);
     1052                    }
     1053                    // XXX: Cleanup before returning
     1054                    psFree(newArgs);
     1055                    return false;
     1056                }
     1057
     1058                // Remove any prior existence in the newArgs --- this is important because we specify
     1059                // adding the new items as DUPLICATE_OK, so if some idiot specifies it twice, we'd end
     1060                // up with two copies of everything.
     1061                psMetadataItem *checkItem = psMetadataLookup(newArgs, argItem->name);
     1062                if (checkItem) {
     1063                    (void)psMetadataRemove(newArgs, 0, argItem->name);
     1064                    (void)psListRemoveData(changed, argItem->name);
     1065                }
     1066
     1067                psListIterator *multiIter = psListIteratorAlloc(multi, PS_LIST_HEAD, true);
     1068                psMetadataItem *nextItem = NULL; // Item from list
     1069                while ( (nextItem = psListGetAndIncrement(multiIter)) ) {
     1070                    psMetadataItem *newItem = argumentRead(nextItem, i, argc, argv);
     1071                    psMetadataAddItem(newArgs, newItem, PS_LIST_TAIL, PS_META_DUPLICATE_OK);
     1072                    //psFree(newItem);
     1073                }
     1074                psFree(multiIter);
     1075            }
     1076
     1077            // Some book-keeping
     1078            //     psString name = psStringCopy(argItem->name);
     1079            psListAdd(changed, PS_LIST_TAIL, argItem->name);
     1080            i--;
     1081
     1082        } else if ( (strncmp(argv[i], "-", 1) == 0 ) || (strncmp(argv[i], "+", 1) == 0) ) {
     1083            // Someone's specified a bad option
     1084            psError(PS_ERR_IO, true, "Unknown option: %s\n", argv[i]);
     1085            psFree(newArgs);
     1086            return false;
     1087        }
     1088    }
     1089
     1090    // All the arguments are good, so now we can copy the newArgs over
     1091    psListIterator *changedIter = psListIteratorAlloc(changed, PS_LIST_HEAD, false); // Iterator
     1092    psString name = NULL;  // Item from iteration
     1093    while ( (name = psListGetAndIncrement(changedIter)) ) {
     1094        printf("Updating %s\n", name);
     1095        psMetadataItem *oldItem = psMetadataLookup(arguments, name);
     1096        psMetadataItem *newItem = psMetadataLookup(newArgs, name);
     1097        if ( (oldItem->type != newItem->type) ) {
     1098            psAbort(__func__, "Shouldn't reach here!\n");
     1099        }
     1100        switch (oldItem->type) {
     1101            // Only doing a representative set of types
     1102        case PS_META_S32:
     1103            oldItem->data.S32 = newItem->data.S32;
     1104            break;
     1105        case PS_META_F32:
     1106            oldItem->data.F32 = newItem->data.F32;
     1107            break;
     1108        case PS_META_BOOL:
     1109            oldItem->data.B = newItem->data.B;
     1110            break;
     1111            // XXX: Include the other numerical types
     1112        case PS_META_STR:
     1113            psFree(oldItem->data.V);
     1114            oldItem->data.V = psMemIncrRefCounter(newItem->data.V);
     1115            break;
     1116        case PS_META_MULTI: {
     1117                psList *newMulti = psMemIncrRefCounter(newItem->data.V); // The new list of MULTI
     1118                psList *oldMulti = oldItem->data.V; // The old list of MULTI
     1119                psListIterator *newMultiIter = psListIteratorAlloc(newMulti, PS_LIST_HEAD, false);
     1120                psListIterator *oldMultiIter = psListIteratorAlloc(oldMulti, PS_LIST_HEAD, true);
     1121                psMetadataItem *newMultiItem = NULL; // Item from iterator
     1122                while ( (newMultiItem = psListGetAndIncrement(newMultiIter)) ) {
     1123                    psMetadataItem *oldMultiItem = psListGetAndIncrement(oldMultiIter);
     1124                    if (!oldMultiItem) {
     1125                        psAbort(__func__,
     1126                                "Something went very wrong here!  The lists SHOULD be of the same length!\n");
     1127                    }
     1128                    switch (oldMultiItem->type) {
     1129                        // Only doing a representative set of types
     1130                    case PS_META_S32:
     1131                        oldItem->data.S32 = newItem->data.S32;
     1132                        break;
     1133                    case PS_META_F32:
     1134                        oldItem->data.F32 = newItem->data.F32;
     1135                        break;
     1136                        // XXX: Include the other numerical types
     1137                    case PS_META_STR:
     1138                        psFree(oldItem->data.V);
     1139                        oldItem->data.V = psMemIncrRefCounter(newItem->data.V);
     1140                        break;
     1141                    default:
     1142                        psAbort(__func__, "Should never ever get here, ever.\n");
     1143                    }
     1144                    psFree(oldMultiItem);
     1145                    psFree(newMultiItem);
     1146                }
     1147                psFree(newMultiIter);
     1148                psFree(oldMultiIter);
     1149            }
     1150            break;
     1151        default:
     1152            psAbort(__func__, "Should never ever ever get here.\n");
     1153        }
     1154    }
     1155    psFree(changedIter);
     1156    psFree(changed);
     1157
     1158    // Now, blow away the newArgs and we're done.
     1159    psFree(newArgs);
     1160    return true;
     1161}
     1162
     1163
     1164static int argLength(psMetadataItem *arg)
     1165{
     1166    switch (arg->type) {
     1167        // Only doing a representative set of types
     1168    case PS_META_S32:
     1169        return arg->data.S32 >= 0 ? (int)log10f((float)arg->data.S32) + 1 :
     1170               (int)log10f(-(float)arg->data.S32) + 2;
     1171        // XXX: Other numerical types
     1172    case PS_META_F32:
     1173        return arg->data.F32 >= 0 ? 12 : 13; // -d.dddddde?dd
     1174    case PS_META_F64:
     1175        return arg->data.F64 >= 0 ? 12 : 13; // -d.dddddde?dd
     1176    case PS_META_BOOL:
     1177        return arg->data.B ? 4 : 5;
     1178    case PS_META_STR:
     1179        return strlen(arg->data.V);
     1180    default:
     1181        psAbort(__func__, "Argument type (%x) is not supported.\n", arg->type);
     1182    }
     1183
     1184    return 0;
     1185}
     1186
     1187#define NUM_SPACES 4   // Number of spaces between
     1188
     1189void psArgumentHelp(psMetadata *arguments)
     1190{
     1191    printf("Optional arguments, with default values:\n");
     1192    psMetadataIterator *argIter = psMetadataIteratorAlloc(arguments, PS_LIST_HEAD, NULL);
     1193    psMetadataItem *argItem = NULL; // Item from iterator
     1194    int maxName = 4;   // Maximum length of a name
     1195    int maxValue = 4;   // Maximum length of a value
     1196
     1197    // First pass to get the sizes
     1198    while ( (argItem = psMetadataGetAndIncrement(argIter)) ) {
     1199        if (strlen(argItem->name) > maxName) {
     1200            maxName = strlen(argItem->name);
     1201        }
     1202        int valLength = argLength(argItem);
     1203        if (valLength > maxValue) {
     1204            maxValue = valLength;
     1205        }
     1206    }
     1207
     1208    // Second pass to print
     1209    psMetadataIteratorSet(argIter, PS_LIST_HEAD);
     1210    psString lastName = NULL;  // Last name we printed
     1211    while ( (argItem = psMetadataGetAndIncrement(argIter)) ) {
     1212        // Initial indent
     1213        for (int i = 0; i < NUM_SPACES; i++) {
     1214            printf(" ");
     1215        }
     1216
     1217        // Print the name if required
     1218        int position = 0; // Number of spaces in
     1219        if (! lastName || strcmp(lastName, argItem->name) != 0) {
     1220            // A new name
     1221            printf("%s", argItem->name);
     1222            position += strlen(argItem->name);
     1223            lastName = argItem->name;
     1224        }
     1225        for (int i = position; i < maxName + NUM_SPACES; i++) {
     1226            printf(" ");
     1227        }
     1228
     1229        // Print the value
     1230        printf("(");
     1231        switch (argItem->type) {
     1232            // Only doing a representative set of types
     1233        case PS_META_S32:
     1234            printf("%d", argItem->data.S32);
     1235            break;
     1236            // XXX: Other numerical types
     1237        case PS_META_F32:
     1238            printf("%.6e", argItem->data.F32);
     1239            break;
     1240        case PS_META_F64:
     1241            printf("%.6e", argItem->data.F64);
     1242            break;
     1243        case PS_META_BOOL:
     1244            if (argItem->data.B) {
     1245                printf("TRUE");
     1246            } else {
     1247                printf("FALSE");
     1248            }
     1249            break;
     1250        case PS_META_STR:
     1251            printf("%s", (char*)(argItem->data.V));
     1252            break;
     1253        default:
     1254            psAbort(__func__, "Argument type (%x) is not supported.\n", argItem->type);
     1255        }
     1256        printf(")");
     1257        for (int i = argLength(argItem); i < maxValue + NUM_SPACES; i++) {
     1258            printf(" ");
     1259        }
     1260
     1261        // Print the comment
     1262        if (argItem->comment) {
     1263            printf("%s", argItem->comment);
     1264        }
     1265        printf("\n");
     1266    }
     1267
     1268    psFree(argIter);
     1269}
     1270
  • trunk/psLib/src/types/psMetadata.h

    r5010 r5057  
    1111*  @author Ross Harman, MHPCC
    1212*
    13 *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
    14 *  @date $Date: 2005-09-12 23:27:48 $
     13*  @version $Revision: 1.64 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2005-09-15 21:22:22 $
    1515*
    1616*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    122122    union {
    123123        psBool B;                      ///< boolean data
     124        psS8 S8;                       ///< Signed 8-bit integer data.
     125        psS16 S16;                     ///< Signed 16-bit integer data.
    124126        psS32 S32;                     ///< Signed 32-bit integer data.
     127        psU8 U8;                       ///< Unsigned 8-bit integer data.
     128        psU16 U16;                     ///< Unsigned 16-bit integer data.
     129        psU32 U32;                     ///< Unsigned 32-bit integer data.
    125130        psF32 F32;                     ///< Single-precision float data.
    126131        psF64 F64;                     ///< Double-precision float data.
     
    686691 *  @return char*:           Value of metadata item.
    687692 */
    688 char *psMetadataLookupString(
     693char *psMetadataLookupStr(
    689694    bool *status,                      ///< Status of lookup.
    690695    const psMetadata *md,              ///< Metadata collection to lookup metadata item.
     
    698703);
    699704
     705/** Implements the various verbosity controls.
     706 *
     707 *  Arguments shall be removed from the argument list as they are processed.
     708 *
     709 *  @return int:        The resultant logging level.
     710 */
     711int psArgumentVerbosity(
     712    int *argc,                         ///< number or arguments
     713    char **argv                        ///< the argument list
     714);
     715
     716/** Checks for an argument and returns its index position if found.
     717 *
     718 *  @return int:        The index of the element in the argument list, otherwise 0.
     719 */
     720int psArgumentGet(
     721    int argc,                          ///< number or arguments
     722    char **argv,                       ///< the argument list
     723    const char *arg                    ///< the specified argument to match
     724);
     725
     726/** Removes from the argument list the argument whose index is argnum.
     727 *
     728 *  The number of entries in the argument list shall be decremented.
     729 *
     730 *  @return bool:       True if the argnum is in the argument list, otherwise false.
     731 */
     732bool psArgumentRemove(
     733    int argnum,                        ///< the argument to remove
     734    int *argc,                         ///< number or arguments
     735    char **argv                        ///< the argument list
     736);
     737
     738/** Parses the command line arguments into a metadata container of arguments.
     739 *
     740 *  The input arguments shall contain the list of possible arguments as the keywords providing
     741 *  the default values.  As matching arguments are found on the command line, the values shall be
     742 *  read into the arguments metadata, with the appropriate type.  The arguments and their values
     743 *  shall be removed from the list of command line arguments as they are processed.
     744 *
     745 *  @return bool:       False if any argument was encountered that is not present in arguments.
     746 */
     747bool psArgumentParse(
     748    psMetadata *arguments,             ///< metadata container for arguments
     749    int *argc,                         ///< number or arguments
     750    char **argv                        ///< the argument list
     751);
     752
     753/** Prints to stdout a guide to the command-line arguments.      */
     754void psArgumentHelp(
     755    psMetadata *arguments              ///< metadata container for arguments
     756);
     757
    700758/// @}
    701759
  • trunk/psLib/src/types/psMetadataConfig.c

    r5013 r5057  
    1010*  @author Eric Van Alst, MHPCC
    1111*
    12 *  @version $Revision: 1.43 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2005-09-13 01:04:39 $
     12*  @version $Revision: 1.44 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2005-09-15 21:22:22 $
    1414*
    1515*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    12421242    while ( (item = psMetadataGetAndIncrement(iter)) ) {
    12431243        type = item->type;
    1244         if ( type == 65537)
     1244        if ( type == PS_META_STR)
    12451245            type = PS_DATA_STRING;
    1246         if ( type == 65538)
     1246        if ( type == PS_META_VEC)
    12471247            type = PS_DATA_VECTOR;
    1248         if ( type == 65551)
     1248        if ( type == PS_META_TIME)
    12491249            type = PS_DATA_TIME;
    12501250        if ( item->type == PS_META_META)
Note: See TracChangeset for help on using the changeset viewer.