00001 #ifndef __PRINT_NODE_FORMATTER_H
00002 #define __PRINT_NODE_FORMATTER_H
00003
00004 #include "Printable.h"
00005
00006 namespace mace {
00007 class PrintNodeFormatter {
00008 public:
00009
00010 static bool containsMap(const PrintNode& node) {
00011 if (node.getType().find("map<") == 0) {
00012 return true;
00013 }
00014 const PrintNodeList& l = node.getChildren();
00015 bool r = false;
00016 for (PrintNodeList::const_iterator i = l.begin();
00017 i != l.end() && !r; i++) {
00018 r |= containsMap(*i);
00019 }
00020 return r;
00021 }
00022
00023 static void printStruct(std::ostream& out, const PrintNode& node,
00024 size_t indent,
00025 bool printName,
00026 bool parentFlatten) {
00027
00028 bool flatten = parentFlatten || !containsMap(node);
00029 if (printName) {
00030 out << node.getName();
00031 if (flatten) {
00032 out << "(";
00033 }
00034 else {
00035 out << "(" << std::endl;
00036 }
00037 }
00038 else {
00039 out << "(";
00040 }
00041
00042 const PrintNodeList& l = node.getChildren();
00043 PrintNodeList::const_iterator i = l.begin();
00044 while (i != l.end()) {
00045 if (!flatten) {
00046 spaces(out, indent + 2);
00047 }
00048 print(out, *i, indent + 2, true, flatten);
00049 bool wasMap = isMap(*i);
00050 i++;
00051 if (flatten) {
00052 if (i != l.end()) {
00053 if (wasMap || isMap(*i)) {
00054 out << std::endl;
00055 spaces(out, indent + 2);
00056 }
00057 else {
00058 out << " ";
00059 }
00060 }
00061 else {
00062 out << ")";
00063 }
00064 }
00065 else if (i != l.end()) {
00066 out << std::endl;
00067 }
00068 }
00069 if (!flatten) {
00070 spaces(out, indent);
00071 out << ") //" << node.getName() << std::endl;
00072 }
00073 }
00074
00075 static void printMap(std::ostream& out, const PrintNode& node,
00076 size_t indent,
00077 bool printName,
00078 bool parentFlatten) {
00079 if (printName) {
00080 out << node.getName() << ":";
00081 }
00082 const PrintNodeList& l = node.getChildren();
00083 if (l.empty()) {
00084 out << " ()";
00085 return;
00086 }
00087 else {
00088 if (printName) {
00089 out << std::endl;
00090 }
00091 }
00092 PrintNodeList::const_iterator i = l.begin();
00093 while (i != l.end()) {
00094 if (printName || i != l.begin()) {
00095 spaces(out, indent + 2);
00096 }
00097 print(out, *i, indent + 2, false, true);
00098 i++;
00099 out << "->";
00100 if (containsMap(*i)) {
00101 out << std::endl;
00102 spaces(out, indent + 4);
00103 print(out, *i, indent + 4, false, true);
00104 }
00105 else {
00106 print(out, *i, indent + 2, false, true);
00107 }
00108 i++;
00109 if (i != l.end()) {
00110 out << std::endl;
00111 }
00112 }
00113 }
00114
00115 static void printList(std::ostream& out, const PrintNode& node,
00116 size_t indent,
00117 bool printName) {
00118 if (printName) {
00119 out << node.getName() << "=";
00120 }
00121 out << "[";
00122 const PrintNodeList& l = node.getChildren();
00123 PrintNodeList::const_iterator i = l.begin();
00124 while (i != l.end()) {
00125 print(out, *i, 0, false);
00126 i++;
00127 if (i != l.end()) {
00128 out << " ";
00129 }
00130 }
00131 out << "]";
00132 }
00133
00134 static void printValue(std::ostream& out, const PrintNode& node,
00135 bool printName, bool flatten) {
00136 if (printName) {
00137 if (flatten) {
00138 out << formatName(node.getName()) << "=";
00139 }
00140 else {
00141 out << node.getName() << "=";
00142 }
00143 }
00144 if (node.getValue() == "true") {
00145 out << "1";
00146 }
00147 else if (node.getValue() == "false") {
00148 out << "0";
00149 }
00150 else {
00151 out << node.getValue();
00152 }
00153 }
00154
00155 static void print(std::ostream& out,
00156 const PrintNode& node,
00157 size_t indent = 0,
00158 bool printName = true,
00159 bool parentFlatten = false) {
00160
00161
00162 if (isMap(node)) {
00163
00164 printMap(out, node, indent, printName, parentFlatten);
00165 }
00166 else if (node.getType().find("set<") == 0 ||
00167 node.getType().find("list<") == 0) {
00168
00169 printList(out, node, indent, printName);
00170 }
00171 else if (!node.getValue().empty() || node.getChildren().empty()) {
00172
00173 printValue(out, node, printName, parentFlatten);
00174 }
00175 else {
00176
00177 printStruct(out, node, indent, printName, parentFlatten);
00178 }
00179 }
00180
00181 static std::string formatName(const std::string& name) {
00182 return name.substr(0, 4);
00183 }
00184
00185 static bool isMap(const PrintNode& node) {
00186 return node.getType().find("map<") == 0;
00187 }
00188
00189 static void printWithTypes(std::ostream& out, const PrintNode& node,
00190 size_t indent = 0) {
00191 spaces(out, indent);
00192 out << "[" << node.getName() << "<" << node.getType() << ">";
00193 if (!node.getValue().empty()) {
00194 out << " = " << node.getValue() << "]";
00195 }
00196 else {
00197 const PrintNodeList& l = node.getChildren();
00198 if (!l.empty()) {
00199 out << std::endl;
00200 for (PrintNodeList::const_iterator i = l.begin(); i != l.end(); i++) {
00201 printWithTypes(out, *i, indent + 2);
00202 }
00203 spaces(out, indent);
00204 }
00205 out << "]";
00206 }
00207 out << std::endl;
00208 }
00209
00210 static void spaces(std::ostream& out, size_t sz) {
00211 for (size_t i = 0; i < sz; i++) {
00212 out << " ";
00213 }
00214 }
00215
00216 };
00217 };
00218
00219 #endif // __PRINT_NODE_FORMATTER_H