From a08def47e6d7c0b56ea8a0b51a4b29714375f4d9 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Tue, 1 Mar 2016 13:36:54 +0100 Subject: [PATCH] It is now impossible to create a view with a name identical to one of an existing written view --- python/obitools3/obidms/_obidms.pyx | 7 +++- python/obitools3/obidms/capi/obiview.pxd | 2 ++ src/obiview.c | 42 ++++++++++++++++++++++++ src/obiview.h | 2 ++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/python/obitools3/obidms/_obidms.pyx b/python/obitools3/obidms/_obidms.pyx index aed4743..9eb4760 100644 --- a/python/obitools3/obidms/_obidms.pyx +++ b/python/obitools3/obidms/_obidms.pyx @@ -56,6 +56,7 @@ from .capi.obiview cimport Obiview_p, \ obi_new_view_nuc_seqs_cloned_from_name, \ obi_open_view, \ obi_read_views, \ + obi_unmap_read_views, \ obi_view_delete_column, \ obi_view_add_column, \ obi_view_get_column, \ @@ -682,7 +683,7 @@ cdef class OBIDMS : return all_views[view_name] - cpdef dict read_views(self) : # TODO function that gets 1 view with name, function that prints the dic and function that prints 1 view. Add column type in col ref + cpdef dict read_views(self) : # TODO function that prints the dic nicely and function that prints 1 view. Add column type in col ref cdef Obiviews_infos_all_p all_views_p cdef Obiview_infos_p view_p @@ -696,6 +697,8 @@ cdef class OBIDMS : views = {} all_views_p = obi_read_views(self.pointer) + if all_views_p == NULL : + raise Exception("No views to read") nb_views = (all_views_p.header).view_count for i in range(nb_views) : view_p = ( (all_views_p.view_infos)) + i @@ -721,6 +724,8 @@ cdef class OBIDMS : views[view_name]["column_references"][column_name] = {} views[view_name]["column_references"][column_name]["version"] = column_refs[j].version + obi_unmap_read_views(all_views_p); + return views diff --git a/python/obitools3/obidms/capi/obiview.pxd b/python/obitools3/obidms/capi/obiview.pxd index 29d9f37..5c41332 100644 --- a/python/obitools3/obidms/capi/obiview.pxd +++ b/python/obitools3/obidms/capi/obiview.pxd @@ -81,6 +81,8 @@ cdef extern from "obiview.h" nogil: Obiviews_infos_all_p obi_read_views(OBIDMS_p dms) + int obi_unmap_read_views(Obiviews_infos_all_p views) + int obi_view_add_column(Obiview_p view, const_char_p column_name, obiversion_t version_number, diff --git a/src/obiview.c b/src/obiview.c index a5410dd..c700646 100644 --- a/src/obiview.c +++ b/src/obiview.c @@ -224,6 +224,8 @@ Obiview_p obi_new_view_nuc_seqs(OBIDMS_p dms, const char* view_name, Obiview_p v } 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); @@ -258,6 +260,24 @@ Obiview_p obi_new_view(OBIDMS_p dms, const char* view_name, Obiview_p view_to_cl Obiview_p view; int i; index_t line_nb; + Obiviews_infos_all_p views_infos; + + // Check uniqueness of name TODO but problem if view not written yet has the same name + views_infos = obi_read_views(dms); + if (views_infos != NULL) + { + for (i=0; i<((views_infos->header)->view_count); i++) + { + if (strcmp(((views_infos->view_infos)+i)->name, view_name) == 0) // TODO discuss what to do + { + obi_set_errno(OBIVIEW_ERROR); + obidebug(1, "\nView name already exists for a previous view"); + obi_unmap_read_views(views_infos); + return NULL; + } + } + obi_unmap_read_views(views_infos); + } view = (Obiview_p) malloc(sizeof(Obiview_t)); if (view == NULL) @@ -646,6 +666,28 @@ Obiviews_infos_all_p obi_read_views(OBIDMS_p dms) } +int obi_unmap_read_views(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, diff --git a/src/obiview.h b/src/obiview.h index 9241dbc..89589e7 100644 --- a/src/obiview.h +++ b/src/obiview.h @@ -153,6 +153,8 @@ Obiview_p obi_open_view(OBIDMS_p dms, const char* view_name); Obiviews_infos_all_p obi_read_views(OBIDMS_p dms); +int obi_unmap_read_views(Obiviews_infos_all_p views); + int obi_view_add_column(Obiview_p view, const char* column_name, obiversion_t version_number,