summaryrefslogtreecommitdiff
path: root/tools/incident_section_gen/main.cpp
diff options
context:
space:
mode:
authorYi Jin <jinyithu@google.com>2017-11-16 18:19:45 -0800
committerYi Jin <jinyithu@google.com>2018-01-04 11:03:31 -0800
commit0f2599fbf0ba8128b2a2c4de4d40aa37ea6c3d10 (patch)
treef35fb77b864e463f76bd31e17c2b8c576265377e /tools/incident_section_gen/main.cpp
parent48989c2744dcaaba4d4a23d9ca19b41c88c483bf (diff)
Dump Proto to a csv
Bug: 69427323 Test: N/A Change-Id: I1b89898afc30d5a0df6355ee1f6477df8d271f98
Diffstat (limited to 'tools/incident_section_gen/main.cpp')
-rw-r--r--tools/incident_section_gen/main.cpp58
1 files changed, 55 insertions, 3 deletions
diff --git a/tools/incident_section_gen/main.cpp b/tools/incident_section_gen/main.cpp
index 765079507513..102bbf9d07d7 100644
--- a/tools/incident_section_gen/main.cpp
+++ b/tools/incident_section_gen/main.cpp
@@ -20,6 +20,7 @@
#include <map>
#include <set>
#include <string>
+#include <sstream>
using namespace android;
using namespace android::os;
@@ -482,9 +483,44 @@ static bool generateSectionListCpp(Descriptor const* descriptor) {
}
// ================================================================================
+static string replace_string(const string& str, const char replace, const char with)
+{
+ string result(str);
+ const int N = result.size();
+ for (int i=0; i<N; i++) {
+ if (result[i] == replace) {
+ result[i] = with;
+ }
+ }
+ return result;
+}
+
+static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents) {
+ DebugStringOptions options;
+ options.include_comments = true;
+ for (int i=0; i<descriptor->field_count(); i++) {
+ const FieldDescriptor* field = descriptor->field(i);
+ stringstream text;
+ if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
+ text << field->message_type()->name();
+ } else {
+ text << field->type_name();
+ }
+ text << " " << field->name();
+ printf("%s%s,\n", indent.c_str(), replace_string(text.str(), '\n', ' ').c_str());
+ if (field->type() == FieldDescriptor::TYPE_MESSAGE &&
+ parents->find(field->message_type()->full_name()) == parents->end()) {
+ parents->insert(field->message_type()->full_name());
+ generateCsv(field->message_type(), indent + ",", parents);
+ parents->erase(field->message_type()->full_name());
+ }
+ }
+}
+
+// ================================================================================
int main(int argc, char const *argv[])
{
- if (argc != 2) return 1;
+ if (argc < 2) return 1;
const char* module = argv[1];
Descriptor const* descriptor = IncidentProto::descriptor();
@@ -495,7 +531,23 @@ int main(int argc, char const *argv[])
if (strcmp(module, "incidentd") == 0 ) {
return !generateSectionListCpp(descriptor);
}
-
- // return failure if not called by the whitelisted modules
+ // Generates Csv Format of proto definition for each section.
+ if (strcmp(module, "csv") == 0 && argc > 2) {
+ int sectionId = atoi(argv[2]);
+ for (int i=0; i<descriptor->field_count(); i++) {
+ const FieldDescriptor* field = descriptor->field(i);
+ if (strcmp(field->name().c_str(), argv[2]) == 0
+ || field->number() == sectionId) {
+ set<string> parents;
+ printf("%s\n", field->name().c_str());
+ generateCsv(field->message_type(), "", &parents);
+ break;
+ }
+ }
+ // Returns failure if csv is enabled to prevent Android building with it.
+ // It doesn't matter if this command runs manually.
+ return 1;
+ }
+ // Returns failure if not called by the whitelisted modules
return 1;
}