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 #include "sp_task.h" 28 namespace OHOS { 29 namespace SmartPerf { 30 class SpCsvUtil { 31 public: WriteCsv(const std::string & path,std::vector<SPData> vmap)32 static void WriteCsv(const std::string &path, std::vector<SPData> vmap) 33 { 34 std::ofstream outFile; 35 std::string cmdResult; 36 SPUtils::LoadCmd(CMD_COMMAND_MAP.at(CmdCommand::USER_PERMISSIONS), cmdResult); 37 outFile.open(path.c_str(), std::ios::out | std::ios::trunc); 38 if (SPUtils::GetPathPermissions(path)) { 39 std::string cmd = "chmod 777 " + path; 40 if (outFile.is_open() && path == "/data/local/tmp/data.csv" && cmdResult == "root") { 41 SPUtils::LoadCmd(cmd, cmdResult); 42 } 43 if (outFile.is_open() && path == "/data/local/tmp/data.csv" && cmdResult == "shell") { 44 SPUtils::LoadCmd(cmd, cmdResult); 45 } 46 } 47 int i = 0; 48 for (SPData spdata : vmap) { 49 std::string lineContent = ""; 50 for (auto iter = spdata.values.cbegin(); iter != spdata.values.cend(); ++iter) { 51 lineContent += iter->second + ","; 52 } 53 if (i == 0) { 54 std::string csvTitle = ""; 55 csvTitle = SetDataCsvTitle(spdata); 56 csvTitle.pop_back(); 57 outFile << csvTitle << std::endl; 58 } 59 lineContent.pop_back(); 60 outFile << lineContent << std::endl; 61 ++i; 62 } 63 outFile.close(); 64 } WriteCsvH(std::map<std::string,std::string> vmap)65 static void WriteCsvH(std::map<std::string, std::string> vmap) 66 { 67 const std::string outGeneralPath = "/data/local/tmp/smartperf/1/t_general_info.csv"; 68 std::ofstream outFile(outGeneralPath, std::ios::out | std::ios::trunc); 69 if (!outFile.is_open()) { 70 std::cout << "Error opening file!" << std::endl; 71 return; 72 } 73 for (const auto& [key, value] : vmap) { 74 outFile << key << "," << value << std::endl; 75 } 76 outFile.close(); 77 } SetDataCsvTitle(SPData spdata)78 static std::string SetDataCsvTitle(SPData spdata) 79 { 80 std::string csvTitle = ""; 81 csvTitle = SPTask::GetInstance().GetCsvTitle(); 82 if (csvTitle.empty()) { 83 for (auto iter = spdata.values.cbegin(); iter != spdata.values.cend(); ++iter) { 84 csvTitle += iter->first + ","; 85 } 86 } 87 return csvTitle; 88 } 89 }; 90 } 91 } 92 93 #endif // SP_CSV_UTILS_H 94