obidmscolumn_str.c 3.02 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"
Celine Mercier's avatar
Celine Mercier committed
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
	index_t idx;
30
	char*   new_indexer_name;
31 32 33 34

	if (obi_column_prepare_to_set_value(column, line_nb) < 0)
		return -1;

35 36
	// TODO NA

37
	// Add the value in the indexer
Celine Mercier's avatar
Celine Mercier committed
38
	idx = obi_index_char_str(column->indexer, value);
39 40 41 42
	if (idx == -1)	// An error occurred
	{
		if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR)
		{
43
			// TODO PUT IN A COLUMN FUNCTION
44 45 46 47 48
			// If the error is that the indexer is read-only, clone it
			new_indexer_name = obi_build_indexer_name((column->header)->name, (column->header)->version);
			if (new_indexer_name == NULL)
				return -1;
			column->indexer = obi_clone_indexer(column->indexer, new_indexer_name);	// TODO Need to lock this somehow?
49 50 51 52
			strcpy((column->header)->indexer_name, new_indexer_name);
			free(new_indexer_name);
			obi_set_errno(0);

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

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

65 66 67 68
	return 0;
}


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

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

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

	// Check NA
	if (idx == OBIIdx_NA)
Celine Mercier's avatar
Celine Mercier committed
80
		return OBIStr_NA;
81

Celine Mercier's avatar
Celine Mercier committed
82
	return obi_retrieve_char_str(column->indexer, idx);
83 84
}

85

86
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's avatar
Celine Mercier committed
87
{
88
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
89
	if (element_idx == OBIIdx_NA)
90
		return -1;
Celine Mercier's avatar
Celine Mercier committed
91

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


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

102
	return obi_column_get_obistr_with_elt_idx(column, line_nb, element_idx);
Celine Mercier's avatar
Celine Mercier committed
103 104
}