ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
protocols.cpp
Go to the documentation of this file.
1// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3// Copyright (C) 2015 Cherokees of Idaho.
4//
5// This file is part of GNU uCommon C++.
6//
7// GNU uCommon C++ is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Lesser General Public License as published
9// by the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// GNU uCommon C++ is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
19
20#include <ucommon-config.h>
21#include <ucommon/export.h>
22#include <ucommon/protocols.h>
23#include <ucommon/string.h>
24#include <ucommon/memory.h>
25#include <stdlib.h>
26#include <ctype.h>
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30
31#undef getc
32#undef putc
33#undef puts
34#undef gets
35
36namespace ucommon {
37
38char *MemoryProtocol::dup(const char *str)
39{
40 if(!str)
41 return NULL;
42 size_t len = strlen(str) + 1;
43 char *mem = static_cast<char *>(alloc(len));
44 if(mem)
45 String::set(mem, len, str);
46 else
48 return mem;
49}
50
51void *MemoryProtocol::dup(void *obj, size_t size)
52{
53 assert(obj != NULL);
54 assert(size > 0);
55
56 char *mem = static_cast<char *>(alloc(size));
57 if(mem)
58 memcpy(mem, obj, size);
59 else
61 return mem;
62}
63
64void *MemoryProtocol::zalloc(size_t size)
65{
66 void *mem = alloc(size);
67
68 if(mem)
69 memset(mem, 0, size);
70 else
72
73 return mem;
74}
75
77{
78}
79
81{
82 target = protocol;
83}
84
85void *MemoryRedirect::_alloc(size_t size)
86{
87 // a null redirect uses the heap...
88 if(!target)
89 return malloc(size);
90
91 return target->_alloc(size);
92}
93
95{
96}
97
99{
100}
101
103{
104}
105
107{
108}
109
111{
112}
113
114bool KeyProtocol::equal(const KeyProtocol& key) const
115{
116 if(keytype() != key.keytype())
117 return false;
118
119 if(keysize() != key.keysize() || !keysize())
120 return false;
121
122 if(memcmp(keydata(), key.keydata(), keysize()))
123 return false;
124
125 return true;
126}
127
129{
130 retain();
131 return this;
132}
133
135{
136public:
137 long* ref;
138 size_t pos;
139 char buf[32];
140
141 _input_long(long& v);
142
143 int _input(int code);
144};
145
147{
148public:
149 double* ref;
150 bool dot;
151 bool e;
152 size_t pos;
153 char buf[60];
154
155 _input_double(double& v);
156
157 int _input(int code);
158};
159
161{
162 ref = &v;
163 v = 0l;
164 pos = 0;
165}
166
168{
169 dot = e = false;
170 v = 0.0;
171 pos = 0;
172 ref = &v;
173}
174
176{
177 if(code == '-' && !pos)
178 goto valid;
179
180 if(isdigit(code) && pos < sizeof(buf) - 1)
181 goto valid;
182
183 buf[pos] = 0;
184 if(pos)
185 sscanf(buf, "%ld", ref);
186
187 return code;
188
189valid:
190 buf[pos++] = code;
191 return 0;
192}
193
195{
196 if((code == '-') && !pos)
197 goto valid;
198
199 if((code == '-') && buf[pos] == 'e')
200 goto valid;
201
202 if(tolower(code) == 'e' && !e) {
203 e = true;
204 code = 'e';
205 goto valid;
206 }
207
208 if((code == '.') && !dot) {
209 dot = true;
210 goto valid;
211 }
212
213 if(isdigit(code) && pos < sizeof(buf) - 1)
214 goto valid;
215
216 buf[pos] = 0;
217 if(pos)
218 sscanf(buf, "%lf", ref);
219
220 return code;
221
222valid:
223 buf[pos++] = code;
224 return 0;
225}
226
228{
229}
230
232{
233}
234
235} // namespace ucommon
Used for processing input.
Definition: protocols.h:154
Key data protocol used for things like maps and ordered lists.
Definition: protocols.h:215
virtual size_t keysize(void) const =0
Size of key data.
virtual bool equal(const KeyProtocol &compare) const
Definition: protocols.cpp:114
virtual ~KeyProtocol()
Definition: protocols.cpp:110
virtual int keytype(void) const =0
virtual const void * keydata(void) const =0
Buffer of key value.
virtual void _lock(void)
Definition: protocols.cpp:94
virtual void _unlock(void)
Definition: protocols.cpp:98
void * alloc(size_t size)
Convenience function.
Definition: protocols.h:65
virtual void * _alloc(size_t size)=0
Protocol to allocate memory from the pager heap.
char * dup(const char *string)
Duplicate NULL terminated string into allocated memory.
Definition: protocols.cpp:38
void * zalloc(size_t size)
Allocate memory from the pager heap.
Definition: protocols.cpp:64
MemoryRedirect(MemoryProtocol *protocol)
Definition: protocols.cpp:80
virtual void * _alloc(size_t size) __OVERRIDE
Definition: protocols.cpp:85
MemoryProtocol * target
Definition: protocols.h:103
A common base class for all managed objects.
Definition: protocols.h:174
ObjectProtocol * copy(void)
Retain (increase retention of) object when copying.
Definition: protocols.cpp:128
virtual void retain(void)=0
Method to retain (or increase retention) of an object.
virtual ~ObjectProtocol()
Required virtual destructor.
Definition: protocols.cpp:106
void set(const char *text)
Set string object to text of a null terminated string.
Definition: string.cpp:802
int _input(int code)
Extract formatted string for object.
Definition: protocols.cpp:194
_input_double(double &v)
Definition: protocols.cpp:167
int _input(int code)
Extract formatted string for object.
Definition: protocols.cpp:175
Private heaps, pools, and associations.
Common namespace for all ucommon objects.
Definition: access.cpp:23
String str(Socket &so, size_t size)
Definition: socket.cpp:3507
#define __LOCAL
Definition: platform.h:298
#define __THROW_ALLOC()
Definition: platform.h:52
Abstract interfaces and support.
Export interfaces for library interfaces.
A common string class and character string support functions.