From ee4c513fd4281ff04f7ebcb3d82b9bdea15e8596 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Thu, 8 Oct 2015 10:36:02 +0200 Subject: [PATCH] Fixed a bug where cloning a column would fail if the data was empty --- src/obidmscolumn.c | 29 +++++++++++++++-------------- src/obidmscolumn.h | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index dc7178b..76939da 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -604,17 +604,17 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, size_t nb_elements_per_line, const char* elements_names) { - OBIDMS_column_p new_column; + OBIDMS_column_p new_column; OBIDMS_column_directory_p column_directory; - OBIDMS_column_header_p header; - size_t file_size; - obiversion_t version_number; - char* column_file_name; - int column_file_descriptor; - int column_dir_file_descriptor; - size_t header_size; - size_t data_size; - size_t minimum_line_count; + OBIDMS_column_header_p header; + size_t file_size; + obiversion_t version_number; + char* column_file_name; + int column_file_descriptor; + int column_dir_file_descriptor; + size_t header_size; + size_t data_size; + size_t minimum_line_count; new_column = NULL; @@ -1041,8 +1041,8 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece int column_file_descriptor; char* column_file_name; - // Compute the new line count = the number of lines used rounded to the nearest multiple of page size - multiple = ceil((double) ((column->header)->lines_used * (column->header)->nb_elements_per_line * obi_sizeof((column->header)->data_type)) / (double) getpagesize()); + // Compute the new line count = the number of lines used rounded to the nearest greater multiple of page size greater than 0 + multiple = ceil((double) (ONE_IF_ZERO((column->header)->lines_used) * (column->header)->nb_elements_per_line * obi_sizeof((column->header)->data_type)) / (double) getpagesize()); new_line_count = (int) multiple * getpagesize(); // Check that it is actually greater than the current number of lines allocated in the file, otherwise no need to truncate @@ -1558,13 +1558,14 @@ size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char size_t obi_column_get_nb_elements_per_line(OBIDMS_column_p column) { + // TODO check that column is not NULL? return (column->header)->nb_elements_per_line; } char* obi_column_get_formatted_creation_date(OBIDMS_column_p column) { - const char* formatted_time; + char* formatted_time; struct tm* tmp; formatted_time = (char*) malloc(FORMATTED_TIME_LENGTH*sizeof(char)); @@ -1597,7 +1598,7 @@ char* obi_column_get_formatted_creation_date_from_name(OBIDMS_p dms, const char* int column_dir_file_descriptor; size_t header_size; obiversion_t version_number; - const char* formatted_time; + char* formatted_time; struct tm* tmp; // Get the column directory structure associated to the column diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index ef3a246..d4ae30b 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -27,14 +27,16 @@ #include "obidmscolumndir.h" -#define ELEMENTS_NAMES_MAX (2048) /**< The maximum length of the list of elements names. - */ -#define GROWTH_FACTOR (2) /**< The growth factor when a column is enlarged. - */ -#define MAXIMUM_LINE_COUNT (1000000) /**< The maximum line count for the data of a column. //TODO - */ -#define FORMATTED_TIME_LENGTH (1024) /**< The length allocated for the character string containing a formatted date - */ +#define ONE_IF_ZERO(x) (((x)==0)?1:(x)) /**< If x is equal to 0, x takes the value 1. + */ +#define ELEMENTS_NAMES_MAX (2048) /**< The maximum length of the list of elements names. + */ +#define GROWTH_FACTOR (2) /**< The growth factor when a column is enlarged. + */ +#define MAXIMUM_LINE_COUNT (1000000) /**< The maximum line count for the data of a column. //TODO + */ +#define FORMATTED_TIME_LENGTH (1024) /**< The length allocated for the character string containing a formatted date + */ typedef int32_t obiversion_t; /**< Used to store the column version number */