summaryrefslogtreecommitdiff
path: root/support/dktools/test-rld.ctr
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/test-rld.ctr
Initial commit
Diffstat (limited to 'support/dktools/test-rld.ctr')
-rw-r--r--support/dktools/test-rld.ctr112
1 files changed, 112 insertions, 0 deletions
diff --git a/support/dktools/test-rld.ctr b/support/dktools/test-rld.ctr
new file mode 100644
index 0000000000..2bd2984ceb
--- /dev/null
+++ b/support/dktools/test-rld.ctr
@@ -0,0 +1,112 @@
+
+#include <stdio.h>
+#include "dk4rld.h"
+#include "dk4edstm.h"
+
+/*
+ Example file for run-length decoding.
+ The program reads data from standard input, applies
+ run-length decoding 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_dec_t dec; /* Decoder */
+ const unsigned char *ucptr; /* Address of output buffer */
+ 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 stopped = 0; /* Flag: EOD found */
+ int exval = 0; /* Exit status code */
+
+ /* 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 decoder
+ */
+ dk4rld_init(&dec, NULL);
+
+ /* Process standard input.
+ */
+ while (EOF != (ic = fgetc(stdin))) {
+ if (1 == stopped) {
+ stopped = 2;
+ /* Error: Addtional bytes after EOD marker */
+ }
+ switch (dk4rld_add(&dec, (unsigned char)ic, NULL)) {
+ case DK4_EDSTM_FINISHED : {
+ ucptr = NULL; sz = 0;
+ if (0 != dk4rld_output(&ucptr, &sz, &dec, NULL)) {
+ if ((NULL != ucptr) && (0 < sz)) {
+ for (i = 0; i < sz; i++) {
+ if (EOF == fputc((int)(ucptr[i]), stdout)) {
+ exval = 1;
+ /* Error: Failed to write to standard output */
+ }
+ }
+ }
+ }
+ } break;
+ case DK4_EDSTM_STOP : {
+ if (0 == stopped) { stopped = 1; }
+ } break;
+ case DK4_EDSTM_ERROR : {
+ exval = 1;
+ /* Error: Not properly run-length encoded data */
+ } break;
+ }
+ }
+
+ /* If successful so far, process final data from decoder
+ and check for syntax error.
+ */
+ if (0 == exval) {
+ switch (dk4rld_finish(&dec, NULL)) {
+ case DK4_EDSTM_FINISHED : {
+ ucptr = NULL; sz = 0;
+ if (0 != dk4rld_output(&ucptr, &sz, &dec, NULL)) {
+ if ((NULL != ucptr) && (0 < sz)) {
+ for (i = 0; i < sz; i++) {
+ if (EOF == fputc((int)(ucptr[i]), stdout)) {
+ exval = 1;
+ /* Error: Failed to write to standard output */
+ }
+ }
+ }
+ }
+ } break;
+ case DK4_EDSTM_ERROR : {
+ exval = 1;
+ /* Error: Not properly run-length encoded data */
+ } 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.
+ */
+ exit(exval); return exval;
+}
+