obidmscolumn_char.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_char functions                                              *
 ****************************************************************************/

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


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

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


22
#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)
23 24


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_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obichar_t value)
32
{
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	// Check that the line number is not greater than the maximum allowed
	if (line_nb >= MAXIMUM_LINE_COUNT)
	{
		obi_set_errno(OBICOL_UNKNOWN_ERROR);
		obidebug(1, "\nError trying to set a value at a line number greater than the maximum allowed");
    	return -1;
	}

	// Check if the file needs to be enlarged
	while ((line_nb+1) > (column->header)->line_count)
	{
		// Enlarge the file
		if (obi_enlarge_column(column) < 0)
	    	return -1;
	}

49 50 51
	// Update lines used
	if ((line_nb+1) > (column->header)->lines_used)
		(column->header)->lines_used = line_nb+1;
52 53 54 55

	// Set the value
	*(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = value;

56 57 58 59
	return 0;
}


60
obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx)
61
{
62
	if ((line_nb+1) > (column->header)->lines_used)
63
	{
64 65 66
		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;
67 68
	}
	return *(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
69 70
}

71

72
int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obichar_t value)
73
{
74
	index_t element_idx;
75
	element_idx = obi_column_get_element_index_from_name(column, element_name);
76
	if (element_idx == OBIIdx_NA)
77
		return -1;
78
	obi_column_set_obichar_with_elt_idx(column, line_nb, element_idx, value);
79 80 81 82
	return 0;
}


83
obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name)
84
{
85
	index_t element_idx;
86

87
	element_idx = obi_column_get_element_index_from_name(column, element_name);
88
	if (element_idx == OBIIdx_NA)
89
		return OBIChar_NA;
90
	return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);
91
}
92