From d1324618b81cb50caedb8b20359e84d3b4bad18f Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Tue, 26 May 2015 21:36:55 +0200 Subject: [PATCH] Makes few cleanning and adds an obi_dms C constructor --- src/obidms.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/obidms.h | 40 +++++++++++++++++++++++++++++++++++++ src/obidmscolumn.c | 33 ------------------------------- src/obidmscolumn.h | 21 +------------------- 4 files changed, 90 insertions(+), 53 deletions(-) diff --git a/src/obidms.c b/src/obidms.c index 78c34a6..93b7a8b 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -7,6 +7,14 @@ #include + +/*********************************************************************** + * + * D E C L A R A T I O N O F T H E P R I V A T E F U N C T I O N S + * + ***********************************************************************/ + + /** * Internal function building the directory name from a OBIDMS name. * @@ -50,6 +58,33 @@ static char *build_directory_name(const char *name) { return dirdbname; } +/*********************************************************************** + * + * D E F I N I T I O N O F T H E P R I V A T E F U N C T I O N S + * + ***********************************************************************/ + +int obi_dms_exists(const char* name) { + struct stat buffer; + char *dirdbname; + int exist; + + // Build and check the directory name + dirdbname = build_directory_name(name); + if (dirdbname==NULL) + return -1; + + exist = stat(dirdbname,&buffer); + + free(dirdbname); + + if(exist == 0) + return 1; + else // -1 + return 0; +} + + OBIDMS_p obi_create_dms(const char *name) { char *dirdbname; @@ -127,6 +162,20 @@ OBIDMS_p obi_open_dms(const char *name) { return dms; } +OBIDMS_p obi_dms(const char *name) { + int exist = obi_dms_exists(name); + + switch (exist) { + case 0: + return obi_create_dms(name); + case 1: + return obi_open_dms(name); + }; + + return NULL; + +} + int obi_close_dms(OBIDMS_p dms) { diff --git a/src/obidms.h b/src/obidms.h index 37ed312..76260d0 100644 --- a/src/obidms.h +++ b/src/obidms.h @@ -61,6 +61,23 @@ typedef struct OBIDMS { */ /**@}*/ +/*@ + * @brief Check if a OBIDMS exist + * + * @param name a pointer to a C string containing the name of the database + * the actual directory name used to store the DMS will be + * `.obidms` + * + * @return an integer value indicating the status of the database + * @retvalue 1 the database exist + * @retvalue 0 the database does not exist + * @retvalue -1 an error occurs + * + * @see obi_close_dms() + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ +int obi_dms_exists(const char* name); /** * @brief Create a new OBITools Data Management instance (OBIDMS). @@ -108,6 +125,29 @@ OBIDMS_p obi_create_dms(const char *name); */ OBIDMS_p obi_open_dms(const char *name); +/** + * @brief Create a new OBIDMS instance. + * + * If the database already exist this function open it, otherwise it + * creates a new databse. + * + * @param name a pointer to a C string containing the name of the database + * the actual directory name used to store the DMS is + * `.obidms` + * + * @return a pointer to an OBIDMS structure describing the newly created DMS + * @retval NULL on error and the `obi_errno`variable is set. + * + * ###Error values + * - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit. + * - OBIDMS_MEMORY_ERROR : something wrong occurs during memory allocation. + * + * @see obi_close_dms() + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ +OBIDMS_p obi_dms(const char *name); + /** * @brief Close an opened OBITools Data Management instance (OBIDMS). * diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 8e81d1f..29dac2a 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -579,36 +579,3 @@ OBIDMSColumn_p obi_create_column(OBIDMS_p dms, // } -/************************************************************************ - ************************************************************************ - ************************************************************************/ - -/** - * @brief Reads a memory block from a file. - * - * * @param file The file that should be mapped. - * * @param size The number of bytes that should be mapped. - * * @param start The position at which the mapping should start. - * @return A pointer on the first element of the block. - */ -char* obi_map_read_only(int file, int start, int size) -{ - char* map; - map = (char*) mmap(0, size, PROT_READ, MAP_PRIVATE, file, start); - if (map == MAP_FAILED) - { - fprintf(stderr, "\nmmap read-only error\n"); - exit(1); - } - return(map); -} - -/** - * @brief Unmaps a memory block. - * - * * @param size The size of the block that should be unmapped. - */ -void obi_unmap(int size) -{ - munmap(0, size); -} diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index 61bd157..9328c09 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -23,22 +23,6 @@ #include #include -/** - * @brief Value separator in OBIDMSColumn files. - */ -static const char SEPARATOR = '\n'; - - -/** - * @brief Header size in OBIDMSColumn files. - */ -static const int HEADER_SIZE = 4096; - - -/** - * @brief Number of bytes to map - */ -static const int BUFFER_SIZE = 4; #define OBIDMS_MAX_COLNAME (128) /**< The maximum length of an OBIDMS column name */ @@ -63,7 +47,7 @@ typedef struct OBIDMSColumnHeader { OBIType_t data_type; /**< type of the data */ time_t creation_date; /**< date of creation of the file */ obiversion_t version; /**< version of the OBIColumn */ - char name[OBIDMS_MAX_COLNAME]; /**< The column name as a NULL + char name[OBIDMS_MAX_COLNAME+1]; /**< The column name as a NULL * terminated string. */ char comments[1]; /**< comments stored as a classical @@ -145,9 +129,6 @@ OBIDMSColumn_p obi_create_column(OBIDMS_p dms, */ obiversion_t obi_latest_version(OBIDMS_p dms,char *name); -char* obi_map_read_only(int file, int start, int size); - -void obi_unmap(int size); #endif /* OBIDMSCOLUMN_H_ */