TextCode.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
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     // each line is {\t}*textcoded stuff\n
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 } // namespace oasys

Generated on Sat Sep 8 08:36:18 2007 for DTN Reference Implementation by  doxygen 1.5.3