00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "StringBuffer.h"
00019 #include "TextCode.h"
00020
00021 namespace oasys {
00022
00023 TextCode::TextCode(const char* input_buf, size_t length,
00024 ExpandableBuffer* buf, int cols, int pad)
00025 : input_buf_(input_buf), length_(length),
00026 buf_(buf, false), cols_(cols), pad_(pad)
00027 {
00028 textcodify();
00029 }
00030
00031 bool
00032 TextCode::is_not_escaped(char c) {
00033 return c >= 32 && c <= 126 && c != '\\';
00034 }
00035
00036 void
00037 TextCode::append(char c) {
00038 if (is_not_escaped(c)) {
00039 buf_.append(static_cast<char>(c));
00040 } else if (c == '\\') {
00041 buf_.appendf("\\\\");
00042 } else {
00043 buf_.appendf("\\%02x", ((int)c & 0xff));
00044 }
00045 }
00046
00047 void
00048 TextCode::textcodify()
00049 {
00050 for (size_t i=0; i<length_; ++i)
00051 {
00052 if (i % cols_ == 0)
00053 {
00054 if (i != 0) {
00055 buf_.append('\n');
00056 }
00057 for (int j=0; j<pad_; ++j)
00058 buf_.append('\t');
00059 }
00060 append(input_buf_[i]);
00061 }
00062 buf_.append('\n');
00063 for (int j=0; j<pad_; ++j)
00064 buf_.append('\t');
00065 buf_.append("\n");
00066 }
00067
00068
00069 TextUncode::TextUncode(const char* input_buf, size_t length,
00070 ExpandableBuffer* buf)
00071 : input_buf_(input_buf),
00072 length_(length),
00073 buf_(buf, false),
00074 cur_(input_buf),
00075 error_(false)
00076 {
00077 textuncodify();
00078 }
00079
00080
00081 void
00082 TextUncode::textuncodify()
00083 {
00084
00085 while (true) {
00086 if (! in_buffer()) {
00087 error_ = true;
00088 return;
00089 }
00090
00091 if (*cur_ == '') {
00092 break;
00093 }
00094
00095 if (*cur_ == '\t' || *cur_ == '\n') {
00096 ++cur_;
00097 continue;
00098 }
00099
00100 if (*cur_ == '\\') {
00101 if (!in_buffer(1)) {
00102 error_ = true;
00103 return;
00104 }
00105
00106 if (cur_[1] == '\\') {
00107 buf_.append('\\');
00108 cur_ += 2;
00109 continue;
00110 }
00111
00112 if (!in_buffer(3)) {
00113 error_ = true;
00114 return;
00115 }
00116
00117 ++cur_;
00118 int value = strtol(cur_, 0, 16);
00119 buf_.append(static_cast<char>(value));
00120 } else {
00121 buf_.append(*cur_);
00122 ++cur_;
00123 }
00124 }
00125 }
00126
00127 }