1/*
2 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include <File.h>
7#include <Message.h>
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13
14int
15main(int argc, char *argv[])
16{
17	if (argc < 2 || argc > 3) {
18		printf("usage: %s <flattened message file> [index]\n", argv[0]);
19		return 1;
20	}
21
22	BFile input(argv[1], B_READ_ONLY);
23	if (!input.IsReadable()) {
24		printf("cannot open \"%s\" for reading\n", argv[1]);
25		return 2;
26	}
27
28	off_t fileSize;
29	status_t result;
30	if ((result = input.GetSize(&fileSize)) != B_OK) {
31		printf("cannot determine size of file \"%s\"\n", argv[1]);
32		return 3;
33	}
34
35	int index = argc > 2 ? atoi(argv[2]) : 0;
36
37	for (int i = 1; input.Position() < fileSize; ++i) {
38		BMessage message;
39		result = message.Unflatten(&input);
40		if (result != B_OK) {
41			printf("failed to unflatten message: %s\n", strerror(result));
42			return 4;
43		}
44		if (index == 0 || i == index)
45			message.PrintToStream();
46	}
47
48	return 0;
49}
50