#include #include #include #include using namespace std; enum { ENDIAN_LITTLE, ENDIAN_BIG, ENDIAN_DUNNO, }; int endian=ENDIAN_DUNNO; const int ENDIAN_TEST=1; //Run-time detection of CPU endian-ness //--- inline int is_bigendian() { return (*(char*)&ENDIAN_TEST) == 0 ;} inline int is_littleendian() { return (*(char*)&ENDIAN_TEST) == 1 ;} void detectEndianNess() { if(is_littleendian()) endian=ENDIAN_LITTLE; else if (is_bigendian()) endian=ENDIAN_BIG; else endian=ENDIAN_DUNNO; } //--- struct POS_DATA { float values[4]; }; //A routine for flipping data bytes around between // big and little endian IEEE754 format void floatSwapBytes(float *inFloat) { //Use a union to avoid strict-aliasing error union FloatSwapUnion{ float f; char c[4]; } ; FloatSwapUnion fa,fb; fa.f = *inFloat; fb.c[0] = fa.c[3]; fb.c[1] = fa.c[2]; fb.c[2] = fa.c[1]; fb.c[3] = fa.c[0]; *inFloat=fb.f; } //A not-particularly efficient pos-file loader // returns true on success, false on failure bool loadPosFile(const std::string &str,vector &p) { //open file for "binary" access mode ifstream file(str.c_str(),ios::binary); //Check file opened OK if(!file) return false; //open failed //Check filesize (in bytes) // we do this by jumping to the end, // asking the offset, then jumping back to the start // as this is very cross-platform (but probably inefficient) file.seekg(0,std::ios::end); size_t fileSize=file.tellg(); file.seekg(0,std::ios::beg); //Filesize must be 4 4 byte floats if(fileSize %16) return false; //OK, now read the contents size_t numEntries=fileSize/16; POS_DATA pd; for(size_t ui=0;ui &p) { //This function assumes floats are 4 bytes if(sizeof(float) !=4) return false; //Open the file for output ofstream file(filename.c_str(),ios::binary); if(!file) return false; if(endian == ENDIAN_LITTLE) { //On little endian machines, loading is a little complicated // as we need to convert the pos output back to big-endian mode // first float values[4]; for(size_t ui=0;ui args; for(int ui=1;ui p; try { for(size_t ui=0;ui