The endianness of binary taxonomy files is now correctly checked
This commit is contained in:
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include "obidms_taxonomy.h"
|
#include "obidms_taxonomy.h"
|
||||||
#include "obidms.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 "obidebug.h"
|
||||||
#include "obierrno.h"
|
#include "obierrno.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
@ -25,7 +26,23 @@
|
|||||||
|
|
||||||
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
|
#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)
|
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,
|
read = fread(record_size,
|
||||||
1,
|
|
||||||
sizeof(int32_t),
|
sizeof(int32_t),
|
||||||
|
1,
|
||||||
f);
|
f);
|
||||||
|
|
||||||
if (feof(f))
|
if (feof(f))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (read != sizeof(int32_t))
|
if (read != 1)
|
||||||
{
|
{
|
||||||
obi_set_errno(OBI_TAXONOMY_ERROR);
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
||||||
obidebug(1, "\nError reading a taxonomy file: error reading record size");
|
obidebug(1, "\nError reading a taxonomy file: error reading record size");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (is_big_endian()) // TODO
|
// if (!(obi_is_little_endian())) // TODO
|
||||||
// *recordSize=swap_int32_t(*recordSize);
|
if (is_big_endian())
|
||||||
|
*record_size=swap_int32_t(*record_size);
|
||||||
|
|
||||||
if (buffer_size < *record_size)
|
if (buffer_size < *record_size)
|
||||||
{
|
{
|
||||||
@ -93,11 +111,11 @@ void* read_ecorecord(FILE* f, int32_t* record_size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
read = fread(buffer,
|
read = fread(buffer,
|
||||||
1,
|
|
||||||
*record_size,
|
*record_size,
|
||||||
|
1,
|
||||||
f);
|
f);
|
||||||
|
|
||||||
if (read != *record_size)
|
if (read != 1)
|
||||||
{
|
{
|
||||||
obi_set_errno(OBI_TAXONOMY_ERROR);
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
||||||
obidebug(1, "\nError reading a taxonomy file: error reading a record %d, %d", read, *record_size);
|
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)
|
if (!raw)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// if (is_big_endian()) // TODO
|
// if (!(obi_is_little_endian())) // TODO
|
||||||
// {
|
if (is_big_endian())
|
||||||
// raw->namelength = swap_int32_t(raw->namelength);
|
{
|
||||||
// raw->parent = swap_int32_t(raw->parent);
|
raw->name_length = swap_int32_t(raw->name_length);
|
||||||
// raw->rank = swap_int32_t(raw->rank);
|
raw->parent = swap_int32_t(raw->parent);
|
||||||
// raw->taxid = swap_int32_t(raw->taxid);
|
raw->rank = swap_int32_t(raw->rank);
|
||||||
// }
|
raw->taxid = swap_int32_t(raw->taxid);
|
||||||
|
}
|
||||||
|
|
||||||
taxon->parent = (ecotx_t*) ((size_t) raw->parent);
|
taxon->parent = (ecotx_t*) ((size_t) raw->parent);
|
||||||
taxon->taxid = raw->taxid;
|
taxon->taxid = raw->taxid;
|
||||||
@ -146,8 +165,6 @@ FILE* open_ecorecorddb(const char* file_name,
|
|||||||
FILE* f;
|
FILE* f;
|
||||||
int32_t read;
|
int32_t read;
|
||||||
|
|
||||||
fprintf(stderr, "\n%s\n", file_name);
|
|
||||||
|
|
||||||
f = fopen(file_name, "rb");
|
f = fopen(file_name, "rb");
|
||||||
|
|
||||||
if (!f)
|
if (!f)
|
||||||
@ -166,19 +183,20 @@ FILE* open_ecorecorddb(const char* file_name,
|
|||||||
}
|
}
|
||||||
|
|
||||||
read = fread(count,
|
read = fread(count,
|
||||||
1,
|
|
||||||
sizeof(int32_t),
|
sizeof(int32_t),
|
||||||
|
1,
|
||||||
f);
|
f);
|
||||||
|
|
||||||
if (read != sizeof(int32_t))
|
if (read != 1)
|
||||||
{
|
{
|
||||||
obi_set_errno(OBI_TAXONOMY_ERROR);
|
obi_set_errno(OBI_TAXONOMY_ERROR);
|
||||||
obidebug(1, "\nError reading taxonomy record size");
|
obidebug(1, "\nError reading taxonomy record size");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (!obi_is_little_endian()) // TODO
|
// if (!(obi_is_little_endian())) // TODO
|
||||||
// *count = swap_int32_t(*count);
|
if (is_big_endian())
|
||||||
|
*count = swap_int32_t(*count);
|
||||||
|
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@ -302,13 +320,14 @@ econame_t* readnext_econame(FILE* f, econame_t* name, OBIDMS_taxonomy_p taxonomy
|
|||||||
if (!raw)
|
if (!raw)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// if (is_big_endian()) // TODO
|
// if (!(obi_is_little_endian())) // TODO
|
||||||
// {
|
if (is_big_endian())
|
||||||
// raw->is_scientificname = swap_int32_t(raw->is_scientificname);
|
{
|
||||||
// raw->namelength = swap_int32_t(raw->namelength);
|
raw->is_scientific_name = swap_int32_t(raw->is_scientific_name);
|
||||||
// raw->classlength = swap_int32_t(raw->classlength);
|
raw->name_length = swap_int32_t(raw->name_length);
|
||||||
// raw->taxid = swap_int32_t(raw->taxid);
|
raw->class_length = swap_int32_t(raw->class_length);
|
||||||
// }
|
raw->taxid = swap_int32_t(raw->taxid);
|
||||||
|
}
|
||||||
|
|
||||||
name->is_scientific_name = raw->is_scientific_name;
|
name->is_scientific_name = raw->is_scientific_name;
|
||||||
|
|
||||||
|
@ -17,6 +17,10 @@
|
|||||||
#include "obidms.h"
|
#include "obidms.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define SWAPINT32(x) ((((x) << 24) & 0xFF000000) | (((x) << 8) & 0xFF0000) | \
|
||||||
|
(((x) >> 8) & 0xFF00) | (((x) >> 24) & 0xFF))
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int32_t taxid;
|
int32_t taxid;
|
||||||
int32_t rank;
|
int32_t rank;
|
||||||
|
Reference in New Issue
Block a user