-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput2bytes.c
42 lines (40 loc) · 1.09 KB
/
input2bytes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define NPER 2
#define NCONT 4
int main(int argc, char ** argv)
{
char * ifile = argc > 1 ? argv[1] : "-";
char * ofile = argc > 2 ? argv[2] : "-";
char * line;
FILE * f = strcmp(ifile, "-") ? fopen(ifile,"r") : stdin;
FILE * of = strcmp(ofile, "-") ? fopen(ofile,"w") : stdout;
int i, i1, i2, i3,j;
size_t len = 0;
ssize_t nread;
uint8_t data[NCONT/2][2][NPER];
while((nread=getline(&line, &len, f)) != -1)
{
// Read a full record
memset(data,0,sizeof(data));
for(i1=0;i1<NCONT/2;i1++)
for(i2=0;i2<2;i2++)
for(i3=0;i3<NPER;i3++)
for(i=0;i<8;i++)
{
j=3+i1*2+i2+((i1*2+i2)*NPER+i3)*8+i;
data[i1][i2][i3] <<= 1;
//printf("%d %d %d %d %2d %c %02x",i1,i2,i3,i,j,line[j],data[i1][i2][i3]);
if(line[j] != '.')
data[i1][i2][i3] |= 1;
//printf(" %02x\n", data[i1][i2][i3]);
}
// Output after taking into account the different byte ordering
for(i2=0;i2<2;i2++)
for(i1=0;i1<NCONT/2;i1++)
for(i3=NPER-1;i3>=0;i3--)
fwrite(&data[i1][i2][i3], 1, 1, of);
}
}