1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "include/IcuUtils.h"
18
19#include "unicode/putil.h"
20#include "unicode/uclean.h"
21#include "unicode/utypes.h"
22#include "utils/Log.h"
23
24#include <stdlib.h>
25
26void InitializeIcuOrDie() {
27    const char* systemPathPrefix = getenv("ANDROID_ROOT");
28    LOG_ALWAYS_FATAL_IF(systemPathPrefix == NULL, "ANDROID_ROOT environment variable not set");
29
30    char buf[256];
31    const int num_written = snprintf(buf, sizeof(buf), "%s/usr/icu/", systemPathPrefix);
32    LOG_ALWAYS_FATAL_IF((num_written < 0 || static_cast<size_t>(num_written) >= sizeof(buf)),
33            "Unable to construct ICU path.");
34
35    u_setDataDirectory(buf);
36    UErrorCode status = U_ZERO_ERROR;
37
38    // u_setDataDirectory doesn't try doing anything with the directory we gave
39    // it, so we'll have to call u_init to make sure it was successful.
40    u_init(&status);
41    LOG_ALWAYS_FATAL_IF(!U_SUCCESS(status), "Failed to initialize ICU %s", u_errorName(status));
42}
43