void TestReverseBytes(void) { char szAuto[9] = "Autobody"; char szAutoDash[10] = "Auto-Body"; char szOne[2] = "1"; ReverseBytes(szAuto, 8); AssertString("ydobotuA", szAuto); ReverseBytes(szAutoDash, 9); AssertString("ydoB-otuA", szAutoDash); ReverseBytes(szOne, 1); AssertString("1", szOne); ReverseBytes(szOne, 0); AssertString("1", szOne); }
int WriteBinMesh(char *fname) { FILE *fptr; BINTYPE *data; int i, j; int idx; unsigned char *cdata; if (strlen(fname) < 1) { return(FALSE); } if ((fptr = fopen(fname, "w")) == NULL) { fprintf(stderr, "Failed to open binary map file for writing\n"); return(FALSE); } // write the mesh type fprintf(fptr, "%d\n", meshtype); // write the mesh dimensions fprintf(fptr, "%d %d\n", meshnx, meshny); // allocate mem to store the entire mesh in continguous BINTYPE form data = (BINTYPE *)calloc(meshnx * meshny * 5, sizeof(BINTYPE)); idx = 0; for (j = 0; j < meshny; j++) { for (i = 0; i < meshnx; i++) { data[idx++] = mesh[i][j].u; data[idx++] = mesh[i][j].v; data[idx++] = mesh[i][j].x; data[idx++] = mesh[i][j].y; data[idx++] = mesh[i][j].i; } } cdata = (unsigned char *)data; // switch byte ordering if necessary if (!isLittleEndian()) { int nrev = ReverseBytes(cdata, meshnx * meshny * 5 * sizeof(BINTYPE), sizeof(BINTYPE)); if (nrev != meshnx * meshny * 5) { fprintf(stderr, "Did not reverse correct number of values!\n"); fclose(fptr); free(data); return(FALSE); } } // write the chunk to the file fwrite(cdata, sizeof(unsigned char), meshnx * meshny * 5 * sizeof(BINTYPE), fptr); fclose(fptr); free(data); return(TRUE); }
/////////////////////////////////////////////////////////////////////////////// /// Converts the floats in the passed buffer from big-endian to little-endian /// or vice-versa, if the DGI is running on a little-endian system. /// /// @pre None. /// @post The elements of data are converted in endianness if the DGI is /// running on a little-endian system. Otherwise, nothing happens. /// @param data the data to be endian-swapped /// @param numBytes the number of bytes per word in data /// /// @limitations Assumes the existence of UNIX byte order macros. /////////////////////////////////////////////////////////////////////////////// void CRtdsAdapter::EndianSwapIfNeeded( char * buffer, std::size_t numBytes ) { Logger.Trace << __PRETTY_FUNCTION__ << std::endl; // check endianess at compile time. Middle-Endian not allowed // The parameters __BYTE_ORDER, __LITTLE_ENDIAN, __BIG_ENDIAN should // automatically be defined and determined in sys/param.h, which exists // in most Unix systems. #if __BYTE_ORDER == __LITTLE_ENDIAN for( std::size_t i = 0; i < numBytes; i += sizeof( TSignalValue ) ) { ReverseBytes( buffer+i, sizeof( TSignalValue ) ); } #elif __BYTE_ORDER == __BIG_ENDIAN Logger.Debug << "Endian swap skipped: host is big-endian." << std::endl; #else #error "unsupported endianness or __BYTE_ORDER not defined" #endif }
int ReadBinMesh(char *fname) { FILE *fptr; BINTYPE *data; int i, j; int idx; unsigned char *cdata; unsigned int nread; char string[255]; if (strlen(fname) < 1) { return(FALSE); } if ((fptr = fopen(fname, "r")) == NULL) { fprintf(stderr, "Failed to open binary map file for reading\n"); return(FALSE); } fprintf(stderr, "opened file %s\n", fname); // Get the mesh type fgets(string, 254, fptr); if (sscanf(string,"%d",&meshtype) != 1) { fprintf(stderr,"Failed to read the mesh type\n"); fclose(fptr); return(FALSE); } if (meshtype != POLAR && meshtype != RECTANGULAR) { fprintf(stderr,"Failed to get a recognised map type (%d)\n",meshtype); fclose(fptr); return(FALSE); } if (meshtype != RECTANGULAR) { fprintf(stderr,"Currently only support a rectangular\n"); fclose(fptr); return(FALSE); } // Get the dimensions fgets(string, 254, fptr); if (sscanf(string ,"%d %d",&meshnx,&meshny) != 2) { fprintf(stderr,"Failed to read the mesh dimensions\n"); fclose(fptr); return(FALSE); } if (meshnx < 4 || meshny < 4 || meshnx > 100000 || meshny > 100000) { fprintf(stderr,"Didn't read acceptable mesh resolution (%d,%d)\n", meshnx,meshny); fclose(fptr); return(FALSE); } // Create new mesh mesh = (MESHNODE **)malloc(meshnx*sizeof(MESHNODE *)); for (i=0;i<meshnx;i++) { mesh[i] = (MESHNODE *)malloc(meshny*sizeof(MESHNODE)); } cdata = (unsigned char *)calloc(meshnx * meshny * 5 * sizeof(BINTYPE), sizeof(unsigned char)); nread = fread((void *)cdata, sizeof(unsigned char), meshnx * meshny * 5 * sizeof(BINTYPE), fptr); if (nread != meshnx * meshny * 5 * sizeof(BINTYPE)) { fprintf(stderr, "Did not read correct number of points\n"); fclose(fptr); free(cdata); return(FALSE); } if (!isLittleEndian()) { fprintf(stderr, "Swapping bytes...\n"); int nrev = ReverseBytes(cdata, meshnx * meshny * 5 * sizeof(BINTYPE), sizeof(BINTYPE)); if (nrev != meshnx * meshny * 5) { fprintf(stderr, "Did not reverse correct number of values!\n"); fclose(fptr); free(cdata); } } // stuff values into struct array data = (BINTYPE *)cdata; idx = 0; for (j = 0; j < meshny; j++) { for (i = 0; i < meshnx; i++) { mesh[i][j].u = data[idx++]; mesh[i][j].v = data[idx++]; mesh[i][j].x = data[idx++]; mesh[i][j].y = data[idx++]; mesh[i][j].i = data[idx++]; } } free(cdata); fclose(fptr); return(TRUE); }