Changeset 41354 for trunk/Ohana/src/opihi/lib.shell/expand_vectors.c
- Timestamp:
- May 13, 2020, 5:04:41 PM (6 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/lib.shell/expand_vectors.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/lib.shell/expand_vectors.c
r38002 r41354 1 1 # include "opihi.h" 2 3 // XXX need to unroll this function so we can more flexibly 4 // convert items in the line 2 5 3 6 // converts a[], a[4], a[2][3] to their numeric values 4 7 // add a?[] and a?[][] as equivalent to $?a 5 8 // a[][?] 9 10 enum {ANS_NONE, ANS_INT, ANS_FLT, ANS_STR}; 6 11 7 12 char *expand_vectors (char *line) { … … 11 16 int n, I, J, size, showLength, showXsize, showYsize, checkType, NLINE, Noff, isBuffer, inRange; 12 17 float *ptr; 13 double f1; 18 opihi_flt valueFlt; 19 opihi_int valueInt; 20 char *valueStr; 21 int answerType; 14 22 Vector *vec; 15 23 Buffer *buf; … … 41 49 if (p > q) goto dumpline; /* odd state: unmatched pair: ][ */ 42 50 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 43 60 /* find vector subscript */ 44 61 n = (int) (q - p - 1); … … 47 64 showXsize = FALSE; 48 65 showYsize = FALSE; 66 answerType = ANS_NONE; 49 67 50 68 if (n == 0) { … … 103 121 asVector: 104 122 /* 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 105 128 checkType = FALSE; 106 129 if (*(p-1) == '?') { … … 116 139 buf = SelectBuffer (tmpline, OLDBUFFER, !checkType); 117 140 if (checkType) { 118 f1 = (buf != NULL); 141 valueInt = (buf != NULL); 142 answerType = ANS_INT; 119 143 goto skipBuffer; 120 144 } … … 125 149 if (buf == NULL) goto dumpline; 126 150 if (showXsize) { 127 f1 = buf[0].header.Naxis[0]; 151 valueInt = buf[0].header.Naxis[0]; 152 answerType = ANS_INT; 128 153 goto skipBuffer; 129 154 } 130 155 if (showYsize) { 131 f1 = buf[0].header.Naxis[1]; 156 valueInt = buf[0].header.Naxis[1]; 157 answerType = ANS_INT; 132 158 goto skipBuffer; 133 159 } … … 145 171 if (J < 0) J += buf[0].header.Naxis[1]; 146 172 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; 148 175 } else { 149 176 vec = SelectVector (tmpline, OLDVECTOR, !checkType); 150 177 if (checkType) { 151 f1 = (vec != NULL); 178 valueInt = (vec != NULL); 179 answerType = ANS_INT; 152 180 } else { 153 181 if (vec == NULL) goto dumpline; … … 155 183 /* find vector element */ 156 184 if (showLength) { 157 f1 = vec[0].Nelements; 185 valueInt = vec[0].Nelements; 186 answerType = ANS_INT; 158 187 } else { 159 188 if ((I >= vec[0].Nelements) || (I < -1*vec[0].Nelements)) { … … 162 191 } 163 192 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 } 165 208 } 166 209 } … … 169 212 skipBuffer: 170 213 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 } 175 228 176 229 /* interpolate vector element into newline (being accumulated) */
Note:
See TracChangeset
for help on using the changeset viewer.
