]> hackdaworld.org Git - physik/nlsop.git/commitdiff
added tool to convert big endian save files to little endian (dont tell anyone!)
authorhackbard <hackbard>
Mon, 4 Oct 2004 15:33:50 +0000 (15:33 +0000)
committerhackbard <hackbard>
Mon, 4 Oct 2004 15:33:50 +0000 (15:33 +0000)
.cvsignore
be2le.c [new file with mode: 0644]

index 209faf8a506cb6b93f39da536658536e477b07df..b7561ecad6e0d93285c8ba06a8cb678913118bf9 100644 (file)
@@ -7,3 +7,4 @@ Makefile
 nlsop-*
 parse_trim_collision
 ft
+be2le
diff --git a/be2le.c b/be2le.c
new file mode 100644 (file)
index 0000000..d1fcfaa
--- /dev/null
+++ b/be2le.c
@@ -0,0 +1,150 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "dfbapi.h"
+#include "nlsop.h"
+
+int byte_switch(void *data,int bytes) {
+  
+  unsigned char tmp;
+  unsigned char *d;
+  int i;
+
+  d=(unsigned char *)data;
+
+  for(i=0;i<bytes/2;i++) {
+    tmp=d[i];
+    d[i]=d[bytes-i-1];
+    d[bytes-i-1]=tmp;
+  }
+
+  return 1;
+}
+
+int main(int argc,char **argv) {
+
+  int i,size;
+  int rfd,wfd;
+  info info;
+  d3_lattice l;
+  unsigned char *buf;
+
+  printf("just a short test ...\n");
+  i='U'<<24;
+  i|='N'<<16;
+  i|='I'<<8;
+  i|='X';
+  printf("%d -> ",i);
+  fflush(stdout);
+  write(1,&i,4);
+  printf("\n");
+  byte_switch(&i,sizeof(int));
+  printf("%d -> ",i);
+  fflush(stdout);
+  write(1,&i,4);
+  printf("\n");
+
+  if((rfd=open(argv[1],O_RDONLY))<0) {
+    printf("failed opening file %s\n",argv[1]);
+    return -1;
+  }
+
+  if((wfd=open(argv[2],O_WRONLY|O_CREAT))<0) {
+    printf("failed opening file %s\n",argv[2]);
+    return -1;
+  }
+
+  size=sizeof(d3_lattice);
+  if(read(rfd,&l,size)<size) {
+    printf("failed reading %d bytes\n",size);
+    return -1;
+  }
+
+  byte_switch(&(l.max_x),sizeof(int));
+  byte_switch(&(l.max_y),sizeof(int));
+  byte_switch(&(l.max_z),sizeof(int));
+
+  /*
+  byte_switch(&(l.fakt_x),sizeof(int));
+  byte_switch(&(l.fakt_y),sizeof(int));
+  byte_switch(&(l.info_x),sizeof(int));
+  byte_switch(&(l.info_y),sizeof(int));
+  byte_switch(&(l.o_x),sizeof(int));
+  byte_switch(&(l.info_w),sizeof(int));
+  byte_switch(&(l.info_h),sizeof(int));
+  byte_switch(&(l.font_h),sizeof(int));
+  */
+
+  size=sizeof(info);
+  if(read(rfd,&info,size)<size) {
+    printf("failed reading %d bytes\n",size);
+    return -1;
+  }
+
+  byte_switch(&(info.cc),sizeof(int));
+  byte_switch(&(info.steps),sizeof(int));
+  byte_switch(&(info.range),sizeof(int));
+  byte_switch(&(info.diff_rate),sizeof(int));
+  byte_switch(&(info.cpi),sizeof(int));
+  byte_switch(&(info.c_sat),sizeof(int));
+  byte_switch(&(info.s),sizeof(double));
+  byte_switch(&(info.b),sizeof(double));
+  byte_switch(&(info.c),sizeof(double));
+  byte_switch(&(info.dr_ac),sizeof(double));
+  byte_switch(&(info.dr_cc),sizeof(double));
+
+  /* write lattice and info struct */
+  size=sizeof(d3_lattice);
+  if(write(wfd,&l,size)<size) {
+    printf("failed writing %d bytes\n",size);
+    return -1;
+  }
+
+  size=sizeof(info);
+  if(write(wfd,&info,size)<size) {
+    printf("failed writing %d bytes\n",size);
+    return -1;
+  }
+
+  /* read & write data */
+  /* status info - just chars */
+  size=l.max_x*l.max_y*l.max_z;
+  if((buf=(unsigned char *)malloc(size))==NULL) {
+    printf("unable to malloc %d bytes\n",size);
+    return -1;
+  }
+  if(read(rfd,buf,size)<size) {
+    printf("failed reading %d data bytes\n",size);
+    return -1;
+  }
+  if(write(wfd,buf,size)<size) {
+    printf("failed writing %d data bytes\n",size);
+    return -1;
+  }
+  /* c conc - integers */
+  for(i=0;i<size;i++) {
+    if(read(rfd,buf,sizeof(int))<sizeof(int)) {
+      printf("unable to read c conc nr. %d\n",i);
+      return -1;
+    }
+    byte_switch(buf,sizeof(int));
+    if(write(wfd,buf,sizeof(int))<sizeof(int)) {
+      printf("unable to write c conc nr. %d\n",i);
+      return -1;
+    }
+  }
+
+  printf("done ... (never tell the endian police!)\n");
+
+  free(buf);
+  
+  close(rfd);
+  close(wfd);
+
+  return 1;
+}