obiview.c 41.8 KB
Newer Older
Celine Mercier's avatar
Celine Mercier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/********************************************************************
 * Obiview functions                                                 *
 ********************************************************************/

/**
 * @file obiview.c
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 * @date 16 December 2015
 * @brief Obiview functions.
 */


#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/mman.h>

#include "obiview.h"
20
#include "obidms.h"
Celine Mercier's avatar
Celine Mercier committed
21 22
#include "obidmscolumn.h"
#include "obidmscolumn_idx.h"
23 24 25 26 27 28
#include "obidmscolumn_bool.h"
#include "obidmscolumn_char.h"
#include "obidmscolumn_float.h"
#include "obidmscolumn_int.h"
#include "obidmscolumn_seq.h"
#include "obidmscolumn_str.h"
29 30 31 32
#include "obierrno.h"
#include "obidebug.h"
#include "obilittlebigman.h"
#include "utils.h"
Celine Mercier's avatar
Celine Mercier committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


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


/**************************************************************************
 *
 * D E C L A R A T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 **************************************************************************/


/**
 * Internal function building the file name where obiviews are stored.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @returns A pointer to the file name.
 * @retval NULL if an error occurred.
 *
 * @param dms_name The name of the OBIDMS.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_obiview_file_name();


61
/**	TODO public?
Celine Mercier's avatar
Celine Mercier committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
 * Internal function creating the file containing basic informations on the OBIDMS.
 *
 * This file contains:
 * 		- The endianness of the platform
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param dms_file_descriptor The file descriptor for the OBIDMS directory.
 * @param dms_name The name of the OBIDMS.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since November 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int create_obiview_file(int dms_file_descriptor);


81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/**
 * @brief Internal function to update the line count in the context of a view.
 *
 * All columns of the view are enlarged to contain at least the new line count.
 *
 * @warning The view must be writable.
 *
 * @param view A pointer on the view.
 * @param line_count The new line count.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since February 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int update_lines(Obiview_p view, index_t line_count);


101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/**
 * @brief Internal function to clone a column in the context of a view.
 *
 * Clones with the right line selection and replaces the cloned columns with the new ones in the view.
 * If there is a line selection, all columns have to be cloned, otherwise only the column of interest is cloned.
 *
 * @param view A pointer on the view.
 * @param column_name The name of the column in the view that should be cloned.
 *
 * @returns A pointer on the new column.
 * @retval NULL if an error occurred.
 *
 * @since February 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
OBIDMS_column_p clone_column_in_view(Obiview_p view, const char* column_name);


119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/**
 * Internal function preparing to set a value in a column, in the context of a view.
 *
 * The function checks that the view is not read-only, clones the column or all columns if needed,
 * and updates the line count if needed.
 *
 * @param view The view.
 * @param column_pp A pointer on the pointer on the column, to allow replacing the column if it is cloned.
 * @param line_nb_p A pointer on the index of the line that will be modified, to allow replacing it if needed.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
135 136 137
int prepare_to_set_value_in_column(Obiview_p view, OBIDMS_column_p* column_pp, index_t* line_nb_p);


138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/**
 * Internal function preparing to get a value from a column, in the context of a view.
 *
 * The function checks that the line index is not beyond the current line count of the view,
 * and modifies it if there is a line selection associated with the view.
 *
 * @param view The view.
 * @param line_nb_p A pointer on the index of the line, to allow replacing it if needed.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p);
154 155


Celine Mercier's avatar
Celine Mercier committed
156 157 158 159 160 161 162 163 164 165 166
/************************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 ************************************************************************/

static char* build_obiview_file_name()
{
	char* file_name;

	// Build file name
Celine Mercier's avatar
Celine Mercier committed
167
	file_name = (char*) malloc((strlen(OBIVIEW_FILE_NAME) + 1)*sizeof(char));
168 169 170 171 172 173
	if (file_name == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for a view file name");
		return NULL;
	}
Celine Mercier's avatar
Celine Mercier committed
174
	if (sprintf(file_name, OBIVIEW_FILE_NAME) < 0)
Celine Mercier's avatar
Celine Mercier committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nProblem building an obiview file name");
		return NULL;
	}

	return file_name;
}


size_t get_platform_header_size()
{
	size_t header_size;
	size_t rounded_header_size;
	double multiple;

	header_size = sizeof(Obiviews_header_t);

	multiple = 	ceil((double) header_size / (double) getpagesize());

	rounded_header_size = multiple * getpagesize();

	return rounded_header_size;
}


Celine Mercier's avatar
Celine Mercier committed
201
size_t get_platform_views_size(int nb_of_views)
Celine Mercier's avatar
Celine Mercier committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
{
	size_t obiview_size;
	size_t rounded_obiview_size;
	double multiple;

	obiview_size = sizeof(Obiview_infos_t);

	multiple = 	ceil((double) (obiview_size*nb_of_views) / (double) getpagesize());

	rounded_obiview_size = multiple * getpagesize();

	return rounded_obiview_size;
}


int create_obiview_file(int dms_file_descriptor)
{
	char* 				file_name;
	int 				obiview_file_descriptor;
	size_t  			header_size;
	size_t  			file_size;
	Obiviews_header_p 	header;

	// Create file name
	file_name = build_obiview_file_name();
	if (file_name == NULL)
		return -1;

    // Create file
	obiview_file_descriptor = openat(dms_file_descriptor, file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
	if (obiview_file_descriptor < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError creating an obiview file");
		free(file_name);
		return -1;
	}

	free(file_name);

	// Truncate file to the right size
	header_size = get_platform_header_size();
244
	file_size = header_size;
Celine Mercier's avatar
Celine Mercier committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

	if (ftruncate(obiview_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError truncating an obiview file to the right size");
		close(obiview_file_descriptor);
		return -1;
	}

	// Map the header
	header = mmap(NULL,
  	  	   	      header_size,
				  PROT_READ | PROT_WRITE,
				  MAP_SHARED,
				  obiview_file_descriptor,
				  0
 	 	   	   	 );
	if (header == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping an obiview file header");
		close(obiview_file_descriptor);
		return -1;
	}

	// Initialize the header
	header->header_size = header_size;
	header->views_size = 0;
	header->view_count = 0;

	// Unmap the header
	if (munmap(header, header_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError munmapping an obiview file header");
		close(obiview_file_descriptor);
		return -1;
	}

	close(obiview_file_descriptor);

	return 0;
}


290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
int update_lines(Obiview_p view, index_t line_count)
{
	int i;

	for (i=0; i<(view->column_count); i++)
	{
		while (line_count > (((view->columns)[i])->header)->line_count)
		{
			// Enlarge the column
			if (obi_enlarge_column((view->columns)[i]) < 0)
		    	return -1;
		}
	}

	view->line_count = line_count;

	return 0;
}


310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
OBIDMS_column_p clone_column_in_view(Obiview_p view, const char* column_name)
{
	int i;
	OBIDMS_column_p current_line_selection = NULL;
	OBIDMS_column_p column;
	OBIDMS_column_p column_buffer;

	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to delete a column in a read-only view");
		return NULL;
	}

	if (view->new_line_selection != NULL)	// TODO Probably shouldn't happen, trigger error?
		current_line_selection = view->new_line_selection;
	else
		current_line_selection = view->line_selection;

	for (i=0; i<(view->column_count); i++)
	{
		if ((current_line_selection != NULL) || (!(strcmp((((view->columns)[i])->header)->name, column_name))))
		{ // Clone with the right line selection and replace (for all columns if there is a line selection)

			// Save pointer to close column after cloning
			column_buffer = (view->columns)[i];

			// Clone and replace the column in the view
			(view->columns)[i] = obi_clone_column(view->dms, current_line_selection, (((view->columns)[i])->header)->name, (((view->columns)[i])->header)->version, 1);
			if ((view->columns)[i] == NULL)
			{
				obi_set_errno(OBIVIEW_ERROR);
				obidebug(1, "\nError cloning a column to replace in a view");
				return NULL;
			}

			// Close old cloned column
			obi_close_column(column_buffer);

			if (!(strcmp((((view->columns)[i])->header)->name, column_name)))
				// Found the column to return
				column = (view->columns)[i];
		}
	}

	// Close old line selections
	if (view->line_selection != NULL)
	{
		obi_close_column(view->line_selection);
		view->line_selection = NULL;
	}
	if (view->new_line_selection != NULL)
	{
		obi_close_column(view->new_line_selection);
		view->new_line_selection = NULL;
	}

	return column;
}


372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
int prepare_to_set_value_in_column(Obiview_p view, OBIDMS_column_p* column_pp, index_t* line_nb_p)
{
	// 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;
	}

	// If there is a line selection associated with the view or if the column
	// is read-only, all columns or this column respectively must be cloned
	if ((view->line_selection != NULL) || (!((*column_pp)->writable)))
	{
		// Get the right line number
		if (view->line_selection != NULL)
			(*line_nb_p) = *(((index_t*) ((view->line_selection)->data)) + (*line_nb_p));

390
		(*column_pp) = clone_column_in_view(view, ((*column_pp)->header)->name);
391 392 393 394 395 396 397 398 399
		if ((*column_pp) == NULL)
		{
			obidebug(1, "\nError trying to clone a column to modify it");
			return -1;
		}
	}

	if (((*line_nb_p)+1) > view->line_count)
	{
400
		if (update_lines(view, ((*line_nb_p)+1)) < 0)
401 402 403 404 405 406 407
			return -1;
	}

	return 0;
}


408
int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p)
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
{
	if (((*line_nb_p)+1) > (view->line_count))
	{
		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 -1;
	}

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

	return 0;
}


Celine Mercier's avatar
Celine Mercier committed
424 425 426 427 428 429
/**********************************************************************
 *
 * 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
430
Obiview_p obi_new_view(OBIDMS_p dms, const char* view_name, Obiview_p view_to_clone, index_t* line_selection, const char* comments)
Celine Mercier's avatar
Celine Mercier committed
431 432 433 434
{
	Obiview_p view;
	int i;
	index_t line_nb;
435 436
	Obiviews_infos_all_p views_infos;

437
	// Check uniqueness of name TODO but problem if view not written yet has the same name. Save lists of open views in DMS ?
438
	views_infos = obi_read_view_infos(dms);
439 440 441 442 443 444 445 446
	if (views_infos != NULL)
	{
		for (i=0; i<((views_infos->header)->view_count); i++)
		{
			if (strcmp(((views_infos->view_infos)+i)->name, view_name) == 0)		// TODO discuss what to do
			{
				obi_set_errno(OBIVIEW_ERROR);
				obidebug(1, "\nView name already exists for a previous view");
447
				obi_close_view_infos(views_infos);
448 449 450
				return NULL;
			}
		}
451
	obi_close_view_infos(views_infos);
452
	}
Celine Mercier's avatar
Celine Mercier committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

	view = (Obiview_p) malloc(sizeof(Obiview_t));
	if (view == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError allocating memory for a view");
		return NULL;
	}

	// Clone view to clone if there is one
	if (view_to_clone != NULL)
	{
		if (!(view_to_clone->read_only))
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nA view can not be cloned if it is not read-only");
			free(view);
			return NULL;
		}

Celine Mercier's avatar
Celine Mercier committed
473
		view->dms = dms;
Celine Mercier's avatar
Celine Mercier committed
474
		view->column_count = view_to_clone->column_count;
475 476 477

		// If the view to clone has an associated line selection and there is no new line selection, open the associated line selection
		if ((view_to_clone->line_selection != NULL) && (line_selection == NULL))
Celine Mercier's avatar
Celine Mercier committed
478 479 480 481 482 483
		{
			view->line_selection = obi_open_column(dms, ((view_to_clone->line_selection)->header)->name, ((view_to_clone->line_selection)->header)->version);
			if (view->line_selection == NULL)
				return NULL;
			view->line_count = view_to_clone->line_count;
		}
484
		// If there is a new line selection, build it by combining it with the one from the view to clone if there is one
Celine Mercier's avatar
Celine Mercier committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
		else if (line_selection != NULL)
		{
			view->line_selection = obi_create_column(view->dms, LINES_COLUMN_NAME, OBI_IDX, 0, 1, LINES_COLUMN_NAME, NULL, NULL);
			if ((view->line_selection) == NULL)
			{
				obidebug(1, "\nError creating a column corresponding to a line selection");
				return NULL;
			}
			view->line_count = 0;
			i = 0;
			for (i=0; line_selection[i] != -1; i++)
			{
				line_nb = line_selection[i];

				if (line_nb > view_to_clone->line_count)
				{
					obi_set_errno(OBIVIEW_ERROR);
					obidebug(1, "\nError trying to select a line for a new view that is beyond the line count of the view to clone");
					obi_close_column(view->line_selection);
					free(view);
					return NULL;
				}

				if (view_to_clone->line_selection != NULL)
					line_nb = obi_column_get_index(view_to_clone->line_selection, line_nb);

				if (obi_column_set_index(view->line_selection, ((view->line_selection)->header)->lines_used, line_nb) < 0)
				{
					obi_close_column(view->line_selection);
					free(view);
					return NULL;
				}

				// Update view line count
				(view->line_count)++;
			}
		}
522
		else	// If there is no line selection associated with the view to clone or the new view
Celine Mercier's avatar
Celine Mercier committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		{
			view->line_selection = NULL;
			view->line_count = view_to_clone->line_count;
		}

		for (i=0; i<(view_to_clone->column_count); i++)
		{
			(view->columns)[i] = obi_open_column(dms, (((view_to_clone->columns)[i])->header)->name, (((view_to_clone->columns)[i])->header)->version);
			if ((view->columns)[i] == NULL)
			{
				obi_close_column(view->line_selection);
				free(view);
				return NULL;
			}
		}

539
		strcpy(view->view_type, view_to_clone->view_type);
Celine Mercier's avatar
Celine Mercier committed
540 541 542 543 544 545 546 547 548 549 550
		strcpy(view->created_from, view_to_clone->name);
		view->new_line_selection = NULL;
	}
	// Else, fill empty view structure
	else
	{
		view->dms                = dms;
		view->column_count       = 0;
		view->line_count         = 0;
		view->line_selection     = NULL;
		view->new_line_selection = NULL;
551 552
		(view->created_from)[0]  = '\0';
		(view->view_type)[0]     = '\0';
553
		//view->columns          = NULL;  // TODO
Celine Mercier's avatar
Celine Mercier committed
554 555 556
	}

	strcpy(view->name, view_name);
Celine Mercier's avatar
Celine Mercier committed
557
	strcpy(view->comments, comments);
Celine Mercier's avatar
Celine Mercier committed
558 559 560 561 562 563
	view->read_only = 0;

	return view;
}


Celine Mercier's avatar
Celine Mercier committed
564
Obiview_p obi_new_view_cloned_from_name(OBIDMS_p dms, const char* view_name, const char* view_to_clone_name, index_t* line_selection, const char* comments)
Celine Mercier's avatar
Celine Mercier committed
565 566 567 568 569 570 571
{
	Obiview_p view;
	Obiview_p view_to_clone;

	view_to_clone = obi_open_view(dms, view_to_clone_name);
	if (view_to_clone == NULL)
		return NULL;
Celine Mercier's avatar
Celine Mercier committed
572
	view = obi_new_view(dms, view_name, view_to_clone, line_selection, comments);
573 574 575 576 577 578 579

	obi_close_view(view_to_clone);

	return view;
}


580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
Obiview_p obi_new_view_nuc_seqs(OBIDMS_p dms, const char* view_name, Obiview_p view_to_clone, index_t* line_selection, const char* comments)
{
	Obiview_p view;

	if (view_to_clone != NULL)
	{ // Check that the view to clone is already a NUC_SEQS view (TODO discuss possibility of transforming type of a view)
		if (strcmp(view_to_clone->view_type, VIEW_TYPE_NUC_SEQS))
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "Trying to clone a non-NUC SEQS view to create a NUC SEQS view");
			return NULL;
		}
	}

	view = obi_new_view(dms, view_name, view_to_clone, line_selection, comments);
	if (view== NULL)
		return NULL;

	strcpy(view->view_type, VIEW_TYPE_NUC_SEQS);

	if (view_to_clone == NULL)
	{
		// Adding sequence column
		if (obi_view_add_column(view, NUC_SEQUENCE_COLUMN, -1, OBI_SEQ, 0, 1, NUC_SEQUENCE_COLUMN, NUC_SEQUENCE_INDEXER, "Nucleotide sequences", true) < 0)
		{
			obidebug(1, "Error adding an obligatory column in a nucleotide sequences view");
			return NULL;
		}
		// Adding id column
		if (obi_view_add_column(view, ID_COLUMN, -1, OBI_STR, 0, 1, ID_COLUMN, ID_INDEXER, "Ids", true) < 0)
		{
			obidebug(1, "Error adding an obligatory column in a nucleotide sequences view");
			return NULL;
		}
		// Adding definition column
		if (obi_view_add_column(view, DEFINITION_COLUMN, -1, OBI_STR, 0, 1, DEFINITION_COLUMN, DEFINITION_INDEXER, "Definitions", true) < 0)
		{
			obidebug(1, "Error adding an obligatory column in a nucleotide sequences view");
			return NULL;
		}
	}

	return view;
}


Celine Mercier's avatar
Celine Mercier committed
626
Obiview_p obi_new_view_nuc_seqs_cloned_from_name(OBIDMS_p dms, const char* view_name, const char* view_to_clone_name, index_t* line_selection, const char* comments)
627 628 629 630 631 632 633
{
	Obiview_p view;
	Obiview_p view_to_clone;

	view_to_clone = obi_open_view(dms, view_to_clone_name);
	if (view_to_clone == NULL)
		return NULL;
Celine Mercier's avatar
Celine Mercier committed
634
	view = obi_new_view_nuc_seqs(dms, view_name, view_to_clone, line_selection, comments);
635 636 637

	obi_close_view(view_to_clone);

Celine Mercier's avatar
Celine Mercier committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	return view;
}


Obiview_p obi_open_view(OBIDMS_p dms, const char* view_name)
{
	Obiview_p			view;
	char*       		view_file_name;
	int 				obiview_file_descriptor;
	size_t  			header_size;
	Obiviews_header_p 	header;
	Obiview_infos_p 	views;
	Obiview_infos_p     view_infos;
	OBIDMS_column_p     column;
	int					i;
	int 				view_number;

	view_file_name = build_obiview_file_name();
	if (view_file_name == NULL)
		return NULL;

	// Open view file, read header size and map header and views
	obiview_file_descriptor = openat(dms->dir_fd, view_file_name, O_RDWR, 0777);
	if (obiview_file_descriptor < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError opening an obiview file");
		free(view_file_name);
		return NULL;
	}

	free(view_file_name);

	// Read the header size
	if (read(obiview_file_descriptor, &header_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t)))
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError reading the header size of an obiview file (trying to open a view when there are none?)");
		close(obiview_file_descriptor);
		return NULL;
	}

	// Map the header
	header = mmap(NULL,
				  header_size,
				  PROT_READ | PROT_WRITE,
				  MAP_SHARED,
				  obiview_file_descriptor,
				  0
 	 	   	   	 );
	if (header == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping an obiview file header");
		close(obiview_file_descriptor);
		return NULL;
	}

	// Map the views
	views = mmap(NULL,
				 header->views_size,
				 PROT_READ | PROT_WRITE,
				 MAP_SHARED,
				 obiview_file_descriptor,
				 header_size
 	 	   	   	);
	if (views == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping the views from an obiview file");
		munmap(header, header_size);
		close(obiview_file_descriptor);
		return NULL;
	}

713
	// Find and open the view that should be read with the line selection associated
Celine Mercier's avatar
Celine Mercier committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	view_number = -1;
	if (view_name == NULL)	// If view name is NULL, open the latest view TODO discuss
		view_number = (header->view_count) - 1;
	else
	{
		for (i=0; i<(header->view_count); i++)
		{
			if (!strcmp((views+i)->name, view_name))
			{	// Found the view to open
				view_number = i;
			}
		}
	}

	if (view_number == -1)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError: the view '%s' to open was not found", view_name);
		munmap(views, header->views_size);
		munmap(header, header_size);
		close(obiview_file_descriptor);
		return NULL;
	}

	view_infos = views + view_number;

	view = (Obiview_p) malloc(sizeof(Obiview_t));
	if (view == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError allocating memory for a view");
		munmap(views, header->views_size);
		munmap(header, header_size);
		close(obiview_file_descriptor);
		return NULL;
	}

	if (view_infos->all_lines)
		view->line_selection = NULL;
	else
	{
		view->line_selection = obi_open_column(dms, (view_infos->line_selection).column_name, (view_infos->line_selection).version);
		if (view->line_selection == NULL)
		{
			munmap(views, header->views_size);
			munmap(header, header_size);
			close(obiview_file_descriptor);
			return NULL;
		}
	}

	view->dms = dms;
	view->new_line_selection = NULL;
	view->read_only = 1;
	view->column_count = 0;
	view->line_count = view_infos->line_count;
	strcpy(view->created_from, view_infos->created_from);
	strcpy(view->name, view_infos->name);
772
	strcpy(view->view_type, view_infos->view_type);
Celine Mercier's avatar
Celine Mercier committed
773
	strcpy(view->comments, view_infos->comments);
Celine Mercier's avatar
Celine Mercier committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

	// Open the columns to read
	for (i=0; i<(view_infos->column_count); i++)
	{
		column = obi_open_column(dms, ((view_infos->column_references)+i)->column_name, ((view_infos->column_references)+i)->version);
		if (column == NULL)
		{
			obidebug(1, "\nError opening a column for a view: column %d: %s, version %d", i, ((view_infos->column_references)+i)->column_name, ((view_infos->column_references)+i)->version);
			munmap(views, header->views_size);
			munmap(header, header_size);
			close(obiview_file_descriptor);
			return NULL;
		}
		// Store column in the view
		(view->columns)[view->column_count] = column;
		view->column_count++;
	}

792
	// Munmap and close things
Celine Mercier's avatar
Celine Mercier committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	munmap(views, header->views_size);
	munmap(header, header_size);
	close(obiview_file_descriptor);

	return view;
}


int obi_view_add_column(Obiview_p view,
						const char* column_name,
						obiversion_t version_number,
						OBIType_t data_type,
						index_t nb_lines,
						index_t nb_elements_per_line,
						const char* elements_names,
Celine Mercier's avatar
Celine Mercier committed
808
						const char* indexer_name,
Celine Mercier's avatar
Celine Mercier committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
						const char* comments,
						bool create)	// all infos for creation or open
{
	OBIDMS_column_p column;

	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to add a column in a read-only view");
		return -1;
	}

	if (view->line_count > nb_lines)
		nb_lines = view->line_count;
	else if (nb_lines > view->line_count)
825
		update_lines(view, nb_lines);
Celine Mercier's avatar
Celine Mercier committed
826 827 828 829

	// Open or create the column
	if (create)
	{	// Create column
Celine Mercier's avatar
Celine Mercier committed
830
		column = obi_create_column(view->dms, column_name, data_type, nb_lines, nb_elements_per_line, elements_names, indexer_name, comments);
Celine Mercier's avatar
Celine Mercier committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	}
	else
	{ // Open column
		column = obi_open_column(view->dms, column_name, version_number);
	}

	if (column == NULL)
	{
		obidebug(1, "\nError creating or opening a column to add to a view");
		return -1;
	}

	// Store column in the view
	(view->columns)[view->column_count] = column;
	view->column_count++;
	if (view->column_count == 1)	// first column in the view
		view->line_count = (column->header)->lines_used;

	return 0;
}


int obi_view_delete_column(Obiview_p view, const char* column_name)
{
	int  i;
	bool found;

858 859
	// TODO check that not deleting an obligatory column?

Celine Mercier's avatar
Celine Mercier committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873
	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to delete a column in a read-only view");
		return -1;
	}

	found = 0;

	for (i=0; i<(view->column_count); i++)
	{
		if (!strcmp((((view->columns)[i])->header)->name, column_name))
		{
874
			obi_close_column((view->columns)[i]);
Celine Mercier's avatar
Celine Mercier committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
			found = 1;
		}
		if (found)
		{
			if (i != ((view->column_count) - 1))	// not the last one
				(view->columns)[i] = (view->columns)[i+1];
			else  	// Last column
				(view->columns)[i] = NULL;
		}
	}

	if (!found)
		return -1;

	(view->column_count)--;

	return 0;
}


895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
OBIDMS_column_p obi_view_get_column(Obiview_p view, const char* column_name)
{
	int i;

	for (i=0; i<(view->column_count); i++)
	{
		if (!(strcmp((((view->columns)[i])->header)->name, column_name)))
			return (view->columns)[i];
	}
	return NULL;
}


OBIDMS_column_p* obi_view_get_pointer_on_column_in_view(Obiview_p view, const char* column_name)
{
	int i;

	for (i=0; i<(view->column_count); i++)
	{
		if (!(strcmp((((view->columns)[i])->header)->name, column_name)))
			return ((view->columns)+i);
	}
	return NULL;
}


Celine Mercier's avatar
Celine Mercier committed
921 922 923 924 925 926 927 928 929 930
int obi_select_line(Obiview_p view, index_t line_nb)
{
	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to select a line in a read-only view");
		return -1;
	}

931
	// If the column for line selection doesn't already exists, create it and store its informations
Celine Mercier's avatar
Celine Mercier committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	if ((view->new_line_selection) == NULL)
	{
		view->new_line_selection = obi_create_column(view->dms, LINES_COLUMN_NAME, OBI_IDX, 0, 1, LINES_COLUMN_NAME, NULL, NULL);
		if ((view->new_line_selection) == NULL)
		{
			obidebug(1, "\nError creating a column corresponding to a line selection");
			return -1;
		}
	}

	// If we are already working on a line selection, get the pointed line number
	if (view->line_selection)
		line_nb = obi_column_get_index(view->line_selection, line_nb);

	if (obi_column_set_index(view->new_line_selection, ((view->new_line_selection)->header)->lines_used, line_nb) < 0)
		return -1;

	return 0;
}


int obi_select_lines(Obiview_p view, index_t* line_nbs)
{
	int i;
	index_t line_nb;

	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to select a line in a read-only view");
		return -1;
	}

	// If column for line selection doesn't already exists, create it and store its informations
	if ((view->new_line_selection) == NULL)
	{
		view->new_line_selection = obi_create_column(view->dms, LINES_COLUMN_NAME, OBI_IDX, 0, 1, LINES_COLUMN_NAME, NULL, NULL);
		if ((view->new_line_selection) == NULL)
		{
			obidebug(1, "\nError creating a column corresponding to a line selection");
			return -1;
		}
	}

	for (i=0; line_nbs[i] != -1; i++)
	{
		line_nb = line_nbs[i];

		// If we are already working on a line selection, get the pointed line number
		if (view->line_selection)
			line_nb = obi_column_get_index(view->line_selection, line_nb);

		if (obi_column_set_index(view->new_line_selection, ((view->new_line_selection)->header)->lines_used, line_nb) < 0)
			return -1;
	}

	return 0;
}


int obi_save_view(Obiview_p view)
{
	int i;
    struct stat 		buffer;
	char*       		view_file_name;
	char*  				full_path;
    int         		check_file;
	int 				obiview_file_descriptor;
	size_t  			header_size;
	size_t				views_size;
	size_t  			file_size;
	Obiviews_header_p 	header;
	Obiview_infos_p 	views;
	Obiview_infos_p     view_infos;
	OBIDMS_column_p     current_line_selection;

	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to save a read-only view");
		return -1;
	}

	view_file_name = build_obiview_file_name();
	if (view_file_name == NULL)
		return -1;

	// Get the full path for the column directory
1022
	full_path = obi_dms_get_full_path(view->dms, view_file_name);
Celine Mercier's avatar
Celine Mercier committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	if (full_path == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError getting the full path of an obiview file");
		free(view_file_name);
		return -1;
	}

	check_file = stat(full_path, &buffer);
    if (check_file < 0)
    { // 1st view: create view file
    	if (create_obiview_file((view->dms)->dir_fd) < 0)
    		return -1;
    }

	// Open view file, read header size and map header and views
	obiview_file_descriptor = openat((view->dms)->dir_fd, view_file_name, O_RDWR, 0777);
	if (obiview_file_descriptor < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError opening an obiview file");
		free(view_file_name);
		return -1;
	}

	free(view_file_name);

	// Read the header size
	if (read(obiview_file_descriptor, &header_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t)))
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError reading the header size of an obiview file");
		close(obiview_file_descriptor);
		return -1;
	}

	// Map the header
	header = mmap(NULL,
				  header_size,
				  PROT_READ | PROT_WRITE,
				  MAP_SHARED,
				  obiview_file_descriptor,
				  0
 	 	   	   	 );
	if (header == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping an obiview file header");
		close(obiview_file_descriptor);
		return -1;
	}

	// Truncate file to the right size to add a new view // TODO lock for multithreading
	views_size = get_platform_views_size((header->view_count)+1);
	file_size = header_size + views_size;

	if (ftruncate(obiview_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError truncating an obiview file to the right size");
		munmap(header, header_size);
		close(obiview_file_descriptor);
		return -1;
	}

	header->views_size = views_size;

	// Map the views
	views = mmap(NULL,
				 header->views_size,
				 PROT_READ | PROT_WRITE,
				 MAP_SHARED,
				 obiview_file_descriptor,
				 header_size
 	 	   	   	);
	if (views == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping the views from an obiview file");
		munmap(header, header_size);
		close(obiview_file_descriptor);
		return -1;
	}

    // Write view infos
	view_infos = views + (header->view_count);
	view_infos->view_number = (header->view_count);
	view_infos->column_count = view->column_count;
	view_infos->creation_date = time(NULL);
	strcpy(view_infos->created_from, view->created_from);
	strcpy(view_infos->name, view->name);
1114
	strcpy(view_infos->view_type, view->view_type);
Celine Mercier's avatar
Celine Mercier committed
1115
	strcpy(view_infos->comments, view->comments);
1116

Celine Mercier's avatar
Celine Mercier committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	// Store reference for the line selection associated with that view
	if (view->new_line_selection != NULL)
	{
		view_infos->line_count = ((view->new_line_selection)->header)->lines_used;
		current_line_selection = view->new_line_selection;
	}
	else
	{
		view_infos->line_count = view->line_count;
		current_line_selection = view->line_selection;
	}
	if (current_line_selection != NULL)
	{
		view_infos->all_lines = 0;
		strcpy((view_infos->line_selection).column_name, (current_line_selection->header)->name);
		(view_infos->line_selection).version = (current_line_selection->header)->version;
	}
	else
	{
		view_infos->all_lines = 1;
		((view_infos->line_selection).column_name)[0] = '\0';
		(view_infos->line_selection).version = -1;
	}
	for (i=0; i<view->column_count; i++)
	{
		strcpy(((view_infos->column_references)+i)->column_name, (((view->columns)[i])->header)->name);
		((view_infos->column_references)+i)->version = (((view->columns)[i])->header)->version;
	}

	// Increment view count
	(header->view_count)++;

	// Unmap and close file
	munmap(views, header->views_size);
	munmap(header, header_size);
	close(obiview_file_descriptor);

	return 0;
}


int obi_close_view(Obiview_p view)
{
	int i;

	for (i=0; i < (view->column_count); i++)
	{
		obi_close_column((view->columns)[i]);
	}

	free(view);

	return 0;
}


int obi_save_and_close_view(Obiview_p view)
{
1175
	if (!(view->read_only))
Celine Mercier's avatar
Celine Mercier committed
1176 1177 1178 1179 1180 1181 1182 1183 1184
	{
		if (obi_save_view(view) < 0)
			return -1;
	}
	if (obi_close_view(view) < 0)
		return -1;
	return 0;
}

1185

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
Obiviews_infos_all_p obi_read_view_infos(OBIDMS_p dms)
{
	char*       		 view_file_name;
	int 				 obiview_file_descriptor;
	size_t  			 header_size;
	Obiviews_header_p 	 header;
	Obiview_infos_p 	 view_infos;
	Obiviews_infos_all_p views;

	view_file_name = build_obiview_file_name();
	if (view_file_name == NULL)
		return NULL;

	// Open view file, read header size and map header and views
	obiview_file_descriptor = openat(dms->dir_fd, view_file_name, O_RDWR, 0777);
	if (obiview_file_descriptor < 0)
	{	// No views yet
		free(view_file_name);
		return NULL;
	}

	free(view_file_name);

	// Read the header size
	if (read(obiview_file_descriptor, &header_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t)))
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError reading the header size of an obiview file (trying to open a view when there are none?)");
		close(obiview_file_descriptor);
		return NULL;
	}

	// Map the header
	header = mmap(NULL,
				  header_size,
				  PROT_READ | PROT_WRITE,
				  MAP_SHARED,
				  obiview_file_descriptor,
				  0
 	 	   	   	 );
	if (header == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping an obiview file header");
		close(obiview_file_descriptor);
		return NULL;
	}

	// Map the views
	view_infos = mmap(NULL,
				 header->views_size,
				 PROT_READ | PROT_WRITE,
				 MAP_SHARED,
				 obiview_file_descriptor,
				 header_size
 	 	   	   	);
	if (view_infos == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping the views from an obiview file");
		munmap(header, header_size);
		close(obiview_file_descriptor);
		return NULL;
	}

	views = (Obiviews_infos_all_p) malloc(sizeof(Obiviews_infos_all_t));
	if (views == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError mmapping the views from an obiview file");
		munmap(view_infos, header->views_size);
		munmap(header, header_size);
		close(obiview_file_descriptor);
		return NULL;
	}

	views->header = header;
	views->view_infos = view_infos;

	close(obiview_file_descriptor);

	return views;
}


int obi_close_view_infos(Obiviews_infos_all_p views)
{
	if (munmap(views->view_infos, (views->header)->views_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError unmapping the views of an obiview file");
		free(views);
		return -1;
	}
	if (munmap(views->header, (views->header)->header_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError unmapping the header of an obiview file");
		free(views);
		return -1;

	}
	free(views);
	return 0;
}


1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
/*********** FOR BOOL COLUMNS ***********/

int obi_column_set_obibool_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx, obibool_t value)
{
	if (prepare_to_set_value_in_column(view, &column, &line_nb) < 0)
		return -1;
	return obi_column_set_obibool_with_elt_idx(column, line_nb, element_idx, value);
}


obibool_t obi_column_get_obibool_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx)
{
1305
	if (prepare_to_get_value_from_column(view, &line_nb) < 0)
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
		return OBIBool_NA;
	return obi_column_get_obibool_with_elt_idx(column, line_nb, element_idx);
}


int obi_column_set_obibool_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name, obibool_t value)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;
	return obi_column_set_obibool_with_elt_idx_in_view(view, column, line_nb, element_idx, value);
}


obibool_t obi_column_get_obibool_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIBool_NA;
	return obi_column_get_obibool_with_elt_idx_in_view(view, column, line_nb, element_idx);
}

/****************************************/


/*********** FOR CHAR COLUMNS ***********/

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)
{
	if (prepare_to_set_value_in_column(view, &column, &line_nb) < 0)
		return -1;
	return obi_column_set_obichar_with_elt_idx(column, line_nb, element_idx, value);
}


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)
{
1343
	if (prepare_to_get_value_from_column(view, &line_nb) < 0)
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
		return OBIChar_NA;
	return obi_column_get_obichar_with_elt_idx(column, line_nb, element_idx);
}


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 = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;
	return obi_column_set_obichar_with_elt_idx_in_view(view, column, line_nb, element_idx, value);
}


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 = 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);
}

/****************************************/


/*********** FOR FLOAT COLUMNS ***********/

int obi_column_set_obifloat_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx, obifloat_t value)
{
	if (prepare_to_set_value_in_column(view, &column, &line_nb) < 0)
		return -1;
	return obi_column_set_obifloat_with_elt_idx(column, line_nb, element_idx, value);
}


obifloat_t obi_column_get_obifloat_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx)
{
1381
	if (prepare_to_get_value_from_column(view, &line_nb) < 0)
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
		return OBIFloat_NA;
	return obi_column_get_obifloat_with_elt_idx(column, line_nb, element_idx);
}


int obi_column_set_obifloat_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name, obifloat_t value)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;
	return obi_column_set_obifloat_with_elt_idx_in_view(view, column, line_nb, element_idx, value);
}


obifloat_t obi_column_get_obifloat_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIFloat_NA;
	return obi_column_get_obifloat_with_elt_idx_in_view(view, column, line_nb, element_idx);
}

/****************************************/


/*********** FOR INT COLUMNS ***********/

int obi_column_set_obiint_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx, obiint_t value)
{
	if (prepare_to_set_value_in_column(view, &column, &line_nb) < 0)
		return -1;
	return obi_column_set_obiint_with_elt_idx(column, line_nb, element_idx, value);
}


obiint_t obi_column_get_obiint_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx)
{
1419
	if (prepare_to_get_value_from_column(view, &line_nb) < 0)
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
		return OBIInt_NA;
	return obi_column_get_obiint_with_elt_idx(column, line_nb, element_idx);
}


int obi_column_set_obiint_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name, obiint_t value)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;
	return obi_column_set_obiint_with_elt_idx_in_view(view, column, line_nb, element_idx, value);
}


obiint_t obi_column_get_obiint_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIInt_NA;
	return obi_column_get_obiint_with_elt_idx_in_view(view, column, line_nb, element_idx);
}

/****************************************/


/*********** FOR SEQ COLUMNS ***********/

int obi_column_set_obiseq_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx, const char* value)
{
	if (prepare_to_set_value_in_column(view, &column, &line_nb) < 0)
		return -1;
	return obi_column_set_obiseq_with_elt_idx(column, line_nb, element_idx, value);
}


char* obi_column_get_obiseq_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx)
{
1457
	if (prepare_to_get_value_from_column(view, &line_nb) < 0)
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
		return OBISeq_NA;
	return obi_column_get_obiseq_with_elt_idx(column, line_nb, element_idx);
}


int obi_column_set_obiseq_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name, const char* value)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;
	return obi_column_set_obiseq_with_elt_idx_in_view(view, column, line_nb, element_idx, value);
}


char* obi_column_get_obiseq_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBISeq_NA;
	return obi_column_get_obiseq_with_elt_idx_in_view(view, column, line_nb, element_idx);
}

/****************************************/


/*********** FOR STR COLUMNS ***********/

int obi_column_set_obistr_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx, const char* value)
{
	if (prepare_to_set_value_in_column(view, &column, &line_nb) < 0)
		return -1;
	return obi_column_set_obistr_with_elt_idx(column, line_nb, element_idx, value);
}


const char* obi_column_get_obistr_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, index_t element_idx)
{
1495
	if (prepare_to_get_value_from_column(view, &line_nb) < 0)
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
		return OBIStr_NA;
	return obi_column_get_obistr_with_elt_idx(column, line_nb, element_idx);
}


int obi_column_set_obistr_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name, const char* value)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return -1;
	return obi_column_set_obistr_with_elt_idx_in_view(view, column, line_nb, element_idx, value);
}


const char* obi_column_get_obistr_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p column, index_t line_nb, const char* element_name)
{
	index_t element_idx = obi_column_get_element_index_from_name(column, element_name);
	if (element_idx == OBIIdx_NA)
		return OBIStr_NA;
	return obi_column_get_obistr_with_elt_idx_in_view(view, column, line_nb, element_idx);
}

/****************************************/