i18n/test/test1.cpp

Go to the documentation of this file.
00001 /*
00002  * test1.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Small test of internationalization library.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 #include <fstream>
00013 
00014 #include "i18n/i18n.h"
00015 #include "nstream/nstream.h"
00016 #include "perf/perf.h"
00017 #include "util/file.h"
00018 
00019 
00020 ////////////////////////////////////////////////////////////////////////////////
00021 //
00022 //      static helper methods
00023 //
00024 ////////////////////////////////////////////////////////////////////////////////
00025 
00026 static void
00027 displayString
00028 (
00029 IN i18n::Manager * mgr,
00030 IN const char * id
00031 )
00032 throw()
00033 {
00034         ASSERT(mgr, "null");
00035         ASSERT(id, "null");
00036 
00037         const char * val = mgr->getString(id);
00038         if (!val) {
00039                 DPRINTF("No string found with id='%s'", id);
00040                 return;
00041         }
00042 
00043         DPRINTF("String[%s] = '%s'", id, val);
00044 }
00045 
00046 
00047 
00048 static void
00049 doTest
00050 (
00051 IN const char * locale,
00052 IN const char * dir
00053 )
00054 {
00055         ASSERT(locale, "null");
00056         ASSERT(dir, "null");
00057 
00058         DPRINTF("Host locale: '%s'", i18n::getHostLocale());
00059 
00060         i18n::locale_t lc;
00061         i18n::getLocaleFromString(locale, lc);
00062         ASSERT_THROW(lc.isValid(), "should be valid locale!");
00063 
00064         smart_ptr<i18n::Manager> mgr = i18n::Manager::create(locale);
00065         ASSERT_THROW(mgr, "failed to create i18n Manager object");
00066 
00067         // open named stream
00068         smart_ptr<nstream::Manager> fsMgr = nstream::getFilesystemManager(dir);
00069         ASSERT(fsMgr, "null");
00070 
00071         SetString extensions;
00072         extensions.insert("txt");       // only parse .txt files
00073         mgr->parseFolder(fsMgr->getRoot(), &extensions);
00074 
00075         // attempt to look up some strings
00076         displayString(mgr, "id0");
00077         displayString(mgr, "id1");
00078         displayString(mgr, "xyz");
00079         displayString(mgr, "yabbadabbado");
00080 }
00081 
00082 
00083 
00084 ////////////////////////////////////////////////////////////////////////////////
00085 //
00086 //      entry point
00087 //
00088 ////////////////////////////////////////////////////////////////////////////////
00089 
00090 int
00091 main
00092 (
00093 IN int argc,
00094 IN const char * argv[]
00095 )
00096 {
00097         int retval = 0;
00098         DPRINTF("Example locale: en_US.UTF-8");
00099         ASSERT(3 == argc,
00100             "Usage: i18n-test1 <locale> <dir>");
00101 
00102         const char * locale = argv[1];
00103         const char * dir = argv[2];
00104         DPRINTF("Requested locale: %s", locale);
00105 
00106         try {
00107                 perf::Timer timer("overall timer");
00108                 doTest(locale, dir);
00109 
00110         } catch (std::exception& e) {
00111                 retval = 1;
00112         }
00113 
00114         perf::dumpTimingSummary(std::cerr);
00115 
00116         return retval;
00117 }
00118