diff --git a/common/base/LoggingTest.cpp b/common/base/LoggingTest.cpp index 83007ff9b5..a68ef638bb 100644 --- a/common/base/LoggingTest.cpp +++ b/common/base/LoggingTest.cpp @@ -70,7 +70,7 @@ void MockLogDestination::AddExpected(log_level level, string log_line) { */ void MockLogDestination::Write(log_level level, const string &log_line) { vector tokens; - ola::StringSplit(log_line, tokens, ":"); + ola::StringSplit(log_line, &tokens, ":"); vector::iterator iter; OLA_ASSERT_EQ(tokens.size() , (size_t) 3); OLA_ASSERT_GT(m_log_lines.size(), 0); diff --git a/common/network/IPV4Address.cpp b/common/network/IPV4Address.cpp index 1d9e90bef6..3bd2d17e12 100644 --- a/common/network/IPV4Address.cpp +++ b/common/network/IPV4Address.cpp @@ -67,7 +67,7 @@ bool IPV4StringToAddress(const string &address, struct in_addr *addr) { // TODO(Peter): This currently allows some rather quirky values as per // inet_aton, we may want to restrict that in future to match IPV4Validator - if (address.length() == 0) { + if (address.empty()) { // Don't bother trying to extract an address if we weren't given one return false; } @@ -98,16 +98,18 @@ string IPV4Address::ToString() const { IPV4Address* IPV4Address::FromString(const string &address) { struct in_addr addr; - if (!IPV4StringToAddress(address, &addr)) + if (!IPV4StringToAddress(address, &addr)) { return NULL; + } return new IPV4Address(addr.s_addr); } bool IPV4Address::FromString(const string &address, IPV4Address *target) { struct in_addr addr; - if (!IPV4StringToAddress(address, &addr)) + if (!IPV4StringToAddress(address, &addr)) { return false; + } *target = IPV4Address(addr.s_addr); return true; } diff --git a/common/network/MACAddress.cpp b/common/network/MACAddress.cpp index 873762892a..25f82a48be 100644 --- a/common/network/MACAddress.cpp +++ b/common/network/MACAddress.cpp @@ -144,13 +144,15 @@ bool StringToEther(const string &address, ether_addr *target) { * handle dots as well as colons as seperators) */ vector tokens; - ola::StringSplit(address, tokens, ":."); - if (tokens.size() != MACAddress::LENGTH) + ola::StringSplit(address, &tokens, ":."); + if (tokens.size() != MACAddress::LENGTH) { return false; + } for (unsigned int i = 0; i < MACAddress::LENGTH; i++) { - if (!ola::HexStringToInt(tokens[i], target->ether_addr_octet + i)) + if (!ola::HexStringToInt(tokens[i], target->ether_addr_octet + i)) { return false; + } } return true; } @@ -158,8 +160,9 @@ bool StringToEther(const string &address, ether_addr *target) { MACAddress* MACAddress::FromString(const string &address) { struct ether_addr addr; - if (!StringToEther(address, &addr)) + if (!StringToEther(address, &addr)) { return NULL; + } return new MACAddress(addr.ether_addr_octet); } @@ -167,8 +170,9 @@ MACAddress* MACAddress::FromString(const string &address) { bool MACAddress::FromString(const string &address, MACAddress *target) { struct ether_addr addr; - if (!StringToEther(address, &addr)) + if (!StringToEther(address, &addr)) { return false; + } *target = MACAddress(addr.ether_addr_octet); return true; diff --git a/common/rdm/UID.cpp b/common/rdm/UID.cpp index a3ab7a02e3..eed7e24f03 100644 --- a/common/rdm/UID.cpp +++ b/common/rdm/UID.cpp @@ -31,17 +31,20 @@ using std::vector; UID* UID::FromString(const string &uid) { vector tokens; - ola::StringSplit(uid, tokens, ":"); + ola::StringSplit(uid, &tokens, ":"); - if (tokens.size() != 2 || tokens[0].size() != 4 || tokens[1].size() != 8) + if (tokens.size() != 2 || tokens[0].size() != 4 || tokens[1].size() != 8) { return NULL; + } uint16_t esta_id; unsigned int device_id; - if (!ola::HexStringToInt(tokens[0], &esta_id)) + if (!ola::HexStringToInt(tokens[0], &esta_id)) { return NULL; - if (!ola::HexStringToInt(tokens[1], &device_id)) + } + if (!ola::HexStringToInt(tokens[1], &device_id)) { return NULL; + } return new UID(esta_id, device_id); } diff --git a/common/utils/DmxBuffer.cpp b/common/utils/DmxBuffer.cpp index 96fbac4339..91eee1adb8 100644 --- a/common/utils/DmxBuffer.cpp +++ b/common/utils/DmxBuffer.cpp @@ -175,7 +175,7 @@ bool DmxBuffer::SetFromString(const string &input) { m_length = 0; return true; } - StringSplit(input, dmx_values, ","); + StringSplit(input, &dmx_values, ","); for (iter = dmx_values.begin(); iter != dmx_values.end() && i < DMX_UNIVERSE_SIZE; ++iter, ++i) { m_data[i] = atoi(iter->data()); diff --git a/common/utils/StringUtils.cpp b/common/utils/StringUtils.cpp index 360d35f597..a2f2501ecc 100644 --- a/common/utils/StringUtils.cpp +++ b/common/utils/StringUtils.cpp @@ -39,7 +39,7 @@ using std::string; using std::vector; void StringSplit(const string &input, - vector &tokens, // NOLINT + vector *tokens, const string &delimiters) { string::size_type start_offset = 0; string::size_type end_offset = 0; @@ -47,12 +47,13 @@ void StringSplit(const string &input, while (1) { end_offset = input.find_first_of(delimiters, start_offset); if (end_offset == string::npos) { - tokens.push_back(input.substr(start_offset, input.size() - start_offset)); + tokens->push_back( + input.substr(start_offset, input.size() - start_offset)); return; } - tokens.push_back(input.substr(start_offset, end_offset - start_offset)); - start_offset = end_offset + 1 > input.size() ? string::npos : - end_offset + 1; + tokens->push_back(input.substr(start_offset, end_offset - start_offset)); + start_offset = (end_offset + 1 > input.size()) ? + string::npos : (end_offset + 1); } } @@ -61,16 +62,18 @@ void StringTrim(string *input) { string::size_type start = input->find_first_not_of(characters_to_trim); string::size_type end = input->find_last_not_of(characters_to_trim); - if (start == string::npos) + if (start == string::npos) { input->clear(); - else + } else { *input = input->substr(start, end - start + 1); + } } void ShortenString(string *input) { size_t index = input->find(static_cast(0)); - if (index != string::npos) + if (index != string::npos) { input->erase(index); + } } bool StringBeginsWith(const string &s, const string &prefix) { @@ -149,77 +152,95 @@ bool StringToBoolTolerant(const string &value, bool *output) { } bool StringToInt(const string &value, unsigned int *output, bool strict) { - if (value.empty()) + if (value.empty()) { return false; + } char *end_ptr; errno = 0; long long l = strtoll(value.data(), &end_ptr, 10); // NOLINT(runtime/int) - if (l < 0 || (l == 0 && errno != 0)) + if (l < 0 || (l == 0 && errno != 0)) { return false; - if (value == end_ptr) + } + if (value == end_ptr) { return false; - if (strict && *end_ptr != 0) + } + if (strict && *end_ptr != 0) { return false; - if (l > static_cast(UINT32_MAX)) // NOLINT(runtime/int) + } + if (l > static_cast(UINT32_MAX)) { // NOLINT(runtime/int) return false; + } *output = static_cast(l); return true; } bool StringToInt(const string &value, uint16_t *output, bool strict) { unsigned int v; - if (!StringToInt(value, &v, strict)) + if (!StringToInt(value, &v, strict)) { return false; - if (v > UINT16_MAX) + } + if (v > UINT16_MAX) { return false; + } *output = static_cast(v); return true; } bool StringToInt(const string &value, uint8_t *output, bool strict) { unsigned int v; - if (!StringToInt(value, &v, strict)) + if (!StringToInt(value, &v, strict)) { return false; - if (v > UINT8_MAX) + } + if (v > UINT8_MAX) { return false; + } *output = static_cast(v); return true; } bool StringToInt(const string &value, int *output, bool strict) { - if (value.empty()) + if (value.empty()) { return false; + } char *end_ptr; errno = 0; long long l = strtoll(value.data(), &end_ptr, 10); // NOLINT(runtime/int) - if (l == 0 && errno != 0) + if (l == 0 && errno != 0) { return false; - if (value == end_ptr) + } + if (value == end_ptr) { return false; - if (strict && *end_ptr != 0) + } + if (strict && *end_ptr != 0) { return false; - if (l < INT32_MIN || l > INT32_MAX) + } + if (l < INT32_MIN || l > INT32_MAX) { return false; + } *output = static_cast(l); return true; } bool StringToInt(const string &value, int16_t *output, bool strict) { int v; - if (!StringToInt(value, &v, strict)) + if (!StringToInt(value, &v, strict)) { return false; - if (v < INT16_MIN || v > INT16_MAX) + } + if (v < INT16_MIN || v > INT16_MAX) { return false; + } *output = static_cast(v); return true; } bool StringToInt(const string &value, int8_t *output, bool strict) { int v; - if (!StringToInt(value, &v, strict)) + if (!StringToInt(value, &v, strict)) { return false; - if (v < INT8_MIN || v > INT8_MAX) + } + if (v < INT8_MIN || v > INT8_MAX) { return false; + } *output = static_cast(v); return true; } @@ -285,19 +306,17 @@ string EncodeString(const string &original) { if (isprint(*iter)) { encoded << *iter; } else { - encoded << "\\x" << std::setfill('0') << std::setw(2) << std::hex << - static_cast(*iter); + encoded << "\\x" + << ola::strings::ToHex(static_cast(*iter), false); } } return encoded.str(); } void ReplaceAll(string *original, const string &find, const string &replace) { - if (original->empty()) - return; // No text, so nothing to do - - if (find.empty()) - return; // Nothing to find, so nothing to do + if (original->empty() || find.empty()) { + return; // No text or nothing to find, so nothing to do + } size_t start = 0; while ((start = original->find(find, start)) != string::npos) { @@ -309,74 +328,86 @@ void ReplaceAll(string *original, const string &find, const string &replace) { bool HexStringToInt(const string &value, uint8_t *output) { uint32_t temp; - if (!HexStringToInt(value, &temp)) + if (!HexStringToInt(value, &temp)) { return false; - if (temp > 0xff) + } + if (temp > UINT8_MAX) { return false; + } *output = static_cast(temp); return true; } bool HexStringToInt(const string &value, uint16_t *output) { uint32_t temp; - if (!HexStringToInt(value, &temp)) + if (!HexStringToInt(value, &temp)) { return false; - if (temp > UINT16_MAX) + } + if (temp > UINT16_MAX) { return false; + } *output = static_cast(temp); return true; } bool HexStringToInt(const string &value, uint32_t *output) { - if (value.empty()) + if (value.empty()) { return false; + } size_t found = value.find_first_not_of("ABCDEFabcdef0123456789"); - if (found != string::npos) + if (found != string::npos) { return false; + } *output = strtoul(value.data(), NULL, 16); return true; } bool HexStringToInt(const string &value, int8_t *output) { int32_t temp; - if (!HexStringToInt(value, &temp)) + if (!HexStringToInt(value, &temp)) { return false; - if (temp < 0 || temp > static_cast(UINT8_MAX)) + } + if (temp < 0 || temp > static_cast(UINT8_MAX)) { return false; + } *output = static_cast(temp); return true; } bool HexStringToInt(const string &value, int16_t *output) { int32_t temp; - if (!HexStringToInt(value, &temp)) + if (!HexStringToInt(value, &temp)) { return false; - if (temp < 0 || temp > static_cast(UINT16_MAX)) + } + if (temp < 0 || temp > static_cast(UINT16_MAX)) { return false; + } *output = static_cast(temp); return true; } bool HexStringToInt(const string &value, int32_t *output) { - if (value.empty()) + if (value.empty()) { return false; + } size_t found = value.find_first_not_of("ABCDEFabcdef0123456789"); - if (found != string::npos) + if (found != string::npos) { return false; + } *output = strtoll(value.data(), NULL, 16); return true; } void ToLower(string *s) { std::transform(s->begin(), s->end(), s->begin(), - std::ptr_fun(std::tolower)); + std::ptr_fun(std::tolower)); } void ToUpper(string *s) { std::transform(s->begin(), s->end(), s->begin(), - std::ptr_fun(std::toupper)); + std::ptr_fun(std::toupper)); } void CapitalizeLabel(string *s) { @@ -384,15 +415,17 @@ void CapitalizeLabel(string *s) { for (string::iterator iter = s->begin(); iter != s->end(); ++iter) { switch (*iter) { case '-': - // fall through, also convert to space + // fall through, also convert to space then capitalize next character case '_': *iter = ' '; + // fall through, also convert to space then capitalize next character case ' ': capitalize = true; break; default: - if (capitalize && islower(*iter)) + if (capitalize && islower(*iter)) { *iter = toupper(*iter); + } capitalize = false; } } @@ -419,8 +452,9 @@ void CustomCapitalizeLabel(string *s) { while (true) { size_t match_position = s->find(ancronym, last_match); - if (match_position == string::npos) + if (match_position == string::npos) { break; + } last_match = match_position + 1; size_t end_position = match_position + ancronym_size; diff --git a/common/utils/StringUtilsTest.cpp b/common/utils/StringUtilsTest.cpp index da7d11f19c..2979bda11d 100644 --- a/common/utils/StringUtilsTest.cpp +++ b/common/utils/StringUtilsTest.cpp @@ -128,13 +128,13 @@ CPPUNIT_TEST_SUITE_REGISTRATION(StringUtilsTest); void StringUtilsTest::testSplit() { vector tokens; string input = ""; - StringSplit(input, tokens); + StringSplit(input, &tokens); OLA_ASSERT_EQ((size_t) 1, tokens.size()); OLA_ASSERT_EQ(string(""), tokens[0]); input = "1 2 345"; tokens.clear(); - StringSplit(input, tokens); + StringSplit(input, &tokens); OLA_ASSERT_EQ((size_t) 3, tokens.size()); OLA_ASSERT_EQ(string("1"), tokens[0]); @@ -143,7 +143,7 @@ void StringUtilsTest::testSplit() { input = "1,2,345"; tokens.clear(); - StringSplit(input, tokens, ","); + StringSplit(input, &tokens, ","); OLA_ASSERT_EQ((size_t) 3, tokens.size()); OLA_ASSERT_EQ(string("1"), tokens[0]); @@ -152,7 +152,7 @@ void StringUtilsTest::testSplit() { input = ",1,2,345,,"; tokens.clear(); - StringSplit(input, tokens, ","); + StringSplit(input, &tokens, ","); OLA_ASSERT_EQ((size_t) 6, tokens.size()); OLA_ASSERT_EQ(string(""), tokens[0]); @@ -164,7 +164,7 @@ void StringUtilsTest::testSplit() { input = "1 2,345"; tokens.clear(); - StringSplit(input, tokens, " ,"); + StringSplit(input, &tokens, " ,"); OLA_ASSERT_EQ((size_t) 3, tokens.size()); OLA_ASSERT_EQ(string("1"), tokens[0]); @@ -173,7 +173,7 @@ void StringUtilsTest::testSplit() { input = "1, 2,345"; tokens.clear(); - StringSplit(input, tokens, " ,"); + StringSplit(input, &tokens, " ,"); OLA_ASSERT_EQ((size_t) 4, tokens.size()); OLA_ASSERT_EQ(string("1"), tokens[0]); @@ -183,10 +183,23 @@ void StringUtilsTest::testSplit() { input = "1"; tokens.clear(); - StringSplit(input, tokens, "."); + StringSplit(input, &tokens, "."); OLA_ASSERT_EQ((size_t) 1, tokens.size()); OLA_ASSERT_EQ(string("1"), tokens[0]); + + // And the old non-pointer version + input = ",1,2,345,,"; + tokens.clear(); + StringSplit(input, tokens, ","); + + OLA_ASSERT_EQ((size_t) 6, tokens.size()); + OLA_ASSERT_EQ(string(""), tokens[0]); + OLA_ASSERT_EQ(string("1"), tokens[1]); + OLA_ASSERT_EQ(string("2"), tokens[2]); + OLA_ASSERT_EQ(string("345"), tokens[3]); + OLA_ASSERT_EQ(string(""), tokens[4]); + OLA_ASSERT_EQ(string(""), tokens[5]); } diff --git a/common/web/JsonPointer.cpp b/common/web/JsonPointer.cpp index 5307a39327..f50cd6be7f 100644 --- a/common/web/JsonPointer.cpp +++ b/common/web/JsonPointer.cpp @@ -49,7 +49,7 @@ JsonPointer::JsonPointer(const string &path) } Tokens escaped_tokens; - StringSplit(path.substr(1), escaped_tokens, "/"); + StringSplit(path.substr(1), &escaped_tokens, "/"); Tokens::const_iterator iter = escaped_tokens.begin(); for (; iter != escaped_tokens.end(); ++iter) { diff --git a/examples/ShowLoader.cpp b/examples/ShowLoader.cpp index 2a67e378b3..441f3c47bf 100644 --- a/examples/ShowLoader.cpp +++ b/examples/ShowLoader.cpp @@ -123,7 +123,7 @@ ShowLoader::State ShowLoader::NextFrame(unsigned int *universe, return END_OF_FILE; vector inputs; - ola::StringSplit(line, inputs); + ola::StringSplit(line, &inputs); if (inputs.size() != 2) { OLA_WARN << "Line " << m_line << " invalid: " << line; diff --git a/examples/ola-recorder.cpp b/examples/ola-recorder.cpp index 421ad48294..117f437b51 100644 --- a/examples/ola-recorder.cpp +++ b/examples/ola-recorder.cpp @@ -70,7 +70,7 @@ int RecordShow() { vector universe_strs; vector universes; - ola::StringSplit(FLAGS_universes.str(), universe_strs, ","); + ola::StringSplit(FLAGS_universes.str(), &universe_strs, ","); vector::const_iterator iter = universe_strs.begin(); for (; iter != universe_strs.end(); ++iter) { unsigned int universe; diff --git a/examples/ola-timecode.cpp b/examples/ola-timecode.cpp index 5b464d5854..4063987b68 100644 --- a/examples/ola-timecode.cpp +++ b/examples/ola-timecode.cpp @@ -88,7 +88,7 @@ int main(int argc, char *argv[]) { } vector tokens; - ola::StringSplit(argv[1], tokens, ":"); + ola::StringSplit(argv[1], &tokens, ":"); if (tokens.size() != 4) { cerr << "Invalid TimeCode value " << argv[1] << endl; exit(ola::EXIT_USAGE); diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index 099e1ea89f..6f11df108b 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -44,13 +44,30 @@ namespace ola { * If two delimiters appear next to each other an empty string is added to the * output vector. * @param[in] input the string to split - * @param[out] tokens the parts of the string + * @param[out] tokens pointer to a vector with the parts of the string * @param delimiters the delimiiter to use for splitting. Defaults to ' ' */ void StringSplit(const std::string &input, - std::vector &tokens, // NOLINT + std::vector *tokens, const std::string &delimiters = " "); +/** + * @brief Split a string into pieces. + * + * If two delimiters appear next to each other an empty string is added to the + * output vector. + * @param[in] input the string to split + * @param[out] tokens the parts of the string + * @param delimiters the delimiiter to use for splitting. Defaults to ' ' + * @deprecated Use the version with a vector pointer instead (3 Jan 2015). + */ +inline void StringSplit( + const std::string &input, + std::vector &tokens, // NOLINT(runtime/references) + const std::string &delimiters = " ") { + StringSplit(input, &tokens, delimiters); +} + /** * @brief Trim leading and trailing whitespace from a string * @param input the string to trim. diff --git a/include/ola/rdm/ResponderSettings.h b/include/ola/rdm/ResponderSettings.h index 4b758e1803..b21fc6bc75 100644 --- a/include/ola/rdm/ResponderSettings.h +++ b/include/ola/rdm/ResponderSettings.h @@ -272,7 +272,8 @@ const RDMResponse *SettingManager::GetDescription( return NackWithReason(request, NR_DATA_OUT_OF_RANGE); } else { const SettingType *setting = m_settings->Lookup(arg - offset); - uint8_t output[setting->DescriptionResponseSize()]; // NOLINT + uint8_t output[ + setting->DescriptionResponseSize()]; // NOLINT(runtime/arrays) unsigned int size = setting->GenerateDescriptionResponse(arg, output); return GetResponseFromData(request, output, size, RDM_ACK); } diff --git a/olad/AvahiDiscoveryAgent.cpp b/olad/AvahiDiscoveryAgent.cpp index a05554d4a1..5ce9d5a883 100644 --- a/olad/AvahiDiscoveryAgent.cpp +++ b/olad/AvahiDiscoveryAgent.cpp @@ -110,7 +110,7 @@ AvahiDiscoveryAgent::ServiceEntry::ServiceEntry( params(NULL), m_type_spec(type_spec) { vector tokens; - StringSplit(type_spec, tokens, ","); + StringSplit(type_spec, &tokens, ","); m_type = tokens[0]; if (tokens.size() > 1) { copy(tokens.begin() + 1, tokens.end(), std::back_inserter(m_sub_types)); diff --git a/olad/OladHTTPServer.cpp b/olad/OladHTTPServer.cpp index 71c1911c14..4d8052c4a3 100644 --- a/olad/OladHTTPServer.cpp +++ b/olad/OladHTTPServer.cpp @@ -1007,16 +1007,17 @@ void OladHTTPServer::AddPriorityActions(ActionQueue *action_queue, void OladHTTPServer::DecodePortIds(const string &port_ids, vector *ports) { vector port_strings; - StringSplit(port_ids, port_strings, ","); + StringSplit(port_ids, &port_strings, ","); vector::const_iterator iter; vector tokens; for (iter = port_strings.begin(); iter != port_strings.end(); ++iter) { - if (iter->empty()) + if (iter->empty()) { continue; + } tokens.clear(); - StringSplit(*iter, tokens, "-"); + StringSplit(*iter, &tokens, "-"); if (tokens.size() != 3 || (tokens[1] != "I" && tokens[1] != "O")) { OLA_INFO << "Not a valid port id " << *iter; diff --git a/olad/Preferences.cpp b/olad/Preferences.cpp index e908a47991..c9fc28296d 100644 --- a/olad/Preferences.cpp +++ b/olad/Preferences.cpp @@ -17,10 +17,13 @@ * This class stores preferences in files * Copyright (C) 2005 Simon Newton */ + +#define __STDC_LIMIT_MACROS // for UINT8_MAX & friends #include #include #include #include +#include #include #include #include @@ -36,6 +39,7 @@ #include "ola/Logging.h" #include "ola/StringUtils.h" #include "ola/file/Util.h" +#include "ola/network/IPV4Address.h" #include "ola/stl/STLUtils.h" #include "ola/thread/Thread.h" #include "olad/Preferences.h" @@ -96,8 +100,9 @@ bool BoolValidator::IsValid(const string &value) const { bool UIntValidator::IsValid(const string &value) const { unsigned int output; - if (!StringToInt(value, &output)) + if (!StringToInt(value, &output)) { return false; + } return (output >= m_gt && output <= m_lt); } @@ -105,8 +110,9 @@ bool UIntValidator::IsValid(const string &value) const { bool IntValidator::IsValid(const string &value) const { int output; - if (!StringToInt(value, &output)) + if (!StringToInt(value, &output)) { return false; + } return (output >= m_gt && output <= m_lt); } @@ -123,8 +129,9 @@ bool SetValidator::IsValid(const string &value) const { unsigned int output; // It's an integer based set validator, so if we can't parse it to an // integer, it can't possibly match an integer and be valid - if (!StringToInt(value, &output)) + if (!StringToInt(value, &output)) { return false; + } return STLContains(m_values, output); } @@ -135,28 +142,33 @@ bool SetValidator::IsValid(const string &value) const { int output; // It's an integer based set validator, so if we can't parse it to an // integer, it can't possibly match an integer and be valid - if (!StringToInt(value, &output)) + if (!StringToInt(value, &output)) { return false; + } return STLContains(m_values, output); } bool IPv4Validator::IsValid(const string &value) const { - if (value.empty()) + if (value.empty()) { return m_empty_ok; + } vector tokens; - StringSplit(value, tokens, "."); - if (tokens.size() != 4) + StringSplit(value, &tokens, "."); + if (tokens.size() != ola::network::IPV4Address::LENGTH) { return false; + } for (unsigned int i = 0 ; i < 4; i++) { unsigned int octet; - if (!StringToInt(tokens[i], &octet)) + if (!StringToInt(tokens[i], &octet)) { return false; - if (octet > 255) + } + if (octet > UINT8_MAX) { return false; + } } return true; } @@ -403,11 +415,12 @@ bool FileBackedPreferences::LoadFromFile(const string &filename) { while (getline(pref_file, line)) { StringTrim(&line); - if (line.empty() || line.at(0) == '#') + if (line.empty() || line.at(0) == '#') { continue; + } vector tokens; - StringSplit(line, tokens, "="); + StringSplit(line, &tokens, "="); if (tokens.size() != 2) { OLA_INFO << "Skipping line: " << line; diff --git a/plugins/artnet/ArtNetNode.cpp b/plugins/artnet/ArtNetNode.cpp index 9728764522..b918c45177 100644 --- a/plugins/artnet/ArtNetNode.cpp +++ b/plugins/artnet/ArtNetNode.cpp @@ -405,12 +405,15 @@ uint8_t ArtNetNodeImpl::GetInputPortUniverse(uint8_t port_id) const { void ArtNetNodeImpl::DisableInputPort(uint8_t port_id) { InputPort *port = GetInputPort(port_id); - bool was_enabled = port->enabled; - if (port) + bool was_enabled = false; + if (port) { + was_enabled = port->enabled; port->enabled = false; + } - if (was_enabled) + if (was_enabled) { SendPollReplyIfRequired(); + } } bool ArtNetNodeImpl::InputPortState(uint8_t port_id) const { @@ -421,11 +424,13 @@ bool ArtNetNodeImpl::InputPortState(uint8_t port_id) const { bool ArtNetNodeImpl::SetOutputPortUniverse(uint8_t port_id, uint8_t universe_id) { OutputPort *port = GetOutputPort(port_id); - if (!port) + if (!port) { return false; + } - if (port->enabled && (port->universe_address & 0xf) == (universe_id & 0xf)) + if (port->enabled && (port->universe_address & 0xf) == (universe_id & 0xf)) { return true; + } port->universe_address = ( (universe_id & 0x0f) | (port->universe_address & 0xf0)); diff --git a/plugins/ftdidmx/FtdiDmxPlugin.cpp b/plugins/ftdidmx/FtdiDmxPlugin.cpp index 7dd4a52275..2bf57c634b 100644 --- a/plugins/ftdidmx/FtdiDmxPlugin.cpp +++ b/plugins/ftdidmx/FtdiDmxPlugin.cpp @@ -35,7 +35,6 @@ namespace ftdidmx { using std::string; using std::vector; -const char FtdiDmxPlugin::DEFAULT_FREQUENCY[] = "30"; const char FtdiDmxPlugin::K_FREQUENCY[] = "frequency"; const char FtdiDmxPlugin::PLUGIN_NAME[] = "FTDI USB DMX"; const char FtdiDmxPlugin::PLUGIN_PREFIX[] = "ftdidmx"; @@ -139,8 +138,9 @@ bool FtdiDmxPlugin::SetDefaultPreferences() { int unsigned FtdiDmxPlugin::GetFrequency() { unsigned int frequency; - if (!StringToInt(m_preferences->GetValue(K_FREQUENCY), &frequency)) - StringToInt(DEFAULT_FREQUENCY, &frequency); + if (!StringToInt(m_preferences->GetValue(K_FREQUENCY), &frequency)) { + return DEFAULT_FREQUENCY; + } return frequency; } } // namespace ftdidmx diff --git a/plugins/ftdidmx/FtdiDmxPlugin.h b/plugins/ftdidmx/FtdiDmxPlugin.h index 5b63091e05..f636d46247 100644 --- a/plugins/ftdidmx/FtdiDmxPlugin.h +++ b/plugins/ftdidmx/FtdiDmxPlugin.h @@ -64,7 +64,8 @@ class FtdiDmxPlugin : public Plugin { bool SetDefaultPreferences(); unsigned int GetFrequency(); - static const char DEFAULT_FREQUENCY[]; + static const uint8_t DEFAULT_FREQUENCY = 30; + static const char K_FREQUENCY[]; static const char PLUGIN_NAME[]; static const char PLUGIN_PREFIX[]; diff --git a/plugins/gpio/GPIOPlugin.cpp b/plugins/gpio/GPIOPlugin.cpp index c47d809e99..f032ae1779 100644 --- a/plugins/gpio/GPIOPlugin.cpp +++ b/plugins/gpio/GPIOPlugin.cpp @@ -71,7 +71,7 @@ bool GPIOPlugin::StartHook() { } vector pin_list; - StringSplit(m_preferences->GetValue(GPIO_PINS_KEY), pin_list, ","); + StringSplit(m_preferences->GetValue(GPIO_PINS_KEY), &pin_list, ","); vector::const_iterator iter = pin_list.begin(); for (; iter != pin_list.end(); ++iter) { if (iter->empty()) { diff --git a/plugins/osc/OSCPlugin.cpp b/plugins/osc/OSCPlugin.cpp index 1cf6ec58b0..a9c10b414e 100644 --- a/plugins/osc/OSCPlugin.cpp +++ b/plugins/osc/OSCPlugin.cpp @@ -18,6 +18,8 @@ * Copyright (C) 2012 Simon Newton */ +#define __STDC_LIMIT_MACROS // for UINT8_MAX & friends +#include #include #include #include @@ -40,9 +42,7 @@ using std::string; using std::vector; const char OSCPlugin::DEFAULT_ADDRESS_TEMPLATE[] = "/dmx/universe/%d"; -const char OSCPlugin::DEFAULT_PORT_COUNT[] = "5"; const char OSCPlugin::DEFAULT_TARGETS_TEMPLATE[] = ""; -const char OSCPlugin::DEFAULT_UDP_PORT[] = "7770"; const char OSCPlugin::INPUT_PORT_COUNT_KEY[] = "input_ports"; const char OSCPlugin::OUTPUT_PORT_COUNT_KEY[] = "output_ports"; const char OSCPlugin::PLUGIN_NAME[] = "OSC"; @@ -64,8 +64,9 @@ const char OSCPlugin::INT_INDIVIDUAL_FORMAT[] = "individual_int"; bool OSCPlugin::StartHook() { // Get the value of UDP_PORT_KEY or use the default value if it isn't valid. uint16_t udp_port; - if (!StringToInt(m_preferences->GetValue(UDP_PORT_KEY), &udp_port)) - StringToInt(DEFAULT_UDP_PORT, &udp_port); + if (!StringToInt(m_preferences->GetValue(UDP_PORT_KEY), &udp_port)) { + udp_port = DEFAULT_UDP_PORT; + } // For each input port, add the address to the vector vector port_addresses; @@ -85,7 +86,7 @@ bool OSCPlugin::StartHook() { const string key = ExpandTemplate(PORT_TARGETS_TEMPLATE, i); vector tokens; - StringSplit(m_preferences->GetValue(key), tokens, ","); + StringSplit(m_preferences->GetValue(key), &tokens, ","); vector::const_iterator iter = tokens.begin(); for (; iter != tokens.end(); ++iter) { @@ -183,7 +184,7 @@ bool OSCPlugin::SetDefaultPreferences() { DEFAULT_PORT_COUNT); save |= m_preferences->SetDefaultValue(UDP_PORT_KEY, - UIntValidator(1, 0xffff), + UIntValidator(1, UINT16_MAX), DEFAULT_UDP_PORT); for (unsigned int i = 0; i < GetPortCount(INPUT_PORT_COUNT_KEY); i++) { @@ -211,13 +212,15 @@ bool OSCPlugin::SetDefaultPreferences() { /** - * Given a key, return the port count from the preferences. Defaults to - * DEFAULT_PORT_COUNT if the value was invalid. + * @brief Given a key, return the port count from the preferences. + * + * Defaults to DEFAULT_PORT_COUNT if the value was invalid. */ unsigned int OSCPlugin::GetPortCount(const string& key) const { unsigned int port_count; - if (!StringToInt(m_preferences->GetValue(key), &port_count)) - StringToInt(DEFAULT_PORT_COUNT, &port_count); + if (!StringToInt(m_preferences->GetValue(key), &port_count)) { + return DEFAULT_PORT_COUNT; + } return port_count; } @@ -228,12 +231,14 @@ unsigned int OSCPlugin::GetPortCount(const string& key) const { bool OSCPlugin::ExtractOSCTarget(const string &str, OSCTarget *target) { size_t pos = str.find_first_of("/"); - if (pos == string::npos) + if (pos == string::npos) { return false; + } if (!IPV4SocketAddress::FromString(str.substr(0, pos), - &target->socket_address)) + &target->socket_address)) { return false; + } target->osc_address = str.substr(pos); return true; } diff --git a/plugins/osc/OSCPlugin.h b/plugins/osc/OSCPlugin.h index 09bcd64eb8..54d99f21de 100644 --- a/plugins/osc/OSCPlugin.h +++ b/plugins/osc/OSCPlugin.h @@ -55,10 +55,11 @@ class OSCPlugin: public ola::Plugin { OSCDevice::PortConfig *port_config); OSCDevice *m_device; + static const uint8_t DEFAULT_PORT_COUNT = 5; + static const uint16_t DEFAULT_UDP_PORT = 7770; + static const char DEFAULT_ADDRESS_TEMPLATE[]; - static const char DEFAULT_PORT_COUNT[]; static const char DEFAULT_TARGETS_TEMPLATE[]; - static const char DEFAULT_UDP_PORT[]; static const char INPUT_PORT_COUNT_KEY[]; static const char OUTPUT_PORT_COUNT_KEY[]; static const char PLUGIN_NAME[]; diff --git a/plugins/usbpro/UsbSerialPlugin.cpp b/plugins/usbpro/UsbSerialPlugin.cpp index 5f0bed5114..9fb74a1d04 100644 --- a/plugins/usbpro/UsbSerialPlugin.cpp +++ b/plugins/usbpro/UsbSerialPlugin.cpp @@ -51,8 +51,6 @@ using std::string; using std::vector; const char UsbSerialPlugin::DEFAULT_DEVICE_DIR[] = "/dev"; -const char UsbSerialPlugin::DEFAULT_PRO_FPS_LIMIT[] = "190"; -const char UsbSerialPlugin::DEFAULT_ULTRA_FPS_LIMIT[] = "40"; const char UsbSerialPlugin::DEVICE_DIR_KEY[] = "device_dir"; const char UsbSerialPlugin::DEVICE_PREFIX_KEY[] = "device_prefix"; const char UsbSerialPlugin::IGNORED_DEVICES_KEY[] = "ignore_device"; @@ -361,8 +359,9 @@ string UsbSerialPlugin::GetDeviceName( unsigned int UsbSerialPlugin::GetProFrameLimit() { unsigned int fps_limit; if (!StringToInt(m_preferences->GetValue(USB_PRO_FPS_LIMIT_KEY) , - &fps_limit)) - StringToInt(DEFAULT_PRO_FPS_LIMIT, &fps_limit); + &fps_limit)) { + return DEFAULT_PRO_FPS_LIMIT; + } return fps_limit; } @@ -373,8 +372,9 @@ unsigned int UsbSerialPlugin::GetProFrameLimit() { unsigned int UsbSerialPlugin::GetUltraDMXProFrameLimit() { unsigned int fps_limit; if (!StringToInt(m_preferences->GetValue(ULTRA_FPS_LIMIT_KEY) , - &fps_limit)) - StringToInt(DEFAULT_ULTRA_FPS_LIMIT, &fps_limit); + &fps_limit)) { + return DEFAULT_ULTRA_FPS_LIMIT; + } return fps_limit; } } // namespace usbpro diff --git a/plugins/usbpro/UsbSerialPlugin.h b/plugins/usbpro/UsbSerialPlugin.h index e75aef7772..5df6993eff 100644 --- a/plugins/usbpro/UsbSerialPlugin.h +++ b/plugins/usbpro/UsbSerialPlugin.h @@ -71,8 +71,6 @@ class UsbSerialPlugin: public ola::Plugin, public NewWidgetHandler { WidgetDetectorThread m_detector_thread; static const char DEFAULT_DEVICE_DIR[]; - static const char DEFAULT_PRO_FPS_LIMIT[]; - static const char DEFAULT_ULTRA_FPS_LIMIT[]; static const char DEVICE_DIR_KEY[]; static const char DEVICE_PREFIX_KEY[]; static const char IGNORED_DEVICES_KEY[]; @@ -86,6 +84,9 @@ class UsbSerialPlugin: public ola::Plugin, public NewWidgetHandler { static const char USBPRO_DEVICE_NAME[]; static const char USB_PRO_FPS_LIMIT_KEY[]; static const char ULTRA_FPS_LIMIT_KEY[]; + + static const uint8_t DEFAULT_PRO_FPS_LIMIT = 190; + static const uint8_t DEFAULT_ULTRA_FPS_LIMIT = 40; static const unsigned int MAX_PRO_FPS_LIMIT = 1000; static const unsigned int MAX_ULTRA_FPS_LIMIT = 1000; }; diff --git a/tools/e133/e133-monitor.cpp b/tools/e133/e133-monitor.cpp index e42b6c2df2..2379265bc9 100644 --- a/tools/e133/e133-monitor.cpp +++ b/tools/e133/e133-monitor.cpp @@ -169,7 +169,7 @@ int main(int argc, char *argv[]) { vector targets; if (!FLAGS_target_addresses.str().empty()) { vector tokens; - ola::StringSplit(FLAGS_target_addresses, tokens, ","); + ola::StringSplit(FLAGS_target_addresses, &tokens, ","); vector::const_iterator iter = tokens.begin(); for (; iter != tokens.end(); ++iter) {