-
Notifications
You must be signed in to change notification settings - Fork 93
/
alist-to-pchk.c
168 lines (132 loc) · 3.43 KB
/
alist-to-pchk.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ALIST-TO-PCHK.C - Convert a parity check matrix from alist format. */
/* Copyright (c) 1995-2012 by Radford M. Neal.
*
* Permission is granted for anyone to copy, use, modify, and distribute
* these programs and accompanying documents for any purpose, provided
* this copyright notice is retained and prominently displayed, and note
* is made of any changes made to these programs. These programs and
* documents are distributed without any warranty, express or implied.
* As the programs were written for research purposes only, they have not
* been tested to the degree that would be advisable in any important
* application. All use of these programs is entirely at the user's own
* risk.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "alloc.h"
#include "intio.h"
#include "open.h"
#include "mod2sparse.h"
#include "mod2dense.h"
#include "mod2convert.h"
#include "rcode.h"
void bad_alist_file(void);
void usage(void);
/* MAIN PROGRAM. */
int main
( int argc,
char **argv
)
{
char *alist_file, *pchk_file;
FILE *af, *pf;
int mxrw, mxcw;
int *rw, *cw;
int i, j, k;
int tot, trans;
int nxt;
trans = 0;
for (;;)
{
if (argc>1 && strcmp(argv[1],"-t")==0)
{ trans = 1;
argc -= 1;
argv += 1;
}
else
{ break;
}
}
if (argc!=3)
{ usage();
}
pchk_file = argv[2];
alist_file = argv[1];
af = open_file_std(alist_file,"r");
if (af==NULL)
{ fprintf(stderr,"Can't open alist file: %s\n",alist_file);
exit(1);
}
if (fscanf(af,"%d",&M)!=1 || M<1
|| fscanf(af,"%d",&N)!=1 || N<1
|| fscanf(af,"%d",&mxrw)!=1 || mxrw<0 || mxrw>N
|| fscanf(af,"%d",&mxcw)!=1 || mxcw<0 || mxcw>M)
{ bad_alist_file();
}
rw = (int *) chk_alloc (M, sizeof *rw);
for (i = 0; i<M; i++)
{ if (fscanf(af,"%d",&rw[i])!=1 || rw[i]<0 || rw[i]>N)
{ bad_alist_file();
}
}
cw = (int *) chk_alloc (N, sizeof *cw);
for (j = 0; j<N; j++)
{ if (fscanf(af,"%d",&cw[j])!=1 || cw[j]<0 || cw[j]>M)
{ bad_alist_file();
}
}
H = mod2sparse_allocate(M,N);
do { if (fscanf(af,"%d",&nxt)!=1) nxt = -1; } while (nxt==0);
tot = 0;
for (i = 0; i<M; i++)
{ for (k = 0; k<rw[i]; k++)
{ if (nxt<=0 || nxt>N || mod2sparse_find(H,i,nxt-1))
{ bad_alist_file();
}
mod2sparse_insert(H,i,nxt-1);
tot += 1;
do { if (fscanf(af,"%d",&nxt)!=1) nxt = -1; } while (nxt==0);
}
}
for (j = 0; j<N; j++)
{ for (k = 0; k<cw[j]; k++)
{ if (nxt<=0 || nxt>M || !mod2sparse_find(H,nxt-1,j))
{ bad_alist_file();
}
tot -= 1;
do { if (fscanf(af,"%d",&nxt)!=1) nxt = -1; } while (nxt==0);
}
}
if (tot!=0 || nxt!=-1 || !feof(af))
{ bad_alist_file();
}
if (trans)
{ mod2sparse *HT;
HT = H;
H = mod2sparse_allocate(N,M);
mod2sparse_transpose(HT,H);
}
pf = open_file_std(pchk_file,"wb");
if (pf==NULL)
{ fprintf(stderr,"Can't create parity check file: %s\n",pchk_file);
exit(1);
}
intio_write(pf,('P'<<8)+0x80);
if (ferror(pf) || !mod2sparse_write(pf,H) || fclose(pf)!=0)
{ fprintf(stderr,"Error writing to parity check file %s\n",pchk_file);
exit(1);
}
return 0;
}
/* COMPLAIN THAT ALIST FILE IS BAD. */
void bad_alist_file()
{ fprintf(stderr,"Alist file doesn't have the right format\n");
exit(1);
}
/* PRINT USAGE MESSAGE AND EXIT. */
void usage(void)
{ fprintf(stderr,"Usage: alist-to-pchk [ -t ] alist-file pchk-file\n");
exit(1);
}