summaryrefslogtreecommitdiff
path: root/test/inputstream.c
blob: 3a8341905b6a437a641a96741a486faa460dbdd0 (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
#include <inttypes.h>
#include <stdio.h>

#include <hubbub/hubbub.h>

#include "utils/utils.h"

#include "input/inputstream.h"

#include "testutils.h"

static void buffer_moved_handler(const uint8_t *buffer, size_t len,
		void *pw);

static void *myrealloc(void *ptr, size_t len, void *pw)
{
	UNUSED(pw);

	return realloc(ptr, len);
}

int main(int argc, char **argv)
{
	hubbub_inputstream *stream;
	FILE *fp;
	size_t len, origlen;
#define CHUNK_SIZE (4096)
	uint8_t buf[CHUNK_SIZE];
	uint8_t *isb;
	size_t isblen;
	uint32_t c;

	if (argc != 3) {
		printf("Usage: %s <aliases_file> <filename>\n", argv[0]);
		return 1;
	}

	/* Initialise library */
	assert(hubbub_initialise(argv[1], myrealloc, NULL) == HUBBUB_OK);

	stream = hubbub_inputstream_create("UTF-8", "UTF-8", myrealloc, NULL);
	assert(stream != NULL);

	assert(hubbub_inputstream_register_movehandler(stream,
			buffer_moved_handler, NULL) == HUBBUB_OK);

	fp = fopen(argv[2], "rb");
	if (fp == NULL) {
		printf("Failed opening %s\n", argv[2]);
		return 1;
	}

	fseek(fp, 0, SEEK_END);
	origlen = len = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	while (len >= CHUNK_SIZE) {
		fread(buf, 1, CHUNK_SIZE, fp);

		assert(hubbub_inputstream_append(stream,
				buf, CHUNK_SIZE) == HUBBUB_OK);

		len -= CHUNK_SIZE;

		while ((c = hubbub_inputstream_peek(stream)) !=
				HUBBUB_INPUTSTREAM_OOD) {
			size_t len;
			hubbub_inputstream_cur_pos(stream, &len);
			hubbub_inputstream_advance(stream);
			assert(hubbub_inputstream_push_back(stream, c) ==
					HUBBUB_OK);
			hubbub_inputstream_advance(stream);
		}
	}

	if (len > 0) {
		fread(buf, 1, len, fp);

		assert(hubbub_inputstream_append(stream,
				buf, len) == HUBBUB_OK);

		len = 0;
	}

	fclose(fp);

	assert(hubbub_inputstream_insert(stream,
			(const uint8_t *) "hello!!!",
			SLEN("hello!!!")) == HUBBUB_OK);

	assert(hubbub_inputstream_append(stream, NULL, 0) == HUBBUB_OK);

	while (hubbub_inputstream_peek(stream) !=
			HUBBUB_INPUTSTREAM_EOF) {
		size_t len;
		hubbub_inputstream_cur_pos(stream, &len);
		hubbub_inputstream_advance(stream);
	}

	assert(hubbub_inputstream_claim_buffer(stream, &isb, &isblen) ==
			HUBBUB_OK);

	printf("Input size: %zu, Output size: %zu\n", origlen, isblen);
	printf("Buffer at %p\n", isb);

	free(isb);

	assert(hubbub_inputstream_deregister_movehandler(stream,
			buffer_moved_handler, NULL) == HUBBUB_OK);

	hubbub_inputstream_destroy(stream);

	assert(hubbub_finalise(myrealloc, NULL) == HUBBUB_OK);

	printf("PASS\n");

	return 0;
}

void buffer_moved_handler(const uint8_t *buffer, size_t len,
		void *pw)
{
	UNUSED(pw);

	printf("Buffer moved to: %p (%zu)\n", buffer, len);
}