Dump Proto to a csv

Bug: 69427323
Test: N/A
Change-Id: I1b89898afc30d5a0df6355ee1f6477df8d271f98
diff --git a/tools/incident_section_gen/main.cpp b/tools/incident_section_gen/main.cpp
index 7650795..102bbf9 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 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 @@
     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;
 }