Mercurial > hg > audiodb
comparison audioDB.cpp @ 118:c0789661f232 endian-neutral
Towards endian-neutrality, part 3.
The feature files have an integer expressing their dimensionality. If
it's out of bounds, but the byte-swapped version of it isn't, then
assume that we're reading a feature file created on an other-endian
machine, and silently (or verbosely, if verbosity is on) Do The Right
Thing.
author | mas01cr |
---|---|
date | Fri, 12 Oct 2007 11:52:28 +0000 |
parents | e800eac265c3 |
children | 6062e6f1dcc1 |
comparison
equal
deleted
inserted
replaced
117:e800eac265c3 | 118:c0789661f232 |
---|---|
7 | 7 |
8 void sighup_action(int signal, siginfo_t *info, void *context) { | 8 void sighup_action(int signal, siginfo_t *info, void *context) { |
9 // FIXME: reread any configuration files | 9 // FIXME: reread any configuration files |
10 } | 10 } |
11 #endif | 11 #endif |
12 | |
13 uint32_t uint32_byte_swap(uint32_t arg) { | |
14 return (((arg & 0xff000000) >> 24) | | |
15 ((arg & 0xff0000) >> 8) | | |
16 ((arg & 0xff00) << 8) | | |
17 ((arg & 0xff) << 24)); | |
18 } | |
12 | 19 |
13 void audioDB::error(const char* a, const char* b, const char *sysFunc) { | 20 void audioDB::error(const char* a, const char* b, const char *sysFunc) { |
14 if(isServer) { | 21 if(isServer) { |
15 /* FIXME: I think this is leaky -- we never delete err. actually | 22 /* FIXME: I think this is leaky -- we never delete err. actually |
16 deleting it is tricky, though; it gets placed into some | 23 deleting it is tricky, though; it gets placed into some |
482 } | 489 } |
483 | 490 |
484 if(inFile) { | 491 if(inFile) { |
485 uint32_t inDim; | 492 uint32_t inDim; |
486 read(infid, &inDim, sizeof(uint32_t)); | 493 read(infid, &inDim, sizeof(uint32_t)); |
494 if (inDim > O2_MAXDIM && uint32_byte_swap(inDim) < O2_MAXDIM) { | |
495 // quite possibly we're being asked to read a feature file | |
496 // generated on the other endianness platform. Do The Right | |
497 // Thing. -- CSR, 2007-10-12 | |
498 if (verbosity) { | |
499 cerr << "byte swapping dimensionality: " << inDim << endl; | |
500 } | |
501 inDim = uint32_byte_swap(inDim); | |
502 } | |
487 if(ntohl(dbH->dim) == 0 && ntohl(dbH->length) == 0) { | 503 if(ntohl(dbH->dim) == 0 && ntohl(dbH->length) == 0) { |
488 // empty database: initialize with input dimensionality | 504 // empty database: initialize with input dimensionality |
489 dbH->dim = htonl(inDim); | 505 dbH->dim = htonl(inDim); |
490 } else { | 506 } else { |
491 if(dbH->dim != htonl(inDim)) { | 507 if(dbH->dim != htonl(inDim)) { |
723 error("No more room in database","insert failed: reason database is full."); | 739 error("No more room in database","insert failed: reason database is full."); |
724 | 740 |
725 if(thisFile) { | 741 if(thisFile) { |
726 uint32_t thisDim; | 742 uint32_t thisDim; |
727 read(infid,&thisDim,sizeof(uint32_t)); | 743 read(infid,&thisDim,sizeof(uint32_t)); |
744 if (thisDim > O2_MAXDIM && uint32_byte_swap(thisDim) < O2_MAXDIM) { | |
745 // quite possibly we're being asked to read a feature file | |
746 // generated on the other endianness platform. Do The Right | |
747 // Thing. -- CSR, 2007-10-12 | |
748 if (verbosity) { | |
749 cerr << "byte swapping dimensionality: " << thisDim << endl; | |
750 } | |
751 thisDim = uint32_byte_swap(thisDim); | |
752 } | |
728 if(ntohl(dbH->dim) == 0 && ntohl(dbH->length)==0) { | 753 if(ntohl(dbH->dim) == 0 && ntohl(dbH->length)==0) { |
729 // empty database: initialize with input dimensionality | 754 // empty database: initialize with input dimensionality |
730 dbH->dim = htonl(thisDim); | 755 dbH->dim = htonl(thisDim); |
731 } else { | 756 } else { |
732 if(ntohl(dbH->dim) != thisDim) { | 757 if(ntohl(dbH->dim) != thisDim) { |