StringSerialize.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-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 "StringSerialize.h"
00019 
00020 namespace oasys {
00021 
00022 //----------------------------------------------------------------------
00023 StringSerialize::StringSerialize(context_t context, int options)
00024     : SerializeAction(Serialize::INFO, context, options)
00025 {
00026     if (options_ & DOT_SEPARATED) {
00027         sep_ = '.';
00028     } else {
00029         sep_ = ' ';
00030     }
00031 }
00032 
00033 //----------------------------------------------------------------------
00034 void
00035 StringSerialize::add_preamble(const char* name, const char* type)
00036 {
00037     if (options_ & INCLUDE_NAME) {
00038         buf_.append(name);
00039         buf_.append(sep_);
00040     }
00041 
00042     if (options_ & INCLUDE_TYPE) {
00043         buf_.append(type);
00044         buf_.append(sep_);
00045     }
00046 }
00047 
00048 //----------------------------------------------------------------------
00049 void
00050 StringSerialize::process(const char* name, u_int64_t* i)
00051 {
00052     add_preamble(name, "u_int64_t");
00053     if (options_ & SCHEMA_ONLY) {
00054         return;
00055     }
00056             
00057     buf_.append_int(*i, 10);
00058     buf_.append(sep_);
00059 }
00060 
00061 //----------------------------------------------------------------------
00062 void
00063 StringSerialize::process(const char* name, u_int32_t* i)
00064 {
00065     add_preamble(name, "u_int32_t");
00066     if (options_ & SCHEMA_ONLY) {
00067         return;
00068     }
00069             
00070     buf_.append_int(*i, 10);
00071     buf_.append(sep_);
00072 }
00073 
00074 //----------------------------------------------------------------------
00075 void
00076 StringSerialize::process(const char* name, u_int16_t* i)
00077 {
00078     add_preamble(name, "u_int16_t");
00079     if (options_ & SCHEMA_ONLY) {
00080         return;
00081     }
00082             
00083     buf_.append_int(static_cast<u_int32_t>(*i), 10);
00084     buf_.append(sep_);
00085 }
00086 
00087 //----------------------------------------------------------------------
00088 void
00089 StringSerialize::process(const char* name, u_int8_t* i)
00090 {
00091     add_preamble(name, "u_int8_t");
00092     if (options_ & SCHEMA_ONLY) {
00093         return;
00094     }
00095 
00096     buf_.append_int(static_cast<u_int32_t>(*i), 10);
00097     buf_.append(sep_);
00098 }
00099 
00100 //----------------------------------------------------------------------
00101 void
00102 StringSerialize::process(const char* name, bool* b)
00103 {
00104     add_preamble(name, "bool");
00105     if (options_ & SCHEMA_ONLY) {
00106         return;
00107     }
00108 
00109     if (*b) {
00110         buf_.append("true", 4);
00111     } else {
00112         buf_.append("false", 5);
00113     }
00114         
00115     buf_.append(sep_);
00116 }
00117 
00118 //----------------------------------------------------------------------
00119 void
00120 StringSerialize::process(const char* name, u_char* bp, u_int32_t len)
00121 {
00122     add_preamble(name, "char_buf");
00123     if (options_ & SCHEMA_ONLY) {
00124         return;
00125     }
00126 
00127     buf_.append((const char*)bp, len);
00128     buf_.append(sep_);
00129 }
00130 
00131 //----------------------------------------------------------------------
00132 void
00133 StringSerialize::process(const char* name, std::string* s)
00134 {
00135     add_preamble(name, "string");
00136     if (options_ & SCHEMA_ONLY) {
00137         return;
00138     }
00139 
00140     buf_.append(s->data(), s->length());
00141     buf_.append(sep_);
00142 }
00143 
00144 //----------------------------------------------------------------------
00145 void
00146 StringSerialize::process(const char* name, u_char** bp,
00147                          u_int32_t* lenp, int flags)
00148 {
00149     add_preamble(name, "char_buf_var");
00150     if (options_ & SCHEMA_ONLY) {
00151         return;
00152     }
00153 
00154     if (flags & Serialize::NULL_TERMINATED) {
00155         buf_.append((const char*)*bp);
00156         buf_.append(sep_);
00157     } else {
00158         buf_.append((const char*)*bp, *lenp);
00159         buf_.append(sep_);
00160     }
00161 }
00162 
00163 //----------------------------------------------------------------------
00164 void
00165 StringSerialize::end_action()
00166 {
00167     // trim trailing separator
00168     if (buf_.length() != 0) {
00169         buf_.trim(1);
00170     }
00171 }
00172 
00173 } // namespace oasys

Generated on Sat Sep 8 08:43:34 2007 for DTN Reference Implementation by  doxygen 1.5.3