Esempio n. 1
0
frameT *read_csv (char *file_name)
{
	FILE			*fp;
	unsigned char	buf[CSV_BUF_SIZE];
	unsigned char	header_buf[CSV_BUF_SIZE];
	unsigned char	*row[MAX_COLUMNS];
	unsigned char	*header[MAX_COLUMNS];
	int				ncol, cols;
	int				n, i;
	frameT			*frame;
	csvT			*csv;

	csv = open_csv (file_name);
	fp = csv->fp;


	// Parse header
	n = csv_row_fread (fp, header_buf, CSV_BUF_SIZE, header, MAX_COLUMNS, ',', CSV_TRIM | CSV_QUOTES, &ncol);
	if (n <= 0)
	{
		fprintf (stderr, "Failed to read header from CSV file\n");
		exit (-1);
	}

	// Parse the first data row to determine data types

	n = csv_row_fread (fp, buf, CSV_BUF_SIZE, row, ncol, ',', CSV_TRIM | CSV_QUOTES, &cols);
	if (n <= 0)
	{
		fprintf (stderr, "File has no data\n");
		exit (-1);
	}
	frame = new_frame (file_name, ncol);
	frame->csv = csv;
	for (i=0; i<ncol; i++) init_column (frame, i, (char *)header[i], guess_type((char *) row[i]));

	for (i=0; i<ncol; i++) column_init_data (frame, i, csv->est_rows);
	frame->allocated_rows = csv->est_rows;
		
	frame->region_rows = (unsigned char *) malloc (frame->allocated_rows);
	if (!frame->region_rows)
	{
		fprintf (stderr, "Failed to allocate region_rows\n");	
		exit (-1);
	}
	frame->allocated_region_rows = frame->allocated_rows;

	if (csv->est_rows < 10000)
	{
		load_all_rows (frame);
	} else
	{
		load_random_rows (frame, 1.0);
	}

	return (frame);
}
Esempio n. 2
0
int add_column(screen_t* const s, char* header, char* format, char* desc,
               char* expr)
{
  int col_width, err=0;
  int n = s->num_columns;

  expression* e = parser_expression(expr);

  if (e == NULL || e->type == ERROR) {
    free_expression(e);
    error_printf("Invalid expression in column '%s', screen '%s': column ignored\n",
                 header, s->name);
    return -1;
  }

  check_counters_used(e, s, &err);
  if( err > 0 ) {
    free_expression(e);
    return -1;
  }

  if (n == s->num_alloc_columns) {
    s->columns = realloc(s->columns, sizeof(column_t) * (n + alloc_chunk));
    s->num_alloc_columns += alloc_chunk;
  }
  init_column(&s->columns[n]);
  s->columns[n].expression = e;
  s->columns[n].header = strdup(header);
  s->columns[n].format = strdup(format);

  col_width = strlen(header);
  /* setup an empty field with proper width */
  s->columns[n].empty_field = malloc(col_width + 1);
  memset(s->columns[n].empty_field, ' ', col_width - 1);
  s->columns[n].empty_field[col_width - 1] = '-';
  s->columns[n].empty_field[col_width] = '\0';

  /* setup an error field with proper width */
  s->columns[n].error_field = malloc(col_width + 1);
  memset(s->columns[n].error_field, ' ', col_width - 1);
  s->columns[n].error_field[col_width - 1] = '?';
  s->columns[n].error_field[col_width] = '\0';

  if (desc)
    s->columns[n].description = strdup(desc);
  else
    s->columns[n].description = strdup("(unknown)");

  s->num_columns++;
  return n;
}