|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <oi/exporters/CSV.h> |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <ranges> |
| 20 | +#include <stdexcept> |
| 21 | + |
| 22 | +namespace oi::exporters { |
| 23 | + |
| 24 | +void CSV::print(const IntrospectionResult& result) { |
| 25 | + auto begin = result.cbegin(); |
| 26 | + return print(begin, result.cend()); |
| 27 | +} |
| 28 | + |
| 29 | +template <typename Seq> |
| 30 | +std::string CSV::escapeField(const Seq& seq) { |
| 31 | + std::string field; |
| 32 | + size_t index = 0; |
| 33 | + for (const auto item : seq) { |
| 34 | + if (index++ > 0) { |
| 35 | + field += kListDelimiter; |
| 36 | + } |
| 37 | + |
| 38 | + field += item; |
| 39 | + } |
| 40 | + |
| 41 | + return escapeField(field); |
| 42 | +} |
| 43 | + |
| 44 | +std::string CSV::escapeField(std::string_view field) { |
| 45 | + return escapeField(std::string(field)); |
| 46 | +} |
| 47 | + |
| 48 | +std::string CSV::escapeField(std::string field) { |
| 49 | + // Escape every instance of double quotes. |
| 50 | + auto it = field.find(kQuote); |
| 51 | + while (it != std::string::npos) { |
| 52 | + field.replace(it, 1, kEscapedQuote); |
| 53 | + it = field.find(kQuote, it + kEscapedQuote.size()); |
| 54 | + } |
| 55 | + |
| 56 | + field.insert(0, kQuote); |
| 57 | + field.append(kQuote); |
| 58 | + return field; |
| 59 | +} |
| 60 | + |
| 61 | +void CSV::print(IntrospectionResult::const_iterator& it, |
| 62 | + IntrospectionResult::const_iterator end) { |
| 63 | + printHeader(); |
| 64 | + |
| 65 | + parentIdStack_.resize(1); // Reset to parentIdStack_ = {0} |
| 66 | + for (/* it */; it != end; ++it) { |
| 67 | + out_ << ++id_ << kDelimiter; |
| 68 | + out_ << escapeField(it->name) << kDelimiter; |
| 69 | + out_ << escapeField(it->type_path) << kDelimiter; |
| 70 | + out_ << escapeField(it->type_names) << kDelimiter; |
| 71 | + out_ << it->static_size << kDelimiter; |
| 72 | + out_ << it->exclusive_size << kDelimiter; |
| 73 | + |
| 74 | + if (!it->pointer.has_value()) { |
| 75 | + out_ << kDelimiter; |
| 76 | + } else { |
| 77 | + out_ << it->pointer.value() << kDelimiter; |
| 78 | + } |
| 79 | + |
| 80 | + if (!it->container_stats.has_value()) { |
| 81 | + out_ << kDelimiter << kDelimiter; |
| 82 | + } else { |
| 83 | + out_ << it->container_stats->length << kDelimiter; |
| 84 | + out_ << it->container_stats->capacity << kDelimiter; |
| 85 | + } |
| 86 | + |
| 87 | + if (!it->is_set_stats.has_value()) { |
| 88 | + out_ << kDelimiter; |
| 89 | + } else { |
| 90 | + out_ << it->is_set_stats->is_set << kDelimiter; |
| 91 | + } |
| 92 | + |
| 93 | + while (parentIdStack_.size() > it->type_path.size()) { |
| 94 | + parentIdStack_.pop_back(); |
| 95 | + } |
| 96 | + |
| 97 | + out_ << parentIdStack_.back(); |
| 98 | + |
| 99 | + parentIdStack_.push_back(id_); |
| 100 | + |
| 101 | + out_ << kCRLF; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void CSV::printHeader() { |
| 106 | + size_t index = 0; |
| 107 | + for (const auto column : kColumns) { |
| 108 | + if (index++ > 0) { |
| 109 | + out_ << kDelimiter; |
| 110 | + } |
| 111 | + |
| 112 | + out_ << column; |
| 113 | + } |
| 114 | + out_ << kCRLF; |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace oi::exporters |
0 commit comments