Makes few cleanning and adds an obi_dms C constructor

This commit is contained in:
2015-05-26 21:36:55 +02:00
parent 9f69f76704
commit d1324618b8
4 changed files with 90 additions and 53 deletions

View File

@ -7,6 +7,14 @@
#include <obidms.h>
/***********************************************************************
*
* 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) {

View File

@ -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
* `<name>.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
* `<name>.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).
*

View File

@ -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);
}

View File

@ -23,22 +23,6 @@
#include <obierrno.h>
#include <obilittlebigman.h>
/**
* @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_ */