Goby Underwater Autonomy Project
Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
|
00001 // copyright 2008, 2009 t. schneider tes@mit.edu 00002 // 00003 // this file is part of the Dynamic Compact Control Language (DCCL), 00004 // the goby-acomms codec. goby-acomms is a collection of libraries 00005 // for acoustic underwater networking 00006 // 00007 // This program is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // This software is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with this software. If not, see <http://www.gnu.org/licenses/>. 00019 00020 #ifndef MESSAGE_VAR_STRING20100317H 00021 #define MESSAGE_VAR_STRING20100317H 00022 00023 #include "goby/util/string.h" 00024 00025 #include "message_var.h" 00026 00027 namespace goby 00028 { 00029 namespace acomms 00030 { 00031 class DCCLMessageVarString : public DCCLMessageVar 00032 { 00033 public: 00034 DCCLMessageVarString() 00035 : DCCLMessageVar(), 00036 max_length_(0) 00037 { } 00038 00039 int calc_size() const 00040 { return max_length_*acomms::BITS_IN_BYTE; } 00041 00042 void set_max_length(unsigned max_length) {max_length_ = max_length;} 00043 void set_max_length(const std::string& s) { set_max_length(util::as<unsigned>(s)); } 00044 00045 unsigned max_length() const {return max_length_;} 00046 00047 DCCLType type() const { return dccl_string; } 00048 00049 private: 00050 void initialize_specific() 00051 { } 00052 00053 boost::dynamic_bitset<unsigned char> encode_specific(const DCCLMessageVal& v) 00054 { 00055 unsigned size = calc_size(); 00056 boost::dynamic_bitset<unsigned char> bits(size); 00057 00058 std::string s = v; 00059 00060 // tack on null terminators (probably a byte of zeros in ASCII) 00061 s += std::string(max_length_, '\0'); 00062 00063 // one byte per char 00064 for (size_t j = 0; j < (size_t)max_length_; ++j) 00065 { 00066 bits <<= acomms::BITS_IN_BYTE; 00067 bits |= boost::dynamic_bitset<unsigned char>(size, s[j]);; 00068 } 00069 return bits; 00070 } 00071 00072 00073 DCCLMessageVal decode_specific(boost::dynamic_bitset<unsigned char>& b) 00074 { 00075 char s[max_length_+1]; 00076 s[max_length_] = '\0'; 00077 00078 for (size_t j = 0; j < max_length_; ++j) 00079 { 00080 s[max_length_-j-1] = (b & boost::dynamic_bitset<unsigned char>(static_cast<size_t>(calc_size()), static_cast<unsigned long>(0xff))).to_ulong(); 00081 b >>= acomms::BITS_IN_BYTE; 00082 } 00083 00084 if(!std::string(s).empty()) 00085 return DCCLMessageVal(std::string(s)); 00086 else 00087 return DCCLMessageVal(); 00088 } 00089 00090 void get_display_specific(std::stringstream& ss) const 00091 { } 00092 00093 private: 00094 unsigned max_length_; 00095 00096 }; 00097 } 00098 } 00099 #endif