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