• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "input_method_config_parser.h"
17 
18 #include <algorithm>
19 #include <cstdio>
20 #include <fcntl.h>
21 #include <ios>
22 #include <string>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <fstream>
27 
28 #include "file_ex.h"
29 #include "global.h"
30 
31 namespace OHOS {
32 namespace MiscServices {
33 namespace {
34     constexpr const char *IME_INPUT_TYPE_CFG_FILE_PATH = "etc/inputmethod/inputmethod_framework_config.json";
35     using json = nlohmann::json;
36 } // namespace
37 
ParseFromCustom()38 CfgFiles* ImeConfigParse::ParseFromCustom()
39 {
40     return GetCfgFiles(IME_INPUT_TYPE_CFG_FILE_PATH);
41 }
42 
ParseJson(const std::string & cfgPath,const std::string & parseKey,nlohmann::json & jsonCfg)43 bool ImeConfigParse::ParseJson(const std::string &cfgPath,  const std::string &parseKey, nlohmann::json &jsonCfg)
44 {
45     IMSA_HILOGD("in");
46     std::string jsonStr = ImeConfigParse::ReadFile(cfgPath);
47     if (jsonStr.empty()) {
48         IMSA_HILOGE("json config size is invalid");
49         return false;
50     }
51     jsonCfg = json::parse(jsonStr, nullptr, false);
52     if (jsonCfg.is_discarded()) {
53         IMSA_HILOGE("jsonStr parse failed");
54         return false;
55     }
56     if (!jsonCfg.contains(parseKey)) {
57         IMSA_HILOGE("%{public}s not find or abnormal", parseKey.c_str());
58         return false;
59     }
60     IMSA_HILOGD("get json: %{public}s", jsonCfg.dump().c_str());
61     return true;
62 }
63 
ReadFile(const std::string & path)64 std::string ImeConfigParse::ReadFile(const std::string &path)
65 {
66     std::ifstream infile;
67     std::string sLine;
68     std::string sAll = "";
69     infile.open(path);
70     if (!infile.is_open()) {
71         IMSA_HILOGE("path: %s Readfile fail", path.c_str());
72         return sAll;
73     }
74 
75     while (getline(infile, sLine)) {
76         sAll.append(sLine);
77     }
78     infile.close();
79     return sAll;
80 }
81 } // namespace MiscServices
82 } // namespace OHOS