1 /*
2 * Copyright (c) 2022 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 #ifndef OHOS_CACHEABLE_COMMON_H
17 #define OHOS_CACHEABLE_COMMON_H
18 #include "securec.h"
19 #include "util/string.h"
20 #include "util/string_builder.h"
21 #include "util/file.h"
22
23 namespace OHOS {
24 namespace Idl {
25 namespace TestCommon {
26 const int ERR_FAIL = -1;
27 const int ERR_OK = 0;
28
29 class ParameterArgv {
30 public:
ParameterArgv(const char ** args,const int argc)31 ParameterArgv(const char** args, const int argc) : argc_(argc)
32 {
33 argv_ = new char* [argc_] {nullptr};
34 for (int i = 0; i < argc_; ++i) {
35 const int itemSize = strlen(args[i]);
36 argv_[i] = new char[itemSize + 1] {0};
37 if (strcpy_s(argv_[i], itemSize + 1, args[i]) != ERR_OK) {
38 GTEST_LOG_(ERROR) << "strcpy_s error [!EOK]";
39 return;
40 }
41 }
42 };
~ParameterArgv()43 ~ParameterArgv()
44 {
45 if (argv_ == nullptr) {
46 return;
47 }
48
49 for (int i = 0; i < argc_; ++i) {
50 if (argv_[i] == nullptr) {
51 continue;
52 }
53 delete[] argv_[i];
54 argv_[i] = nullptr;
55 }
56 delete[] argv_;
57 argv_ = nullptr;
58 };
59
GetArgv()60 char** GetArgv()
61 {
62 return argv_;
63 };
64
65 char** argv_;
66 const int argc_;
67 };
68
PrepareIdlFile(const std::string & fileName,const std::string & fileContent)69 int PrepareIdlFile(const std::string &fileName, const std::string &fileContent)
70 {
71 String filePath = String::Format("%s/%s", "./", fileName.c_str());
72 File file(filePath, File::WRITE);
73 if (!file.IsValid()) {
74 GTEST_LOG_(INFO) << "OPEN FILE FAIL";
75 return ERR_FAIL;
76 }
77
78 StringBuilder stringBuilder;
79 stringBuilder.Append(fileContent.c_str());
80
81 String data = stringBuilder.ToString();
82 file.WriteData(data.string(), data.GetLength());
83 file.Flush();
84 file.Close();
85 return ERR_OK;
86 }
87
88 }
89 }
90 }
91 #endif