obidmscolumn_char.c 3.43 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 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_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 63 64 65 66 67
	if ((line_nb+1) > (column->header)->line_count)
	{
		obidebug(1, "\nError trying to get a value that is beyond the current line count");
		return '\0'; // TODO return NA value?
	}
	return *(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
68 69
}

70

71
int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, size_t line_nb, char* element_name, obichar_t value)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
{
	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;
    }

93
	obi_column_set_obichar_with_elt_idx(column, line_nb, element_idx, value);
94 95 96 97 98

	return 0;
}


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

121
	return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);
122
}
123