diff --git a/src/customers.c b/src/customers.c index 4f350bb..74b8d22 100644 --- a/src/customers.c +++ b/src/customers.c @@ -181,18 +181,13 @@ void select_customers( int count, char** customer_list_array ) nwipe_log( NWIPE_LOG_INFO, "Line selected = %d", selected_entry ); - /* Save the selected customer details to nwipe's config file /etc/nwipe/nwipe.conf */ - save_selected_customer( &customer_list_array[selected_entry - 1] ); -} - -void add_customer() -{ - char window_title[] = " Add Customer "; - int selected_entry = 0; - - // nwipe_gui_list( count, window_title, &list[8], &selected_entry ); - - nwipe_log( NWIPE_LOG_INFO, "Line selected = %d", selected_entry ); + /* Save the selected customer details to nwipe's config file /etc/nwipe/nwipe.conf + * If selected entry equals 0, then the customer did not select an entry so skip save. + */ + if( selected_entry != 0 ) + { + save_selected_customer( &customer_list_array[selected_entry - 1] ); + } } void delete_customer( int count, char** customer_list_array ) @@ -204,3 +199,89 @@ void delete_customer( int count, char** customer_list_array ) nwipe_log( NWIPE_LOG_INFO, "Line selected = %d", selected_entry ); } + +void write_customer_csv_entry( char* customer_name, + char* customer_address, + char* customer_contact_name, + char* customer_contact_phone ) +{ + /** + * Write the attached strings in csv format to the first + * line after the header (line 2 of file) + */ + + FILE* fptr; + + size_t result_size; + + /* Length of the new customer line */ + int csv_line_length; + + struct stat st; + + extern char nwipe_customers_file[]; + + intmax_t existing_file_size = 0; + + /* pointer to the new customer entry in csv format. */ + char* csv_buffer = 0; + + /* pointer to the buffer containing the existing customer file */ + char* customers_buffer = 0; + + /* pointer to the buffer containing the existing customer file plus the new entry */ + char* new_customers_buffer = 0; + + /* Determine length of all four strings and malloc sufficient storage + 12 = 8 quotes + three colons + null */ + csv_line_length = strlen( customer_name ) + strlen( customer_address ) + strlen( customer_contact_name ) + + strlen( customer_contact_phone ) + 12; + if( !( csv_buffer = calloc( 1, csv_line_length == 0 ) ) ) + { + nwipe_log( NWIPE_LOG_ERROR, "func:nwipe_gui_add_customer:csv_buffer, calloc returned NULL " ); + } + else + { + /* Determine current size of the csv file containing the customers */ + stat( nwipe_customers_file, &st ); + existing_file_size = st.st_size; + + /* calloc sufficient storage to hold the existing customers file */ + if( !( customers_buffer = calloc( 1, existing_file_size + 1 ) ) ) + { + nwipe_log( NWIPE_LOG_ERROR, "func:nwipe_gui_add_customer:customers_buffer, calloc returned NULL " ); + } + else + { + /* create a third buffer which is the combined size of the previous two, i.e existing file size, plus the + * new customer entry + 1 (NULL) */ + if( !( new_customers_buffer = calloc( 1, existing_file_size + csv_line_length + 1 == 0 ) ) ) + { + nwipe_log( NWIPE_LOG_ERROR, "func:nwipe_gui_add_customer:customers_buffer, calloc returned NULL " ); + } + else + { + /* Read the whole of customers.csv file into customers_buffer */ + if( ( fptr = fopen( nwipe_customers_file, "rb" ) ) == NULL ) + { + nwipe_log( NWIPE_LOG_ERROR, "Unable to open %s", nwipe_customers_file ); + } + + /* Read the customers.csv file and populate the list array with the data */ + if( ( result_size = fread( customers_buffer, existing_file_size, 1, fptr ) ) != 1 ) + { + nwipe_log( NWIPE_LOG_ERROR, + "func:nwipe_gui_add_customer:Error reading customers file, # bytes read not as expected " + "%i bytes", + result_size ); + } + else + { + /* Read the first line of the existing customer buffer & write to the new buffer */ + } + } + } + } + free( csv_buffer ); + free( customers_buffer ); + free( new_customers_buffer ); +} diff --git a/src/customers.h b/src/customers.h index 7533c57..226daa4 100644 --- a/src/customers.h +++ b/src/customers.h @@ -28,6 +28,7 @@ void customer_processes( int ); void select_customers( int, char** ); void delete_customer(); void add_customer(); +void write_customer_csv_entry( char*, char*, char*, char* ); typedef char* nwipe_customers_buffer_t; typedef char** nwipe_customers_pointers_t; diff --git a/src/gui.c b/src/gui.c index 851a2d6..9757db8 100644 --- a/src/gui.c +++ b/src/gui.c @@ -33,6 +33,9 @@ #include #include #include +#include +#include +#include #include "nwipe.h" #include "context.h" @@ -148,6 +151,7 @@ const char* selection_footer_add_customer = "S=Save J=Down K=Up Space=Select Bac const char* selection_footer_add_customer_yes_no = "Save Customer Details Y/N"; const char* end_wipe_footer = "B=[Toggle between dark\\blank\\blue screen] Ctrl+C=Quit"; const char* rounds_footer = "Left=Erase Esc=Cancel Ctrl+C=Quit"; +const char* selection_footer_text_entry = "Esc=Cancel Ctrl+C=Quit"; const char* wipes_finished_footer = "Wipe finished - press enter to exit. Logged to STDOUT"; @@ -3854,7 +3858,7 @@ void nwipe_gui_list( int count, char* window_title, char** list, int* selected_e case KEY_BACKSPACE: case KEY_LEFT: case 127: - + *selected_entry = 1; return; break; @@ -4098,12 +4102,10 @@ void nwipe_gui_add_customer( void ) } while( save != YES && keystroke != 's' && keystroke != KEY_ENTER && keystroke != ' ' && keystroke != 10 && terminate_signal != 1 ); - /* If save customer details selected */ + /* If save set, or user pressed s or S then save the customer details */ if( keystroke == 's' || keystroke == 'S' || save == 1 ) { - /* NOTE Append a csv line to /etc/nwipe/customers.csv */ - - /* NOTE ADD CODE HERE NOTE */ + write_customer_csv_entry( customer_name, customer_address, customer_contact_name, customer_contact_phone ); } } /* end of nwipe_gui_add_customer( void ) */ @@ -4138,15 +4140,18 @@ void nwipe_gui_add_customer_name( char* customer_name ) /* Update the footer window. */ werase( footer_window ); - nwipe_gui_title( footer_window, rounds_footer ); + nwipe_gui_title( footer_window, selection_footer_text_entry ); wrefresh( footer_window ); + /* Set the buffer index to point to the end of the string, i.e the NULL */ + idx = strlen( customer_name ); + do { /* Erase the main window. */ werase( main_window ); - nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer ); + nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer_text_entry ); /* Add a border. */ box( main_window, 0, 0 ); @@ -4242,15 +4247,18 @@ void nwipe_gui_add_customer_address( char* customer_address ) /* Update the footer window. */ werase( footer_window ); - nwipe_gui_title( footer_window, rounds_footer ); + nwipe_gui_title( footer_window, selection_footer_text_entry ); wrefresh( footer_window ); + /* Set the buffer index to point to the end of the string, i.e the NULL */ + idx = strlen( customer_address ); + do { /* Erase the main window. */ werase( main_window ); - nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer ); + nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer_text_entry ); /* Add a border. */ box( main_window, 0, 0 ); @@ -4346,15 +4354,18 @@ void nwipe_gui_add_customer_contact_name( char* customer_contact_name ) /* Update the footer window. */ werase( footer_window ); - nwipe_gui_title( footer_window, rounds_footer ); + nwipe_gui_title( footer_window, selection_footer_text_entry ); wrefresh( footer_window ); + /* Set the buffer index to point to the end of the string, i.e the NULL */ + idx = strlen( customer_contact_name ); + do { /* Erase the main window. */ werase( main_window ); - nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer ); + nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer_text_entry ); /* Add a border. */ box( main_window, 0, 0 ); @@ -4450,15 +4461,18 @@ void nwipe_gui_add_customer_contact_phone( char* customer_contact_phone ) /* Update the footer window. */ werase( footer_window ); - nwipe_gui_title( footer_window, rounds_footer ); + nwipe_gui_title( footer_window, selection_footer_text_entry ); wrefresh( footer_window ); + /* Set the buffer index to point to the end of the string, i.e the NULL */ + idx = strlen( customer_contact_phone ); + do { /* Erase the main window. */ werase( main_window ); - nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer ); + nwipe_gui_create_all_windows_on_terminal_resize( 0, selection_footer_text_entry ); /* Add a border. */ box( main_window, 0, 0 );