From 767d9c78040366b1aa080c0efa0bf60def1fd078 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 25 Apr 2016 18:07:58 +0200 Subject: [PATCH] Reordered view functions for better coherence --- src/obiview.c | 359 +++++++++++++++++++++++++------------------------- src/obiview.h | 158 +++++++++++----------- 2 files changed, 258 insertions(+), 259 deletions(-) diff --git a/src/obiview.c b/src/obiview.c index 4d0b4a6..7820c76 100644 --- a/src/obiview.c +++ b/src/obiview.c @@ -307,53 +307,6 @@ int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p) * **********************************************************************/ - -Obiview_p obi_new_view_nuc_seqs(OBIDMS_p dms, const char* view_name, Obiview_p view_to_clone, index_t* line_selection, const char* comments) -{ - Obiview_p view; - - if (view_to_clone != NULL) - { // Check that the view to clone is already a NUC_SEQS view (TODO discuss possibility of transforming type of a view) - if (strcmp(view_to_clone->view_type, VIEW_TYPE_NUC_SEQS)) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "Trying to clone a non-NUC SEQS view to create a NUC SEQS view"); - return NULL; - } - } - - view = obi_new_view(dms, view_name, view_to_clone, line_selection, comments); - if (view== NULL) - return NULL; - - strcpy(view->view_type, VIEW_TYPE_NUC_SEQS); - - if (view_to_clone == NULL) - { - // Adding sequence column - if (obi_view_add_column(view, NUC_SEQUENCE_COLUMN, -1, OBI_SEQ, 0, 1, NUC_SEQUENCE_COLUMN, NUC_SEQUENCE_INDEXER, "Nucleotide sequences", true) < 0) - { - obidebug(1, "Error adding an obligatory column in a nucleotide sequences view"); - return NULL; - } - // Adding id column - if (obi_view_add_column(view, ID_COLUMN, -1, OBI_STR, 0, 1, ID_COLUMN, ID_INDEXER, "Ids", true) < 0) - { - obidebug(1, "Error adding an obligatory column in a nucleotide sequences view"); - return NULL; - } - // Adding definition column - if (obi_view_add_column(view, DEFINITION_COLUMN, -1, OBI_STR, 0, 1, DEFINITION_COLUMN, DEFINITION_INDEXER, "Definitions", true) < 0) - { - obidebug(1, "Error adding an obligatory column in a nucleotide sequences view"); - return NULL; - } - } - - return view; -} - - Obiview_p obi_new_view(OBIDMS_p dms, const char* view_name, Obiview_p view_to_clone, index_t* line_selection, const char* comments) { Obiview_p view; @@ -504,6 +457,52 @@ Obiview_p obi_new_view_cloned_from_name(OBIDMS_p dms, const char* view_name, con } +Obiview_p obi_new_view_nuc_seqs(OBIDMS_p dms, const char* view_name, Obiview_p view_to_clone, index_t* line_selection, const char* comments) +{ + Obiview_p view; + + if (view_to_clone != NULL) + { // Check that the view to clone is already a NUC_SEQS view (TODO discuss possibility of transforming type of a view) + if (strcmp(view_to_clone->view_type, VIEW_TYPE_NUC_SEQS)) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "Trying to clone a non-NUC SEQS view to create a NUC SEQS view"); + return NULL; + } + } + + view = obi_new_view(dms, view_name, view_to_clone, line_selection, comments); + if (view== NULL) + return NULL; + + strcpy(view->view_type, VIEW_TYPE_NUC_SEQS); + + if (view_to_clone == NULL) + { + // Adding sequence column + if (obi_view_add_column(view, NUC_SEQUENCE_COLUMN, -1, OBI_SEQ, 0, 1, NUC_SEQUENCE_COLUMN, NUC_SEQUENCE_INDEXER, "Nucleotide sequences", true) < 0) + { + obidebug(1, "Error adding an obligatory column in a nucleotide sequences view"); + return NULL; + } + // Adding id column + if (obi_view_add_column(view, ID_COLUMN, -1, OBI_STR, 0, 1, ID_COLUMN, ID_INDEXER, "Ids", true) < 0) + { + obidebug(1, "Error adding an obligatory column in a nucleotide sequences view"); + return NULL; + } + // Adding definition column + if (obi_view_add_column(view, DEFINITION_COLUMN, -1, OBI_STR, 0, 1, DEFINITION_COLUMN, DEFINITION_INDEXER, "Definitions", true) < 0) + { + obidebug(1, "Error adding an obligatory column in a nucleotide sequences view"); + return NULL; + } + } + + return view; +} + + Obiview_p obi_new_view_nuc_seqs_cloned_from_name(OBIDMS_p dms, const char* view_name, const char* view_to_clone_name, index_t* line_selection, const char* comments) { Obiview_p view; @@ -683,113 +682,6 @@ Obiview_p obi_open_view(OBIDMS_p dms, const char* view_name) } -Obiviews_infos_all_p obi_read_view_infos(OBIDMS_p dms) -{ - char* view_file_name; - int obiview_file_descriptor; - size_t header_size; - Obiviews_header_p header; - Obiview_infos_p view_infos; - Obiviews_infos_all_p views; - - view_file_name = build_obiview_file_name(); - if (view_file_name == NULL) - return NULL; - - // Open view file, read header size and map header and views - obiview_file_descriptor = openat(dms->dir_fd, view_file_name, O_RDWR, 0777); - if (obiview_file_descriptor < 0) - { // No views yet - free(view_file_name); - return NULL; - } - - free(view_file_name); - - // Read the header size - if (read(obiview_file_descriptor, &header_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t))) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError reading the header size of an obiview file (trying to open a view when there are none?)"); - close(obiview_file_descriptor); - return NULL; - } - - // Map the header - header = mmap(NULL, - header_size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - obiview_file_descriptor, - 0 - ); - if (header == MAP_FAILED) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError mmapping an obiview file header"); - close(obiview_file_descriptor); - return NULL; - } - - // Map the views - view_infos = mmap(NULL, - header->views_size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - obiview_file_descriptor, - header_size - ); - if (view_infos == MAP_FAILED) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError mmapping the views from an obiview file"); - munmap(header, header_size); - close(obiview_file_descriptor); - return NULL; - } - - views = (Obiviews_infos_all_p) malloc(sizeof(Obiviews_infos_all_t)); - if (views == NULL) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError mmapping the views from an obiview file"); - munmap(view_infos, header->views_size); - munmap(header, header_size); - close(obiview_file_descriptor); - return NULL; - } - - views->header = header; - views->view_infos = view_infos; - - close(obiview_file_descriptor); - - return views; -} - - -int obi_close_view_infos(Obiviews_infos_all_p views) -{ - if (munmap(views->view_infos, (views->header)->views_size) < 0) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError unmapping the views of an obiview file"); - free(views); - return -1; - } - if (munmap(views->header, (views->header)->header_size) < 0) - { - obi_set_errno(OBIVIEW_ERROR); - obidebug(1, "\nError unmapping the header of an obiview file"); - free(views); - return -1; - - } - free(views); - return 0; -} - - int obi_view_add_column(Obiview_p view, const char* column_name, obiversion_t version_number, @@ -946,6 +838,32 @@ int obi_view_delete_column(Obiview_p view, const char* column_name) } +OBIDMS_column_p obi_view_get_column(Obiview_p view, const char* column_name) +{ + int i; + + for (i=0; i<(view->column_count); i++) + { + if (!(strcmp((((view->columns)[i])->header)->name, column_name))) + return (view->columns)[i]; + } + return NULL; +} + + +OBIDMS_column_p* obi_view_get_pointer_on_column_in_view(Obiview_p view, const char* column_name) +{ + int i; + + for (i=0; i<(view->column_count); i++) + { + if (!(strcmp((((view->columns)[i])->header)->name, column_name))) + return ((view->columns)+i); + } + return NULL; +} + + int obi_select_line(Obiview_p view, index_t line_nb) { // Check that the view is not read-only @@ -1038,32 +956,6 @@ int obi_view_update_lines(Obiview_p view, index_t line_count) } -OBIDMS_column_p obi_view_get_column(Obiview_p view, const char* column_name) -{ - int i; - - for (i=0; i<(view->column_count); i++) - { - if (!(strcmp((((view->columns)[i])->header)->name, column_name))) - return (view->columns)[i]; - } - return NULL; -} - - -OBIDMS_column_p* obi_view_get_pointer_on_column_in_view(Obiview_p view, const char* column_name) -{ - int i; - - for (i=0; i<(view->column_count); i++) - { - if (!(strcmp((((view->columns)[i])->header)->name, column_name))) - return ((view->columns)+i); - } - return NULL; -} - - int obi_save_view(Obiview_p view) { int i; @@ -1257,6 +1149,113 @@ int obi_save_and_close_view(Obiview_p view) } +Obiviews_infos_all_p obi_read_view_infos(OBIDMS_p dms) +{ + char* view_file_name; + int obiview_file_descriptor; + size_t header_size; + Obiviews_header_p header; + Obiview_infos_p view_infos; + Obiviews_infos_all_p views; + + view_file_name = build_obiview_file_name(); + if (view_file_name == NULL) + return NULL; + + // Open view file, read header size and map header and views + obiview_file_descriptor = openat(dms->dir_fd, view_file_name, O_RDWR, 0777); + if (obiview_file_descriptor < 0) + { // No views yet + free(view_file_name); + return NULL; + } + + free(view_file_name); + + // Read the header size + if (read(obiview_file_descriptor, &header_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t))) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError reading the header size of an obiview file (trying to open a view when there are none?)"); + close(obiview_file_descriptor); + return NULL; + } + + // Map the header + header = mmap(NULL, + header_size, + PROT_READ | PROT_WRITE, + MAP_SHARED, + obiview_file_descriptor, + 0 + ); + if (header == MAP_FAILED) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError mmapping an obiview file header"); + close(obiview_file_descriptor); + return NULL; + } + + // Map the views + view_infos = mmap(NULL, + header->views_size, + PROT_READ | PROT_WRITE, + MAP_SHARED, + obiview_file_descriptor, + header_size + ); + if (view_infos == MAP_FAILED) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError mmapping the views from an obiview file"); + munmap(header, header_size); + close(obiview_file_descriptor); + return NULL; + } + + views = (Obiviews_infos_all_p) malloc(sizeof(Obiviews_infos_all_t)); + if (views == NULL) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError mmapping the views from an obiview file"); + munmap(view_infos, header->views_size); + munmap(header, header_size); + close(obiview_file_descriptor); + return NULL; + } + + views->header = header; + views->view_infos = view_infos; + + close(obiview_file_descriptor); + + return views; +} + + +int obi_close_view_infos(Obiviews_infos_all_p views) +{ + if (munmap(views->view_infos, (views->header)->views_size) < 0) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError unmapping the views of an obiview file"); + free(views); + return -1; + } + if (munmap(views->header, (views->header)->header_size) < 0) + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nError unmapping the header of an obiview file"); + free(views); + return -1; + + } + free(views); + return 0; +} + + /*********** FOR BOOL COLUMNS ***********/ int obi_column_set_obibool_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx, obibool_t value) diff --git a/src/obiview.h b/src/obiview.h index 1b0095d..528593d 100644 --- a/src/obiview.h +++ b/src/obiview.h @@ -291,35 +291,6 @@ Obiview_p obi_new_view_nuc_seqs_cloned_from_name(OBIDMS_p dms, const char* view_ Obiview_p obi_open_view(OBIDMS_p dms, const char* view_name); -/** - * @brief Opens the structure containing all the informations written in the view file. - * - * @param dms A pointer on the OBIDMS to which the view file belongs. - * - * @returns A pointer on the view informations structure. - * @retval NULL if an error occurred. - * - * @since February 2016 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -Obiviews_infos_all_p obi_read_view_infos(OBIDMS_p dms); - - -/** - * @brief Closes the structure containing all the informations written in the view file. - * - * @param views A pointer on the view informations structure. - * - * @returns A value indicating the success of the operation. - * @retval 0 if the operation was successfully completed. - * @retval -1 if an error occurred. - * - * @since February 2016 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -int obi_close_view_infos(Obiviews_infos_all_p views); - - /** * @brief Adds a column to a view. * @@ -355,6 +326,24 @@ 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. * @@ -373,6 +362,38 @@ int obi_view_add_column(Obiview_p view, int obi_view_delete_column(Obiview_p view, const char* column_name); +/** + * @brief Gets the pointer on a column from its name in a view. + * + * @param view A pointer on the view. + * @param column_name The name of the column in the view. + * + * @returns A pointer on the column. + * @retval NULL if an error occurred. + * + * @since February 2016 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +OBIDMS_column_p obi_view_get_column(Obiview_p view, const char* column_name); + + +/** + * @brief Gets the pointer on the pointer on a column from its name in a view. + * + * Note: This is used to replace old cloned columns with new ones in views in layers above the C layer. + * + * @param view A pointer on the view. + * @param column_name The name of the column in the view. + * + * @returns A pointer on the pointer on the column. + * @retval NULL if an error occurred. + * + * @since February 2016 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +OBIDMS_column_p* obi_view_get_pointer_on_column_in_view(Obiview_p view, const char* column_name); + + /** * @brief Selects a line in the context of a view. * @@ -438,56 +459,6 @@ int obi_select_lines(Obiview_p view, index_t* line_nbs); int obi_view_update_lines(Obiview_p view, index_t line_count); -/** - * @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 Gets the pointer on a column from its name in a view. - * - * @param view A pointer on the view. - * @param column_name The name of the column in the view. - * - * @returns A pointer on the column. - * @retval NULL if an error occurred. - * - * @since February 2016 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -OBIDMS_column_p obi_view_get_column(Obiview_p view, const char* column_name); - - -/** - * @brief Gets the pointer on the pointer on a column from its name in a view. - * - * Note: This is used to replace old cloned columns with new ones in views in layers above the C layer. - * - * @param view A pointer on the view. - * @param column_name The name of the column in the view. - * - * @returns A pointer on the pointer on the column. - * @retval NULL if an error occurred. - * - * @since February 2016 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -OBIDMS_column_p* obi_view_get_pointer_on_column_in_view(Obiview_p view, const char* column_name); - - /** * @brief Saves a view, writing it in the view file. * @@ -539,6 +510,35 @@ int obi_close_view(Obiview_p view); int obi_save_and_close_view(Obiview_p view); +/** + * @brief Opens the structure containing all the informations written in the view file. + * + * @param dms A pointer on the OBIDMS to which the view file belongs. + * + * @returns A pointer on the view informations structure. + * @retval NULL if an error occurred. + * + * @since February 2016 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +Obiviews_infos_all_p obi_read_view_infos(OBIDMS_p dms); + + +/** + * @brief Closes the structure containing all the informations written in the view file. + * + * @param views A pointer on the view informations structure. + * + * @returns A value indicating the success of the operation. + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since February 2016 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +int obi_close_view_infos(Obiviews_infos_all_p views); + + // TODO in following functions would it be better to use column names instead of column pointers? // check if it would be a gain or loss of time