int mod2sparse_write ( FILE *f, mod2sparse *m ) { mod2entry *e; int i; intio_write(f,m->n_rows); if (ferror(f)) return 0; intio_write(f,m->n_cols); if (ferror(f)) return 0; for (i = 0; i<mod2sparse_rows(m); i++) { e = mod2sparse_first_in_row(m,i); if (!mod2sparse_at_end(e)) { intio_write (f, -(i+1)); if (ferror(f)) return 0; while (!mod2sparse_at_end(e)) { intio_write (f, mod2sparse_col(e)+1); if (ferror(f)) return 0; e = mod2sparse_next_in_row(e); } } } intio_write(f,0); if (ferror(f)) return 0; return 1; }
int main ( int argc, char **argv ) { mod2dense *D,*Z,*G; mod2sparse *T; int *a_col,*a_row; int *cols2; char *pchk_file; char fn[128]; char *code; FILE *fp; int i,j,n; int a,b,c; int codeLen,checkLen; pchk_file = argv[3]; sscanf(argv[1],"%ld",&codeLen); sscanf(argv[2],"%ld",&checkLen); sprintf(fn,"data/code%dX%d.bin",codeLen,checkLen); D = mod2dense_allocate( checkLen, codeLen ); code=malloc(codeLen*checkLen); printf(" read from %s\n",fn); fp=fopen(fn,"rb"); if( fp==NULL ) { printf("open file error\n"); return -1; } fread(code,codeLen*checkLen,1,fp); fclose(fp); mod2dense_clear( D ); for( i=0;i<checkLen;i++ ) for( j=0;j<codeLen;j++ ) { if( code[i*codeLen+j] ) mod2dense_set( D, i, j, 1 ); } T = mod2sparse_allocate( checkLen, codeLen ); mod2dense_to_sparse( D , T ); fp = open_file_std(pchk_file,"wb"); if (fp==NULL) { fprintf(stderr,"Can't create parity check file: %s\n",pchk_file); exit(1); } intio_write(fp,('P'<<8)+0x80); if (ferror(fp) || !mod2sparse_write(fp,T) || fclose(fp)!=0) { fprintf(stderr,"Error writing to parity check file %s\n",pchk_file); exit(1); } free(code); return 0; }
int main ( int argc, char **argv ) { make_method method; char *file, **meth; int seed, no4cycle; distrib *d; char junk; FILE *f; /* Look at initial arguments. */ if (!(file = argv[1]) || !argv[2] || sscanf(argv[2],"%d%c",&M,&junk)!=1 || M<=0 || !argv[3] || sscanf(argv[3],"%d%c",&N,&junk)!=1 || N<=0 || !argv[4] || sscanf(argv[4],"%d%c",&seed,&junk)!=1) { usage(); } /* Look at the arguments specifying the method for producing the code. */ meth = argv+5; if (!meth[0]) usage(); no4cycle = 0; if (strcmp(meth[0],"evencol")==0 || strcmp(meth[0],"evenboth")==0) { method = strcmp(meth[0],"evencol")==0 ? Evencol : Evenboth; if (!meth[1]) { usage(); } d = distrib_create(meth[1]); if (d==0) { usage(); } if (meth[2]) { if (strcmp(meth[2],"no4cycle")==0) { no4cycle = 1; if (meth[3]) { usage(); } } else { usage(); } } } else { usage(); } /* Check for some problems. */ if (distrib_max(d)>M) { fprintf(stderr, "At least one checks per bit (%d) is greater than total checks (%d)\n", distrib_max(d), M); exit(1); } if (distrib_max(d)==M && N>1 && no4cycle) { fprintf(stderr, "Can't eliminate cycles of length four with this many checks per bit\n"); exit(1); } /* Make the parity check matrix. */ make_ldpc(seed,method,d,no4cycle); /* Write out the parity check matrix. */ f = open_file_std(file,"wb"); if (f==NULL) { fprintf(stderr,"Can't create parity check file: %s\n",file); exit(1); } intio_write(f,('P'<<8)+0x80); if (ferror(f) || !mod2sparse_write(f,H) || fclose(f)!=0) { fprintf(stderr,"Error writing to parity check file %s\n",file); exit(1); } return 0; }