obidmscolumn_bool.c 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/****************************************************************************
 * OBIDMS_column_bool functions                                              *
 ****************************************************************************/

/**
 * @file obidsmcolumn_bool.c
 * @author Celine Mercier
 * @date August 10th 2015
 * @brief Functions handling OBIColumns containing data with the OBIType OBI_BOOL.
 */


#include <stdlib.h>
#include <stdio.h>

#include "obidmscolumn.h"
#include "obitypes.h"
#include "obierrno.h"
#include "obidebug.h"


22 23 24
#define DEBUG_LEVEL 0


25 26 27 28 29 30
/**********************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P U B L I C   F U N C T I O N S
 *
 **********************************************************************/

31
int obi_column_set_bool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx, obibool_t value)
32
{
33 34 35
	// Update lines used
	if ((line_nb+1) > (column->header)->lines_used)
		(column->header)->lines_used = line_nb+1;
36 37 38 39 40
	*(((obibool_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = value;
	return 0;
}


41
obibool_t obi_column_get_bool_with_elt_idx(OBIDMS_column_p column, size_t line_nb, size_t element_idx)
42 43 44 45
{
	return *(((obibool_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

int obi_column_set_bool_with_elt_name(OBIDMS_column_p column, size_t line_nb, char* element_name, obibool_t value)
{
	size_t element_idx;

	if (!strcmp(element_name, "\0"))	// element name is empty
	{
		if (obi_column_get_nb_elements_per_line(column) == 1)	// check that there is only one element per line
			element_idx = 0;
		else													// there is more than one element per line
		{
			obi_set_errno(OBICOL_UNKNOWN_ERROR);
			obidebug(1, "\nAn element name must be specified");
        	return -1;
		}
	}
    else
    {
		element_idx = obi_column_get_element_index_from_name(column, element_name);
		if (element_idx == -1)
			return -1;
    }

	obi_column_set_bool_with_elt_idx(column, line_nb, element_idx, value);

	return 0;
}


obibool_t obi_column_get_bool_with_elt_name(OBIDMS_column_p column, size_t line_nb, char* element_name)
{
	size_t element_idx;

	if (!strcmp(element_name, "\0"))	// element name is empty
	{
		if (obi_column_get_nb_elements_per_line(column) == 1)	// check that there is only one element per line
			element_idx = 0;
		else													// there is more than one element per line
		{
			obi_set_errno(OBICOL_UNKNOWN_ERROR);
			obidebug(1, "\nAn element name must be specified");
        	return -1;
		}
	}
    else
    {
		element_idx = obi_column_get_element_index_from_name(column, element_name);
		if (element_idx == -1)
			return -1;
    }

	return obi_column_get_bool_with_elt_idx(column, line_nb, element_idx);
}