예제 #1
0
static int record_response (Elem_Type *e, float f, unsigned int ch) /*{{{*/
{
   unsigned int i, start_size = 16;

   e->num_chan++;

   if (e->num_chan == 1)
     {
        e->chan.s = ch;
        e->resp.s = f;
        return 0;
     }
   else if (e->num_chan == 2)
     {
        unsigned int ch0 = e->chan.s;
        float f0 = e->resp.s;

        e->resp.v = (float *) MALLOC (start_size * sizeof (float));
        e->chan.v = (unsigned int *) MALLOC (start_size * sizeof (unsigned int));
        if (e->resp.v == NULL || e->chan.v == NULL)
          return -1;

        e->size = start_size;
        e->resp.v[0] = f0;
        e->chan.v[0] = ch0;
     }

   if (e->num_chan > e->size)
     {
        unsigned int *i_tmp;
        float *f_tmp;
        e->size *= 2;

        if (NULL == (i_tmp = (unsigned int *) ISIS_REALLOC (e->chan.v, e->size * sizeof(unsigned int))))
          return -1;
        e->chan.v = i_tmp;

        if (NULL == (f_tmp = (float *) ISIS_REALLOC (e->resp.v, e->size * sizeof(float))))
          return -1;
        e->resp.v = f_tmp;
     }

   i = e->num_chan - 1;
   e->chan.v[i] = ch;
   e->resp.v[i] = f;

   return 0;
}
예제 #2
0
static int readline (FILE *fp, Buffer_Type *b) /*{{{*/
{
   if (-1 == init_buf (b))
     return -1;

   b->len = 0;

   for (;;)
     {
        int ch = getc (fp);

        switch (ch)
          {
           case COMMENT_CHAR:
             b->buf[b->len] = 0;
             if (-1 == skip_to_eol (fp))
               return -1;
             return b->len ? 0 : COMMENT_CHAR;

           case '\n':
             b->buf[b->len] = 0;
             return 0;

           case EOF:
             b->buf[b->len] = 0;
             return -1;

           default:
             b->buf[b->len++] = ch;
             break;
          }

        if (b->len == b->bufsize)
          {
             unsigned int new_size = b->bufsize * 2 * sizeof(char);
             char *tmp;
             if (NULL == (tmp = (char *) ISIS_REALLOC (b->buf, new_size)))
               return -1;
             b->buf = tmp;
             b->bufsize *= 2;
          }
     }
}
예제 #3
0
/* cols[] must be sorted in increasing order and with no values repeated */
static int ascii_readcol (FILE *fp, unsigned int *cols, unsigned int ncols, /*{{{*/
                          double **px, unsigned int *pnx)
{
   enum {START_ROWS = 1024};
   const char *sepchars = " ,\t";
   Buffer_Type b = {NULL, 0, 0};
   unsigned int *last_col = cols + ncols;
   unsigned int k, line, nx = START_ROWS * ncols;
   double *x = NULL;
   int ret = -1;

   *px = NULL;
   *pnx = 0;

   if (ncols == 0)
     return -1;

   if (NULL == (x = (double *) ISIS_MALLOC (nx * sizeof(double))))
     return -1;

   k = 0;
   line = 0;

   do
     {
        unsigned int *want_col = NULL;
        unsigned int c, k_save;
        char *word = NULL;
        int ch;

        k_save = k;

        if (-1 == (ch = readline (fp, &b)))
          break;

        line++;

        if (ch == COMMENT_CHAR)
          continue;

        word = strtok (b.buf, sepchars);
        want_col = cols;
        c = 1;

        while ((word != NULL) && (want_col < last_col))
          {
             if (c == *want_col)
               {
                  double d;

                  if (1 != sscanf (word, "%le", &d))
                    {
                       char *p = strchr (word, '\n');
                       if (p) *p = 0;
                       isis_vmesg (FAIL, I_READ_FAILED, __FILE__, __LINE__, "invalid input: '%s'", word);
                       goto finish;
                    }

                  if (k >= nx)
                    {
                       unsigned int new_size = 2 * nx * sizeof(double);
                       double *tmp;
                       if (NULL == (tmp = (double *) ISIS_REALLOC (x, new_size)))
                         goto finish;
                       x = tmp;
                       nx *= 2;
                    }

                  x[k++] = d;

                  want_col++;
               }

             word = strtok (NULL, sepchars);
             c++;
          }

        /* support skipping blank lines */
        if ((want_col < last_col) && (c > 1))
          {
             isis_vmesg (FAIL, I_READ_FAILED, __FILE__, __LINE__, "at line %u: couldn't read all requested columns", line);
             k = k_save;
             break;
          }

     } while (!feof(fp));

   ret = 0;
   finish:

   free_buf (&b);

   if (ret)
     ISIS_FREE (x);
   else
     {
        *px = x;
        *pnx = k;
     }

   return ret;
}