TextCode.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // each line is {\t}*textcoded stuff\n
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 } // namespace oasys

Generated on Fri Dec 22 14:48:01 2006 for DTN Reference Implementation by  doxygen 1.5.1