StringSerialize.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) 2005 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 "StringSerialize.h"
00040 
00041 namespace oasys {
00042 
00043 StringSerialize::StringSerialize(context_t context, int options)
00044     : SerializeAction(Serialize::INFO, context, options)
00045 {
00046     if (options_ & DOT_SEPARATED) {
00047         sep_ = '.';
00048     } else {
00049         sep_ = ' ';
00050     }
00051 }
00052 
00053 void
00054 StringSerialize::process(const char* name, u_int32_t* i)
00055 {
00056     if (options_ & INCLUDE_NAME) {
00057         buf_.append(name);
00058         buf_.append(sep_);
00059     }
00060 
00061     buf_.append_int(*i, 10);
00062     buf_.append(sep_);
00063 }
00064 
00065 void
00066 StringSerialize::process(const char* name, u_int16_t* i)
00067 {
00068     if (options_ & INCLUDE_NAME) {
00069         buf_.append(name);
00070         buf_.append(sep_);
00071     }
00072     
00073     buf_.append_int(*i, 10);
00074     buf_.append(sep_);
00075 }
00076 
00077 void
00078 StringSerialize::process(const char* name, u_int8_t* i)
00079 {
00080     if (options_ & INCLUDE_NAME) {
00081         buf_.append(name);
00082         buf_.append(sep_);
00083     }
00084     
00085     buf_.append_int(*i, 10);
00086     buf_.append(sep_);
00087 }
00088 
00089 void
00090 StringSerialize::process(const char* name, bool* b)
00091 {
00092     if (options_ & INCLUDE_NAME) {
00093         buf_.append(name);
00094         buf_.append(sep_);
00095     }
00096 
00097     if (*b) {
00098         buf_.append("true", 4);
00099     } else {
00100         buf_.append("false", 5);
00101     }
00102         
00103     buf_.append(sep_);
00104 }
00105 
00106 void
00107 StringSerialize::process(const char* name, u_char* bp, size_t len)
00108 {
00109     if (options_ & INCLUDE_NAME) {
00110         buf_.append(name);
00111         buf_.append(sep_);
00112     }
00113 
00114     buf_.append((const char*)bp, len);
00115     buf_.append(sep_);
00116 }
00117 
00118 void
00119 StringSerialize::process(const char* name, std::string* s)
00120 {
00121     if (options_ & INCLUDE_NAME) {
00122         buf_.append(name);
00123         buf_.append(sep_);
00124     }
00125 
00126     buf_.append(s->data(), s->length());
00127     buf_.append(sep_);
00128 }
00129 
00130 void
00131 StringSerialize::process(const char* name, u_char** bp,
00132                          size_t* lenp, int flags)
00133 {
00134     if (options_ & INCLUDE_NAME) {
00135         buf_.append(name);
00136         buf_.append(sep_);
00137     }
00138     
00139     if (flags & Serialize::NULL_TERMINATED) {
00140         buf_.append((const char*)*bp);
00141         buf_.append(sep_);
00142     } else {
00143         buf_.append((const char*)*bp, *lenp);
00144         buf_.append(sep_);
00145     }
00146 }
00147 
00148 void
00149 StringSerialize::end_action()
00150 {
00151     // trim trailing separator
00152     if (buf_.length() != 0) {
00153         buf_.trim(1);
00154     }
00155 }
00156 
00157 } // namespace oasys

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