xref: /haiku/src/tools/locale/linkcatkeys.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
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 			BString translatedString = walker.GetValue();
115 
116 			if (!targetCatImpl.GetString(key)) {
117 				targetCatImpl.SetString(key,
118 					translatedString.String());
119 			}
120 			walker.Next();
121 		}
122 	}
123 
124 	switch(outputTarget) {
125 		case TARGET_ATTRIBUTE: {
126 			BEntry entry(outputFile.String());
127 			entry_ref eref;
128 			entry.GetRef(&eref);
129 			res = targetCatImpl.WriteToAttribute(&eref);
130 			if (res != B_OK) {
131 				fprintf(stderr,
132 					"couldn't write target-attribute to %s - error: %s\n",
133 					outputFile.String(), strerror(res));
134 				exit(-1);
135 			}
136 			break;
137 		}
138 		case TARGET_RESOURCE: {
139 			BEntry entry(outputFile.String());
140 			entry_ref eref;
141 			entry.GetRef(&eref);
142 			res = targetCatImpl.WriteToResource(&eref);
143 			if (res != B_OK) {
144 				fprintf(stderr,
145 					"couldn't write target-resource to %s - error: %s\n",
146 					outputFile.String(), strerror(res));
147 				exit(-1);
148 			}
149 		}
150 		default: {
151 			res = targetCatImpl.WriteToFile(outputFile.String());
152 			if (res != B_OK) {
153 				fprintf(stderr,
154 					"couldn't write target-catalog to %s - error: %s\n",
155 					outputFile.String(), strerror(res));
156 				exit(-1);
157 			}
158 		}
159 	}
160 	if (showSummary) {
161 		int32 count = targetCatImpl.CountItems();
162 		if (count) {
163 			fprintf(stderr, "%ld key%s found and written to %s\n",
164 				count, (count==1 ? "": "s"), outputFile.String());
165 		} else
166 			fprintf(stderr, "no keys found\n");
167 	}
168 
169 	return res;
170 }
171