Changeset 31598
- Timestamp:
- Jun 2, 2011, 12:43:07 PM (15 years ago)
- Location:
- branches/eam_branches/ipp-20110505/Ohana/src
- Files:
-
- 2 added
- 3 edited
-
libohana/include/ohana.h (modified) (1 diff)
-
libohana/src/string.c (modified) (2 diffs)
-
opihi/cmd.data/read_vectors.c (modified) (6 diffs)
-
opihi/cmd.data/test/csv.dat (added)
-
opihi/cmd.data/test/csv1.dat (added)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20110505/Ohana/src/libohana/include/ohana.h
r31593 r31598 235 235 int scan_line PROTO((FILE *f, char *line)); 236 236 int dparse PROTO((double *X, int NX, char *line)); 237 int dparse_csv PROTO((double *X, int NX, char *line)); 237 238 int fparse PROTO((float *X, int NX, char *line)); 238 239 int get_argument PROTO((int argc, char **argv, char *arg)); -
branches/eam_branches/ipp-20110505/Ohana/src/libohana/src/string.c
r27435 r31598 153 153 } 154 154 155 // advance to the next word, separated by commas: 156 // AA,BB,CC : go from AA to BB to CC to 0 157 // AA,,CC : go from AA to , to CC to 0 158 // ,,, : go from , to , to , to 0 159 160 char *_parse_nextword_csv (char *string) { 161 162 if (string == (char *) NULL) return ((char *) NULL); 163 164 for (; (*string != 0) && (*string != ','); string++); 165 if (*string == ',') string ++; 166 return (string); 167 } 168 155 169 int dparse (double *X, int NX, char *line) { 156 170 … … 162 176 for (i = 0; i < NX - 1; i++) 163 177 word = _parse_nextword (word); 178 179 *X = strtod (word, &ptr); 180 if (ptr == word) return (FALSE); 181 if (word[0] == '-') return (-1); 182 return (1); 183 } 184 185 int dparse_csv (double *X, int NX, char *line) { 186 187 int i; 188 char *word; 189 char *ptr; 190 191 word = line; 192 for (i = 0; i < NX - 1; i++) 193 word = _parse_nextword_csv (word); 194 195 if (word[0] == ',') { 196 *X = NAN; 197 return 1; 198 } 164 199 165 200 *X = strtod (word, &ptr); -
branches/eam_branches/ipp-20110505/Ohana/src/opihi/cmd.data/read_vectors.c
r29938 r31598 2 2 3 3 FILE *f = (FILE *) NULL; 4 char filename[ 256];4 char filename[1024]; 5 5 6 6 int datafile (int argc, char **argv) { … … 23 23 int read_vectors (int argc, char **argv) { 24 24 25 int i, j, Nskip, Nvec, *col, done, status ;25 int i, j, Nskip, Nvec, *col, done, status, IsCSV; 26 26 int Nbytes, nbytes, Nstart, NELEM, N, nread; 27 27 char *colstr, *c0, *c1, *buffer, *extname; … … 47 47 } 48 48 49 IsCSV = FALSE; 50 if ((N = get_argument (argc, argv, "-csv"))) { 51 remove_argument (N, &argc, argv); 52 IsCSV = TRUE; 53 } 54 49 55 if ((argc < 3) || !(argc % 2)) { 50 56 gprint (GP_ERR, "USAGE: read name N name N ...\n"); … … 58 64 } 59 65 fseeko (f, 0LL, SEEK_SET); 66 67 // if (IsCSV) { 68 // status = read_vectors_csv (argc, argv, Nskip, f); 69 // return status; 70 // } 60 71 61 72 Nvec = (argc - 1) / 2; … … 122 133 if ((*c0 != '#') && (*c0 != '!')) { 123 134 for (i = 0; (i < Nvec) && status; i++) { 124 status = dparse (&value, col[i], c0); 135 if (IsCSV) { 136 status = dparse_csv (&value, col[i], c0); 137 } else { 138 status = dparse (&value, col[i], c0); 139 } 125 140 vec[i][0].elements.Flt[N] = value; 126 141 if (!status) vec[i][0].elements.Flt[N] = NAN; … … 341 356 return (TRUE); 342 357 } 358 359 # if (0) 360 int read_vectors_csv (int argc, char **argv, int Nskip, FILE *f) { 361 362 int i, j, Nvec, *col, done, status; 363 int Nbytes, nbytes, Nstart, NELEM, N, nread; 364 char *colstr, *c0, *c1, *buffer, *extname; 365 double value; 366 Vector **vec; 367 368 Nvec = (argc - 1) / 2; 369 ALLOCATE (vec, Vector *, Nvec); 370 ALLOCATE (col, int, Nvec); 371 372 for (i = 0; i < Nvec; i++) { 373 if ((vec[i] = SelectVector (argv[2*i + 1], ANYVECTOR, TRUE)) == NULL) { 374 gprint (GP_ERR, "USAGE: read name N name N ...\n"); 375 free (vec); 376 free (col); 377 return (FALSE); 378 } 379 // XXX we could allow flags (eg, N.i) to specify INT vs FLT types vectors... 380 colstr = argv[2*i+2]; 381 for (j = 0; j < strlen (colstr); j++) { 382 if (!isdigit(colstr[j])) { 383 gprint (GP_ERR, "USAGE: read name N name N ...\n"); 384 free (vec); 385 free (col); 386 return (FALSE); 387 } 388 } 389 col[i] = atof (colstr); 390 } 391 392 // currently, all read vectors are forced to be type FLT 393 NELEM = 1000; 394 for (i = 0; i < Nvec; i++) { 395 ResetVector (vec[i], OPIHI_FLT, NELEM); 396 } 397 398 ALLOCATE (buffer, char, 0x10001); 399 bzero (buffer, 0x10001); 400 for (i = 0; i < Nskip; i++) { 401 scan_line (f, buffer); 402 } 403 404 Nstart = 0; 405 N = 0; 406 done = FALSE; 407 while (!done) { 408 Nbytes = 0x10000 - Nstart; 409 bzero (&buffer[Nstart], Nbytes); 410 nread = fread (&buffer[Nstart], 1, Nbytes, f); 411 if (ferror (f)) { 412 perror ("error reading data file"); 413 break; 414 } 415 if (nread == 0) break; 416 nbytes = nread + Nstart; 417 418 status = TRUE; 419 c0 = buffer; 420 while (status) { 421 c1 = strchr (c0, '\n'); 422 if (c1 == (char *) NULL) { 423 Nstart = strlen (c0); 424 memmove (buffer, c0, Nstart); 425 status = FALSE; 426 } else { 427 *c1 = 0; 428 } 429 if ((*c0 != '#') && (*c0 != '!')) { 430 for (i = 0; (i < Nvec) && status; i++) { 431 status = dparse_csv (&value, col[i], c0); 432 vec[i][0].elements.Flt[N] = value; 433 if (!status) vec[i][0].elements.Flt[N] = NAN; 434 } 435 if (status) N++; 436 } 437 c0 = c1 + 1; 438 if (N == NELEM) { 439 NELEM += 1000; 440 for (i = 0; i < Nvec; i++) { 441 REALLOCATE (vec[i][0].elements.Flt, opihi_flt, NELEM); 442 } 443 } 444 445 } 446 } 447 for (i = 0; i < Nvec; i++) { 448 REALLOCATE (vec[i][0].elements.Flt, opihi_flt, MAX (N,1)); 449 vec[i][0].Nelements = N; 450 } 451 452 free (vec); 453 free (col); 454 free (buffer); 455 return (TRUE); 456 457 } 458 459 # endif
Note:
See TracChangeset
for help on using the changeset viewer.
