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 <aicu/AIcu.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
AIcu_initializeIcuOrDie()26 void AIcu_initializeIcuOrDie() {
27 const char* runtimeModulePathPrefix = getenv("ANDROID_RUNTIME_ROOT");
28 LOG_ALWAYS_FATAL_IF(runtimeModulePathPrefix == NULL,
29 "ANDROID_RUNTIME_ROOT environment variable not set");
30
31 char buf[256];
32 const int num_written = snprintf(buf, sizeof(buf), "%s/etc/icu/", runtimeModulePathPrefix);
33 LOG_ALWAYS_FATAL_IF((num_written < 0 || static_cast<size_t>(num_written) >= sizeof(buf)),
34 "Unable to construct ICU path.");
35
36 u_setDataDirectory(buf);
37 UErrorCode status = U_ZERO_ERROR;
38
39 // u_setDataDirectory doesn't try doing anything with the directory we gave
40 // it, so we'll have to call u_init to make sure it was successful.
41 u_init(&status);
42 LOG_ALWAYS_FATAL_IF(!U_SUCCESS(status), "Failed to initialize ICU %s", u_errorName(status));
43 }
44