forked from JadeMatrix/debencode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbnc_value.cpp
173 lines (151 loc) · 4.31 KB
/
dbnc_value.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* dbnc.cpp
*
* Implementation/definitions for the dbnc::value class
*
* debencode copyright (C) 2015 Joseph Durel [ [email protected] ]
* debencode homepage: [ github.com/JadeMatrix/debencode ]
*/
/* INCLUDES *******************************************************************//******************************************************************************/
#include "debencode.hpp"
/* DEFINITIONS ****************************************************************//******************************************************************************/
namespace dbnc
{
value::value()
{
clearCurrent();
}
value::value( std::istream& stream )
{
// IMPLEMENT: istream constructor
}
value::value( const value& original )
{
val_type = original.val_type;
switch( val_type )
{
case NONE:
clearCurrent();
break;
case STRING:
string_val = new std::string( *original.string_val );
break;
case INTEGER:
int_val = original.int_val;
break;
case LIST:
list_val = new std::vector< value >( *original.list_val );
break;
case DICTIONARY:
dict_val = new std::map< std::string, value >( *original.dict_val );
break;
default:
throw WRONG_TYPE;
break;
}
}
value::~value()
{
clearCurrent();
}
void value::set( std::string string_val )
{
clearCurrent();
val_type = STRING;
this -> string_val = new std::string( string_val );
}
void value::set( long int_val )
{
clearCurrent();
val_type = INTEGER;
this -> int_val = int_val;
}
void value::push( value& val )
{
if( val_type != LIST )
{
clearCurrent();
val_type = LIST;
list_val = new std::vector< value >;
}
list_val -> push_back( val );
}
void value::set( std::string key, value& val )
{
if( val_type != DICTIONARY )
{
clearCurrent();
val_type = DICTIONARY;
dict_val = new std::map< std::string, value >;
}
( *dict_val )[ key ] = val;
}
value::type value::getType()
{
return val_type;
}
std::string value::getString()
{
if( val_type != STRING )
throw WRONG_TYPE;
return *string_val;
}
long value::getInteger()
{
if( val_type != INTEGER )
throw WRONG_TYPE;
return int_val;
}
std::pair< std::vector< value >::iterator,
std::vector< value >::iterator > value::getList()
{
if( val_type != LIST )
throw WRONG_TYPE;
return std::pair< std::vector< value >::iterator,
std::vector< value >::iterator >( list_val -> begin(),
list_val -> end() );
}
std::pair< std::map< std::string, value >::iterator,
std::map< std::string, value >::iterator > value::getDictionary()
{
if( val_type != DICTIONARY )
throw WRONG_TYPE;
return std::pair< std::map< std::string, value >::iterator,
std::map< std::string, value >::iterator >( dict_val -> begin(),
dict_val -> end() );
}
void value::serializeBencode( std::ostream& stream )
{
// IMPLEMENT: Bencode serializer
}
void value::serializeJSON( std::ostream& stream )
{
// IMPLEMENT: JSON serializer
}
void value::serializeXML( std::ostream& stream )
{
// IMPLEMENT: XML serializer
}
void value::clearCurrent()
{
switch( val_type )
{
case NONE:
case INTEGER:
default:
// Nothing to delete
break;
case STRING:
delete string_val;
break;
case LIST:
delete list_val;
break;
case DICTIONARY:
delete dict_val;
break;
}
val_type = NONE;
void_val = 0x00;
}
}