1/* 2 * Copyright (c) 2022 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 16#include "format_csv.h" 17 18#include <iomanip> 19#include <sstream> 20 21namespace OHOS { 22namespace WuKong { 23FormatCSV::FormatCSV() : Format() 24{ 25} 26 27FormatCSV::~FormatCSV() 28{ 29} 30 31void FormatCSV::FormatDetail(std::shared_ptr<Table> tablePtr, std::string &target) 32{ 33 std::vector<std::string> header = tablePtr->GetHeader(); 34 std::vector<std::vector<std::string>> record = tablePtr->GetRecord(); 35 std::vector<int> column_size_ = tablePtr->GetColumnSize(); 36 std::stringstream ss; 37 38 ss << "name, " << tablePtr->GetName() << ", detail, " << tablePtr->GetDetail() << std::endl; 39 for (uint32_t col = 0; col < header.size(); col++) { 40 ss << std::setw(column_size_[col]) << std::setiosflags(std::ios::left) << std::setfill(' ') << header[col]; 41 if (col == (header.size() - 1)) { 42 break; 43 } 44 ss << ','; 45 } 46 ss << std::endl; 47 48 for (uint32_t row = 0; row < record.size(); row++) { 49 for (uint32_t col = 0; col < header.size(); col++) { 50 ss << std::setw(column_size_[col]) << std::setiosflags(std::ios::left) << std::setfill(' '); 51 ss << record[row][col]; 52 if (col == (header.size() - 1)) { 53 break; 54 } 55 ss << ','; 56 } 57 ss << std::endl; 58 } 59 target += ss.str(); 60} 61} // namespace WuKong 62} // namespace OHOS 63