-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_array_xdr.c
138 lines (131 loc) · 3.19 KB
/
extract_array_xdr.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
** extract_array_xdr.c
**
** Written by Marcel Zemp
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#include <iof.h>
void usage(void);
int main(int argc, char **argv) {
int i;
int verboselevel;
int iindex, findex, dindex;
ARRAY_HEADER ah1, ah2;
ARRAY_PARTICLE ap1, ap2;
XDR xdrs1, xdrs2;
iindex = 0;
findex = 0;
dindex = 0;
verboselevel = 0;
i = 1;
while (i < argc) {
if ((strcmp(argv[i],"-h") == 0) || (strcmp(argv[i],"-help") == 0)) {
usage();
}
if (strcmp(argv[i],"-version") == 0) {
fprintf(stderr,"%s (%s)\n",NAME,VERSION);
exit(1);
}
if (strcmp(argv[i],"-i") == 0) {
i++;
if (i >= argc) usage();
iindex = atoi(argv[i]);
i++;
}
else if (strcmp(argv[i],"-f") == 0) {
i++;
if (i >= argc) usage();
findex = atoi(argv[i]);
i++;
}
else if (strcmp(argv[i],"-d") == 0) {
i++;
if (i >= argc) usage();
dindex = atoi(argv[i]);
i++;
}
else if (strcmp(argv[i],"-verbose") == 0) {
verboselevel = 1;
i++;
}
else {
usage();
}
}
xdrstdio_create(&xdrs1,stdin,XDR_DECODE);
xdrstdio_create(&xdrs2,stdout,XDR_ENCODE);
read_array_xdr_header(&xdrs1,&ah1);
ah2.N[0] = ah1.N[0];
if (iindex != 0) {
assert(findex == 0);
assert(dindex == 0);
ah2.N[1] = 1;
ah2.N[2] = 0;
ah2.N[3] = 0;
}
if (findex != 0) {
assert(iindex == 0);
assert(dindex == 0);
ah2.N[1] = 0;
ah2.N[2] = 1;
ah2.N[3] = 0;
}
if (dindex != 0) {
assert(iindex == 0);
assert(findex == 0);
ah2.N[1] = 0;
ah2.N[2] = 0;
ah2.N[3] = 1;
}
write_array_xdr_header(&xdrs2,&ah2);
allocate_array_particle(&ah1,&ap1);
allocate_array_particle(&ah2,&ap2);
for (i = 0; i < ah2.N[0]; i++) {
read_array_xdr_particle(&xdrs1,&ah1,&ap1);
if (iindex != 0) {
ap2.ia[0] = ap1.ia[iindex-1];
}
else if (findex != 0) {
ap2.fa[0] = ap1.fa[findex-1];
}
else if (dindex != 0) {
ap2.da[0] = ap1.da[dindex-1];
}
write_array_xdr_particle(&xdrs2,&ah2,&ap2);
}
xdr_destroy(&xdrs1);
xdr_destroy(&xdrs2);
if (verboselevel > 0) {
fprintf(stderr,"Ntotal_in: %d Ni_in: %d Nf_in: %d Nd_in: %d\n",
ah1.N[0],ah1.N[1],ah1.N[2],ah1.N[3]);
fprintf(stderr,"Ntotal_out: %d Ni_out: %d Nf_out: %d Nd_out: %d\n",
ah2.N[0],ah2.N[1],ah2.N[2],ah2.N[3]);
}
exit(0);
}
void usage(void) {
fprintf(stderr,"\n");
fprintf(stderr,"%s (%s)\n",NAME,VERSION);
fprintf(stderr,"\n");
fprintf(stderr,"Program extracts specific array from array file in XDR format.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Please specify the following parameters:\n");
fprintf(stderr,"\n");
fprintf(stderr,"-i <value> : index of integer array to be extracted or\n");
fprintf(stderr,"-f <value> : index of float array to be extracted or\n");
fprintf(stderr,"-d <value> : index of double array to be extracted\n");
fprintf(stderr,"-verbose : verbose\n");
fprintf(stderr,"< <name> : input file in XDR format\n");
fprintf(stderr,"> <name> : output file in XDR format\n");
fprintf(stderr,"\n");
fprintf(stderr,"Other options:\n");
fprintf(stderr,"\n");
fprintf(stderr,"-h or -help : display this help and exit\n");
fprintf(stderr,"-version : display version information and exit\n");
fprintf(stderr,"\n");
exit(1);
}