added general header file
[physik/nlsop.git] / be2le.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 #include "dfbapi.h"
10 #include "nlsop.h"
11
12 int byte_switch(void *data,int bytes) {
13   
14   unsigned char tmp;
15   unsigned char *d;
16   int i;
17
18   d=(unsigned char *)data;
19
20   for(i=0;i<bytes/2;i++) {
21     tmp=d[i];
22     d[i]=d[bytes-i-1];
23     d[bytes-i-1]=tmp;
24   }
25
26   return 1;
27 }
28
29 int main(int argc,char **argv) {
30
31   int i,size;
32   int rfd,wfd;
33   info info;
34   d3_lattice l;
35   unsigned char *buf;
36
37   printf("just a short test ...\n");
38   i='U'<<24;
39   i|='N'<<16;
40   i|='I'<<8;
41   i|='X';
42   printf("%d -> ",i);
43   fflush(stdout);
44   write(1,&i,4);
45   printf("\n");
46   byte_switch(&i,sizeof(int));
47   printf("%d -> ",i);
48   fflush(stdout);
49   write(1,&i,4);
50   printf("\n");
51
52   if((rfd=open(argv[1],O_RDONLY))<0) {
53     printf("failed opening file %s\n",argv[1]);
54     return -1;
55   }
56
57   if((wfd=open(argv[2],O_WRONLY|O_CREAT))<0) {
58     printf("failed opening file %s\n",argv[2]);
59     return -1;
60   }
61
62   size=sizeof(d3_lattice);
63   if(read(rfd,&l,size)<size) {
64     printf("failed reading %d bytes\n",size);
65     return -1;
66   }
67
68   byte_switch(&(l.max_x),sizeof(int));
69   byte_switch(&(l.max_y),sizeof(int));
70   byte_switch(&(l.max_z),sizeof(int));
71
72   /*
73   byte_switch(&(l.fakt_x),sizeof(int));
74   byte_switch(&(l.fakt_y),sizeof(int));
75   byte_switch(&(l.info_x),sizeof(int));
76   byte_switch(&(l.info_y),sizeof(int));
77   byte_switch(&(l.o_x),sizeof(int));
78   byte_switch(&(l.info_w),sizeof(int));
79   byte_switch(&(l.info_h),sizeof(int));
80   byte_switch(&(l.font_h),sizeof(int));
81   */
82
83   size=sizeof(info);
84   if(read(rfd,&info,size)<size) {
85     printf("failed reading %d bytes\n",size);
86     return -1;
87   }
88
89   byte_switch(&(info.cc),sizeof(int));
90   byte_switch(&(info.steps),sizeof(int));
91   byte_switch(&(info.range),sizeof(int));
92   byte_switch(&(info.diff_rate),sizeof(int));
93   byte_switch(&(info.cpi),sizeof(int));
94   byte_switch(&(info.c_sat),sizeof(int));
95   byte_switch(&(info.s),sizeof(double));
96   byte_switch(&(info.b),sizeof(double));
97   byte_switch(&(info.c),sizeof(double));
98   byte_switch(&(info.dr_ac),sizeof(double));
99   byte_switch(&(info.dr_cc),sizeof(double));
100
101   /* write lattice and info struct */
102   size=sizeof(d3_lattice);
103   if(write(wfd,&l,size)<size) {
104     printf("failed writing %d bytes\n",size);
105     return -1;
106   }
107
108   size=sizeof(info);
109   if(write(wfd,&info,size)<size) {
110     printf("failed writing %d bytes\n",size);
111     return -1;
112   }
113
114   /* read & write data */
115   /* status info - just chars */
116   size=l.max_x*l.max_y*l.max_z;
117   if((buf=(unsigned char *)malloc(size))==NULL) {
118     printf("unable to malloc %d bytes\n",size);
119     return -1;
120   }
121   if(read(rfd,buf,size)<size) {
122     printf("failed reading %d data bytes\n",size);
123     return -1;
124   }
125   if(write(wfd,buf,size)<size) {
126     printf("failed writing %d data bytes\n",size);
127     return -1;
128   }
129   /* c conc - integers */
130   for(i=0;i<size;i++) {
131     if(read(rfd,buf,sizeof(int))<sizeof(int)) {
132       printf("unable to read c conc nr. %d\n",i);
133       return -1;
134     }
135     byte_switch(buf,sizeof(int));
136     if(write(wfd,buf,sizeof(int))<sizeof(int)) {
137       printf("unable to write c conc nr. %d\n",i);
138       return -1;
139     }
140   }
141
142   printf("done ... (never tell the endian police!)\n");
143
144   free(buf);
145   
146   close(rfd);
147   close(wfd);
148
149   return 1;
150 }