Skip to content
Snippets Groups Projects
Commit d84d537a authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

generalized swap_bytes

parent 48b058d8
No related branches found
No related tags found
No related merge requests found
/* $Id: List.cpp,v 1.8 2000-11-27 17:13:42 geuzaine Exp $ */
/* $Id: List.cpp,v 1.9 2000-11-28 08:19:28 geuzaine Exp $ */
#include <stdlib.h>
#include <stdio.h>
......@@ -341,18 +341,20 @@ void List_Copy(List_T *a , List_T *b){
}
}
void swap_doubles(double *array, unsigned int n){
unsigned int i, c;
double x;
char *px, *pp;
void swap_bytes(char *array, int size, int n){
int i, c;
char *x, *a;
x = (char*)Malloc(size);
for (i = 0; i < n; i++) {
x = array[i];
px = (char *) &x;
pp = (char *) (array+i);
for (c = 0; c < sizeof(double); ++c)
pp[sizeof(double)-1-c] = px[c];
a = &array[i*size];
memcpy(x, a, size);
for (c = 0; c < size; c++)
a[size-1-c] = x[c];
}
Free(x);
}
List_T *List_CreateFromFile(int n, int size, FILE *file, int format, int swap){
......@@ -369,13 +371,7 @@ List_T *List_CreateFromFile(int n, int size, FILE *file, int format, int swap){
return liste;
case LIST_FORMAT_BINARY :
fread(liste->array, size, n, file);
if(swap){
if(size != sizeof(double)){
Msg(ERROR, "Swapping Bytes only done for doubles");
return NULL;
}
swap_doubles((double*)liste->array, n);
}
if(swap) swap_bytes(liste->array, size, n);
return liste;
default :
Msg(ERROR, "Unknown List Format");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment