/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: test-rle.ctr */ /** @file test-rle.c The test-rle module. */ #line 1 "test-rle.ctr" #include #include "dk4rle.h" #include "dk4edstm.h" #line 9 "test-rle.ctr" /* Example file for run-length encoding. The program reads data from standard input, applies run-length encoding and writes result data to standard output. For better understanding we only do minimal diagnostics here (set exit status code). In real world programs one would issue more diagnostics on problems. */ int main(int argc, char *argv[]) { dk4_rl_enc_t enc; /* Encoder */ const unsigned char *ucptr; /* Address of output buffer */ unsigned long offset; /* Current offset in file */ size_t sz; /* Number of output characters */ size_t i; /* Current output char index */ #if DK4_ON_WINDOWS int oldimode; /* Old file mode for stdin */ int oldomode; /* Old file mode for stdout */ #endif int ic; /* Current input char from stdin */ int exval = 0; /* Exit status code */ #line 40 "test-rle.ctr" #line 41 "test-rle.ctr" /* On Windows we must set stdin and stdout to binary mode explicitly. */ #if DK4_ON_WINDOWS oldimode = _setmode(_fileno(stdin), _O_BINARY); oldomode = _setmode(_fileno(stdout), _O_BINARY); #endif /* Initialize encoder. */ dk4rle_init(&enc, 0, NULL); #line 51 "test-rle.ctr" #line 52 "test-rle.ctr" /* Process standard input. */ offset = 0UL; while (EOF != (ic = fgetc(stdin))) { #line 58 "test-rle.ctr" switch (dk4rle_add(&enc, (unsigned char)ic, NULL)) { case DK4_EDSTM_FINISHED : { #line 60 "test-rle.ctr" ucptr = NULL; sz = 0; if (0 != dk4rle_output(&ucptr, &sz, &enc, NULL)) { #line 62 "test-rle.ctr" if ((NULL != ucptr) && (0 < sz)) { #line 63 "test-rle.ctr" #line 64 "test-rle.ctr" for (i = 0; i < sz; i++) { #line 66 "test-rle.ctr" if (EOF == fputc((int)(ucptr[i]), stdout)) { #line 67 "test-rle.ctr" exval = 1; } } } } } break; case DK4_EDSTM_ERROR : { #line 74 "test-rle.ctr" exval = 1; } break; default : { #line 77 "test-rle.ctr" } break; } offset++; } #line 82 "test-rle.ctr" /* If successful so far, process final data from encoder. */ if (0 == exval) { #line 86 "test-rle.ctr" switch (dk4rle_finish(&enc, NULL)) { case DK4_EDSTM_FINISHED : { #line 88 "test-rle.ctr" ucptr = NULL; sz = 0; if (0 != dk4rle_output(&ucptr, &sz, &enc, NULL)) { if ((NULL != ucptr) && (0 < sz)) { #line 92 "test-rle.ctr" for (i = 0; i < sz; i++) { #line 94 "test-rle.ctr" if (EOF == fputc((int)(ucptr[i]), stdout)) { exval = 1; } } } } } break; case DK4_EDSTM_ERROR : { #line 102 "test-rle.ctr" exval = 1; } break; default : { #line 105 "test-rle.ctr" } break; } } /* Restore previous file mode for stdin and stdout on Windows. */ #if DK4_ON_WINDOWS fflush(stdout); _setmode(_fileno(stdout), oldomode); _setmode(_fileno(stdin), oldimode); #endif /* Exit, indicate success or error. */ #line 120 "test-rle.ctr" #line 121 "test-rle.ctr" exit(exval); return exval; }