1/*
2 * Copyright 2016 Google Inc. All rights reserved.
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
17#include "flatbuffers/code_generators.h"
18#include <assert.h>
19#include "flatbuffers/util.h"
20
21namespace flatbuffers {
22
23void CodeWriter::operator+=(std::string text) {
24
25  while (true) {
26    auto begin = text.find("{{");
27    if (begin == std::string::npos) {
28      break;
29    }
30
31    auto end = text.find("}}");
32    if (end == std::string::npos || end < begin) {
33      break;
34    }
35
36    // Write all the text before the first {{ into the stream.
37    stream_.write(text.c_str(), begin);
38
39    // The key is between the {{ and }}.
40    const std::string key = text.substr(begin + 2, end - begin - 2);
41
42    // Find the value associated with the key.  If it exists, write the
43    // value into the stream, otherwise write the key itself into the stream.
44    auto iter = value_map_.find(key);
45    if (iter != value_map_.end()) {
46      const std::string &value = iter->second;
47      stream_ << value;
48    } else {
49      assert(false && "could not find key");
50      stream_ << key;
51    }
52
53    // Update the text to everything after the }}.
54    text = text.substr(end + 2);
55  }
56  if (!text.empty() && text.back() == '\\') {
57    text.pop_back();
58    stream_ << text;
59  } else {
60    stream_ << text << std::endl;
61  }
62}
63
64const char *BaseGenerator::FlatBuffersGeneratedWarning() {
65  return "automatically generated by the FlatBuffers compiler,"
66         " do not modify\n\n";
67}
68
69std::string BaseGenerator::NamespaceDir(const Parser &parser,
70                                        const std::string &path,
71                                        const Namespace &ns) {
72  EnsureDirExists(path.c_str());
73  if (parser.opts.one_file) return path;
74  std::string namespace_dir = path;  // Either empty or ends in separator.
75  auto &namespaces = ns.components;
76  for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
77    namespace_dir += *it + kPathSeparator;
78    EnsureDirExists(namespace_dir.c_str());
79  }
80  return namespace_dir;
81}
82
83std::string BaseGenerator::NamespaceDir(const Namespace &ns) const {
84  return BaseGenerator::NamespaceDir(parser_, path_, ns);
85}
86
87bool BaseGenerator::IsEverythingGenerated() const {
88    for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
89         ++it) {
90      if (!(*it)->generated) return false;
91    }
92    for (auto it = parser_.structs_.vec.begin();
93         it != parser_.structs_.vec.end(); ++it) {
94      if (!(*it)->generated) return false;
95    }
96    return true;
97  }
98
99std::string BaseGenerator::FullNamespace(const char *separator,
100                                         const Namespace &ns) {
101  std::string namespace_name;
102  auto &namespaces = ns.components;
103  for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
104    if (namespace_name.length()) namespace_name += separator;
105    namespace_name += *it;
106  }
107  return namespace_name;
108}
109
110std::string BaseGenerator::LastNamespacePart(const Namespace &ns) {
111  if (!ns.components.empty())
112    return ns.components.back();
113  else
114    return std::string("");
115}
116
117// Ensure that a type is prefixed with its namespace whenever it is used
118// outside of its namespace.
119std::string BaseGenerator::WrapInNameSpace(const Namespace *ns,
120                                           const std::string &name) const {
121  if (CurrentNameSpace() == ns) return name;
122  std::string qualified_name = qualifying_start_;
123  for (auto it = ns->components.begin(); it != ns->components.end(); ++it)
124    qualified_name += *it + qualifying_separator_;
125  return qualified_name + name;
126}
127
128
129std::string BaseGenerator::WrapInNameSpace(const Definition &def) const {
130  return WrapInNameSpace(def.defined_namespace, def.name);
131}
132
133// Generate a documentation comment, if available.
134void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
135                const CommentConfig *config, const char *prefix) {
136  if (dc.begin() == dc.end()) {
137    // Don't output empty comment blocks with 0 lines of comment content.
138    return;
139  }
140
141  std::string &code = *code_ptr;
142  if (config != nullptr && config->first_line != nullptr) {
143    code += std::string(prefix) + std::string(config->first_line) + "\n";
144  }
145  std::string line_prefix = std::string(prefix) +
146      ((config != nullptr && config->content_line_prefix != nullptr) ?
147       config->content_line_prefix : "///");
148  for (auto it = dc.begin();
149       it != dc.end();
150       ++it) {
151    code += line_prefix + *it + "\n";
152  }
153  if (config != nullptr && config->last_line != nullptr) {
154    code += std::string(prefix) + std::string(config->last_line) + "\n";
155  }
156}
157
158}  // namespace flatbuffers
159