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