obidmscolumn_char.c 4.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/****************************************************************************
 * 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"
Celine Mercier's avatar
Celine Mercier committed
17
#include "obiview.h"
18 19 20 21 22
#include "obitypes.h"
#include "obierrno.h"
#include "obidebug.h"


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


26 27 28 29 30 31
/**********************************************************************
 *
 * 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
 *
 **********************************************************************/

Celine Mercier's avatar
Celine Mercier committed
32

33
int obi_column_set_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, obichar_t value)
34
{
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	// 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;
	}

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

	// Set the value
Celine Mercier's avatar
Celine Mercier committed
56
	*(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = value;
57

58 59 60 61
	return 0;
}


Celine Mercier's avatar
Celine Mercier committed
62
int obi_column_set_obichar_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx, obichar_t value)
63
{
Celine Mercier's avatar
Celine Mercier committed
64 65 66 67 68 69 70
	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to set a value in a column in a read-only view");
		return -1;
	}
71

Celine Mercier's avatar
Celine Mercier committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	if ((view->line_selection != NULL) || (!(column->writable)))
	{
		// Get the right line number
		if (column->writable)
			line_nb = *(((index_t*) ((view->line_selection)->data)) + line_nb);

		column = obi_view_clone_column(view, (column->header)->name);
		if (column == NULL)
		{
			obidebug(1, "\nError trying to clone a column to modify it");
			return -1;
		}
	}

	if ((line_nb+1) > view->line_count)
	{
		if (obi_view_update_lines(view, (line_nb+1)) < 0)
			return -1;
	}

	if (obi_column_set_obichar_with_elt_idx(column, line_nb, element_idx, value) < 0)
		return -1;

	return 0;
}


obichar_t obi_column_get_obichar_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx)
{
	if ((line_nb+1) > ((column->header)->line_count))
102
	{
103
		obi_set_errno(OBICOL_UNKNOWN_ERROR);
Celine Mercier's avatar
Celine Mercier committed
104
		obidebug(1, "\nError trying to get a value that is beyond the current number of lines used in the column");
105
		return OBIChar_NA;
106
	}
107

Celine Mercier's avatar
Celine Mercier committed
108 109 110 111 112 113 114
	return *(((obichar_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx);
}


obichar_t obi_column_get_obichar_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx)
{
	if ((line_nb+1) > (view->line_count))
115
	{
Celine Mercier's avatar
Celine Mercier committed
116 117 118
		obi_set_errno(OBICOL_UNKNOWN_ERROR);
		obidebug(1, "\nError trying to get a value that is beyond the current line count of the view");
		return OBIChar_NA;
119
	}
Celine Mercier's avatar
Celine Mercier committed
120 121 122 123 124

	if (view->line_selection)
		line_nb = *(((index_t*) ((view->line_selection)->data)) + line_nb);

	return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);
125 126
}

127

128
int obi_column_set_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name, obichar_t value)
129
{
130
	index_t element_idx;
131
	element_idx = obi_column_get_element_index_from_name(column, element_name);
132
	if (element_idx == OBIIdx_NA)
133
		return -1;
134
	obi_column_set_obichar_with_elt_idx(column, line_nb, element_idx, value);
135 136 137 138
	return 0;
}


Celine Mercier's avatar
Celine Mercier committed
139 140 141 142 143 144 145 146 147 148 149
int obi_column_set_obichar_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name, obichar_t value)
{
	index_t element_idx;
	element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;
	obi_column_set_obichar_with_elt_idx_in_view(view, column, line_nb, element_idx, value);
	return 0;
}


150
obichar_t obi_column_get_obichar_with_elt_name(OBIDMS_column_p column, index_t line_nb, const char* element_name)
151
{
152
	index_t element_idx;
153

154
	element_idx = obi_column_get_element_index_from_name(column, element_name);
155
	if (element_idx == OBIIdx_NA)
156
		return OBIChar_NA;
157
	return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);
158
}
159

Celine Mercier's avatar
Celine Mercier committed
160 161 162 163 164 165 166 167 168 169

obichar_t obi_column_get_obichar_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name)
{
	index_t element_idx;

	element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIChar_NA;
	return obi_column_get_obichar_with_elt_idx_in_view(view, column, line_nb, element_idx);
}