1/*
2** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3** Distributed under the terms of the MIT License.
4*/
5
6#include <cstdio>
7#include <cstdlib>
8
9#include <File.h>
10#include <String.h>
11
12#include <DefaultCatalog.h>
13#include <EditableCatalog.h>
14
15
16using BPrivate::CatKey;
17using BPrivate::DefaultCatalog;
18using BPrivate::EditableCatalog;
19
20
21void
22usage()
23{
24	fprintf(stderr, "usage: dumpcatalog <catalogFiles>\n");
25	exit(-1);
26}
27
28
29int
30main(int argc, char **argv)
31{
32	const char *inputFile = NULL;
33	status_t res;
34	if (!argv[1] || !strcmp(argv[1], "--help")) {
35		usage();
36	} else {
37		inputFile = argv[1];
38	}
39	if (!inputFile || !strlen(inputFile))
40		usage();
41
42	EditableCatalog inputCatalog("Default", "dummy", "dummy");
43	if ((res = inputCatalog.InitCheck()) != B_OK) {
44		fprintf(stderr, "couldn't construct catalog %s - error: %s\n",
45			inputFile, strerror(res));
46		exit(-1);
47	}
48	if ((res = inputCatalog.ReadFromFile(inputFile)) != B_OK) {
49		fprintf(stderr, "couldn't load input-catalog %s - error: %s\n",
50			inputFile, strerror(res));
51		exit(-1);
52	}
53	DefaultCatalog* inputCatImpl
54		= dynamic_cast<DefaultCatalog*>(inputCatalog.CatalogData());
55	if (!inputCatImpl) {
56		fprintf(stderr, "couldn't access impl of input-catalog %s\n",
57			inputFile);
58		exit(-1);
59	}
60	// now walk over all entries in input-catalog and dump them to
61	// stdout
62	DefaultCatalog::CatWalker walker(inputCatImpl);
63	BString str, ctx, cmt;
64	while (!walker.AtEnd()) {
65		const CatKey &key(walker.GetKey());
66		key.GetStringParts(&str, &ctx, &cmt);
67		printf("Hash:\t\t%" B_PRIu32 "\nKey:\t\t<%s:%s:%s>\nTranslation:\t%s\n"
68			"-----\n", key.fHashVal, str.String(), ctx.String(), cmt.String(),
69			walker.GetValue());
70		walker.Next();
71	}
72	int32 count = inputCatalog.CountItems();
73	if (count) {
74		fprintf(stderr, "%" B_PRId32 " entr%s dumped\n", count,
75			(count==1 ? "y": "ies"));
76	} else
77		fprintf(stderr, "no entries found\n");
78	return res;
79}
80