From d84d537a2fd9c2614cfc8c3a465c3c3e1ca90dc5 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Tue, 28 Nov 2000 08:19:28 +0000
Subject: [PATCH] generalized swap_bytes

---
 DataStr/List.cpp | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/DataStr/List.cpp b/DataStr/List.cpp
index cd3e00b93d..117747c778 100644
--- a/DataStr/List.cpp
+++ b/DataStr/List.cpp
@@ -1,4 +1,4 @@
-/* $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");
-- 
GitLab