Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * string_content.cpp - A dynamically sized string message content 00004 * 00005 * Created: Mon Mar 17 13:58:03 2008 00006 * Copyright 2007-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <netcomm/utils/string_content.h> 00025 00026 #include <core/exception.h> 00027 #include <cstring> 00028 #include <cstdlib> 00029 00030 namespace fawkes { 00031 00032 /** @class StringContent <netcomm/utils/string_content.h> 00033 * Content for a variable length string. 00034 * This content class can be used with a FawkesNetworkMessage. It takes a 00035 * single string of variable size and stuffs it into a message. 00036 * 00037 * @author Tim Niemueller 00038 */ 00039 00040 00041 /** Primary constructor. 00042 * @param initial_string initial string 00043 */ 00044 StringContent::StringContent(const char *initial_string) 00045 { 00046 __string_owner = true; 00047 set_string(initial_string); 00048 } 00049 00050 00051 /** Constructor. 00052 * This ctor can be used with FawkesNetworkMessage::msgc(). 00053 * @param cid component ID, ignored 00054 * @param msgid message ID, ignored 00055 * @param payload Payload, checked if it can be a valid string. 00056 * @param payload_size size in bytes of payload 00057 */ 00058 StringContent::StringContent(unsigned int cid, unsigned int msgid, 00059 void *payload, size_t payload_size) 00060 { 00061 __string_owner = false; 00062 _payload = payload; 00063 _payload_size = payload_size; 00064 __string = (char *)payload; 00065 if ( __string[payload_size - 1] != 0 ) { 00066 // string is not null-terminated, it has not been created with this class, error! 00067 throw Exception("String content of network message is not null-terminated."); 00068 } 00069 } 00070 00071 00072 /** Destructor. */ 00073 StringContent::~StringContent() 00074 { 00075 if ( __string_owner ) { 00076 free(__string); 00077 } 00078 } 00079 00080 00081 /** Set the string. 00082 * Can only be called if the instance has been created with the primary constructor. 00083 * @param s the new string, must be null-terminated. 00084 */ 00085 void 00086 StringContent::set_string(const char *s) 00087 { 00088 if ( __string_owner ) { 00089 free(__string); 00090 __string = strdup(s); 00091 _payload = __string; 00092 _payload_size = strlen(__string) + 1; 00093 } else { 00094 throw Exception("Cannot set read-only string extracted from network message."); 00095 } 00096 } 00097 00098 00099 /** Get string. 00100 * @return null-terminated string 00101 */ 00102 const char * 00103 StringContent::get_string() const 00104 { 00105 return __string; 00106 } 00107 00108 00109 /** Get length of string. 00110 * @return string length 00111 */ 00112 size_t 00113 StringContent::get_string_length() 00114 { 00115 return _payload_size - 1; 00116 } 00117 00118 00119 void 00120 StringContent::serialize() 00121 { 00122 // nothing to do... 00123 } 00124 00125 } // end namespace fawkes