• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef SP_CSV_UTIL_H
16 #define SP_CSV_UTIL_H
17 #include <iostream>
18 #include <string>
19 #include <fstream>
20 #include <sstream>
21 #include <climits>
22 #include <cstdlib>
23 #include <map>
24 #include "common.h"
25 #include "sp_data.h"
26 #include "sp_utils.h"
27 namespace OHOS {
28 namespace SmartPerf {
29 class SpCsvUtil {
30 public:
WriteCsv(const std::string & path,std::vector<SPData> vmap)31     static void WriteCsv(const std::string &path, std::vector<SPData> vmap)
32     {
33         std::ofstream outFile;
34         std::string cmdResult;
35         SPUtils::LoadCmd(CMD_COMMAND_MAP.at(CmdCommand::USER_PERMISSIONS), cmdResult);
36         outFile.open(path.c_str(), std::ios::out | std::ios::trunc);
37         if (SPUtils::GetPathPermissions(path)) {
38             std::string cmd = "chmod 777 " + path;
39             if (outFile.is_open() && path == "/data/local/tmp/data.csv" && cmdResult == "root") {
40                 SPUtils::LoadCmd(cmd, cmdResult);
41             }
42             if (outFile.is_open() && path == "/data/local/tmp/data.csv" && cmdResult == "shell") {
43                 SPUtils::LoadCmd(cmd, cmdResult);
44             }
45         }
46         int i = 0;
47         std::string title = "";
48         for (SPData spdata : vmap) {
49             std::string lineContent = "";
50             for (auto iter = spdata.values.cbegin(); iter != spdata.values.cend(); ++iter) {
51                 if (i == 0) {
52                     title += iter->first + ",";
53                 }
54                 lineContent += iter->second + ",";
55             }
56             if (i == 0) {
57                 title.pop_back();
58                 outFile << title << std::endl;
59             }
60             lineContent.pop_back();
61             outFile << lineContent << std::endl;
62             ++i;
63         }
64         outFile.close();
65     }
WriteCsvH(std::map<std::string,std::string> vmap)66     static void WriteCsvH(std::map<std::string, std::string> vmap)
67     {
68         const std::string outGeneralPath = "/data/local/tmp/smartperf/1/t_general_info.csv";
69         std::ofstream outFile(outGeneralPath, std::ios::out | std::ios::trunc);
70         if (!outFile.is_open()) {
71             std::cout << "Error opening file!" << std::endl;
72             return;
73         }
74         for (const auto& [key, value] : vmap) {
75             outFile << key << "," << value << std::endl;
76         }
77         outFile.close();
78     }
79 };
80 }
81 }
82 
83 #endif // SP_CSV_UTILS_H
84