Example #1
0
constraint_t * get_constraints(FILE *cons_fp, uint32_t *num_constraints) {
   constraint_t *cons;
   char *line, **strs;
   uint32_t ndx, num_strs, size, length;

   if (!(cons_fp && num_constraints)) return NULL;

   length = DFLT_NUM_CONS;

   CALL_ERR((strs = (char**)malloc(sizeof(char*) * DFLT_NUM_CONS)), NULL);
   for (ndx = 0; (line = read_long_line(cons_fp)); ++ndx) {
      if (ndx == length)
         CALL_ERR((strs = (char**)realloc(strs, sizeof(char*) *
               (length <<= 1))), NULL);

      strs[ndx] = line;
   }
   num_strs = ndx;

   CALL_ERR((cons = (constraint_t*)malloc(sizeof(constraint_t) * num_strs)),
         NULL);
   for (size = 0, ndx = 0; ndx < num_strs && size < MAX_NUM_CONS; ++ndx)
      if (get_ineq_expr(strs[ndx], &cons[ndx].expr, &cons[ndx].cmp))
         ++size;
   *num_constraints = size;

   for (ndx = 0; ndx < num_strs; ++ndx)
      if (strs[ndx]) free(strs[ndx]);
   free(strs);

   return cons;
}
Example #2
0
int uniread(Quark *pr, FILE *fp,
    DataParser parse_cb, DataStore store_cb, void *udata)
{
    int nrows, nrows_allocated;
    int ok, readerror;
    Quark *q = NULL;
    char *linebuf = NULL;
    int linebuflen = 0;
    int linecount;

    linecount = 0;
    readerror = 0;
    nrows = 0;
    nrows_allocated = 0;
    
    ok = TRUE;
    while (ok) {
        char *s;
	int maybe_data;
        int ncols, nncols, nscols;
        int *formats;
        
        if (read_long_line(fp, &linebuf, &linebuflen) == RETURN_SUCCESS) {
            linecount++;
            s = linebuf;

	    /* skip leading whitespaces */
            while (*s == ' ' || *s == '\t') {
                s++;
            }

	    /* skip comments */
            if (*s == '#') {
                continue;
            }

            /*     EOL           EOD    */
            if (*s == '\n' || *s == '\0') {
                maybe_data = FALSE;
            } else
            if (parse_cb && parse_cb(s, udata) == RETURN_SUCCESS) {
                maybe_data = FALSE;
            } else {
                maybe_data = TRUE;
            }
        } else {
            ok = FALSE;
            maybe_data = FALSE;
        }
        
        if (maybe_data) {
	    if (!nrows) {
		/* parse the data line */
                if (parse_ss_row(pr, s, &nncols, &nscols, &formats) != RETURN_SUCCESS) {
		    errmsg("Can't parse data");
		    xfree(linebuf);
		    return RETURN_FAILURE;
                }
                ncols = nncols + nscols;

                /* init the SSD */
                q = gapp_ssd_new(pr);
                if (!q || ssd_set_ncols(q, ncols, formats) != RETURN_SUCCESS) {
		    errmsg("Malloc failed in uniread()");
		    quark_free(q);
                    xfree(formats);
		    xfree(linebuf);
                    return RETURN_FAILURE;
                }
                xfree(formats);
	    }
            
	    if (nrows >= nrows_allocated) {
		if (!nrows_allocated) {
                    nrows_allocated = BUFSIZE;
                } else {
                    nrows_allocated *= 2;
                }
                if (ssd_set_nrows(q, nrows_allocated) != RETURN_SUCCESS) {
		    errmsg("Malloc failed in uniread()");
                    quark_free(q);
		    xfree(linebuf);
		    return RETURN_FAILURE;
                }
	    }

            if (insert_data_row(q, nrows, s) != RETURN_SUCCESS) {
                char tbuf[128];
                
                sprintf(tbuf, "Error parsing line %d, skipped", linecount);
                errmsg(tbuf);
                readerror++;
                if (readerror > MAXERR) {
                    if (yesno("Lots of errors, abort?", NULL, NULL, NULL)) {
                        quark_free(q);
		        xfree(linebuf);
                        return RETURN_FAILURE;
                    } else {
                        readerror = 0;
                    }
                }
            } else {
	        nrows++;
            }
	} else
        if (nrows) {
            /* free excessive storage */
            ssd_set_nrows(q, nrows);

            /* store accumulated data */
            if (store_cb && store_cb(q, udata) != RETURN_SUCCESS) {
		quark_free(q);
                xfree(linebuf);
                return RETURN_FAILURE;
            }

            /* reset state registers */
            nrows = 0;
            nrows_allocated = 0;
            readerror = 0;
        }
    }

    xfree(linebuf);
    
    return RETURN_SUCCESS;
}