blob: 4c969262209696db555d41f94345d5fc2ab0fb6c [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
avi@google.com981d0572008-08-07 22:41:24 +09005#include "build/build_config.h"
6
evanm@google.come41d3b32008-08-15 10:04:11 +09007#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <windows.h>
avi@google.com981d0572008-08-07 22:41:24 +09009#endif
10
initial.commit3f4a7322008-07-27 06:49:38 +090011#include <string>
12
13#include "base/icu_util.h"
14
15#include "base/logging.h"
16#include "base/file_util.h"
17#include "base/path_service.h"
evanm@google.come41d3b32008-08-15 10:04:11 +090018#include "base/sys_string_conversions.h"
19#include "unicode/putil.h"
initial.commit3f4a7322008-07-27 06:49:38 +090020#include "unicode/udata.h"
21
22namespace icu_util {
23
24bool Initialize() {
evanm@google.come41d3b32008-08-15 10:04:11 +090025#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090026 // Assert that we are not called more than once. Even though calling this
27 // function isn't harmful (ICU can handle it), being called twice probably
28 // indicates a programming error.
avi@google.com981d0572008-08-07 22:41:24 +090029#ifndef NDEBUG
initial.commit3f4a7322008-07-27 06:49:38 +090030 static bool called_once = false;
31 DCHECK(!called_once);
32 called_once = true;
33#endif
34
35 // We expect to find the ICU data module alongside the current module.
36 std::wstring data_path;
37 PathService::Get(base::DIR_MODULE, &data_path);
38 file_util::AppendToPath(&data_path, L"icudt38.dll");
39
40 HMODULE module = LoadLibrary(data_path.c_str());
41 if (!module)
42 return false;
43
44 FARPROC addr = GetProcAddress(module, "icudt38_dat");
45 if (!addr)
46 return false;
47
48 UErrorCode err = U_ZERO_ERROR;
49 udata_setCommonData(reinterpret_cast<void*>(addr), &err);
50 return err == U_ZERO_ERROR;
evanm@google.come41d3b32008-08-15 10:04:11 +090051#elif defined(OS_MACOSX)
52 // Mac bundles the ICU data in.
avi@google.com981d0572008-08-07 22:41:24 +090053 return true;
evanm@google.come41d3b32008-08-15 10:04:11 +090054#elif defined(OS_LINUX)
55 // For now, expect the data file to be alongside the executable.
56 // This is sufficient while we work on unit tests, but will eventually
57 // likely live in a data directory.
58 std::wstring data_path;
59 bool path_ok = PathService::Get(base::DIR_EXE, &data_path);
60 DCHECK(path_ok);
61 u_setDataDirectory(base::SysWideToNativeMB(data_path).c_str());
62 return true;
63#endif
initial.commit3f4a7322008-07-27 06:49:38 +090064}
65
66} // namespace icu_util
license.botf003cfe2008-08-24 09:55:55 +090067