obitypes.c 2.17 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 * OBITypes functions                                                       *
 ****************************************************************************/

/**
 * @file obitypes.h
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 * @date 23 May 2015
 * @brief Functions for the handling of OBITypes.
10 11
 */

12 13 14 15 16 17 18 19

#include <string.h>

#include "obitypes.h"
#include "obidebug.h"
#include "obierrno.h"


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

22

23 24
size_t obi_sizeof(OBIType_t type)
{
Eric Coissac's avatar
Eric Coissac committed
25
	size_t size=0;
26 27 28 29 30 31 32 33 34 35 36

	switch (type) {
	case OBI_VOID:  size = 1;
					break;

	case OBI_INT:   size = sizeof(obiint_t);
					break;

	case OBI_FLOAT: size = sizeof(obifloat_t);
					break;

37 38 39
	case OBI_BOOL:  size = sizeof(obibool_t);
					break;

40 41 42
	case OBI_CHAR:  size = sizeof(obichar_t);
					break;

43 44 45
	case OBI_QUAL:	size = sizeof(index_t);
					break;

46 47 48 49 50 51
	case OBI_STR:	size = sizeof(index_t);
					break;

	case OBI_SEQ:	size = sizeof(index_t);
					break;

52
	case OBI_IDX:	size = sizeof(index_t);
53 54 55 56 57 58 59 60 61
					break;

	default:        size = 0;
	}

	return size;
}


62
size_t obi_array_sizeof(OBIType_t type, index_t nb_lines, index_t nb_elements_per_line)
63
{
64 65 66 67
	size_t size;
	size_t rsize;
	size_t psize;

68
	size = obi_sizeof(type) * nb_lines * nb_elements_per_line;
69

70 71
	psize = getpagesize();
	rsize = size % psize;
72 73 74 75 76 77 78 79

	// Round at a size multiple of pagesize
	if (rsize)
		size = (size / psize) * psize + psize;

	return size;
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

char* name_data_type(int data_type)
{
	char* name = NULL;

	switch (data_type)
	{
		case OBI_VOID:  name = strdup("OBI_VOID");
						break;

		case OBI_INT:   name = strdup("OBI_INT");
						break;

		case OBI_FLOAT: name = strdup("OBI_FLOAT");
						break;

96 97 98
		case OBI_BOOL:  name = strdup("OBI_BOOL");
						break;

99 100 101
		case OBI_CHAR:  name = strdup("OBI_CHAR");
						break;

102 103 104
		case OBI_QUAL:  name = strdup("OBI_QUAL");
						break;

105 106 107 108
		case OBI_STR:   name = strdup("OBI_STR");
						break;

		case OBI_SEQ:   name = strdup("OBI_SEQ");
109
						break;
110 111 112

		case OBI_IDX:   name = strdup("OBI_IDX");
						break;
113 114 115 116 117 118 119 120 121 122 123
	}

	if (name == NULL)
		obidebug(1, "Problem with the data type");

	return name;
}