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 <stdlib.h>
#include <stdio.h> #include <stdio.h>
...@@ -341,18 +341,20 @@ void List_Copy(List_T *a , List_T *b){ ...@@ -341,18 +341,20 @@ void List_Copy(List_T *a , List_T *b){
} }
} }
void swap_doubles(double *array, unsigned int n){ void swap_bytes(char *array, int size, int n){
unsigned int i, c; int i, c;
double x; char *x, *a;
char *px, *pp;
x = (char*)Malloc(size);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
x = array[i]; a = &array[i*size];
px = (char *) &x; memcpy(x, a, size);
pp = (char *) (array+i); for (c = 0; c < size; c++)
for (c = 0; c < sizeof(double); ++c) a[size-1-c] = x[c];
pp[sizeof(double)-1-c] = px[c];
} }
Free(x);
} }
List_T *List_CreateFromFile(int n, int size, FILE *file, int format, int swap){ 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){ ...@@ -369,13 +371,7 @@ List_T *List_CreateFromFile(int n, int size, FILE *file, int format, int swap){
return liste; return liste;
case LIST_FORMAT_BINARY : case LIST_FORMAT_BINARY :
fread(liste->array, size, n, file); fread(liste->array, size, n, file);
if(swap){ if(swap) swap_bytes(liste->array, size, n);
if(size != sizeof(double)){
Msg(ERROR, "Swapping Bytes only done for doubles");
return NULL;
}
swap_doubles((double*)liste->array, n);
}
return liste; return liste;
default : default :
Msg(ERROR, "Unknown List Format"); 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