summaryrefslogtreecommitdiff
path: root/support/dktools/test-rle.c
blob: 5aa2f27288252397ac824ef38d87472464809a7d (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
	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 <stdio.h>
#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;
}