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