/** @file test-dk4a85d.c Decoding ASCII-85 encoded data. The program converts ASCII-85 encoded text read from stdin and writes binary data to stdout. This file is an example how to use the dk4_a85_dec_t type. */ #include "dk4conf.h" #include #if DK4_ON_WINDOWS #include #endif #include "dk4a85d.h" #include "dk4edstm.h" $!trace-include int main(int argc, char *argv[]) { dk4_a85_dec_t a85d; /* Decoder */ const unsigned char *uptr; /* Pointer to decoders output buffer */ size_t sz; /* Decoder output buffer size */ size_t i; /* Current output buffer index */ int c; /* Input character from stdin */ int cc = 1; /* Flag: Can continue */ int exval = 0; /* Exit status code */ #if DK4_ON_WINDOWS int oldmode; /* Previous file mode for stdout */ #endif $!trace-init test-a85d.deb $? "+ main" /* Initialize decoder. */ dk4a85_dec_init(&a85d, NULL); /* Change file mode for stdout to binary on Windows. */ #if DK4_ON_WINDOWS oldmode = _setmode(_fileno(stdout), _O_BINARY); #endif /* Process standard input contents. */ while (1 == cc) { c = fgetc(stdin); if (EOF != c) { switch (dk4a85_dec_add(&a85d, c, NULL)) { case DK4_EDSTM_FINISHED : { /* Decoder indicates that output is available. */ uptr = NULL; sz = 0; if (1 == dk4a85_dec_output(&uptr, &sz, &a85d, NULL)) { if ((NULL != uptr) && (0 < sz)) { /* Output really found, write to stdout. */ for (i = 0; i < sz; i++) { if (EOF == fputc(uptr[i], stdout)) { exval = 1; /* ERROR: Failed to write output */ } } } } } break; case DK4_EDSTM_STOP : { /* EOD marker found, stop processing. */ cc = 0; } break; case DK4_EDSTM_ERROR : { /* ERROR: Decoding error occured. */ exval = 1; cc = -1; } break; } } else { cc = 0; } } /* Check for incomplete final sequence in decoder. */ if (0 == cc) { switch (dk4a85_dec_finish(&a85d, NULL)) { case DK4_EDSTM_FINISHED : { /* Decoder indicates that output is available. */ uptr = NULL; sz = 0; if (1 == dk4a85_dec_output(&uptr, &sz, &a85d, NULL)) { if ((NULL != uptr) && (0 < sz)) { /* Output really found, write to stdout. */ for (i = 0; i < sz; i++) { if (EOF == fputc(uptr[i], stdout)) { exval = 1; /* ERROR: Failed to write output */ } } } } } break; case DK4_EDSTM_ERROR : { exval = 1; /* ERROR: Decoding error occured */ } break; } } /* Restore previous file mode on Windows. */ fflush(stdout); #if DK4_ON_WINDOWS _setmode(_fileno(stdout), oldmode); #endif $? "- main 0" $!trace-end exit(exval); return exval; }