IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 41354 for trunk


Ignore:
Timestamp:
May 13, 2020, 5:04:41 PM (6 years ago)
Author:
eugene
Message:

handle string vectors in vector[n] context

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/expand_vectors.c

    r38002 r41354  
    11# include "opihi.h"
     2
     3// XXX need to unroll this function so we can more flexibly
     4// convert items in the line
    25
    36// converts a[], a[4], a[2][3] to their numeric values
    47// add a?[] and a?[][] as equivalent to $?a
    58// a[][?]
     9
     10enum {ANS_NONE, ANS_INT, ANS_FLT, ANS_STR};
    611
    712char *expand_vectors (char *line) {
     
    1116  int n, I, J, size, showLength, showXsize, showYsize, checkType, NLINE, Noff, isBuffer, inRange;
    1217  float *ptr;
    13   double f1;
     18  opihi_flt valueFlt;
     19  opihi_int valueInt;
     20  char *valueStr;
     21  int answerType;
    1422  Vector *vec;
    1523  Buffer *buf;
     
    4149    if (p > q) goto dumpline; /* odd state: unmatched pair: ][ */
    4250
     51    // we have a bracket pair, now interpret the context:
     52    // a[N] - vector value
     53    // a?[] or a?[N] - vector existence
     54    // a[]  - vector length
     55    // a[N][M] - buffer value
     56    // a?[][], a?[N][], a?[][M] - buffer existence
     57    // a[][N] - buffer X size
     58    // a[N][] - buffer Y size
     59
    4360    /* find vector subscript */
    4461    n = (int) (q - p - 1);
     
    4764    showXsize  = FALSE;
    4865    showYsize  = FALSE;
     66    answerType = ANS_NONE;
    4967
    5068    if (n == 0) {
     
    103121  asVector:
    104122    /* find vector/buffer name */
     123    /* 'checkType' really means check for existence.
     124       if 'a' is a vector, then 'a?[]' returns TRUE, else FALSE
     125       if 'a' is a buffer, then 'a?[][]' returns TRUE, else FALSE
     126    */
     127
    105128    checkType = FALSE;
    106129    if (*(p-1) == '?') {
     
    116139      buf = SelectBuffer (tmpline, OLDBUFFER, !checkType);
    117140      if (checkType) {
    118         f1 = (buf != NULL);
     141        valueInt = (buf != NULL);
     142        answerType = ANS_INT;
    119143        goto skipBuffer;
    120144      }
     
    125149      if (buf == NULL) goto dumpline;
    126150      if (showXsize) {
    127         f1 = buf[0].header.Naxis[0];
     151        valueInt = buf[0].header.Naxis[0];
     152        answerType = ANS_INT;
    128153        goto skipBuffer;
    129154      }
    130155      if (showYsize) {
    131         f1 = buf[0].header.Naxis[1];
     156        valueInt = buf[0].header.Naxis[1];
     157        answerType = ANS_INT;
    132158        goto skipBuffer;
    133159      }
     
    145171      if (J < 0) J += buf[0].header.Naxis[1];
    146172      ptr = (float *) buf[0].matrix.buffer;
    147       f1 = ptr[I + J*buf[0].header.Naxis[0]];
     173      valueFlt = ptr[I + J*buf[0].header.Naxis[0]];
     174      answerType = ANS_FLT;
    148175    } else {
    149176      vec = SelectVector (tmpline, OLDVECTOR, !checkType);
    150177      if (checkType) {
    151         f1 = (vec != NULL);
     178        valueInt = (vec != NULL);
     179        answerType = ANS_INT;
    152180      } else {
    153181        if (vec == NULL) goto dumpline;
     
    155183        /* find vector element */
    156184        if (showLength) {
    157           f1 = vec[0].Nelements;
     185          valueInt = vec[0].Nelements;
     186          answerType = ANS_INT;
    158187        } else {
    159188          if ((I >= vec[0].Nelements) || (I < -1*vec[0].Nelements)) {
     
    162191          }
    163192          if (I < 0) I += vec[0].Nelements;
    164           f1 = (vec[0].type == OPIHI_FLT) ? vec[0].elements.Flt[I] : vec[0].elements.Int[I];
     193
     194          switch (vec[0].type) {
     195            case OPIHI_FLT:
     196              valueFlt = vec[0].elements.Flt[I];
     197              answerType = ANS_FLT;
     198              break;
     199            case OPIHI_INT:
     200              valueInt = vec[0].elements.Int[I];
     201              answerType = ANS_INT;
     202              break;
     203            case OPIHI_STR:
     204              valueStr = vec[0].elements.Str[I];
     205              answerType = ANS_STR;
     206              break;
     207          }
    165208        }
    166209      }
     
    169212  skipBuffer:
    170213    free (tmpline);
    171     if ((int)f1 == f1)
    172       snprintf (strValue, 128, "%.0f", f1);
    173     else
    174       snprintf (strValue, 128, "%.12g", f1);
     214
     215    switch (answerType) {
     216      case ANS_NONE:
     217        goto dumpline;
     218      case ANS_INT:
     219        snprintf (strValue, 128, OPIHI_INT_FMT, valueInt);
     220        break;
     221      case ANS_FLT:
     222        snprintf (strValue, 128, "%.12g", valueFlt);
     223        break;
     224      case ANS_STR:
     225        snprintf (strValue, 128, "%s", valueStr);
     226        break;
     227    }
    175228
    176229    /* interpolate vector element into newline (being accumulated) */
Note: See TracChangeset for help on using the changeset viewer.