• 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 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         outFile.open(path.c_str(), std::ios::out | std::ios::trunc);
34         int i = 0;
35         std::string title = "";
36         for (SPData spdata : vmap) {
37             std::string lineContent = "";
38             for (auto iter = spdata.values.cbegin(); iter != spdata.values.cend(); ++iter) {
39                 if (i == 0) {
40                     title += iter->first + ",";
41                 }
42                 lineContent += iter->second + ",";
43             }
44             if (i == 0) {
45                 title.pop_back();
46                 outFile << title << std::endl;
47             }
48             lineContent.pop_back();
49             outFile << lineContent << std::endl;
50             ++i;
51         }
52         outFile.close();
53     }
WriteCsvH(std::map<std::string,std::string> vmap)54     static void WriteCsvH(std::map<std::string, std::string> vmap)
55     {
56         const std::string outGeneralPath = "/data/local/tmp/smartperf/1/t_general_info.csv";
57         std::ofstream outFile(outGeneralPath, std::ios::out | std::ios::trunc);
58         if (!outFile.is_open()) {
59             std::cout << "Error opening file!" << std::endl;
60             return;
61         }
62         for (const auto& [key, value] : vmap) {
63             outFile << key << "," << value << std::endl;
64         }
65         outFile.close();
66     }
67 };
68 }
69 }
70 
71 #endif // SP_CSV_UTILS_H
72