diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-09 18:20:30 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-09 18:20:30 +0000 |
commit | fb895c694e2117c29b6afb699095f6e187a44da7 (patch) | |
tree | 04a0d1434a470f55f0e639a3e6f15c18416d80e2 /base/i18n/icu_util.cc | |
parent | 8ecb6aa0a92d5426c2c98c23e0e3f3c4f06972c5 (diff) | |
download | chromium_src-fb895c694e2117c29b6afb699095f6e187a44da7.zip chromium_src-fb895c694e2117c29b6afb699095f6e187a44da7.tar.gz chromium_src-fb895c694e2117c29b6afb699095f6e187a44da7.tar.bz2 |
Move more ICU-dependent stuff from base into base/i18n. Some test stuff also
depended on this, so to make the DEPS work out, I made a new base/test
directory where I moved the testing-related files into a new directory
base/test.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/266038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28569 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/i18n/icu_util.cc')
-rw-r--r-- | base/i18n/icu_util.cc | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/base/i18n/icu_util.cc b/base/i18n/icu_util.cc new file mode 100644 index 0000000..6239a01 --- /dev/null +++ b/base/i18n/icu_util.cc @@ -0,0 +1,99 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/i18n/icu_util.h" + +#include "build/build_config.h" + +#if defined(OS_WIN) +#include <windows.h> +#endif + +#include <string> + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/path_service.h" +#include "base/string_util.h" +#include "base/sys_string_conversions.h" +#include "unicode/putil.h" +#include "unicode/udata.h" + +#define ICU_UTIL_DATA_FILE 0 +#define ICU_UTIL_DATA_SHARED 1 +#define ICU_UTIL_DATA_STATIC 2 + +#ifndef ICU_UTIL_DATA_IMPL + +#if defined(OS_WIN) +#define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_SHARED +#elif defined(OS_MACOSX) +#define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_STATIC +#elif defined(OS_LINUX) +#define ICU_UTIL_DATA_IMPL ICU_UTIL_DATA_FILE +#endif + +#endif // ICU_UTIL_DATA_IMPL + +#if defined(OS_WIN) +#define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" +#define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt" U_ICU_VERSION_SHORT ".dll" +#endif + +namespace icu_util { + +bool Initialize() { +#ifndef NDEBUG + // Assert that we are not called more than once. Even though calling this + // function isn't harmful (ICU can handle it), being called twice probably + // indicates a programming error. + static bool called_once = false; + DCHECK(!called_once); + called_once = true; +#endif + +#if (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED) + // We expect to find the ICU data module alongside the current module. + std::wstring data_path; + PathService::Get(base::DIR_MODULE, &data_path); + file_util::AppendToPath(&data_path, + ASCIIToWide(ICU_UTIL_DATA_SHARED_MODULE_NAME)); + + HMODULE module = LoadLibrary(data_path.c_str()); + if (!module) { + LOG(ERROR) << "Failed to load " << ICU_UTIL_DATA_SHARED_MODULE_NAME; + return false; + } + + FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); + if (!addr) { + LOG(ERROR) << ICU_UTIL_DATA_SYMBOL << ": not found in " + << ICU_UTIL_DATA_SHARED_MODULE_NAME; + return false; + } + + UErrorCode err = U_ZERO_ERROR; + udata_setCommonData(reinterpret_cast<void*>(addr), &err); + return err == U_ZERO_ERROR; +#elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC) + // Mac bundles the ICU data in. + return true; +#elif (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) + // For now, expect the data file to be alongside the executable. + // This is sufficient while we work on unit tests, but will eventually + // likely live in a data directory. + FilePath data_path; + bool path_ok = PathService::Get(base::DIR_EXE, &data_path); + DCHECK(path_ok); + u_setDataDirectory(data_path.value().c_str()); + // Only look for the packaged data file; + // the default behavior is to look for individual files. + UErrorCode err = U_ZERO_ERROR; + udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); + return err == U_ZERO_ERROR; +#endif +} + +} // namespace icu_util |