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
00032
00033
00034
00035
00036
00037
00038
00039 #include "StringBuffer.h"
00040 #include "TextCode.h"
00041
00042 namespace oasys {
00043
00044 TextCode::TextCode(const char* input_buf, size_t length,
00045 ExpandableBuffer* buf, int cols, int pad)
00046 : input_buf_(input_buf), length_(length),
00047 buf_(buf, false), cols_(cols), pad_(pad)
00048 {
00049 textcodify();
00050 }
00051
00052 bool
00053 TextCode::is_not_escaped(char c) {
00054 return c >= 32 && c <= 126 && c != '\\';
00055 }
00056
00057 void
00058 TextCode::append(char c) {
00059 if (is_not_escaped(c)) {
00060 buf_.append(static_cast<char>(c));
00061 } else if (c == '\\') {
00062 buf_.appendf("\\\\");
00063 } else {
00064 buf_.appendf("\\%02x", ((int)c & 0xff));
00065 }
00066 }
00067
00068 void
00069 TextCode::textcodify()
00070 {
00071 for (size_t i=0; i<length_; ++i)
00072 {
00073 if (i % cols_ == 0)
00074 {
00075 if (i != 0) {
00076 buf_.append('\n');
00077 }
00078 for (int j=0; j<pad_; ++j)
00079 buf_.append('\t');
00080 }
00081 append(input_buf_[i]);
00082 }
00083 buf_.append('\n');
00084 for (int j=0; j<pad_; ++j)
00085 buf_.append('\t');
00086 buf_.append("\n");
00087 }
00088
00089
00090 TextUncode::TextUncode(const char* input_buf, size_t length,
00091 ExpandableBuffer* buf)
00092 : input_buf_(input_buf),
00093 length_(length),
00094 buf_(buf, false),
00095 cur_(input_buf),
00096 error_(false)
00097 {
00098 textuncodify();
00099 }
00100
00101
00102 void
00103 TextUncode::textuncodify()
00104 {
00105
00106 while (true) {
00107 if (! in_buffer()) {
00108 error_ = true;
00109 return;
00110 }
00111
00112 if (*cur_ == '') {
00113 break;
00114 }
00115
00116 if (*cur_ == '\t' || *cur_ == '\n') {
00117 ++cur_;
00118 continue;
00119 }
00120
00121 if (*cur_ == '\\') {
00122 if (!in_buffer(1)) {
00123 error_ = true;
00124 return;
00125 }
00126
00127 if (cur_[1] == '\\') {
00128 buf_.append('\\');
00129 cur_ += 2;
00130 continue;
00131 }
00132
00133 if (!in_buffer(3)) {
00134 error_ = true;
00135 return;
00136 }
00137
00138 ++cur_;
00139 int value = strtol(cur_, 0, 16);
00140 buf_.append(static_cast<char>(value));
00141 } else {
00142 buf_.append(*cur_);
00143 ++cur_;
00144 }
00145 }
00146 }
00147
00148 }