summaryrefslogtreecommitdiff
path: root/support/dktools/test-a85d.ctr
blob: febb9e2df6d13d90613a982ae343cd293ca9e881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**	@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 <stdio.h>
#if DK4_ON_WINDOWS
#include <io.h>
#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;
}