IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 16, 2007, 2:27:00 PM (19 years ago)
Author:
eugene
Message:

fixed cases of hard-wired line length; some api cleanup in stack_math

File:
1 edited

Legend:

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

    r10073 r15878  
    33char *expand_vectors (char *line) {
    44
    5   char *newline, *tmpline, *val;
     5  char *newline, *tmpline, strValue[128], *val;
    66  char *L, *N, *p, *q, *w;
    7   int n, I, size;
     7  int n, I, size, showLength, NLINE, Noff;
    88  double f1;
    99  Vector *vec;
     
    1111  if (line == NULL) return (NULL);
    1212
    13   ALLOCATE (newline, char, 1024);  /* WARNING: this limits the length of the input line */
    14   ALLOCATE (tmpline, char, 1024);  /* WARNING: this limits the length of the input line */
     13  NLINE = MAX (128, strlen(line));
     14  ALLOCATE (newline, char, NLINE);
    1515
    1616  /* look for form fred[stuff] */
     
    1919  if (L == NULL) {
    2020    free (newline);
    21     free (tmpline);
    2221    return (line);
    2322  }
     
    3534    if (p > q) goto dumpline; /* odd state: unmatched pair: ][ */
    3635    n = (int) (q - p - 1);
     36    val = NULL;
     37    showLength = FALSE;
    3738    if (n == 0) {
    38       ALLOCATE (val, char, 64);
    39       sprintf (val, "-1");
    40       /* this choice of sentinel prevents us from using
    41          x[-1], x[-2], and is not really needed */
     39      showLength = TRUE;
    4240    } else {
    43       strncpy (tmpline, p+1, n);
    44       tmpline[n] = 0;
     41      tmpline = strncreate (p+1, n);
    4542      val = dvomath (1, &tmpline, &size, 0);
     43      free (tmpline);
    4644      if (val == NULL) goto dumpline; /* not a valid vector subscript */
    4745    }
    48     I = atoi (val);
    49     free (val);
     46    I = 0;
     47    if (val != NULL) {
     48      I = atoi (val);
     49      free (val);
     50    }     
    5051
    5152    /* find vector name */
     
    5354    w ++;
    5455    n = (int)(p - w);
    55     strncpy (tmpline, w, n);
    56     tmpline[n] = 0;
     56    tmpline = strncreate (w, n);
    5757    if ((vec = SelectVector (tmpline, OLDVECTOR, TRUE)) == NULL) goto dumpline;
     58    free (tmpline);
    5859
    5960    /* find vector element */
    60     if (I >= vec[0].Nelements) {
     61    if ((I >= vec[0].Nelements) || (I < -1*vec[0].Nelements)) {
    6162      gprint (GP_ERR, "vector subscript out of range\n");
    6263      goto escape;
    6364    }
    64     if (I < 0) {
     65    if (showLength) {
    6566      f1 = vec[0].Nelements;
    6667    } else {
    67       f1 = vec[0].elements[I];
     68      if (I < 0) {
     69        f1 = vec[0].elements[vec[0].Nelements+I];
     70      } else {
     71        f1 = vec[0].elements[I];
     72      }
    6873    }
    6974    if ((int)f1 == f1)
    70       sprintf (tmpline, "%.0f", f1);
     75      snprintf (strValue, 128, "%.0f", f1);
    7176    else
    72       sprintf (tmpline, "%.9g", f1);
    73     /* interpolate vector element */
     77      snprintf (strValue, 128, "%.9g", f1);
     78
     79    /* interpolate vector element into newline (being accumulated) */
     80    size = (N - newline) + (w - L) + strlen(strValue);
     81    if (size >= NLINE) {
     82      Noff = N - newline;
     83      NLINE += 128 + strlen(strValue) + (w - L);
     84      REALLOCATE (newline, char, NLINE);
     85      N = newline + Noff;
     86    }
     87
    7488    n = (int) (w - L);
    7589    strncpy (N, L, n);
    7690    N += n;
    77     n = strlen (tmpline);
    78     strncpy (N, tmpline, n);
     91    n = strlen (strValue);
     92    strncpy (N, strValue, n);
    7993    N += n;
    8094    L = q + 1;
     
    8296
    8397dumpline:
     98  size = (N - newline) + strlen(L);
     99  if (size >= NLINE) {
     100    Noff = N - newline;
     101    NLINE += 128 + strlen(L);
     102    REALLOCATE (newline, char, NLINE);
     103    N = newline + Noff;
     104  }
     105
    84106  n = strlen (L);
    85107  strncpy (N, L, n);
    86108  N[n] = 0;
    87109  free (line);
    88   free (tmpline);
    89110  return (newline);
    90111 
     
    92113  free (line);
    93114  free (newline);
    94   free (tmpline);
    95115  return (NULL);
    96116}
Note: See TracChangeset for help on using the changeset viewer.