Skip to content
Snippets Groups Projects
Commit 4ecda605 authored by tag2y19's avatar tag2y19 Committed by Tom Greig
Browse files

Add option to set the x-axis column

-x <column name> means we'll use that column's values as the x
coordinates.
parent 9a9b6bf1
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,9 @@ struct context {
GtkWidget* plot;
char* xaxis;
size_t xaxis_n;
float (* colours)[3];
size_t n_colours;
......@@ -98,8 +101,24 @@ int main( int argc, char** argv ) {
fcntl( STDIN_FILENO, F_SETFL,
fcntl( STDIN_FILENO, F_GETFL ) | O_NONBLOCK );
const GOptionEntry options[] = {
{
.long_name = "x-axis",
.short_name = 'x',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_STRING,
.arg_data = &( context->xaxis ),
.description = "Column to use as the x-axis",
.arg_description = "column",
}, {
NULL,
},
};
GtkApplication* app = gtk_application_new(
"uk.ac.soton.ecs.e-plot", G_APPLICATION_DEFAULT_FLAGS );
g_application_add_main_option_entries(
G_APPLICATION( app ), options );
g_signal_connect(
app, "activate", G_CALLBACK( app_activate ), context );
......@@ -418,6 +437,8 @@ static void parse_headers( struct context* context ) {
c = context->read_buffer;
quote = '\0';
bool found = false;
while ( '\0' != *c ) {
header[0] = '\0';
......@@ -482,6 +503,13 @@ static void parse_headers( struct context* context ) {
stripped_header[strlen( stripped_header ) - 1] = '\0';
}
if ( NULL != context->xaxis &&
0 == strcmp( stripped_header, context->xaxis ) ) {
found = true;
} else {
context->headers[n] = strdup( stripped_header );
n++;
......@@ -489,6 +517,14 @@ static void parse_headers( struct context* context ) {
}
if ( !found ) {
free( context->xaxis );
context->xaxis = NULL;
context->xaxis_n = -1;
}
}
static void parse_data( struct context* context ) {
assert( 0 != context->n_headers );
......@@ -514,6 +550,7 @@ static void parse_data( struct context* context ) {
size_t n = 0;
char value[strlen( context->read_buffer )];
char quote = '\0';
bool timed = false;
while ( '\0' != *c && n < context->n_headers ) {
......@@ -579,8 +616,23 @@ static void parse_data( struct context* context ) {
}
char* end_ptr = NULL;
data->y[n].v = strtod( stripped_value, &end_ptr );
double value = strtod( stripped_value, &end_ptr );
if ( n == context->xaxis_n && !timed ) {
timed = true;
if ( '\0' == *end_ptr ) {
data->x = value;
} else {
free( data );
return;
}
} else {
if ( '\0' == *end_ptr ) {
data->y[n].v = value;
data->y[n].present = true;
} else {
data->y[n].present = false;
......@@ -590,6 +642,8 @@ static void parse_data( struct context* context ) {
}
}
if ( NULL == context->data ) {
context->data = data;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment