00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00039 #include "fastlib/base/otrav.h"
00040
00041
00042 void ot::StandardFormat::PrintIndent_() {
00043 for (int i = 0; i < indent_; ++i) {
00044 putc(' ', stream_);
00045 }
00046 }
00047
00048 void ot::StandardFormat::PrintHeader_(const char *name, index_t index,
00049 const char *type, index_t len) {
00050 if (index >= 0) {
00051 fprintf(stream_, "[%"LI"d]", index);
00052 } else {
00053 fprintf(stream_, "%s", name);
00054 }
00055 fprintf(stream_, " : ");
00056 if (len >= 0) {
00057 fprintf(stream_, "len %"LI"d ", len);
00058 }
00059 fprintf(stream_, "%s = ", type);
00060 }
00061
00062 void ot::StandardFormat::Untraversed(const unsigned char *obj_cp,
00063 size_t bytes) {
00064 while (bytes > 8) {
00065 PrintIndent_();
00066 for (size_t i = 0; i < 8; ++i) {
00067 fprintf(stream_, "%02X ", obj_cp[i]);
00068 }
00069 putc('\n', stream_);
00070 bytes -= 8;
00071 obj_cp += 8;
00072 }
00073 PrintIndent_();
00074 for (size_t i = 0; i < bytes; ++i) {
00075 fprintf(stream_, "%02X ", obj_cp[i]);
00076 }
00077 putc('\n', stream_);
00078 }
00079
00080 #define STANDARD_FORMAT__PRIMITIVE(T, TF) \
00081 void ot::StandardFormat::Primitive(const char *name, index_t index, \
00082 const char *type, T val) { \
00083 PrintIndent_(); \
00084 PrintHeader_(name, index, type, -1); \
00085 fprintf(stream_, TF, val); \
00086 putc('\n', stream_); \
00087 }
00088
00089 FOR_ALL_PRIMITIVES_DO(STANDARD_FORMAT__PRIMITIVE)
00090
00091 void ot::StandardFormat::Primitive(const char *name, index_t index,
00092 const char *type, bool val) {
00093 PrintIndent_();
00094 PrintHeader_(name, index, type, -1);
00095 fprintf(stream_, val ? "true" : "false");
00096 putc('\n', stream_);
00097 }
00098
00099 #undef STANDARD_FORMAT__PRIMITIVE
00100
00101 void ot::StandardFormat::Str(const char *name, index_t index,
00102 const char *type, const char *str) {
00103 PrintIndent_();
00104 PrintHeader_(name, index, type, -1);
00105 if (!str) {
00106 fprintf(stream_, "0x0");
00107 } else {
00108 char c;
00109
00110 putc('"', stream_);
00111 while ((c = *str++)) {
00112 if (c == '"') {
00113 fprintf(stream_, "\\\"");
00114 } else if (c == '\a') {
00115 fprintf(stream_, "\\a");
00116 } else if (c == '\b') {
00117 fprintf(stream_, "\\b");
00118 } else if (c == '\f') {
00119 fprintf(stream_, "\\f");
00120 } else if (c == '\n') {
00121 fprintf(stream_, "\\n");
00122 } else if (c == '\t') {
00123 fprintf(stream_, "\\t");
00124 } else if (c == '\v') {
00125 fprintf(stream_, "\\v");
00126 } else if (c == '\\') {
00127 fprintf(stream_, "\\\\");
00128 } else {
00129 putc(c, stream_);
00130 }
00131 }
00132 putc('"', stream_);
00133 }
00134 putc('\n', stream_);
00135 }
00136
00137 void ot::StandardFormat::Ptr(const char *name, index_t index,
00138 const char *type, ptrdiff_t ptr) {
00139 PrintIndent_();
00140 PrintHeader_(name, index, type, -1);
00141 if (sizeof(ptrdiff_t) >= sizeof(int)) {
00142 fprintf(stream_, "0x%X", (int)ptr);
00143 } else if (sizeof(ptrdiff_t) >= sizeof(long)) {
00144 fprintf(stream_, "0x%lX", (long)ptr);
00145 } else {
00146 fprintf(stream_, "0x%llX", (long long)ptr);
00147 }
00148 putc('\n', stream_);
00149 }
00150
00151 void ot::StandardFormat::Open(const char *name, index_t index,
00152 const char *type, index_t len) {
00153 PrintIndent_();
00154 PrintHeader_(name, index, type, len);
00155 putc('\n', stream_);
00156 indent_ += 2;
00157 PrintIndent_();
00158 putc('{', stream_);
00159 putc('\n', stream_);
00160 indent_ += 2;
00161 }
00162
00163 void ot::StandardFormat::Close(const char *name, const char *type) {
00164 indent_ -= 2;
00165 PrintIndent_();
00166 putc('}', stream_);
00167 putc('\n', stream_);
00168 indent_ -= 2;
00169 }
00170
00171
00172
00173 void ot::XMLFormat::PrintIndent_() {
00174 for (int i = 0; i < indent_; ++i) {
00175 putc(' ', stream_);
00176 }
00177 }
00178
00179 void ot::XMLFormat::PrintHeader_(const char *name, index_t index,
00180 const char *type, index_t len) {
00181 fprintf(stream_, "<_%s", type);
00182 if (index >= 0) {
00183 fprintf(stream_, " index=\"%"LI"d\"", index);
00184 } else {
00185 fprintf(stream_, " name=\"%s\"", name);
00186 }
00187 if (len >= 0) {
00188 fprintf(stream_, " len=\"%"LI"d\"", len);
00189 }
00190 fprintf(stream_, ">");
00191 }
00192
00193 void ot::XMLFormat::PrintFooter_(const char *type) {
00194 fprintf(stream_, "</_%s>", type);
00195 }
00196
00197 void ot::XMLFormat::Untraversed(const unsigned char *obj_cp,
00198 size_t bytes) {
00199 while (bytes > 8) {
00200 PrintIndent_();
00201 for (size_t i = 0; i < 8; ++i) {
00202 fprintf(stream_, "%02X ", obj_cp[i]);
00203 }
00204 putc('\n', stream_);
00205 bytes -= 8;
00206 obj_cp += 8;
00207 }
00208 PrintIndent_();
00209 for (size_t i = 0; i < bytes; ++i) {
00210 fprintf(stream_, "%02X ", obj_cp[i]);
00211 }
00212 putc('\n', stream_);
00213 }
00214
00215 #define XML_FORMAT__PRIMITIVE(T, TF) \
00216 void ot::XMLFormat::Primitive(const char *name, index_t index, \
00217 const char *type, T val) { \
00218 PrintIndent_(); \
00219 PrintHeader_(name, index, type, -1); \
00220 fprintf(stream_, TF, val); \
00221 PrintFooter_(type); \
00222 putc('\n', stream_); \
00223 }
00224
00225 FOR_ALL_PRIMITIVES_DO(XML_FORMAT__PRIMITIVE)
00226
00227 void ot::XMLFormat::Primitive(const char *name, index_t index,
00228 const char *type, bool val) {
00229 PrintIndent_();
00230 PrintHeader_(name, index, type, -1);
00231 fprintf(stream_, val ? "true" : "false");
00232 PrintFooter_(type);
00233 putc('\n', stream_);
00234 }
00235
00236 #undef XML_FORMAT__PRIMITIVE
00237
00238 void ot::XMLFormat::Str(const char *name, index_t index,
00239 const char *type, const char *str) {
00240 PrintIndent_();
00241 PrintHeader_(name, index, type, -1);
00242 if (str) {
00243 char c;
00244
00245 while ((c = *str++)) {
00246 if (c == '"') {
00247 fprintf(stream_, """);
00248 } else if (c == '\'') {
00249 fprintf(stream_, "'");
00250 } else if (c == '&') {
00251 fprintf(stream_, "&");
00252 } else if (c == '<') {
00253 fprintf(stream_, "<");
00254 } else if (c == '>') {
00255 fprintf(stream_, ">");
00256 } else {
00257 putc(c, stream_);
00258 }
00259 }
00260 }
00261 PrintFooter_(type);
00262 putc('\n', stream_);
00263 }
00264
00265 void ot::XMLFormat::Ptr(const char *name, index_t index,
00266 const char *type, ptrdiff_t ptr) {
00267 PrintIndent_();
00268 PrintHeader_(name, index, type, -1);
00269 if (sizeof(ptrdiff_t) >= sizeof(int)) {
00270 fprintf(stream_, "0x%X", (int)ptr);
00271 } else if (sizeof(ptrdiff_t) >= sizeof(long)) {
00272 fprintf(stream_, "0x%lX", (long)ptr);
00273 } else {
00274 fprintf(stream_, "0x%llX", (long long)ptr);
00275 }
00276 PrintFooter_(type);
00277 putc('\n', stream_);
00278 }
00279
00280 void ot::XMLFormat::Open(const char *name, index_t index,
00281 const char *type, index_t len) {
00282 PrintIndent_();
00283 PrintHeader_(name, index, type, len);
00284 putc('\n', stream_);
00285 indent_ += 4;
00286 }
00287
00288 void ot::XMLFormat::Close(const char *name, const char *type) {
00289 indent_ -= 4;
00290 PrintIndent_();
00291 PrintFooter_(type);
00292 putc('\n', stream_);
00293 }