The endianness of binary taxonomy files is now correctly checked

This commit is contained in:
Celine Mercier
2016-10-10 17:04:29 +02:00
parent 0faaac49cf
commit 0dfd67ec89
2 changed files with 50 additions and 27 deletions

View File

@ -18,6 +18,7 @@
#include "obidms_taxonomy.h"
#include "obidms.h"
#include "obilittlebigman.h" // TODO the function from this checking the endianness does not seem to work properly
#include "obidebug.h"
#include "obierrno.h"
#include "utils.h"
@ -25,7 +26,23 @@
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
// TODO : the malloc aren't checked but won't exist for long because mapping instead
// TODO : the malloc aren't checked but shouldn't exist for long because mapping instead
// error checking and file closing in general aren't done properly yet
// The endianness eventually shouldn't need checking too, as the machine will write the taxonomy with its endianness.
int32_t is_big_endian()
{
int32_t i=1;
return (int32_t)((char*)&i)[0];
}
int32_t swap_int32_t(int32_t i)
{
return SWAPINT32(i);
}
int compareRankLabel(const void *label1, const void *label2)
@ -61,22 +78,23 @@ void* read_ecorecord(FILE* f, int32_t* record_size)
}
read = fread(record_size,
1,
sizeof(int32_t),
1,
f);
if (feof(f))
return NULL;
if (read != sizeof(int32_t))
if (read != 1)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError reading a taxonomy file: error reading record size");
return NULL;
}
// if (is_big_endian()) // TODO
// *recordSize=swap_int32_t(*recordSize);
// if (!(obi_is_little_endian())) // TODO
if (is_big_endian())
*record_size=swap_int32_t(*record_size);
if (buffer_size < *record_size)
{
@ -93,11 +111,11 @@ void* read_ecorecord(FILE* f, int32_t* record_size)
}
read = fread(buffer,
1,
*record_size,
1,
f);
if (read != *record_size)
if (read != 1)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError reading a taxonomy file: error reading a record %d, %d", read, *record_size);
@ -118,13 +136,14 @@ ecotx_t* readnext_ecotaxon(FILE* f, ecotx_t* taxon)
if (!raw)
return NULL;
// if (is_big_endian()) // TODO
// {
// raw->namelength = swap_int32_t(raw->namelength);
// raw->parent = swap_int32_t(raw->parent);
// raw->rank = swap_int32_t(raw->rank);
// raw->taxid = swap_int32_t(raw->taxid);
// }
// if (!(obi_is_little_endian())) // TODO
if (is_big_endian())
{
raw->name_length = swap_int32_t(raw->name_length);
raw->parent = swap_int32_t(raw->parent);
raw->rank = swap_int32_t(raw->rank);
raw->taxid = swap_int32_t(raw->taxid);
}
taxon->parent = (ecotx_t*) ((size_t) raw->parent);
taxon->taxid = raw->taxid;
@ -146,8 +165,6 @@ FILE* open_ecorecorddb(const char* file_name,
FILE* f;
int32_t read;
fprintf(stderr, "\n%s\n", file_name);
f = fopen(file_name, "rb");
if (!f)
@ -166,19 +183,20 @@ FILE* open_ecorecorddb(const char* file_name,
}
read = fread(count,
1,
sizeof(int32_t),
1,
f);
if (read != sizeof(int32_t))
if (read != 1)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError reading taxonomy record size");
return NULL;
}
// if (!obi_is_little_endian()) // TODO
// *count = swap_int32_t(*count);
// if (!(obi_is_little_endian())) // TODO
if (is_big_endian())
*count = swap_int32_t(*count);
return f;
}
@ -302,13 +320,14 @@ econame_t* readnext_econame(FILE* f, econame_t* name, OBIDMS_taxonomy_p taxonomy
if (!raw)
return NULL;
// if (is_big_endian()) // TODO
// {
// raw->is_scientificname = swap_int32_t(raw->is_scientificname);
// raw->namelength = swap_int32_t(raw->namelength);
// raw->classlength = swap_int32_t(raw->classlength);
// raw->taxid = swap_int32_t(raw->taxid);
// }
// if (!(obi_is_little_endian())) // TODO
if (is_big_endian())
{
raw->is_scientific_name = swap_int32_t(raw->is_scientific_name);
raw->name_length = swap_int32_t(raw->name_length);
raw->class_length = swap_int32_t(raw->class_length);
raw->taxid = swap_int32_t(raw->taxid);
}
name->is_scientific_name = raw->is_scientific_name;

View File

@ -17,6 +17,10 @@
#include "obidms.h"
#define SWAPINT32(x) ((((x) << 24) & 0xFF000000) | (((x) << 8) & 0xFF0000) | \
(((x) >> 8) & 0xFF00) | (((x) >> 24) & 0xFF))
typedef struct {
int32_t taxid;
int32_t rank;