1c3ac87e8SOliver Tappe /* 2c3ac87e8SOliver Tappe ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved. 3c3ac87e8SOliver Tappe ** Distributed under the terms of the OpenBeOS License. 4c3ac87e8SOliver Tappe */ 5c3ac87e8SOliver Tappe 6c3ac87e8SOliver Tappe #include <assert.h> 7c3ac87e8SOliver Tappe #include <stdio.h> 8*75f15221SOliver Tappe #include <stdlib.h> 9c3ac87e8SOliver Tappe #include <unistd.h> 10c3ac87e8SOliver Tappe 11c3ac87e8SOliver Tappe #include <Application.h> 12c3ac87e8SOliver Tappe #include <Catalog.h> 13c3ac87e8SOliver Tappe #include <DefaultCatalog.h> 14c3ac87e8SOliver Tappe #include <Entry.h> 15c3ac87e8SOliver Tappe #include <Locale.h> 16c3ac87e8SOliver Tappe #include <Path.h> 17c3ac87e8SOliver Tappe #include <Roster.h> 18c3ac87e8SOliver Tappe 19c3ac87e8SOliver Tappe class CatalogTest { 20c3ac87e8SOliver Tappe public: 21c3ac87e8SOliver Tappe void Run(); 22c3ac87e8SOliver Tappe void Check(); 23c3ac87e8SOliver Tappe }; 24c3ac87e8SOliver Tappe 25c3ac87e8SOliver Tappe #define TR_CONTEXT "CatalogTest" 26c3ac87e8SOliver Tappe 27c3ac87e8SOliver Tappe 28c3ac87e8SOliver Tappe #define catSig "x-vnd.Be.locale.catalogTest" 29c3ac87e8SOliver Tappe #define catName catSig".catalog" 30c3ac87e8SOliver Tappe 31c3ac87e8SOliver Tappe void 32c3ac87e8SOliver Tappe CatalogTest::Run() 33c3ac87e8SOliver Tappe { 34c3ac87e8SOliver Tappe printf("app..."); 35c3ac87e8SOliver Tappe status_t res; 36c3ac87e8SOliver Tappe BString s; 37c3ac87e8SOliver Tappe s << "string" << "\x01" << TR_CONTEXT << "\x01"; 38c3ac87e8SOliver Tappe size_t hashVal = CatKey::HashFun(s.String()); 39c3ac87e8SOliver Tappe assert(be_locale != NULL); 40c3ac87e8SOliver Tappe system("mkdir -p ./locale/catalogs/"catSig); 41c3ac87e8SOliver Tappe 42c3ac87e8SOliver Tappe // create an empty catalog of default type... 43c3ac87e8SOliver Tappe BPrivate::EditableCatalog cata("Default", catSig, "German"); 44c3ac87e8SOliver Tappe assert(cata.InitCheck() == B_OK); 45c3ac87e8SOliver Tappe 46c3ac87e8SOliver Tappe // ...and populate the catalog with some data: 47c3ac87e8SOliver Tappe res = cata.SetString("string", "Schnur", TR_CONTEXT); 48c3ac87e8SOliver Tappe assert(res == B_OK); 49c3ac87e8SOliver Tappe res = cata.SetString(hashVal, "Schnur_id"); 50c3ac87e8SOliver Tappe // add a second entry for the same hash-value, but with different 51c3ac87e8SOliver Tappe // translation 52c3ac87e8SOliver Tappe assert(res == B_OK); 53c3ac87e8SOliver Tappe res = cata.SetString("string", "String", "programming"); 54c3ac87e8SOliver Tappe assert(res == B_OK); 55c3ac87e8SOliver Tappe res = cata.SetString("string", "Textpuffer", "programming", 56c3ac87e8SOliver Tappe "Deutsches Fachbuch"); 57c3ac87e8SOliver Tappe assert(res == B_OK); 58c3ac87e8SOliver Tappe res = cata.SetString("string", "Leine", TR_CONTEXT, "Deutsches Fachbuch"); 59c3ac87e8SOliver Tappe assert(res == B_OK); 60c3ac87e8SOliver Tappe res = cata.WriteToFile("./locale/catalogs/"catSig"/german.catalog"); 61c3ac87e8SOliver Tappe assert(res == B_OK); 62c3ac87e8SOliver Tappe 63c3ac87e8SOliver Tappe // check if we are getting back the correct strings: 64c3ac87e8SOliver Tappe s = cata.GetString(("string"), TR_CONTEXT); 65c3ac87e8SOliver Tappe assert(s == "Schnur"); 66c3ac87e8SOliver Tappe s = cata.GetString(hashVal); 67c3ac87e8SOliver Tappe assert(s == "Schnur_id"); 68c3ac87e8SOliver Tappe s = cata.GetString("string", "programming"); 69c3ac87e8SOliver Tappe assert(s == "String"); 70c3ac87e8SOliver Tappe s = cata.GetString("string", "programming", "Deutsches Fachbuch"); 71c3ac87e8SOliver Tappe assert(s == "Textpuffer"); 72c3ac87e8SOliver Tappe s = cata.GetString("string", TR_CONTEXT, "Deutsches Fachbuch"); 73c3ac87e8SOliver Tappe assert(s == "Leine"); 74c3ac87e8SOliver Tappe 75c3ac87e8SOliver Tappe // now we create a new (base) catalog and embed this one into the app-file: 76c3ac87e8SOliver Tappe BPrivate::EditableCatalog catb("Default", catSig, "English"); 77c3ac87e8SOliver Tappe assert(catb.InitCheck() == B_OK); 78c3ac87e8SOliver Tappe // the following string is unique to the embedded catalog: 79c3ac87e8SOliver Tappe res = catb.SetString("string", "string", "base"); 80c3ac87e8SOliver Tappe assert(res == B_OK); 81c3ac87e8SOliver Tappe // the following id is unique to the embedded catalog: 82c3ac87e8SOliver Tappe res = catb.SetString(32, "hashed string"); 83c3ac87e8SOliver Tappe assert(res == B_OK); 84c3ac87e8SOliver Tappe // the following string will be hidden by the definition inside the 85c3ac87e8SOliver Tappe // german catalog: 86c3ac87e8SOliver Tappe res = catb.SetString("string", "hidden", TR_CONTEXT); 87c3ac87e8SOliver Tappe assert(res == B_OK); 88c3ac87e8SOliver Tappe app_info appInfo; 89c3ac87e8SOliver Tappe res = be_app->GetAppInfo(&appInfo); 90c3ac87e8SOliver Tappe assert(res == B_OK); 91c3ac87e8SOliver Tappe // embed created catalog into application file (catalogTest): 92c3ac87e8SOliver Tappe res = catb.WriteToResource(&appInfo.ref); 93c3ac87e8SOliver Tappe assert(res == B_OK); 94c3ac87e8SOliver Tappe 95c3ac87e8SOliver Tappe printf("ok.\n"); 96c3ac87e8SOliver Tappe Check(); 97c3ac87e8SOliver Tappe } 98c3ac87e8SOliver Tappe 99c3ac87e8SOliver Tappe 100c3ac87e8SOliver Tappe void 101c3ac87e8SOliver Tappe CatalogTest::Check() 102c3ac87e8SOliver Tappe { 103c3ac87e8SOliver Tappe status_t res; 104c3ac87e8SOliver Tappe printf("app-check..."); 105c3ac87e8SOliver Tappe BString s; 106c3ac87e8SOliver Tappe s << "string" << "\x01" << TR_CONTEXT << "\x01"; 107c3ac87e8SOliver Tappe size_t hashVal = CatKey::HashFun(s.String()); 108c3ac87e8SOliver Tappe // ok, we now try to re-load the catalog that has just been written: 109c3ac87e8SOliver Tappe // 110c3ac87e8SOliver Tappe // actually, the following code can be seen as an example of what an 111c3ac87e8SOliver Tappe // app needs in order to translate strings: 112c3ac87e8SOliver Tappe BCatalog cat; 113c3ac87e8SOliver Tappe res = be_locale->GetAppCatalog(&cat); 114c3ac87e8SOliver Tappe assert(res == B_OK); 115c3ac87e8SOliver Tappe // fetch basic data: 116*75f15221SOliver Tappe uint32 fingerprint = 0; 117c3ac87e8SOliver Tappe res = cat.GetFingerprint(&fingerprint); 118c3ac87e8SOliver Tappe assert(res == B_OK); 119c3ac87e8SOliver Tappe BString lang; 120c3ac87e8SOliver Tappe res = cat.GetLanguage(&lang); 121c3ac87e8SOliver Tappe assert(res == B_OK); 122c3ac87e8SOliver Tappe BString sig; 123c3ac87e8SOliver Tappe res = cat.GetSignature(&sig); 124c3ac87e8SOliver Tappe assert(res == B_OK); 125c3ac87e8SOliver Tappe 126c3ac87e8SOliver Tappe // now check strings: 127c3ac87e8SOliver Tappe s = TR_ID(hashVal); 128c3ac87e8SOliver Tappe assert(s == "Schnur_id"); 129c3ac87e8SOliver Tappe s = TR_ALL("string", "programming", ""); 130c3ac87e8SOliver Tappe assert(s == "String"); 131c3ac87e8SOliver Tappe s = TR_ALL("string", "programming", "Deutsches Fachbuch"); 132c3ac87e8SOliver Tappe assert(s == "Textpuffer"); 133c3ac87e8SOliver Tappe s = TR_CMT("string", "Deutsches Fachbuch"); 134c3ac87e8SOliver Tappe assert(s == "Leine"); 135c3ac87e8SOliver Tappe // the following string should be found in the embedded catalog only: 136c3ac87e8SOliver Tappe s = TR_ALL("string", "base", NULL); 137c3ac87e8SOliver Tappe assert(s == "string"); 138c3ac87e8SOliver Tappe // the following id should be found in the embedded catalog only: 139c3ac87e8SOliver Tappe s = TR_ID(32); 140c3ac87e8SOliver Tappe assert(s == "hashed string"); 141c3ac87e8SOliver Tappe // the following id doesn't exist anywhere (hopefully): 142c3ac87e8SOliver Tappe s = TR_ID(-1); 143c3ac87e8SOliver Tappe assert(s == ""); 144c3ac87e8SOliver Tappe // the following string exists twice, in the embedded as well as in the 145c3ac87e8SOliver Tappe // external catalog. So we should get the external translation (as it should 146c3ac87e8SOliver Tappe // override the embedded one): 147c3ac87e8SOliver Tappe s = TR("string"); 148c3ac87e8SOliver Tappe assert(s == "Schnur"); 149c3ac87e8SOliver Tappe 150c3ac87e8SOliver Tappe // now check if trying to access same catalog by specifying its data works: 151c3ac87e8SOliver Tappe BCatalog cat2(sig.String(), lang.String(), fingerprint); 152c3ac87e8SOliver Tappe assert(cat2.InitCheck() == B_OK); 153c3ac87e8SOliver Tappe // now check if trying to access same catalog with wrong fingerprint fails: 154c3ac87e8SOliver Tappe BCatalog cat3(sig.String(), lang.String(), fingerprint*-1); 155c3ac87e8SOliver Tappe assert(cat3.InitCheck() == B_NO_INIT); 156c3ac87e8SOliver Tappe // translating through an invalid catalog should yield the native string: 157c3ac87e8SOliver Tappe s = cat3.GetString("string"); 158c3ac87e8SOliver Tappe assert(s == "string"); 159c3ac87e8SOliver Tappe 160c3ac87e8SOliver Tappe printf("ok.\n"); 161c3ac87e8SOliver Tappe } 162c3ac87e8SOliver Tappe 163c3ac87e8SOliver Tappe 164c3ac87e8SOliver Tappe int 165c3ac87e8SOliver Tappe main(int argc, char **argv) 166c3ac87e8SOliver Tappe { 167c3ac87e8SOliver Tappe BApplication* testApp 168c3ac87e8SOliver Tappe = new BApplication("application/"catSig); 169c3ac87e8SOliver Tappe 170c3ac87e8SOliver Tappe // change to app-folder: 171c3ac87e8SOliver Tappe app_info appInfo; 172c3ac87e8SOliver Tappe be_app->GetAppInfo(&appInfo); 173c3ac87e8SOliver Tappe BEntry appEntry(&appInfo.ref); 174c3ac87e8SOliver Tappe BEntry appFolder; 175c3ac87e8SOliver Tappe appEntry.GetParent( &appFolder); 176c3ac87e8SOliver Tappe BPath appPath; 177c3ac87e8SOliver Tappe appFolder.GetPath( &appPath); 178c3ac87e8SOliver Tappe chdir( appPath.Path()); 179c3ac87e8SOliver Tappe 180c3ac87e8SOliver Tappe CatalogTest catTest; 181c3ac87e8SOliver Tappe catTest.Run(); 182c3ac87e8SOliver Tappe 183c3ac87e8SOliver Tappe char cwd[B_FILE_NAME_LENGTH]; 184c3ac87e8SOliver Tappe getcwd(cwd, B_FILE_NAME_LENGTH); 185c3ac87e8SOliver Tappe BString addonName(cwd); 186c3ac87e8SOliver Tappe addonName << "/" "catalogTestAddOn"; 187c3ac87e8SOliver Tappe image_id image = load_add_on(addonName.String()); 188c3ac87e8SOliver Tappe assert(image >= B_OK); 189c3ac87e8SOliver Tappe void (*runAddonFunc)() = 0; 190c3ac87e8SOliver Tappe get_image_symbol(image, "run_test_add_on", 191c3ac87e8SOliver Tappe B_SYMBOL_TYPE_TEXT, (void **)&runAddonFunc); 192c3ac87e8SOliver Tappe assert(runAddonFunc); 193c3ac87e8SOliver Tappe runAddonFunc(); 194c3ac87e8SOliver Tappe 195c3ac87e8SOliver Tappe catTest.Check(); 196c3ac87e8SOliver Tappe 197c3ac87e8SOliver Tappe delete testApp; 198c3ac87e8SOliver Tappe } 199