-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathversion.cpp
More file actions
83 lines (67 loc) · 2.19 KB
/
version.cpp
File metadata and controls
83 lines (67 loc) · 2.19 KB
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
#include <steemit/protocol/version.hpp>
#include <fc/exception/exception.hpp>
#include <fc/variant.hpp>
#include <sstream>
namespace steemit { namespace protocol {
/* Quick conversion utilities from http://joelverhagen.com/blog/2010/11/convert-an-int-to-a-string-and-vice-versa-in-c/ */
inline int string_to_int( fc::string input )
{
std::stringstream s( input );
int i;
s >> i;
return i;
}
inline fc::string int_to_string( int input )
{
std::stringstream s;
s << input;
return s.str();
}
version::version( uint8_t m, uint8_t h, uint16_t r )
{
v_num = ( 0 | m ) << 8;
v_num = ( v_num | h ) << 16;
v_num = v_num | r;
}
version::operator fc::string()const
{
std::stringstream s;
s << ( ( v_num >> 24 ) & 0x000000FF )
<< '.'
<< ( ( v_num >> 16 ) & 0x000000FF )
<< '.'
<< ( ( v_num & 0x0000FFFF ) );
return s.str();
}
} } // steemit::protocol
namespace fc
{
void to_variant( const steemit::protocol::version& v, variant& var )
{
var = fc::string( v );
}
void from_variant( const variant& var, steemit::protocol::version& v )
{
uint32_t major = 0, hardfork = 0, revision = 0;
char dot_a = 0, dot_b = 0;
std::stringstream s( var.as_string() );
s >> major >> dot_a >> hardfork >> dot_b >> revision;
// We'll accept either m.h.v or m_h_v as canonical version strings
FC_ASSERT( ( dot_a == '.' || dot_a == '_' ) && dot_a == dot_b, "Variant does not contain proper dotted decimal format" );
FC_ASSERT( major <= 0xFF, "Major version is out of range" );
FC_ASSERT( hardfork <= 0xFF, "Hardfork version is out of range" );
FC_ASSERT( revision <= 0xFFFF, "Revision version is out of range" );
FC_ASSERT( s.eof(), "Extra information at end of version string" );
v.v_num = 0 | ( major << 24 ) | ( hardfork << 16 ) | revision;
}
void to_variant( const steemit::protocol::hardfork_version& hv, variant& var )
{
to_variant( (const steemit::protocol::version&) hv, var );
}
void from_variant( const variant& var, steemit::protocol::hardfork_version& hv )
{
steemit::protocol::version ver;
from_variant( var, ver );
hv.v_num = ver.v_num & 0xffff0000;
}
}