IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7921


Ignore:
Timestamp:
Jul 17, 2006, 3:06:21 PM (20 years ago)
Author:
jhoblitt
Message:

refactor pzsearchs config/CLI option handling to be consistent with the other pXtools

Location:
trunk/ippTools/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/pztool.c

    r6978 r7921  
    1616static bool pzsearchFlushPendingExp(pxConfig *config);
    1717
     18# define MODECASE(caseName, func) \
     19    case caseName: \
     20    if (!func(config)) { \
     21                goto FAIL; \
     22            } \
     23    break;
     24
     25
    1826int main(int argc, char **argv)
    1927{
     28    psLibInit(NULL);
     29
    2030    pxConfig *config = pzsearchConfig(NULL, argc, argv);
    2131
    2232    switch (config->mode) {
    23         case PX_MODE_SEEN:
    24             if (!seenMode(config)) {
    25                 goto FAIL;
    26             }
    27             break;
    28         case PX_MODE_PENDING:
    29             if (!pendingMode(config)) {
    30                 goto FAIL;
    31             }
    32             break;
    33         case PX_MODE_COPYDONE:
    34             if (!copydoneMode(config)) {
    35                 goto FAIL;
    36             }
    37             break;
     33        MODECASE(PZSEARCH_MODE_SEEN, seenMode);
     34        MODECASE(PZSEARCH_MODE_PENDING, pendingMode);
     35        MODECASE(PZSEARCH_MODE_COPYDONE, copydoneMode);
    3836        default:
    3937            psAbort(argv[0], "invalid option (this should not happen)");
    4038    }
    4139
     40    psFree(config);
     41    pmConfigDone();
     42    psLibFinalize();
     43
     44
    4245    exit(EXIT_SUCCESS);
    4346
    4447FAIL:
    4548    psFree(config);
     49    pmConfigDone();
     50    psLibFinalize();
     51
    4652    exit(EXIT_FAILURE);
    4753}
  • trunk/ippTools/src/pztool.h

    r6978 r7921  
    44#include "pxtools.h"
    55
     6typedef enum {
     7    PZSEARCH_MODE_NONE      = 0x0,
     8    PZSEARCH_MODE_SEEN,
     9    PZSEARCH_MODE_PENDING,
     10    PZSEARCH_MODE_COPYDONE
     11} pzsearchMode;
     12
    613pxConfig *pzsearchConfig(pxConfig *config, int argc, char **argv);
    714
  • trunk/ippTools/src/pztoolConfig.c

    r7037 r7921  
    66
    77#include "pxtools.h"
     8#include "pzsearch.h"
    89
     10// this function can not fail -- exits on error
    911pxConfig *pzsearchConfig(pxConfig *config, int argc, char **argv) {
    1012    if (!config) {
     
    1214    }
    1315
     16    // setup site config
    1417    config->modules = pmConfigRead(&argc, argv);
    15 
    1618    if (! config->modules) {
    1719        psError(PS_ERR_UNKNOWN, false, "Can't find site configuration!\n");
     
    1921    }
    2022
    21     int N;
    22     config->mode = PX_MODE_NONE;
    23     if ((N = psArgumentGet (argc, argv, "-seen"))) {
    24         psArgumentRemove (N, &argc, argv);
    25         if (config->mode) {
    26             psError(PS_ERR_UNKNOWN, true, "only one mode selection is allowed");
    27         }
    28         config->mode = PX_MODE_SEEN;
    29     }
    30     if ((N = psArgumentGet (argc, argv, "-pending"))) {
    31         psArgumentRemove (N, &argc, argv);
    32         if (config->mode) {
    33             psError(PS_ERR_UNKNOWN, true, "only one mode selection is allowed");
    34         }
    35         config->mode = PX_MODE_PENDING;
    36     }
    37     if ((N = psArgumentGet (argc, argv, "-copydone"))) {
    38         psArgumentRemove (N, &argc, argv);
    39         if (config->mode) {
    40             psError(PS_ERR_UNKNOWN, true, "only one mode selection is allowed");
    41         }
    42         config->mode = PX_MODE_COPYDONE;
    43     }
     23    // -seen
     24    psMetadata *seenArgs = psMetadataAlloc();
     25    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_id",  0,
     26        "define exposure ID", NULL);
     27    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-inst",  0,
     28        "define camera ID", NULL);
     29    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-telescope",  0,
     30        "define telescope ID", NULL);
     31    psMetadataAddStr(seenArgs, PS_LIST_TAIL, "-exp_type",  0,
     32        "define exposure type", NULL);
     33   
     34    // -pending
     35    psMetadata *pendingArgs = psMetadataAlloc();
     36    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-exp_id",  0,
     37        "define exposure ID", NULL);
     38    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-inst",  0,
     39        "define camera ID", NULL);
     40    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-telescope",  0,
     41        "define telescope ID", NULL);
     42    psMetadataAddStr(pendingArgs, PS_LIST_TAIL, "-exp_type",  0,
     43        "define exposure type", NULL);
    4444
    45     // paul's argument parsing convention requires: -key value
    46     // Parse other command-line arguments
    47     psMetadata *args = psMetadataAlloc();
    48     psMetadataAddStr(args , PS_LIST_TAIL, "-seen", 0,
    49         "list all uncopied exposure IDs", "");
    50     psMetadataAddStr(args , PS_LIST_TAIL, "-pending",  0,
    51         "list all image files pending download", "");
    52     psMetadataAddStr(args , PS_LIST_TAIL, "-copydone",  0,
    53         "indicate that an image file has been downloaded", "");
    54 
    55     // -copydone search
     45    // -copydone
    5646    psMetadata *copydoneArgs = psMetadataAlloc();
    5747    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-exp_id",  0,
    58         "define exposure ID", "");
     48        "define exposure ID", NULL);
    5949    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class",  0,
    60         "define class", "");
     50        "define class", NULL);
    6151    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-class_id",  0,
    62         "define class_id", "");
     52        "define class_id", NULL);
    6353    psMetadataAddStr(copydoneArgs, PS_LIST_TAIL, "-suri",  0,
    64         "define storage uri", "");
     54        "define storage uri", NULL);
     55
     56#define PXTOOL_MODE(option, modeval, argset) \
     57{ \
     58    int N = 0; \
     59    if ((N = psArgumentGet (argc, argv, option))) { \
     60        psArgumentRemove (N, &argc, argv); \
     61        if (config->mode) { \
     62            psError(PS_ERR_UNKNOWN, true, "only one mode selection is allowed"); \
     63        } \
     64        config->mode = modeval; \
     65        config->args = psMemIncrRefCounter(argset); \
     66    } \
     67}
     68
     69    PXTOOL_MODE("-seen",            PZSEARCH_MODE_SEEN,         seenArgs);
     70    PXTOOL_MODE("-pending",         PZSEARCH_MODE_PENDING,      pendingArgs);
     71    PXTOOL_MODE("-copydone",        PZSEARCH_MODE_COPYDONE,     copydoneArgs);
    6572
    6673    bool argErr = false;
    67     if (config->mode == PX_MODE_NONE) {
     74    if (config->mode == PZSEARCH_MODE_NONE) {
    6875        argErr = true;
    6976        fprintf (stderr, "mode argument is required\n");
    70     } else if (! psArgumentParse(args, &argc, argv) || argc != 1) {
     77    } else if (! psArgumentParse(config->args, &argc, argv) || argc != 1) {
    7178        argErr = true;
    7279        fprintf (stderr, "error parsing arguments\n");
     
    7784        printf("Usage: %s <mode> [<options>]\n\n", argv[0]);
    7885        printf(" <mode> : -seen | -pending | -copydone\n\n");
     86
     87        fprintf (stdout, "-seen ");
     88        psArgumentHelp(seenArgs);
     89        psFree(seenArgs);
     90
     91        fprintf (stdout, "-pending ");
     92        psArgumentHelp(pendingArgs);
     93        psFree(pendingArgs);
     94
    7995        fprintf (stdout, "-copydone ");
    8096        psArgumentHelp(copydoneArgs);
    81         psFree(args);
    8297        psFree(copydoneArgs);
    83         exit(EXIT_FAILURE);
     98
     99        goto FAIL;
    84100    }
    85101
     102    psFree(seenArgs);
     103    psArgumentHelp(pendingArgs);
    86104    psFree(copydoneArgs);
    87105
    88     // XXX why is "" being returned when -[foo] isn't specified?
    89 #define EMPTY_TO_NULL_STRING(var) \
    90     if (var != NULL && strcmp(var, "") == 0) { \
    91         var = NULL; \
     106    // setup search criterion
     107#define addWhereStr(name) \
     108{ \
     109    psString str = NULL; \
     110    bool status = false; \
     111    if ((str = psMetadataLookupStr(&status, config->args, "-" #name))) { \
     112        if (!psMetadataAddStr(config->where, PS_LIST_TAIL, #name, 0, "==", str)) {\
     113            psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
     114            goto FAIL; \
     115        } \
     116    } \
     117}
     118
     119    // generate SQL where claus
     120    config->where = psMetadataAlloc();
     121
     122    addWhereStr(exp_id);
     123    addWhereStr(class);
     124    addWhereStr(class_id);
     125
     126    if (config->where->list->n < 1) {
     127        psFree(config->where);
     128        config->where = NULL;
    92129    }
    93130
    94     config->args = args;
    95 
    96 if (config->mode == PX_MODE_COPYDONE) {
    97     psMetadata *where = psMetadataAlloc();
    98 
    99     bool status;
    100     psString str;
    101     if ((str = psMetadataLookupStr(&status, args, "-exp_id"))) {
    102         if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==", str)) {
    103             psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
    104             psFree(where);
    105             exit(EXIT_FAILURE);
    106         }
    107     }
    108     if ((str = psMetadataLookupStr(&status, args, "-class"))) {
    109         if (!psMetadataAddStr(where, PS_LIST_TAIL, "class", 0, "==", str)) {
    110             psError(PS_ERR_UNKNOWN, false, "failed to add item class");
    111             psFree(where);
    112             exit(EXIT_FAILURE);
    113         }
    114     }
    115     if ((str = psMetadataLookupStr(&status, args, "-class_id"))) {
    116         if (!psMetadataAddStr(where, PS_LIST_TAIL, "class_id", 0, "==", str)) {
    117             psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
    118             psFree(where);
    119             exit(EXIT_FAILURE);
    120         }
    121     }
    122 
    123     if (where->list->n < 1) {
    124         psFree(where);
    125         where = NULL;
    126     }
    127     config->where = where;
    128 }
    129 
    130131    // define Database handle, if used
     132    // do this last so we don't setup a connection before CLI options are
     133    // validated
    131134    config->dbh = pmConfigDB(config->modules);
    132135    if (!config->dbh) {
    133136        psError(PS_ERR_UNKNOWN, false, "Can't configure database");
    134         exit(EXIT_FAILURE);
     137        goto FAIL;
    135138    }
    136139
     140    // save argv/argc
     141    config->argv = argv;
     142    config->argc = argc;
     143
    137144    return config;
     145
     146FAIL:
     147    psFree(config);
     148    pmConfigDone();
     149    psLibFinalize();
     150    exit(EXIT_FAILURE);
    138151}
Note: See TracChangeset for help on using the changeset viewer.