File: buffer.c

package info (click to toggle)
dwdiff 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 540 kB
  • sloc: ansic: 4,072; sh: 685; makefile: 83; python: 20
file content (233 lines) | stat: -rw-r--r-- 7,400 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* Copyright (C) 2007-2011 G.P. Halkes
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3, as
   published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "definitions.h"
#include "util.h"
#include "option.h"
#include "buffer.h"

typedef enum {
	PRINTING_CHANGED,
	PRINTING_AFTER_CONTEXT,
	PRE_BUFFERING,
	BUFFERING,
	/* The initial states are only used at the start of printing, to prevent
	   printing of the separator string (--) before the first block of output. */
	PRE_BUFFERING_INITIAL,
	BUFFERING_INITIAL
} PrintState;

static CharBuffer *contextBuffers;
static PrintState state = PRE_BUFFERING_INITIAL;
static int bufferIndex;
static int afterContextLines;

/** Initialize the context buffers. */
void initContextBuffers(void) {
	int i;

	errno = 0;
	contextBuffers = (CharBuffer *) safe_malloc((option.contextLines + 1) * sizeof(CharBuffer));

	for (i = 0; i <= option.contextLines; i++)
		VECTOR_INIT(contextBuffers[i]);
}

/** Write a character, buffering if necessary.
	@param c The character to write.
	@param common Boolean to indicate whether the character belongs to both
		the old and the new file.
*/
void addchar(char c, bool common) {
	/* Using ignore variable to shut up gcc about the warn_unused_result attribute set on fwrite. */
	int i, ignore;

	if (!option.context) {
		putc(c, option.output);
		return;
	}

	/* Check whether we have buffered output that we need to print because of
	   encountering a non-common character. */
	if (!common) {
		switch (state) {
			case PRINTING_CHANGED:
			case PRINTING_AFTER_CONTEXT:
				break;
			/* If we have entered a regular buffering state, all buffers are in use.
			   However, the set of buffers is used as a ring buffer so first print all
			   buffers after the current one, then print all buffers before the current. */
			case BUFFERING:
				ignore = fwrite("--\n", 1, 3, option.output);
        /* FALLTHROUGH */
			case BUFFERING_INITIAL:
				for (i = bufferIndex + 1; i <= option.contextLines; i++)
					ignore = fwrite(contextBuffers[i].data, 1, contextBuffers[i].used, option.output);
				/* FALLTHROUGH */
			/* In pre-buffering state, only the first buffers are used. Once we start
			   wrapping around, we change state to a regular buffering state. */
			case PRE_BUFFERING:
			case PRE_BUFFERING_INITIAL:
				for (i = 0; i <= bufferIndex; i++)
					ignore = fwrite(contextBuffers[i].data, 1, contextBuffers[i].used, option.output);
				break;
			default:
				PANIC();
		}
		state = PRINTING_CHANGED;
	}

	/* Decided whether to buffer or print the character. */
	switch (state) {
		case BUFFERING:
		case PRE_BUFFERING:
		case BUFFERING_INITIAL:
		case PRE_BUFFERING_INITIAL:
			VECTOR_APPEND(contextBuffers[bufferIndex], c);
			break;
		case PRINTING_AFTER_CONTEXT:
		case PRINTING_CHANGED:
			putc(c, option.output);
			break;
		default:
			PANIC();
	}

	if (c == '\n') {
		switch (state) {
			case PRINTING_CHANGED:
				if (option.contextLines == 0) {
					state = PRE_BUFFERING;
					break;
				}
				state = PRINTING_AFTER_CONTEXT;
				afterContextLines = 0;
				break;
			case PRINTING_AFTER_CONTEXT:
				afterContextLines++;
				if (afterContextLines == option.contextLines) {
					state = PRE_BUFFERING;
					bufferIndex = 0;
				}
				break;
			case PRE_BUFFERING_INITIAL:
			case PRE_BUFFERING:
				bufferIndex++;
				/* Once we have filled all buffers, we change to a regular buffering state. */
				if (bufferIndex > option.contextLines) {
					state = state == PRE_BUFFERING ? BUFFERING : BUFFERING_INITIAL;
					bufferIndex = 0;
				}
				break;
			case BUFFERING_INITIAL:
			case BUFFERING:
				bufferIndex++;
				if (bufferIndex > option.contextLines)
					bufferIndex = 0;
				break;
			default:
				PANIC();
		}
		/* We just saw a newline, so we have either entered a buffering state,
		   or already were in one, and finished filling the old buffer. The new
		   buffer must be filled from the start. */
		contextBuffers[bufferIndex].used = 0;
	}
}

/* Macro to ensure we use the same format and arguments for printing the line
   number information in all print statements. */
#define LINENUMBERS_FMT_ARGS "%*d:%-*d ", option.lineNumbers, oldLineNumber, option.lineNumbers, newLineNumber

/** Print line number information, buffering if necessary.
	@param oldLineNumber The line number in the old file.
	@param newLineNumber The line number in the new file.
*/
void printLineNumbers(int oldLineNumber, int newLineNumber) {
	int printed, allowed;

	if (!option.context) {
		printf(LINENUMBERS_FMT_ARGS);
		return;
	}

	switch (state) {
		case BUFFERING:
		case PRE_BUFFERING:
		case BUFFERING_INITIAL:
		case PRE_BUFFERING_INITIAL:
			allowed = contextBuffers[bufferIndex].allocated - contextBuffers[bufferIndex].used;
			/* SUSv2 specification of snprintf does not handle zero sized buffers nicely :-(
			   Therefore, we have to ensure that there is at least room for one byte, but as
			   we will be printing 2 numbers and a colon and a space, we might as well ask for
			   4 bytes. */
			VECTOR_ALLOCATE(contextBuffers[bufferIndex], 4);
			printed = snprintf(contextBuffers[bufferIndex].data + contextBuffers[bufferIndex].used, allowed, LINENUMBERS_FMT_ARGS);
			/* If there was not enough room to hold all the bytes for the line numbers,
			   resize the buffer and try again. */
			if (printed > allowed) {
				VECTOR_ALLOCATE(contextBuffers[bufferIndex], printed);
				allowed = contextBuffers[bufferIndex].allocated - contextBuffers[bufferIndex].used;
				printed = snprintf(contextBuffers[bufferIndex].data + contextBuffers[bufferIndex].used, allowed, LINENUMBERS_FMT_ARGS);
			}
			/* Sanity check: the number of characters printed should be at least 2
			   [probably 4, but I don't know all possible number systems]. */
			ASSERT(printed >= 2);
			contextBuffers[bufferIndex].used += printed;
			break;
		case PRINTING_AFTER_CONTEXT:
		case PRINTING_CHANGED:
			printf(LINENUMBERS_FMT_ARGS);
			break;
		default:
			PANIC();
	}
}

/** Print a string, buffering if necessary.
	@param string The string to print.
	@param bytes The length of the string.

	The string should not contain newline characters.
*/
void writeString(const char *string, size_t bytes) {
	/* Using ignore variable to shut up gcc about the warn_unused_result attribute set on fwrite. */
	int ignore;
	if (!option.context) {
		ignore = fwrite(string, 1, bytes, option.output);
		return;
	}

	switch (state) {
		case BUFFERING:
		case PRE_BUFFERING:
		case BUFFERING_INITIAL:
		case PRE_BUFFERING_INITIAL:
			VECTOR_ALLOCATE(contextBuffers[bufferIndex], bytes);
			memcpy(contextBuffers[bufferIndex].data + contextBuffers[bufferIndex].used, string, bytes);
			contextBuffers[bufferIndex].used += bytes;
			break;
		case PRINTING_AFTER_CONTEXT:
		case PRINTING_CHANGED:
			ignore = fwrite(string, 1, bytes, option.output);
			break;
		default:
			PANIC();
	}
}