2 ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3 ** Distributed under the terms of the OpenBeOS License.
14 #include <EditableCatalog.h>
15 #include <DefaultCatalog.h>
16 #include <HashMapCatalog.h>
19 using BPrivate::CatKey
;
20 using BPrivate::DefaultCatalog
;
21 using BPrivate::EditableCatalog
;
22 using BPrivate::HashMapCatalog
;
30 "usage: linkcatkeys [-v] [-t(a|f|r)] [-o <outfile>] [-l <catalogLang>]\n"
31 " -s <catalogSig> <catalogFiles>\n"
33 " -l <catalogLang>\tlanguage of the target-catalog (default is English)\n"
34 " -o <outfile>\t\texplicitly specifies the name of the output-file\n"
35 " -s <catalogSig>\tsignature of the target-catalog\n"
36 " -t(a|f|r)\t\tspecifies target of resulting catalog (-tf is default)\n"
37 " \t\ta => write catalog as an attribute (to output-file)\n"
38 " \t\tf => write catalog into the output-file\n"
39 " \t\tr => write catalog as a resource (to output-file)\n"
40 " -v\t\t\tbe verbose, show summary\n");
46 main(int argc
, char **argv
)
48 bool showSummary
= false;
49 bool showWarnings
= false;
50 vector
<const char *> inputFiles
;
51 BString
outputFile("default.catalog");
57 TargetType outputTarget
= TARGET_FILE
;
58 const char *catalogSig
= NULL
;
59 BString
catalogLang("English");
62 if (argv
[0][0] == '-' && argv
[0][1] != '-') {
63 char *arg
= argv
[0] + 1;
65 while ((c
= *arg
++) != '\0') {
67 catalogSig
= (++argv
)[0];
69 catalogLang
= (++argv
)[0];
75 outputFile
= (++argv
)[0];
80 case 'a': outputTarget
= TARGET_ATTRIBUTE
; break;
81 case 'f': outputTarget
= TARGET_FILE
; break;
82 case 'r': outputTarget
= TARGET_RESOURCE
; break;
87 } else if (!strcmp(argv
[0], "--help")) {
90 inputFiles
.push_back(argv
[0]);
93 if (inputFiles
.empty() || !catalogSig
|| !outputFile
.Length())
96 EditableCatalog
targetCatalog("Default", catalogSig
, catalogLang
.String());
97 if ((res
= targetCatalog
.InitCheck()) != B_OK
) {
98 fprintf(stderr
, "couldn't construct target-catalog %s - error: %s\n",
99 outputFile
.String(), strerror(res
));
102 DefaultCatalog
* targetCatImpl
103 = dynamic_cast<DefaultCatalog
*>(targetCatalog
.CatalogData());
104 if (!targetCatImpl
) {
105 fprintf(stderr
, "couldn't access impl of target-catalog %s\n",
106 outputFile
.String());
110 uint32 count
= inputFiles
.size();
111 for( uint32 i
=0; i
<count
; ++i
) {
112 EditableCatalog
inputCatalog("plaintext", catalogSig
, "native");
113 if ((res
= inputCatalog
.ReadFromFile(inputFiles
[i
])) != B_OK
) {
114 fprintf(stderr
, "couldn't load source-catalog %s - error: %s\n",
115 inputFiles
[i
], strerror(res
));
118 HashMapCatalog
* inputCatImpl
119 = dynamic_cast<HashMapCatalog
*>(inputCatalog
.CatalogData());
121 fprintf(stderr
, "couldn't access impl of input-catalog %s\n",
126 // now walk over all entries in input-catalog and add them to
127 // target catalog, unless they already exist there.
128 HashMapCatalog::CatWalker
walker(inputCatImpl
);
129 while (!walker
.AtEnd()) {
130 const CatKey
&plainTextKey(walker
.GetKey());
131 BString keyString
, keyComment
, keyContext
;
132 plainTextKey
.GetStringParts(&keyString
,&keyComment
,&keyContext
);
133 const CatKey
fixedCatKey(keyString
.String(), keyComment
.String(),
134 keyContext
.String());
136 if (!targetCatImpl
->GetString(fixedCatKey
))
137 targetCatImpl
->SetRawString(fixedCatKey
, walker
.GetValue());
142 switch(outputTarget
) {
143 case TARGET_ATTRIBUTE
: {
144 BEntry
entry(outputFile
.String());
147 res
= targetCatalog
.WriteToAttribute(eref
);
150 "couldn't write target-attribute to %s - error: %s\n",
151 outputFile
.String(), strerror(res
));
156 case TARGET_RESOURCE
: {
157 BEntry
entry(outputFile
.String());
160 res
= targetCatalog
.WriteToResource(eref
);
163 "couldn't write target-resource to %s - error: %s\n",
164 outputFile
.String(), strerror(res
));
170 res
= targetCatalog
.WriteToFile(outputFile
.String());
173 "couldn't write target-catalog to %s - error: %s\n",
174 outputFile
.String(), strerror(res
));
181 int32 count
= targetCatalog
.CountItems();
183 fprintf(stderr
, "%" B_PRId32
" key%s found and written to %s\n",
184 count
, (count
==1 ? "": "s"), outputFile
.String());
186 fprintf(stderr
, "no keys found\n");