Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
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 <netcomm/utils/string_content.h>
25
26
#include <core/exception.h>
27
#include <cstring>
28
#include <cstdlib>
29
30
namespace
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
41
/** Primary constructor.
42
* @param initial_string initial string
43
*/
44
StringContent::StringContent
(
const
char
*initial_string)
45
{
46
__string_owner =
true
;
47
set_string
(initial_string);
48
}
49
50
51
/** Constructor.
52
* This ctor can be used with FawkesNetworkMessage::msgc().
53
* @param cid component ID, ignored
54
* @param msgid message ID, ignored
55
* @param payload Payload, checked if it can be a valid string.
56
* @param payload_size size in bytes of payload
57
*/
58
StringContent::StringContent
(
unsigned
int
cid,
unsigned
int
msgid,
59
void
*payload,
size_t
payload_size)
60
{
61
__string_owner =
false
;
62
_payload
=
payload
;
63
_payload_size
=
payload_size
;
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
72
/** Destructor. */
73
StringContent::~StringContent
()
74
{
75
if
( __string_owner ) {
76
free(__string);
77
}
78
}
79
80
81
/** Set the string.
82
* Can only be called if the instance has been created with the primary constructor.
83
* @param s the new string, must be null-terminated.
84
*/
85
void
86
StringContent::set_string
(
const
char
*s)
87
{
88
if
( __string_owner ) {
89
free(__string);
90
__string = strdup(s);
91
_payload
= __string;
92
_payload_size
= strlen(__string) + 1;
93
}
else
{
94
throw
Exception
(
"Cannot set read-only string extracted from network message."
);
95
}
96
}
97
98
99
/** Get string.
100
* @return null-terminated string
101
*/
102
const
char
*
103
StringContent::get_string
()
const
104
{
105
return
__string;
106
}
107
108
109
/** Get length of string.
110
* @return string length
111
*/
112
size_t
113
StringContent::get_string_length
()
114
{
115
return
_payload_size
- 1;
116
}
117
118
119
void
120
StringContent::serialize
()
121
{
122
// nothing to do...
123
}
124
125
}
// end namespace fawkes
src
libs
netcomm
utils
string_content.cpp
Generated by
1.8.3.1