obidmscolumn_char.c 3.56 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, size_t line_nb, size_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, size_t line_nb, size_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

69
	return *(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
70 71
}

72

73
int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name, obichar_t value)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
{
	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);
91
		if (element_idx == SIZE_MAX)	//TODO
92 93 94
			return -1;
    }

95
	obi_column_set_obichar_with_elt_idx(column, line_nb, element_idx, value);
96 97 98 99 100

	return 0;
}


101
obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, const char* element_name)
102 103 104 105 106 107 108 109 110 111 112
{
	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");
113
        	return OBIChar_NA;
114 115 116 117 118
		}
	}
    else
    {
		element_idx = obi_column_get_element_index_from_name(column, element_name);
119
		if (element_idx == SIZE_MAX)	//TODO
120
			return OBIChar_NA;
121 122
    }

123
	return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);
124
}
125