-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathUniverseChannelAddress.cpp
More file actions
105 lines (90 loc) · 2.79 KB
/
UniverseChannelAddress.cpp
File metadata and controls
105 lines (90 loc) · 2.79 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* UniverseChannelAddress.cpp
* Represents a universe-channel address pair.
* Copyright (C) 2023 Peter Newman
*/
#include <assert.h>
#include <ola/Logging.h>
#include <ola/StringUtils.h>
#include <ola/dmx/UniverseChannelAddress.h>
#include <string.h>
#include <string>
namespace ola {
namespace dmx {
using std::string;
string UniverseChannelAddress::ToString() const {
std::ostringstream str;
str << Universe() << ":" << Channel();
return str.str();
}
/**
* Extract a UniverseChannelAddress from a string.
*/
bool UniverseChannelAddress::FromString(
const string &input,
UniverseChannelAddress *universe_channel_address) {
size_t pos = input.find_first_of(":");
if (pos == string::npos) {
return false;
}
unsigned int universe;
if (!StringToInt(input.substr(0, pos), &universe)) {
return false;
}
uint16_t channel;
if (!StringToInt(input.substr(pos + 1), &channel)) {
return false;
}
*universe_channel_address = UniverseChannelAddress(universe, channel);
return true;
}
UniverseChannelAddress UniverseChannelAddress::FromStringOrDie(
const string &address) {
UniverseChannelAddress universe_channel_address;
assert(FromString(address, &universe_channel_address));
return universe_channel_address;
}
string UniverseChannelAddressOneBased::ToStringZeroBased() const {
std::ostringstream str;
str << Universe() << ":" << ChannelZeroBased();
return str.str();
}
/**
* Extract a UniverseChannelAddressOneBased from a string.
*/
bool UniverseChannelAddressOneBased::FromString(
const string &input,
UniverseChannelAddressOneBased *universe_channel_address_one_based) {
size_t pos = input.find_first_of(":");
if (pos == string::npos) {
return false;
}
unsigned int universe;
if (!StringToInt(input.substr(0, pos), &universe)) {
return false;
}
uint16_t channel;
if (!StringToInt(input.substr(pos + 1), &channel)) {
return false;
}
*universe_channel_address_one_based = UniverseChannelAddressOneBased(
universe,
channel);
return true;
}
} // namespace dmx
} // namespace ola