#ifndef lint
static char *RCSid() { return RCSid("$Id: binary.c,v 1.8 1999/11/08 19:24:27 lhecking Exp $"); }
#endif
/*
* The addition of gnubin and binary, along with a small patch
* to command.c, will permit gnuplot to plot binary files.
* gnubin - contains the code that relies on gnuplot include files
* and other definitions
* binary - contains those things that are independent of those
* definitions and files
*
* With these routines, hidden line removal of your binary data is possible!
*
* Last update: 3/29/92 memory allocation bugs fixed. jvdwoude@hut.nl
* 3/09/92 spelling errors, general cleanup, use alloc with no
* nasty fatal errors
* 3/03/92 for Gnuplot 3.24.
* Created from code for written by RKC for gnuplot 2.0b.
*
* Copyright (c) 1991,1992 Robert K. Cunningham, MIT Lincoln Laboratory
*
*/
#include "binary.h"
#include "alloc.h"
#include "util.h"
/*
* This routine scans the first block of the file to see if the file is a
* binary file. A file is considered binary if 10% of the characters in it
* are not in the ascii character set. (values < 128), or if a NUL is found.
* I hope this doesn't break when used on the bizzare PC's.
*/
int
is_binary_file(fp)
register FILE *fp;
{
register int i, len;
register int odd; /* Contains a count of the odd characters */
long where;
register unsigned char *c;
unsigned char buffer[512];
if ((where = ftell(fp)) == -1) { /* Find out where we start */
fprintf(stderr, "Notice: Assuming unseekable data is not binary\n");
return (FALSE);
} else {
rewind(fp);
len = fread(buffer, sizeof(char), 512, fp);
if (len = num_rows) { /* We've got to make a bigger rowsize */
temp_array = extend_matrix(m, 0, num_rows - 1, 0, num_cols - 1,
num_rows + ADD_ROWS - 1, num_cols - 1);
rt = extend_vector(rt, 0, num_rows - 1, num_rows + ADD_ROWS - 1);
num_rows += ADD_ROWS;
m = temp_array;
}
}
/* finally we force the matrix to be the correct row size */
/* bug fixed. procedure called with incorrect 6th argument.
* jvdwoude@hut.nl */
temp_array = retract_matrix(m, 0, num_rows - 1, 0, num_cols - 1, current_row - 1, num_cols - 1);
/* Now save the things that change */
*ret_matrix = temp_array;
*row_title = retract_vector(rt, 0, num_rows - 1, current_row - 1);
*column_title = ct;
*nr = current_row; /* Really the total number of rows */
*nc = num_cols;
return (TRUE);
}
/* This writes a matrix to a stream
Note that our ranges are inclusive ranges--and we can specify subsets.
This behaves similarly to the xrange and yrange operators in gnuplot
that we all are familiar with.
*/
int
fwrite_matrix(fout, m, nrl, nrh, ncl, nch, row_title, column_title)
register FILE *fout;
register float GPFAR *GPFAR * m, GPFAR * row_title, GPFAR * column_title;
register int nrl, nrh, ncl, nch;
{
register int j;
float length;
register int col_length;
register int status;
float GPFAR *title = NULL;
length = (float) (col_length = nch - ncl + 1);
if ((status = fwrite((char *) &length, sizeof(float), 1, fout)) != 1) {
fprintf(stderr, "fwrite 1 returned %d\n", status);
return (FALSE);
}
if (!column_title) {
column_title = title = vector(ncl, nch);
for (j = ncl; j