Fawkes API Fawkes Development Version
string_content.cpp
1
2/***************************************************************************
3 * string_content.cpp - A dynamically sized string message content
4 *
5 * Created: Mon Mar 17 13:58:03 2008
6 * Copyright 2007-2008 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#include <core/exception.h>
25#include <netcomm/utils/string_content.h>
26
27#include <cstdlib>
28#include <cstring>
29
30namespace fawkes {
31
32/** @class StringContent <netcomm/utils/string_content.h>
33 * Content for a variable length string.
34 * This content class can be used with a FawkesNetworkMessage. It takes a
35 * single string of variable size and stuffs it into a message.
36 *
37 * @author Tim Niemueller
38 */
39
40/** Primary constructor.
41 * @param initial_string initial string
42 */
43StringContent::StringContent(const char *initial_string)
44{
45 string_owner_ = true;
46 set_string(initial_string);
47}
48
49/** Constructor.
50 * This ctor can be used with FawkesNetworkMessage::msgc().
51 * @param cid component ID, ignored
52 * @param msgid message ID, ignored
53 * @param payload Payload, checked if it can be a valid string.
54 * @param payload_size size in bytes of payload
55 */
57 unsigned int msgid,
58 void * payload,
59 size_t payload_size)
60{
61 string_owner_ = false;
64 string_ = (char *)payload;
65 if (string_[payload_size - 1] != 0) {
66 // string is not null-terminated, it has not been created with this class, error!
67 throw Exception("String content of network message is not null-terminated.");
68 }
69}
70
71/** Destructor. */
73{
74 if (string_owner_) {
75 free(string_);
76 }
77}
78
79/** Set the string.
80 * Can only be called if the instance has been created with the primary constructor.
81 * @param s the new string, must be null-terminated.
82 */
83void
85{
86 if (string_owner_) {
87 free(string_);
88 string_ = strdup(s);
89 _payload = string_;
90 _payload_size = strlen(string_) + 1;
91 } else {
92 throw Exception("Cannot set read-only string extracted from network message.");
93 }
94}
95
96/** Get string.
97 * @return null-terminated string
98 */
99const char *
101{
102 return string_;
103}
104
105/** Get length of string.
106 * @return string length
107 */
108size_t
110{
111 return _payload_size - 1;
112}
113
114void
116{
117 // nothing to do...
118}
119
120} // end namespace fawkes
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual size_t payload_size()
Return payload size.
void * _payload
Pointer to payload.
virtual void * payload()
Return pointer to payload.
const char * get_string() const
Get string.
size_t get_string_length()
Get length of string.
virtual ~StringContent()
Destructor.
StringContent(const char *initial_string)
Primary constructor.
virtual void serialize()
Serialize message content.
void set_string(const char *s)
Set the string.
Fawkes library namespace.