From 92980508c027cd0e4ee514ca7e873502291434ef Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 25 Apr 2016 18:15:25 +0200 Subject: [PATCH] Made the function to clone a column in the context of a view private --- python/obitools3/obidms/capi/obiview.pxd | 2 - src/obiview.c | 144 +++++++++++++---------- src/obiview.h | 18 --- 3 files changed, 81 insertions(+), 83 deletions(-) diff --git a/python/obitools3/obidms/capi/obiview.pxd b/python/obitools3/obidms/capi/obiview.pxd index 8c8ff54..a818110 100644 --- a/python/obitools3/obidms/capi/obiview.pxd +++ b/python/obitools3/obidms/capi/obiview.pxd @@ -104,8 +104,6 @@ cdef extern from "obiview.h" nogil: int obi_select_line(Obiview_p view, index_t line_nb) int obi_select_lines(Obiview_p view, index_t* line_nbs) - - OBIDMS_column_p obi_view_clone_column(Obiview_p view, const_char_p column_name) OBIDMS_column_p obi_view_get_column(Obiview_p view, const_char_p column_name) diff --git a/src/obiview.c b/src/obiview.c index 3a3d4ac..e13266e 100644 --- a/src/obiview.c +++ b/src/obiview.c @@ -98,6 +98,24 @@ int create_obiview_file(int dms_file_descriptor); int update_lines(Obiview_p view, index_t line_count); +/** + * @brief Internal function to clone a column in the context of a view. + * + * Clones with the right line selection and replaces the cloned columns with the new ones in the view. + * If there is a line selection, all columns have to be cloned, otherwise only the column of interest is cloned. + * + * @param view A pointer on the view. + * @param column_name The name of the column in the view that should be cloned. + * + * @returns A pointer on the new column. + * @retval NULL if an error occurred. + * + * @since February 2016 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +OBIDMS_column_p clone_column_in_view(Obiview_p view, const char* column_name); + + /** * Internal function preparing to set a value in a column, in the context of a view. * @@ -289,6 +307,68 @@ int update_lines(Obiview_p view, index_t line_count) } +OBIDMS_column_p clone_column_in_view(Obiview_p view, const char* column_name) +{ + int i; + OBIDMS_column_p current_line_selection = NULL; + OBIDMS_column_p column; + OBIDMS_column_p column_buffer; + + // Check that the view is not read-only + if (view->read_only) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError trying to delete a column in a read-only view"); + return NULL; + } + + if (view->new_line_selection != NULL) // TODO Probably shouldn't happen, trigger error? + current_line_selection = view->new_line_selection; + else + current_line_selection = view->line_selection; + + for (i=0; i<(view->column_count); i++) + { + if ((current_line_selection != NULL) || (!(strcmp((((view->columns)[i])->header)->name, column_name)))) + { // Clone with the right line selection and replace (for all columns if there is a line selection) + + // Save pointer to close column after cloning + column_buffer = (view->columns)[i]; + + // Clone and replace the column in the view + (view->columns)[i] = obi_clone_column(view->dms, current_line_selection, (((view->columns)[i])->header)->name, (((view->columns)[i])->header)->version, 1); + if ((view->columns)[i] == NULL) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError cloning a column to replace in a view"); + return NULL; + } + + // Close old cloned column + obi_close_column(column_buffer); + + if (!(strcmp((((view->columns)[i])->header)->name, column_name))) + // Found the column to return + column = (view->columns)[i]; + } + } + + // Close old line selections + if (view->line_selection != NULL) + { + obi_close_column(view->line_selection); + view->line_selection = NULL; + } + if (view->new_line_selection != NULL) + { + obi_close_column(view->new_line_selection); + view->new_line_selection = NULL; + } + + return column; +} + + int prepare_to_set_value_in_column(Obiview_p view, OBIDMS_column_p* column_pp, index_t* line_nb_p) { // Check that the view is not read-only @@ -307,7 +387,7 @@ int prepare_to_set_value_in_column(Obiview_p view, OBIDMS_column_p* column_pp, i if (view->line_selection != NULL) (*line_nb_p) = *(((index_t*) ((view->line_selection)->data)) + (*line_nb_p)); - (*column_pp) = obi_view_clone_column(view, ((*column_pp)->header)->name); + (*column_pp) = clone_column_in_view(view, ((*column_pp)->header)->name); if ((*column_pp) == NULL) { obidebug(1, "\nError trying to clone a column to modify it"); @@ -774,68 +854,6 @@ int obi_view_add_column(Obiview_p view, } -OBIDMS_column_p obi_view_clone_column(Obiview_p view, const char* column_name) -{ - int i; - OBIDMS_column_p current_line_selection = NULL; - OBIDMS_column_p column; - OBIDMS_column_p column_buffer; - - // Check that the view is not read-only - if (view->read_only) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError trying to delete a column in a read-only view"); - return NULL; - } - - if (view->new_line_selection != NULL) // TODO Probably shouldn't happen, trigger error? - current_line_selection = view->new_line_selection; - else - current_line_selection = view->line_selection; - - for (i=0; i<(view->column_count); i++) - { - if ((current_line_selection != NULL) || (!(strcmp((((view->columns)[i])->header)->name, column_name)))) - { // Clone with the right line selection and replace (for all columns if there is a line selection) - - // Save pointer to close column after cloning - column_buffer = (view->columns)[i]; - - // Clone and replace the column in the view - (view->columns)[i] = obi_clone_column(view->dms, current_line_selection, (((view->columns)[i])->header)->name, (((view->columns)[i])->header)->version, 1); - if ((view->columns)[i] == NULL) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError cloning a column to replace in a view"); - return NULL; - } - - // Close old cloned column - obi_close_column(column_buffer); - - if (!(strcmp((((view->columns)[i])->header)->name, column_name))) - // Found the column to return - column = (view->columns)[i]; - } - } - - // Close old line selections - if (view->line_selection != NULL) - { - obi_close_column(view->line_selection); - view->line_selection = NULL; - } - if (view->new_line_selection != NULL) - { - obi_close_column(view->new_line_selection); - view->new_line_selection = NULL; - } - - return column; -} - - int obi_view_delete_column(Obiview_p view, const char* column_name) { int i; diff --git a/src/obiview.h b/src/obiview.h index cc7f366..50fcfa5 100644 --- a/src/obiview.h +++ b/src/obiview.h @@ -326,24 +326,6 @@ int obi_view_add_column(Obiview_p view, bool create); -/** - * @brief Clones a column in the context of a view. - * - * Clones with the right line selection and replaces the cloned columns with the new ones in the view. - * If there is a line selection, all columns have to be cloned, otherwise only the column of interest is cloned. - * - * @param view A pointer on the view. - * @param column_name The name of the column in the view that should be cloned. - * - * @returns A pointer on the new column. - * @retval NULL if an error occurred. - * - * @since February 2016 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -OBIDMS_column_p obi_view_clone_column(Obiview_p view, const char* column_name); - - /** * @brief Deletes a column from a view. *