Skip to content
Snippets Groups Projects
Commit 0da3f1d3 authored by Tom Greig's avatar Tom Greig
Browse files

Treat -i and -r args as regex

Often the columns to ignore or put on the right axis are, like,
resistance_0, resistance_1, resistance_2... and being able to wrangle
the lot of them in one argument is handy.
parent 5e695300
No related branches found
No related tags found
No related merge requests found
...@@ -7,12 +7,13 @@ ...@@ -7,12 +7,13 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
#include <time.h> #include <time.h>
#include <regex.h>
/* TYPES!!! */ /* TYPES!!! */
struct column_list { struct column_list {
struct column_list* next; struct column_list* next;
char* header; regex_t regex;
}; };
struct data { struct data {
...@@ -37,7 +38,7 @@ struct context { ...@@ -37,7 +38,7 @@ struct context {
struct data* data; struct data* data;
struct data* data_end; struct data* data_end;
struct column_list* left_columns; struct column_list* right_columns;
struct column_list* ignore_columns; struct column_list* ignore_columns;
struct { struct {
...@@ -170,18 +171,18 @@ int main( int argc, char** argv ) { ...@@ -170,18 +171,18 @@ int main( int argc, char** argv ) {
.flags = G_OPTION_FLAG_IN_MAIN, .flags = G_OPTION_FLAG_IN_MAIN,
.arg = G_OPTION_ARG_CALLBACK, .arg = G_OPTION_ARG_CALLBACK,
.arg_data = &add_to_column_list, .arg_data = &add_to_column_list,
.description = "Use right axis for column " .description = "Use right axis for columns matching regex "
"(can be given multiple times)", "(can be given multiple times)",
.arg_description = "column" .arg_description = "regex"
}, { }, {
.long_name = "ignore", .long_name = "ignore",
.short_name = 'i', .short_name = 'i',
.flags = G_OPTION_FLAG_IN_MAIN, .flags = G_OPTION_FLAG_IN_MAIN,
.arg = G_OPTION_ARG_CALLBACK, .arg = G_OPTION_ARG_CALLBACK,
.arg_data = &add_to_column_list, .arg_data = &add_to_column_list,
.description = "Do not plot the data in column " .description = "Ignore columns matching regex "
"(can be given multiple times)", "(can be given multiple times)",
.arg_description = "column" .arg_description = "regex"
}, { }, {
.long_name = "font", .long_name = "font",
.short_name = 'f', .short_name = 'f',
...@@ -269,12 +270,15 @@ static bool add_to_column_list( const char* option_name, ...@@ -269,12 +270,15 @@ static bool add_to_column_list( const char* option_name,
(void) error; (void) error;
struct column_list* item = calloc( 1, sizeof *item ); struct column_list* item = calloc( 1, sizeof *item );
item->header = strdup( value ); if ( 0 != regcomp( &( item->regex ), value, REG_EXTENDED ) ) {
fprintf( stderr, "Invalid column regex: '%s'\n", value );
return false;
}
if ( 0 == strcmp( option_name, "-r" ) || if ( 0 == strcmp( option_name, "-r" ) ||
0 == strcmp( option_name, "--right" ) ) { 0 == strcmp( option_name, "--right" ) ) {
item->next = context->left_columns; item->next = context->right_columns;
context->left_columns = item; context->right_columns = item;
} else if ( 0 == strcmp( option_name, "-i" ) || } else if ( 0 == strcmp( option_name, "-i" ) ||
0 == strcmp( option_name, "--ignore" ) ) { 0 == strcmp( option_name, "--ignore" ) ) {
item->next = context->ignore_columns; item->next = context->ignore_columns;
...@@ -691,10 +695,11 @@ static void parse_headers( struct context* context ) { ...@@ -691,10 +695,11 @@ static void parse_headers( struct context* context ) {
context->columns[n].type = LEFT_AXIS; context->columns[n].type = LEFT_AXIS;
for ( struct column_list* item = context->left_columns; for ( struct column_list* item = context->right_columns;
NULL != item; NULL != item;
item = item->next ) { item = item->next ) {
if ( 0 == strcmp( stripped_header, item->header ) ) { if ( 0 == regexec( &( item->regex ), stripped_header,
0, NULL, 0 ) ) {
context->columns[n].type = RIGHT_AXIS; context->columns[n].type = RIGHT_AXIS;
break; break;
} }
...@@ -703,7 +708,8 @@ static void parse_headers( struct context* context ) { ...@@ -703,7 +708,8 @@ static void parse_headers( struct context* context ) {
for ( struct column_list* item = context->ignore_columns; for ( struct column_list* item = context->ignore_columns;
NULL != item; NULL != item;
item = item->next ) { item = item->next ) {
if ( 0 == strcmp( stripped_header, item->header ) ) { if ( 0 == regexec( &( item->regex ), stripped_header,
0, NULL, 0 ) ) {
context->columns[n].type = IGNORE; context->columns[n].type = IGNORE;
break; break;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment