summaryrefslogtreecommitdiff
path: root/support/dktools/test-a85e.c
blob: 98ac10a8a285186ab79f50b1738b85e52efee766 (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
130
131
132
133
134
/*
	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-a85e.ctr
*/


#line 1 "test-a85e.ctr"
/**	@file	test-a85e.c
	Convert binary input from stdin to ASCII-85 encoded text on stdout.

	This is an example how to used the dk4_a85_enc_t type.
*/

#include "dk4conf.h"

#include <stdio.h>
#if DK4_ON_WINDOWS
#include <io.h>
#endif

#include "dk4a85e.h"
#include "dk4edstm.h"





#line 20 "test-a85e.ctr"



int main(int argc, char *argv[])
{
  dk4_a85_enc_t	 a85e;		/* Encoder */
  const	char	*dptr;		/* Pointer to encoder output buffer */
  size_t	 sz;		/* Size of encoder output buffer */
  size_t	 i;		/* Current output buffer index to process */
  int		 c;		/* Input character */
  int		 linepos = 0;	/* Position within output text line */
#if DK4_ON_WINDOWS
  int		 oldmode;	/* Previous file mode for stdin */
#endif

  

#line 36 "test-a85e.ctr"
  

#line 37 "test-a85e.ctr"

  /*	Set file mode for standard input to binary on Windows.
  */
#if DK4_ON_WINDOWS
  oldmode = _setmode(0, _O_BINARY);
#endif

  /*	Initialize encoder.
  */
  dk4a85_enc_init(&a85e, 1, NULL);

  /*	Process contents from stdin.
  */
  while (EOF != (c = fgetc(stdin))) {
    switch (dk4a85_enc_add(&a85e, (unsigned char)c, NULL)) {
      case DK4_EDSTM_FINISHED: {
        /*	Encoder indicates that output is available.
	*/
        dptr = NULL; sz = 0;
	if (0 != dk4a85_enc_output(&dptr, &sz, &a85e, NULL)) {
	  if ((NULL != dptr) && (0 < sz)) {
	    /*	Output really found, write to stdout.
	    */
	    for (i = 0; i < sz; i++) {
	      fputc(dptr[i], stdout);
	      if (++linepos >= 75) {
	        fputc('\n', stdout);
		linepos = 0;
	      }
	    }
	  }
	}
      } break;
    }
  }

  /*	Restore previous file mode for stdin on Windows
  */
#if DK4_ON_WINDOWS
  _setmode(0, oldmode);
#endif

  /*	Check for final unprocessed sequence in encoder.
  */
  switch (dk4a85_enc_finish(&a85e, NULL)) {
    case DK4_EDSTM_FINISHED: {
      /*	Encoder indicates that output is available.
      */
      dptr = NULL; sz = 0;
      if (0 != dk4a85_enc_output(&dptr, &sz, &a85e, NULL)) {
        if ((NULL != dptr) && (0 < sz)) {
	  /*  Output really found, write to stdout.
	  */
	  for (i = 0; i < sz; i++) {
	    fputc(dptr[i], stdout);
	    if (++linepos >= 75) {
	      fputc('\n', stdout);
	      linepos = 0;
	    }
	  }
	}
      }
    } break;
  }

  /*	Write EOD marker to output.
  */
  if (74 <= linepos) {
    fputc('\n', stdout);
  }
  fputs("~>\n", stdout);

  

#line 110 "test-a85e.ctr"
  

#line 111 "test-a85e.ctr"

  exit(0); return 0;
}