xref: /haiku/src/tools/locale/linkcatkeys.cpp (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
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 #include <vector>
9 
10 #include <Catalog.h>
11 #include <Entry.h>
12 #include <File.h>
13 #include <String.h>
14 
15 #include <DefaultCatalog.h>
16 #include <HashMapCatalog.h>
17 #include <PlainTextCatalog.h>
18 
19 using std::vector;
20 
21 void
22 usage()
23 {
24 	fprintf(stderr,
25 		"usage: linkcatkeys [-v] [-t(a|f|r)] [-o <outfile>] [-l <catalogLang>]\n"
26 		"                   -s <catalogSig> <catalogFiles>\n"
27 		"options:\n"
28 		"  -l <catalogLang>\tlanguage of the target-catalog (default is English)\n"
29 		"  -o <outfile>\t\texplicitly specifies the name of the output-file\n"
30 		"  -s <catalogSig>\tsignature of the target-catalog\n"
31 		"  -t(a|f|r)\t\tspecifies target of resulting catalog (-tf is default)\n"
32 		"           \t\ta => write catalog as an attribute (to output-file)\n"
33 		"           \t\tf => write catalog into the output-file\n"
34 		"           \t\tr => write catalog as a resource (to output-file)\n"
35 		"  -v\t\t\tbe verbose, show summary\n");
36 	exit(-1);
37 }
38 
39 
40 int
41 main(int argc, char **argv)
42 {
43 	bool showSummary = false;
44 	bool showWarnings = false;
45 	vector<const char *> inputFiles;
46 	BString outputFile("default.catalog");
47 	enum TargetType {
48 		TARGET_ATTRIBUTE,
49 		TARGET_FILE,
50 		TARGET_RESOURCE
51 	};
52 	TargetType outputTarget = TARGET_FILE;
53 	const char *catalogSig = NULL;
54 	BString catalogLang("English");
55 	status_t res;
56 	while ((++argv)[0]) {
57 		if (argv[0][0] == '-' && argv[0][1] != '-') {
58 			char *arg = argv[0] + 1;
59 			char c;
60 			while ((c = *arg++) != '\0') {
61 				if (c == 's')
62 					catalogSig = (++argv)[0];
63 				else if (c == 'l')
64 					catalogLang = (++argv)[0];
65 				else if (c == 'v')
66 					showSummary = true;
67 				else if (c == 'w')
68 					showWarnings = true;
69 				else if (c == 'o') {
70 					outputFile = (++argv)[0];
71 					break;
72 				}
73 				else if (c == 't') {
74 					switch(*arg) {
75 						case 'a': outputTarget = TARGET_ATTRIBUTE; break;
76 						case 'f': outputTarget = TARGET_FILE; break;
77 						case 'r': outputTarget = TARGET_RESOURCE; break;
78 						default: usage();
79 					}
80 				}
81 			}
82 		} else if (!strcmp(argv[0], "--help")) {
83 			usage();
84 		} else {
85 			inputFiles.push_back(argv[0]);
86 		}
87 	}
88 	if (inputFiles.empty() || !catalogSig || !outputFile.Length())
89 		usage();
90 
91 	DefaultCatalog targetCatImpl(outputFile.String(), catalogSig,
92 		catalogLang.String());
93 	if ((res = targetCatImpl.InitCheck()) != B_OK) {
94 		fprintf(stderr, "couldn't construct target-catalog %s - error: %s\n",
95 			outputFile.String(), strerror(res));
96 		exit(-1);
97 	}
98 
99 	uint32 count = inputFiles.size();
100 	for (uint32 i = 0; i < count; ++i) {
101 		PlainTextCatalog inputCatalog(inputFiles[i], catalogSig,
102 			catalogLang.String());
103 		if ((res = inputCatalog.ReadFromFile()) != B_OK) {
104 			fprintf(stderr, "couldn't load source-catalog %s - error: %s\n",
105 				inputFiles[i], strerror(res));
106 			exit(-1);
107 		}
108 
109 		// now walk over all entries in input-catalog and add them to
110 		// target catalog, unless they already exist there.
111 		BHashMapCatalog::CatWalker walker(&inputCatalog);
112 		while (!walker.AtEnd()) {
113 			const CatKey &key(walker.GetKey());
114 
115 			if (!targetCatImpl.GetString(key))
116 				targetCatImpl.SetRawString(key, walker.GetValue());
117 			walker.Next();
118 		}
119 	}
120 
121 	switch(outputTarget) {
122 		case TARGET_ATTRIBUTE: {
123 			BEntry entry(outputFile.String());
124 			entry_ref eref;
125 			entry.GetRef(&eref);
126 			res = targetCatImpl.WriteToAttribute(&eref);
127 			if (res != B_OK) {
128 				fprintf(stderr,
129 					"couldn't write target-attribute to %s - error: %s\n",
130 					outputFile.String(), strerror(res));
131 				exit(-1);
132 			}
133 			break;
134 		}
135 		case TARGET_RESOURCE: {
136 			BEntry entry(outputFile.String());
137 			entry_ref eref;
138 			entry.GetRef(&eref);
139 			res = targetCatImpl.WriteToResource(&eref);
140 			if (res != B_OK) {
141 				fprintf(stderr,
142 					"couldn't write target-resource to %s - error: %s\n",
143 					outputFile.String(), strerror(res));
144 				exit(-1);
145 			}
146 		}
147 		default: {
148 			res = targetCatImpl.WriteToFile(outputFile.String());
149 			if (res != B_OK) {
150 				fprintf(stderr,
151 					"couldn't write target-catalog to %s - error: %s\n",
152 					outputFile.String(), strerror(res));
153 				exit(-1);
154 			}
155 		}
156 	}
157 	if (showSummary) {
158 		int32 count = targetCatImpl.CountItems();
159 		if (count) {
160 			fprintf(stderr, "%d key%s found and written to %s\n",
161 				count, (count==1 ? "": "s"), outputFile.String());
162 		} else
163 			fprintf(stderr, "no keys found\n");
164 	}
165 
166 	return res;
167 }
168