const char CB_WRITE_LIST_BLOCK[] =
R"__c_cb(
do {
- GList *iter = $$;
+ GList *iter;
+
+ iter = $$;
while (iter) {
$$value = iter->data;
}
$$
}
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
const char CB_WRITE_ARRAY_BLOCK[] =
R"__c_cb(
for (int i = 0; i < $$; i++) {
$$
}
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
const char CB_READ_LIST_BLOCK[] =
R"__c_cb(do {
$$
$$ = g_list_append($$, value);
}
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
const char CB_READ_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
$$
h->## = calloc(h->##_size, sizeof(*h->##));
$$
}
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
const char CB_FINALIZE_LIST_BLOCK[] =
-R"__c_cb(do {
- GList *iter = $$;
+R"__c_cb(
+do {
+ GList *iter;
+
+ iter = $$;
while (iter) {
$$value = iter->data;
$$
)__c_cb";
const char CB_FINALIZE_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
for (int i = 0; i < $$; i++) {
$$
}
)__c_cb";
const char CB_SETTER_LIST_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
$$value;
value = calloc(1, sizeof(*value));
)__c_cb";
const char CB_SETTER_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
h->## = calloc(##_size, sizeof(*##));
if (!h->##) {
dlog_print(DLOG_ERROR, LOG_TAG, "Out of memory");
)__c_cb";
const char CB_GETTER_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
if (h->##_size == 0) {
dlog_print(DLOG_WARN, LOG_TAG, "## is empty");
break;
)__c_cb";
const char CB_ITERATOR_BLOCK[] =
-R"__c_cb(do {
- GList *iter = $$;
+R"__c_cb(
+do {
+ GList *iter;
+
+ iter = $$;
while (iter) {
$$value = iter->data;
)__c_cb";
const char CB_CLONER_LIST_BLOCK[] =
-R"__c_cb(do {
- GList *iter = $$;
+R"__c_cb(
+do {
+ GList *iter;
+
+ iter = $$;
while (iter) {
$$new_value;
$$value = iter->data;
)__c_cb";
const char CB_CLONER_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
if (h->##_size == 0) {
dlog_print(DLOG_WARN, LOG_TAG, "## is empty");
break;
const char CB_IF_STATEMENT_WITH_BRACES[] =
R"__c_cb(if ($$) {
$$
-})__c_cb";
+}
+)__c_cb";
const char CB_IF_STATEMENT[] =
R"__c_cb(if ($$)
- $$)__c_cb";
+ $$
+)__c_cb";
const char CB_DELEGATE_ENUM_FORMAT[] = "\n$$_DELEGATE_$$ = $$,";
#include <cassert>
#include <vector>
#include <sstream>
+#include <algorithm>
#include "idlc/c_gen/c_gen_base.h"
std::string CGeneratorBase::SmartIndent(std::string lines) {
std::stringstream ss(lines);
std::string result;
- std::string to;
+ std::string line;
+ std::string next_line;
+ std::string tab;
std::string back;
std::size_t found;
bool if_statement = false;
bool continuous = false;
- int indent = 0;
+ int tab_size = 0;
+ int line_count = 0;
- while (std::getline(ss, to, '\n')) {
- to = Trim(to);
- found = to.find('}');
+ while (std::getline(ss, next_line, '\n')) {
+ line_count++;
+ if (line_count == 1) {
+ line = Trim(next_line);
+ continue;
+ }
+
+ tab.clear();
+ found = line.find('}');
if (found != std::string::npos) {
- indent--;
+ tab_size--;
}
- if (to.length() > 0) {
- for (int i = 0; i < indent; i++) {
- result += "\t";
- }
+ if (line.length() > 0) {
+ tab += Tab(tab_size);
}
- if (continuous && !indent &&
+ if (continuous && tab_size == 0 &&
found == std::string::npos) {
- for (int i = 0; i < 2; i++) {
- result += "\t";
- }
+ tab += Tab(2);
}
if (found == std::string::npos && if_statement) {
- indent--;
+ tab_size--;
if_statement = false;
}
continuous = false;
- back = to.back();
+ back = line.back();
if (back == ",")
continuous = true;
- result += to;
- result += "\n";
+ if (line.empty() ||
+ line.length() == 0 ||
+ std::all_of(line.begin(), line.end(), isspace)) {
+ std::string n_line = Trim(next_line);
+ if (n_line.empty() ||
+ n_line.length() == 0 ||
+ n_line.find('}') != std::string::npos ||
+ std::all_of(n_line.begin(), n_line.end(), isspace)) {
+ line = n_line;
+ continue;
+ } else {
+ result += tab + line;
+ result += NLine(1);
+ }
+ } else {
+ result += tab + line;
+ result += NLine(1);
+ }
- found = to.find('{');
+ found = line.find('{');
if (found != std::string::npos) {
- indent++;
+ tab_size++;
} else {
- found = to.find("if (");
+ found = line.find("if (");
if (found != std::string::npos) {
- indent++;
+ tab_size++;
if_statement = true;
}
}
+
+ line = Trim(next_line);
}
+ result += line;
+ result += NLine(1);
+
return result;
}
void CGeneratorBase::GenIncludeDefaultHeaders(std::ofstream& stream,
bool body) {
- stream << NLine(1);
if (body) {
stream << CB_BODY_HEADER;
} else {
);
if (GetReturnTypeString(decl.GetType()) != "void ") {
str += NLine(1);
- str += "return ret;" + NLine(1);;
+ str += "return ret;";
}
return str;
}
rpc_port_parcel_write(parcel, &handle->parcelable, handle);
h->delegates = g_list_append(h->delegates, handle);
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
const char CB_RECEIVE_BLOCK[] =
R"__c_cb(
const Declaration& decl) {
const char handle[] = "typedef struct $$_s *rpc_port_$$_h;";
+ stream << NLine(1);
stream << GenTemplateString(handle,
[&]()->std::string {
return id + "_" + decl.GetID();
return id + "_" + decl.GetID();
});
+ stream << NLine(1);
stream << GenTemplateString(CB_INTERFACE_DELEGATOR,
[&]()->std::string {
return GetReturnTypeString(decl.GetType());