Added the handling of errors with the functions to get a value in a

column
This commit is contained in:
Celine Mercier
2015-09-14 17:04:29 +02:00
parent 0e50fbf706
commit 5c3bc03bd2
12 changed files with 69 additions and 35 deletions

View File

@ -1,6 +1,9 @@
cdef extern from *: cdef extern from *:
ctypedef char* const_char_p "const char*" ctypedef char* const_char_p "const char*"
cdef extern from "obierrno.h" nogil:
extern int obi_errno
cdef extern from "obidms.h" nogil: cdef extern from "obidms.h" nogil:
struct OBIDMS_t: struct OBIDMS_t:
pass pass

View File

@ -6,8 +6,11 @@ from .capidmscolumn_bool cimport *
cdef class OBIDMS_column_bool(OBIDMS_column) : cdef class OBIDMS_column_bool(OBIDMS_column) :
def get_item(self, line_nb, element_name): def get_item(self, line_nb, element_name):
return obi_column_get_obibool_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8')) value = obi_column_get_obibool_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'))
if obi_errno > 0 :
raise IndexError(line_nb, element_name)
return value
cdef class OBIDMS_column_bool_read(OBIDMS_column_bool) : cdef class OBIDMS_column_bool_read(OBIDMS_column_bool) :

View File

@ -6,7 +6,10 @@ from .capidmscolumn_char cimport *
cdef class OBIDMS_column_char(OBIDMS_column) : cdef class OBIDMS_column_char(OBIDMS_column) :
def get_item(self, line_nb, element_name): def get_item(self, line_nb, element_name):
return (obi_column_get_obichar_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'))).decode(encoding='utf-8')[:1] value = (obi_column_get_obichar_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'))).decode(encoding='utf-8')[:1]
if obi_errno > 0 :
raise IndexError(line_nb, element_name)
return value
cdef class OBIDMS_column_char_read(OBIDMS_column_char) : cdef class OBIDMS_column_char_read(OBIDMS_column_char) :

View File

@ -6,7 +6,10 @@ from .capidmscolumn_float cimport *
cdef class OBIDMS_column_float(OBIDMS_column) : cdef class OBIDMS_column_float(OBIDMS_column) :
def get_item(self, line_nb, element_name): def get_item(self, line_nb, element_name):
return obi_column_get_obifloat_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8')) value = obi_column_get_obifloat_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'))
if obi_errno > 0 :
raise IndexError(line_nb, element_name)
return value
cdef class OBIDMS_column_float_read(OBIDMS_column_float) : cdef class OBIDMS_column_float_read(OBIDMS_column_float) :

View File

@ -6,7 +6,10 @@ from .capidmscolumn_idx cimport *
cdef class OBIDMS_column_idx(OBIDMS_column) : cdef class OBIDMS_column_idx(OBIDMS_column) :
def get_item(self, line_nb, element_name): def get_item(self, line_nb, element_name):
return obi_column_get_obiidx_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8')) value = obi_column_get_obiidx_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'))
if obi_errno > 0 :
raise IndexError(line_nb, element_name)
return value
cdef class OBIDMS_column_idx_read(OBIDMS_column_idx) : cdef class OBIDMS_column_idx_read(OBIDMS_column_idx) :

View File

@ -6,8 +6,11 @@ from .capidmscolumn_int cimport *
cdef class OBIDMS_column_int(OBIDMS_column) : cdef class OBIDMS_column_int(OBIDMS_column) :
def get_item(self, line_nb, element_name): def get_item(self, line_nb, element_name):
return obi_column_get_obiint_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8')) value = obi_column_get_obiint_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'))
if obi_errno > 0 :
raise IndexError(line_nb, element_name)
return value
cdef class OBIDMS_column_int_read(OBIDMS_column_int) : cdef class OBIDMS_column_int_read(OBIDMS_column_int) :

View File

@ -603,7 +603,18 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
new_column = NULL; new_column = NULL;
// TODO check that informations are not NULL/invalid? // Check that the informations given are not NULL/invalid/greater than the allowed sizes
if (dms == NULL)
{
obidebug(1, "\nCan't create column because of invalid DMS");
return NULL;
}
if (column_name == NULL)
{
obidebug(1, "\nCan't create column because of empty column name");
return NULL;
}
//if (type < 1)
// Get the column directory structure associated to the column // Get the column directory structure associated to the column
column_directory = obi_column_directory(dms, column_name); column_directory = obi_column_directory(dms, column_name);
@ -731,7 +742,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
strncpy(header->name, column_name, OBIDMS_MAX_COLNAME); strncpy(header->name, column_name, OBIDMS_MAX_COLNAME);
// Fill the data with NA values // Fill the data with NA values
obi_ini_to_NA_values (new_column, 0, nb_lines); obi_ini_to_NA_values(new_column, 0, nb_lines);
free(column_file_name); free(column_file_name);
close(column_file_descriptor); close(column_file_descriptor);
@ -1146,7 +1157,7 @@ int obi_enlarge_column(OBIDMS_column_p column)
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, MAP_SHARED,
column_file_descriptor, column_file_descriptor,
(column->header)->header_size header_size
); );
if (column->data == MAP_FAILED) if (column->data == MAP_FAILED)

View File

@ -59,10 +59,11 @@ int obi_column_set_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb,
obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) obibool_t obi_column_get_obibool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx)
{ {
if ((line_nb+1) > (column->header)->line_count) if ((line_nb+1) > (column->header)->lines_used)
{ {
obidebug(1, "\nError trying to get a value that is beyond the current line count"); obi_set_errno(OBICOL_UNKNOWN_ERROR);
return -1; // TODO return NA value? obidebug(1, "\nError trying to get a value that is beyond the current number of lines used");
return OBIBool_NA;
} }
return *(((obibool_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx); return *(((obibool_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
} }
@ -108,14 +109,14 @@ obibool_t obi_column_get_obibool_with_elt_name(OBIDMS_column_p column, size_t li
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nAn element name must be specified"); obidebug(1, "\nAn element name must be specified");
return -1; return OBIBool_NA;
} }
} }
else else
{ {
element_idx = obi_column_get_element_index_from_name(column, element_name); element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1) if (element_idx == -1)
return -1; return OBIBool_NA;
} }
return obi_column_get_obibool_with_elt_idx(column, line_nb, element_idx); return obi_column_get_obibool_with_elt_idx(column, line_nb, element_idx);

View File

@ -59,10 +59,11 @@ int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb,
obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx)
{ {
if ((line_nb+1) > (column->header)->line_count) if ((line_nb+1) > (column->header)->lines_used)
{ {
obidebug(1, "\nError trying to get a value that is beyond the current line count"); obi_set_errno(OBICOL_UNKNOWN_ERROR);
return '\0'; // TODO return NA value? obidebug(1, "\nError trying to get a value that is beyond the current number of lines used");
return OBIChar_NA;
} }
return *(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx); return *(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
} }
@ -108,14 +109,14 @@ obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, size_t li
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nAn element name must be specified"); obidebug(1, "\nAn element name must be specified");
return '\0'; // TODO return OBIChar_NA;
} }
} }
else else
{ {
element_idx = obi_column_get_element_index_from_name(column, element_name); element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1) if (element_idx == -1)
return '\0'; // TODO return OBIChar_NA;
} }
return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx); return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);

View File

@ -59,10 +59,11 @@ int obi_column_set_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb,
obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) obifloat_t obi_column_get_obifloat_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx)
{ {
if ((line_nb+1) > (column->header)->line_count) if ((line_nb+1) > (column->header)->lines_used)
{ {
obidebug(1, "\nError trying to get a value that is beyond the current line count"); obi_set_errno(OBICOL_UNKNOWN_ERROR);
return -1; // TODO return NA value? obidebug(1, "\nError trying to get a value that is beyond the current number of lines used");
return OBIFloat_NA;
} }
return *(((obifloat_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx); return *(((obifloat_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
} }
@ -108,14 +109,14 @@ obifloat_t obi_column_get_obifloat_with_elt_name(OBIDMS_column_p column, size_t
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nAn element name must be specified"); obidebug(1, "\nAn element name must be specified");
return -1; return OBIFloat_NA;
} }
} }
else else
{ {
element_idx = obi_column_get_element_index_from_name(column, element_name); element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1) if (element_idx == -1)
return -1; return OBIFloat_NA;
} }
return obi_column_get_obifloat_with_elt_idx(column, line_nb, element_idx); return obi_column_get_obifloat_with_elt_idx(column, line_nb, element_idx);

View File

@ -59,10 +59,11 @@ int obi_column_set_obiidx_with_elt_idx(OBIDMS_column_p column, size_t line_nb, s
obiidx_t obi_column_get_obiidx_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) obiidx_t obi_column_get_obiidx_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx)
{ {
if ((line_nb+1) > (column->header)->line_count) if ((line_nb+1) > (column->header)->lines_used)
{ {
obidebug(1, "\nError trying to get a value that is beyond the current line count"); obi_set_errno(OBICOL_UNKNOWN_ERROR);
return -1; // TODO return NA value? obidebug(1, "\nError trying to get a value that is beyond the current number of lines used");
return OBIIdx_NA;
} }
return *(((obiidx_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx); return *(((obiidx_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
} }
@ -108,14 +109,14 @@ obiidx_t obi_column_get_obiidx_with_elt_name(OBIDMS_column_p column, size_t line
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nAn element name must be specified"); obidebug(1, "\nAn element name must be specified");
return -1; return OBIIdx_NA;
} }
} }
else else
{ {
element_idx = obi_column_get_element_index_from_name(column, element_name); element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1) if (element_idx == -1)
return -1; return OBIIdx_NA;
} }
return obi_column_get_obiidx_with_elt_idx(column, line_nb, element_idx); return obi_column_get_obiidx_with_elt_idx(column, line_nb, element_idx);

View File

@ -59,10 +59,11 @@ int obi_column_set_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, s
obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx) obiint_t obi_column_get_obiint_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx)
{ {
if ((line_nb+1) > (column->header)->line_count) if ((line_nb+1) > (column->header)->lines_used)
{ {
obidebug(1, "\nError trying to get a value that is beyond the current line count"); obi_set_errno(OBICOL_UNKNOWN_ERROR);
return -1; // TODO return NA value? obidebug(1, "\nError trying to get a value that is beyond the current number of lines used");
return OBIInt_NA;
} }
return *(((obiint_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx); return *(((obiint_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
} }
@ -108,14 +109,14 @@ obiint_t obi_column_get_obiint_with_elt_name(OBIDMS_column_p column, size_t line
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nAn element name must be specified"); obidebug(1, "\nAn element name must be specified");
return -1; return OBIInt_NA;
} }
} }
else else
{ {
element_idx = obi_column_get_element_index_from_name(column, element_name); element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1) if (element_idx == -1)
return -1; return OBIInt_NA;
} }
return obi_column_get_obiint_with_elt_idx(column, line_nb, element_idx); return obi_column_get_obiint_with_elt_idx(column, line_nb, element_idx);