summaryrefslogtreecommitdiff
path: root/chromeos-dbus-bindings/header_generator.cc
blob: d69cd1c5f6b39ba39cda1aa1790468694ba49578 (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
// Copyright 2014 The Chromium OS 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 "chromeos-dbus-bindings/header_generator.h"

#include <string>

#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
#include <brillo/strings/string_utils.h>

#include "chromeos-dbus-bindings/indented_text.h"

using std::string;
using std::vector;

namespace chromeos_dbus_bindings {

// static
string HeaderGenerator::GenerateHeaderGuard(
    const base::FilePath& output_file) {
  string guard = base::StringPrintf("____chromeos_dbus_binding__%s",
                                    output_file.value().c_str());
  for (auto& c : guard) {
    if (IsAsciiAlpha(c)) {
      c = base::ToUpperASCII(c);
    } else if (!IsAsciiDigit(c)) {
      c = '_';
    }
  }
  return guard;
}

// static
bool HeaderGenerator::IsIntegralType(const string& type) {
  return type.find("::") == std::string::npos;
}

// static
void HeaderGenerator::MakeConstReferenceIfNeeded(std::string* type) {
  if (!IsIntegralType(*type)) {
    *type = base::StringPrintf("const %s&", type->c_str());
  }
}

// static
bool HeaderGenerator::WriteTextToFile(
    const base::FilePath& output_file, const IndentedText &text) {
  string contents = text.GetContents();
  int expected_write_return = contents.size();
  if (base::WriteFile(output_file, contents.c_str(), contents.size()) !=
      expected_write_return) {
    LOG(ERROR) << "Failed to write file " << output_file.value();
    return false;
  }
  return true;
}

// static
string HeaderGenerator::GetArgName(const char* prefix,
                                   const string& arg_name,
                                   int arg_index) {
  string name = arg_name.empty() ? std::to_string(arg_index) : arg_name;
  return base::StringPrintf("%s_%s", prefix, name.c_str());
}

}  // namespace chromeos_dbus_bindings