Guest User

Untitled

a guest
Jan 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. int
  2. read_input(FILE *infile, const char *filename,
  3.            double **datap, int *ncolsp, int **cumsizesp, int *nrunsp)
  4. {
  5.     char b[2];
  6.     double number;
  7.  
  8.     int newline;                /* last input was newline */
  9.     int datai;                  /* the current element of (*datap) */
  10.     int col;                    /* the column being read */
  11.     int set = *nrunsp;          /* the current data set */
  12.     int ncols = *ncolsp;        /* the number of columns */
  13.  
  14.     int datasize;
  15.     int setsize;
  16.  
  17.     datai = (set == 0)
  18.         ? 0
  19.         : ncols * (*cumsizesp)[set-1];
  20.  
  21.     setsize = (set == 0)
  22.         ? 0
  23.         : ((set-1) / SET_INC + 1) * SET_INC;
  24.  
  25.     *cumsizesp = realloc(*cumsizesp, setsize * sizeof(int));
  26.  
  27.     datasize = (datai == 0)
  28.         ? 0
  29.         : ((datai - 1) / DATA_INC + 1) * DATA_INC;
  30.  
  31.     *datap = realloc(*datap, datasize * sizeof(double));
  32.  
  33.     /* remove leading whitespace */
  34.     fscanf (infile, "%*[ \t\n]");
  35.  
  36.     if (feof(infile)) {
  37.         warnprintf("%s: file is empty.", filename);
  38.         return -1;
  39.     }
  40.    
  41.     do {
  42.         newline = 0;
  43.         if (set == setsize) {
  44.             setsize += SET_INC;
  45.             *cumsizesp = realloc(*cumsizesp,  setsize * sizeof(int));
  46.         }
  47.  
  48.         (*cumsizesp)[set] = (set == 0) ? 0 : (*cumsizesp)[set-1];       /* beginning of data set */
  49.  
  50.         while (newline == 0) {
  51.         col = 0;        /* beginning of row */
  52.             while (newline == 0) {
  53.         if (fscanf(infile, "%lf", &number) != 1) {
  54.                     char buffer[64];
  55.  
  56.                     fscanf(infile, "%60s", buffer);
  57.                     errprintf(
  58.                         "could not convert string `%s' to double, exiting...",
  59.                         buffer);
  60.                 }
  61.  
  62.                 if (datai == datasize) {
  63.                     datasize += DATA_INC;
  64.                     *datap = realloc(*datap,  datasize * sizeof(double));
  65.                 }
  66.                 (*datap)[datai++] = number;
  67.                
  68. #if DEBUG > 2
  69.                 fprintf(stderr, "set %d, row %d, column %d, x = %g\n",
  70.                         set, (*cumsizesp)[set], col, number);
  71. #endif
  72.                 col++;    /* new column */
  73.                
  74.                 fscanf(infile, "%*[ \t]");
  75.                 newline = fscanf(infile, "%1[\n]", b);
  76.             }
  77.  
  78.             if (!ncols)
  79.                 ncols = col;
  80.             else if (col != ncols) {
  81.                 if ((*cumsizesp)[0] == 0)
  82.                     errprintf ("reference point has dimension %d"
  83.                                " while input has dimension %d",
  84.                                ncols, col);
  85.                 else
  86.                     errprintf("row %d has different length (%d)"
  87.                               " than previous rows (%d), exiting...",
  88.                               (*cumsizesp)[set], col, ncols);
  89.             }
  90.  
  91.             (*cumsizesp)[set]++;
  92.    
  93.             fscanf (infile, "%*[ \t]");
  94.         newline = fscanf (infile, "%1[\n]", b);
  95.     }
  96. #if DEBUG > 1
  97.         fprintf (stderr, "Set %d, %d rows in total\n", set, (*cumsizesp)[set]);
  98. #endif
  99.     set++; /* new data set */
  100.    
  101.         fscanf (infile, "%*[ \t\n]");
  102.  
  103.     } while (!feof(infile));
  104.  
  105.     *ncolsp = ncols;
  106.     *nrunsp = set;
  107.  
  108.     return 0;
  109. }
Add Comment
Please, Sign In to add comment