Commit 5c3bc03b by Celine Mercier

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

column
parent 0e50fbf7
cdef extern from *:
ctypedef char* const_char_p "const char*"
cdef extern from "obierrno.h" nogil:
extern int obi_errno
cdef extern from "obidms.h" nogil:
struct OBIDMS_t:
pass
......
......@@ -6,8 +6,11 @@ from .capidmscolumn_bool cimport *
cdef class OBIDMS_column_bool(OBIDMS_column) :
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) :
......
......@@ -6,7 +6,10 @@ from .capidmscolumn_char cimport *
cdef class OBIDMS_column_char(OBIDMS_column) :
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) :
......
......@@ -6,7 +6,10 @@ from .capidmscolumn_float cimport *
cdef class OBIDMS_column_float(OBIDMS_column) :
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) :
......
......@@ -6,7 +6,10 @@ from .capidmscolumn_idx cimport *
cdef class OBIDMS_column_idx(OBIDMS_column) :
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) :
......
......@@ -6,8 +6,11 @@ from .capidmscolumn_int cimport *
cdef class OBIDMS_column_int(OBIDMS_column) :
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) :
......
......@@ -603,7 +603,18 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
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
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);
// 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);
close(column_file_descriptor);
......@@ -1146,7 +1157,7 @@ int obi_enlarge_column(OBIDMS_column_p column)
PROT_READ | PROT_WRITE,
MAP_SHARED,
column_file_descriptor,
(column->header)->header_size
header_size
);
if (column->data == MAP_FAILED)
......
......@@ -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)
{
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");
return -1; // TODO return NA value?
obi_set_errno(OBICOL_UNKNOWN_ERROR);
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);
}
......@@ -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);
obidebug(1, "\nAn element name must be specified");
return -1;
return OBIBool_NA;
}
}
else
{
element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1)
return -1;
return OBIBool_NA;
}
return obi_column_get_obibool_with_elt_idx(column, line_nb, element_idx);
......
......@@ -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)
{
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");
return '\0'; // TODO return NA value?
obi_set_errno(OBICOL_UNKNOWN_ERROR);
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);
}
......@@ -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);
obidebug(1, "\nAn element name must be specified");
return '\0'; // TODO
return OBIChar_NA;
}
}
else
{
element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1)
return '\0'; // TODO
return OBIChar_NA;
}
return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);
......
......@@ -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)
{
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");
return -1; // TODO return NA value?
obi_set_errno(OBICOL_UNKNOWN_ERROR);
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);
}
......@@ -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);
obidebug(1, "\nAn element name must be specified");
return -1;
return OBIFloat_NA;
}
}
else
{
element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1)
return -1;
return OBIFloat_NA;
}
return obi_column_get_obifloat_with_elt_idx(column, line_nb, element_idx);
......
......@@ -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)
{
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");
return -1; // TODO return NA value?
obi_set_errno(OBICOL_UNKNOWN_ERROR);
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);
}
......@@ -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);
obidebug(1, "\nAn element name must be specified");
return -1;
return OBIIdx_NA;
}
}
else
{
element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1)
return -1;
return OBIIdx_NA;
}
return obi_column_get_obiidx_with_elt_idx(column, line_nb, element_idx);
......
......@@ -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)
{
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");
return -1; // TODO return NA value?
obi_set_errno(OBICOL_UNKNOWN_ERROR);
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);
}
......@@ -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);
obidebug(1, "\nAn element name must be specified");
return -1;
return OBIInt_NA;
}
}
else
{
element_idx = obi_column_get_element_index_from_name(column, element_name);
if (element_idx == -1)
return -1;
return OBIInt_NA;
}
return obi_column_get_obiint_with_elt_idx(column, line_nb, element_idx);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment