#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
/* generated by ldpc_fsk_lib.m:ldpc_decode() */
tmp &= 1; // only retain the lsb
prev = tmp;
- pbits[p] =tmp;
+ pbits[p] = tmp;
}
}
-int main(void)
+int main(int argc, char *argv[])
{
unsigned char ibits[NUMBERROWSHCOLS];
unsigned char pbits[NUMBERPARITYBITS];
- short k;
- unsigned int x;
-
- printf("Test C Encoder\n");
-
- FILE *infp, *opfp;
- infp = fopen("dat_in2064.txt", "r"); // read info bits from file
- if (infp == NULL) {printf("Unable to open file.\n"); exit(1);}
-
- for (k=0; k<NUMBERROWSHCOLS; k++) {
- fscanf(infp, "%u\n", &x);
- ibits[k] = x;
+ FILE *fin, *fout;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s InputOneBytePerBit OuputOneBytePerBit\n", argv[0]);
+ exit(0);
+ }
+
+ if (strcmp(argv[1], "-") == 0) fin = stdin;
+ else if ( (fin = fopen(argv[1],"rb")) == NULL ) {
+ fprintf(stderr, "Error opening input bit file: %s: %s.\n",
+ argv[2], strerror(errno));
+ exit(1);
}
- fclose(infp);
+
+ if (strcmp(argv[2], "-") == 0) fout = stdout;
+ else if ( (fout = fopen(argv[2],"wb")) == NULL ) {
+ fprintf(stderr, "Error opening output bit file: %s: %s.\n",
+ argv[2], strerror(errno));
+ exit(1);
+ }
+
+ while (fread(ibits, sizeof(char), NUMBERROWSHCOLS, fin) == NUMBERROWSHCOLS) {
- encode(ibits, pbits);
+ encode(ibits, pbits);
+
+ fwrite(ibits, sizeof(char), NUMBERROWSHCOLS, fout);
+ fwrite(pbits, sizeof(char), NUMBERPARITYBITS, fout);
+ }
- opfp = fopen("dat_op2064.bin", "wb"); // write par bits to file
- if (opfp == NULL) {printf("Unable to output file.\n"); exit(1);}
- for (k=0; k<NUMBERROWSHCOLS; k++)
- fwrite(&ibits[k], sizeof(char), 1, opfp);
- for (k=0; k<NUMBERPARITYBITS; k++)
- fwrite(&pbits[k], sizeof(char), 1, opfp);
- fclose(opfp);
+ fclose(fin);
+ fclose(fout);
return 1;
}