obidmscolumn_str.c 2.76 KB
Newer Older
1
/****************************************************************************
2
 * OBIDMS_column_str functions                                              *
3 4 5
 ****************************************************************************/

/**
6
 * @file obidsmcolumn_str.c
7
 * @author Celine Mercier
8
 * @date October 28th 2015
9
 * @brief Functions handling OBIColumns containing data in the form of indices referring to character strings.
10 11 12 13 14 15 16 17
 */


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

#include "obidmscolumn.h"
#include "obitypes.h"
18
#include "char_str_indexer.h"
19 20 21 22 23 24 25 26


/**********************************************************************
 *
 * 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
 *
 **********************************************************************/

27
int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, const char* value)
28
{
29 30
	index_t idx;

31
	if (obi_column_prepare_to_set_value(column, line_nb, element_idx) < 0)
32 33
		return -1;

34 35 36 37 38
	if (value == OBIStr_NA)
	{
		idx = OBIIdx_NA;
	}
	else
39
	{
40 41 42
		// Add the value in the indexer
		idx = obi_index_char_str(column->indexer, value);
		if (idx == -1)	// An error occurred
43
		{
44 45 46
			if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR)
			{
				// If the error is that the indexer is read-only, clone it
Celine Mercier committed
47
				if (obi_clone_column_indexer(column) < 0)
48 49
					return -1;
				obi_set_errno(0);
50

51 52 53 54 55 56
				// Add the value in the new indexer
				idx = obi_index_char_str(column->indexer, value);
				if (idx == -1)
					return -1;
			}
			else
57 58 59
				return -1;
		}
	}
60 61

	// Add the value's index in the column
Celine Mercier committed
62
	*(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = idx;
63

64 65 66 67
	return 0;
}


68
const char* obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx)
69
{
70
	index_t idx;
71

72
	if (obi_column_prepare_to_get_value(column, line_nb) < 0)
73
		return OBIStr_NA;
74

Celine Mercier committed
75
	idx = *(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
76 77 78

	// Check NA
	if (idx == OBIIdx_NA)
Celine Mercier committed
79
		return OBIStr_NA;
80

81
	return obi_retrieve_char_str(column->indexer, idx);
82 83
}

84

85
int obi_column_set_obistr_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, const char* value)
Celine Mercier committed
86
{
87
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
88
	if (element_idx == OBIIdx_NA)
89
		return -1;
Celine Mercier committed
90

91
	return obi_column_set_obistr_with_elt_idx(column, line_nb, element_idx, value);
92 93 94
}


95
const char* obi_column_get_obistr_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name)
96
{
97
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
98
	if (element_idx == OBIIdx_NA)
Celine Mercier committed
99 100
		return OBIStr_NA;

101
	return obi_column_get_obistr_with_elt_idx(column, line_nb, element_idx);
Celine Mercier committed
102 103
}