summaryrefslogtreecommitdiff
path: root/chromium/components/password_manager/core/browser/export/csv_writer.cc
blob: dfc0ad798f9d78617a242b817a8213cea1afb2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/password_manager/core/browser/export/csv_writer.h"

#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"

namespace {

// Encapsulates formatting and outputting values to a CSV file row by row. Takes
// care of escaping and adding the appropriate separators.
class CSVFormatter {
 public:
  explicit CSVFormatter(std::string* output)
      : output_(output), at_beginning_of_line_(true) {}

  // Escapes the |raw_value| if needed, adds a field separator unless we are at
  // the beginning of a line, and appends the escaped value to |output_|.
  void AppendValue(const std::string& raw_value);

  // Appends the platform-specific EOL terminator to |output_|.
  void EndLine();

 private:
  raw_ptr<std::string> output_;
  bool at_beginning_of_line_;
};

void CSVFormatter::AppendValue(const std::string& raw_value) {
  // Append the field separator unless this is the first field on the line.
  if (!at_beginning_of_line_)
    output_->push_back(',');
  at_beginning_of_line_ = false;

  // Fields containing line breaks (CRLF), double quotes, and commas should be
  // enclosed in double-quotes. If double-quotes are used to enclose fields,
  // then double-quotes appearing inside a field must be escaped by preceding
  // them with another double quote.
  if (raw_value.find_first_of("\r\n\",") != std::string::npos) {
    output_->push_back('\"');
    output_->append(raw_value);
    base::ReplaceSubstringsAfterOffset(
        output_, output_->size() - raw_value.size(), "\"", "\"\"");
    output_->push_back('\"');
  } else {
    output_->append(raw_value);
  }
}

void CSVFormatter::EndLine() {
#if defined(OS_WIN)
  const char kLineEnding[] = "\r\n";
#else
  const char kLineEnding[] = "\n";
#endif
  output_->append(kLineEnding);
  at_beginning_of_line_ = true;
}

}  // namespace

namespace password_manager {

void WriteCSV(const std::vector<std::string>& column_names,
              const std::vector<std::map<std::string, std::string>>& records,
              std::string* csv) {
  DCHECK(csv);
  csv->clear();

  // Append header row.
  CSVFormatter formatter(csv);
  for (const auto& column_name : column_names) {
    formatter.AppendValue(column_name);
  }
  formatter.EndLine();

  // Append every other data record row.
  for (const auto& row : records) {
    for (const auto& column_name : column_names) {
      auto it_field = row.find(column_name);
      formatter.AppendValue(it_field != row.end() ?
          it_field->second : std::string());
    }
    formatter.EndLine();
  }
}

}  // namespace password_manager