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 namespace OHOS { 27 namespace SmartPerf { 28 class SpCsvUtil { 29 public: WriteCsv(const std::string & path,std::vector<SPData> vmap)30 static void WriteCsv(const std::string &path, std::vector<SPData> vmap) 31 { 32 std::ofstream outFile; 33 char realPath[PATH_MAX] = {0x00}; 34 if (realpath(path.c_str(), realPath) == nullptr) { 35 std::cout << "" << std::endl; 36 } 37 outFile.open(path.c_str(), std::ios::out); 38 int i = 0; 39 std::string title = ""; 40 for (SPData spdata : vmap) { 41 std::string lineContent = ""; 42 for (auto iter = spdata.values.cbegin(); iter != spdata.values.cend(); ++iter) { 43 if (i == 0) { 44 title += iter->first + ","; 45 } 46 lineContent += iter->second + ","; 47 } 48 if (i == 0) { 49 title.pop_back(); 50 outFile << title << std::endl; 51 } 52 lineContent.pop_back(); 53 outFile << lineContent << std::endl; 54 ++i; 55 } 56 outFile.close(); 57 } WriteCsvH(const std::string & path,std::map<std::string,std::string> vmap)58 static void WriteCsvH(const std::string &path, std::map<std::string, std::string> vmap) 59 { 60 std::ofstream outFile; 61 char realPath[PATH_MAX] = {0x00}; 62 if (realpath(path.c_str(), realPath) == nullptr) { 63 std::cout << "" << std::endl; 64 } 65 outFile.open(path.c_str(), std::ios::out); 66 for (auto iter = vmap.cbegin(); iter != vmap.cend(); ++iter) { 67 outFile << iter->first + "," + iter->second << std::endl; 68 } 69 outFile.close(); 70 } 71 }; 72 } 73 } 74 75 #endif // SP_CSV_UTILS_H 76