Changeset 15878 for trunk/Ohana/src/opihi/lib.shell/expand_vectors.c
- Timestamp:
- Dec 16, 2007, 2:27:00 PM (19 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/lib.shell/expand_vectors.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/lib.shell/expand_vectors.c
r10073 r15878 3 3 char *expand_vectors (char *line) { 4 4 5 char *newline, *tmpline, *val;5 char *newline, *tmpline, strValue[128], *val; 6 6 char *L, *N, *p, *q, *w; 7 int n, I, size ;7 int n, I, size, showLength, NLINE, Noff; 8 8 double f1; 9 9 Vector *vec; … … 11 11 if (line == NULL) return (NULL); 12 12 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); 15 15 16 16 /* look for form fred[stuff] */ … … 19 19 if (L == NULL) { 20 20 free (newline); 21 free (tmpline);22 21 return (line); 23 22 } … … 35 34 if (p > q) goto dumpline; /* odd state: unmatched pair: ][ */ 36 35 n = (int) (q - p - 1); 36 val = NULL; 37 showLength = FALSE; 37 38 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; 42 40 } else { 43 strncpy (tmpline, p+1, n); 44 tmpline[n] = 0; 41 tmpline = strncreate (p+1, n); 45 42 val = dvomath (1, &tmpline, &size, 0); 43 free (tmpline); 46 44 if (val == NULL) goto dumpline; /* not a valid vector subscript */ 47 45 } 48 I = atoi (val); 49 free (val); 46 I = 0; 47 if (val != NULL) { 48 I = atoi (val); 49 free (val); 50 } 50 51 51 52 /* find vector name */ … … 53 54 w ++; 54 55 n = (int)(p - w); 55 strncpy (tmpline, w, n); 56 tmpline[n] = 0; 56 tmpline = strncreate (w, n); 57 57 if ((vec = SelectVector (tmpline, OLDVECTOR, TRUE)) == NULL) goto dumpline; 58 free (tmpline); 58 59 59 60 /* find vector element */ 60 if ( I >= vec[0].Nelements) {61 if ((I >= vec[0].Nelements) || (I < -1*vec[0].Nelements)) { 61 62 gprint (GP_ERR, "vector subscript out of range\n"); 62 63 goto escape; 63 64 } 64 if ( I < 0) {65 if (showLength) { 65 66 f1 = vec[0].Nelements; 66 67 } 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 } 68 73 } 69 74 if ((int)f1 == f1) 70 s printf (tmpline, "%.0f", f1);75 snprintf (strValue, 128, "%.0f", f1); 71 76 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 74 88 n = (int) (w - L); 75 89 strncpy (N, L, n); 76 90 N += n; 77 n = strlen ( tmpline);78 strncpy (N, tmpline, n);91 n = strlen (strValue); 92 strncpy (N, strValue, n); 79 93 N += n; 80 94 L = q + 1; … … 82 96 83 97 dumpline: 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 84 106 n = strlen (L); 85 107 strncpy (N, L, n); 86 108 N[n] = 0; 87 109 free (line); 88 free (tmpline);89 110 return (newline); 90 111 … … 92 113 free (line); 93 114 free (newline); 94 free (tmpline);95 115 return (NULL); 96 116 }
Note:
See TracChangeset
for help on using the changeset viewer.
