Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some more issues #573

Merged
merged 17 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/rdm/RDMHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,15 +994,15 @@ string StatusMessageIdToString(uint16_t message_id,
// Data Value shall be a signed integer." but I'm sure it's what was
// intended. The same thing is technically true with the slots too.
str << "Proxy Drop: PID "
<< IntToHexString(reinterpret_cast<uint16_t&>(data1)) << " at TN "
<< ToHex(reinterpret_cast<uint16_t&>(data1)) << " at TN "
<< data2;
break;
case STS_ASC_RXOK:
str << "DMX ASC " << IntToHexString(reinterpret_cast<uint16_t&>(data1))
str << "DMX ASC " << ToHex(reinterpret_cast<uint16_t&>(data1))
<< " received OK";
break;
case STS_ASC_DROPPED:
str << "DMX ASC " << IntToHexString(reinterpret_cast<uint16_t&>(data1))
str << "DMX ASC " << ToHex(reinterpret_cast<uint16_t&>(data1))
<< " now dropped";
break;
case STS_DMXNSCNONE:
Expand Down
2 changes: 1 addition & 1 deletion common/utils/DmxBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void DmxBuffer::SetChannel(unsigned int channel, uint8_t data) {
}

if (channel > m_length) {
OLA_WARN << "attempting to set channel " << channel << " when length is "
OLA_WARN << "Attempting to set channel " << channel << " when length is "
<< m_length;
return;
}
Expand Down
8 changes: 5 additions & 3 deletions common/utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ string IntToString(unsigned int i) {
}

string IntToHexString(unsigned int i, unsigned int width) {
_ToHex<unsigned int> v = _ToHex<unsigned int>(i,
static_cast<int>(width),
true);
ostringstream str;
// In C++, you only get the 0x on non-zero values, so we have to explicitly
// add it for all values
str << "0x" << std::setw(width) << std::hex << std::setfill('0') << i;
str << v;
return str.str();
}

Expand Down Expand Up @@ -397,6 +398,7 @@ void CapitalizeLabel(string *s) {
for (string::iterator iter = s->begin(); iter != s->end(); ++iter) {
switch (*iter) {
case '-':
// fall through, also convert to space
case '_':
*iter = ' ';
case ' ':
Expand Down
42 changes: 42 additions & 0 deletions common/utils/StringUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ using ola::StripPrefix;
using ola::StripSuffix;
using ola::ToLower;
using ola::ToUpper;
using ola::ToHex;
using std::ostringstream;
using std::string;
using std::vector;

Expand Down Expand Up @@ -313,6 +315,7 @@ void StringUtilsTest::testIntToString() {
* test the IntToHexString function.
*/
void StringUtilsTest::testIntToHexString() {
// Using the old IntToHexString
OLA_ASSERT_EQ(string("0x00"), IntToHexString((uint8_t)0));

OLA_ASSERT_EQ(string("0x01"), IntToHexString((uint8_t)1));
Expand All @@ -323,6 +326,45 @@ void StringUtilsTest::testIntToHexString() {

unsigned int i = 0x42;
OLA_ASSERT_EQ(string("0x00000042"), IntToHexString(i));

// Using the inline string concatenation
ostringstream str;
str << ToHex((uint8_t)0);
OLA_ASSERT_EQ(string("0x00"), str.str());
str.str("");

str << ToHex((uint8_t)1);
OLA_ASSERT_EQ(string("0x01"), str.str());
str.str("");

str << ToHex((uint8_t)0x42);
OLA_ASSERT_EQ(string("0x42"), str.str());
str.str("");

str << ToHex((uint16_t)0x0001);
OLA_ASSERT_EQ(string("0x0001"), str.str());
str.str("");

str << ToHex((uint16_t)0xABCD);
OLA_ASSERT_EQ(string("0xabcd"), str.str());
str.str("");

str << ToHex((uint32_t)0xDEADBEEF);
OLA_ASSERT_EQ(string("0xdeadbeef"), str.str());
str.str("");

str << ToHex(i);
OLA_ASSERT_EQ(string("0x00000042"), str.str());
str.str("");

// Without prefix
str << ToHex((uint8_t)0x42, false);
OLA_ASSERT_EQ(string("42"), str.str());
str.str("");

str << ToHex((uint16_t)0xABCD, false);
OLA_ASSERT_EQ(string("abcd"), str.str());
str.str("");
}


Expand Down
1 change: 1 addition & 0 deletions examples/ola-dmxconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ void changepalette(int p) {
switch (p) {
default:
palette_number = 0;
// fall through, use 0 as default palette
case 0:
init_pair(CHANNEL, COLOR_BLACK, COLOR_CYAN);
init_pair(ZERO, COLOR_BLACK, COLOR_WHITE);
Expand Down
1 change: 1 addition & 0 deletions examples/ola-dmxmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ void DmxMonitor::ChangePalette(int p) {
switch (p) {
default:
m_palette_number = 0;
// fall through, use 0 as default palette
case 0:
init_pair(CHANNEL, COLOR_BLACK, COLOR_CYAN);
init_pair(ZERO, COLOR_BLACK, COLOR_WHITE);
Expand Down
5 changes: 3 additions & 2 deletions examples/ola-rdm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <ola/Logging.h>
#include <ola/OlaCallbackClient.h>
#include <ola/OlaClientWrapper.h>
#include <ola/StringUtils.h>
#include <ola/base/Init.h>
#include <ola/base/SysExits.h>
#include <ola/file/Util.h>
Expand Down Expand Up @@ -308,8 +309,8 @@ void RDMController::HandleResponse(
cout << "Request NACKed: " <<
ola::rdm::NackReasonToString(response_status.NackReason()) << endl;
} else {
cout << "Unknown RDM response type " << std::hex <<
static_cast<int>(response_status.response_type) << endl;
cout << "Unknown RDM response type "
<< ola::ToHex(response_status.response_type) << endl;
}
PrintRemainingMessages(response_status.message_count);
m_ola_client.GetSelectServer()->Terminate();
Expand Down
80 changes: 74 additions & 6 deletions include/ola/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
#define INCLUDE_OLA_STRINGUTILS_H_

#include <stdint.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -108,44 +111,109 @@ std::string IntToString(int i);
*/
std::string IntToString(unsigned int i);

/**
* Internal type used by ToHex
*/
template<typename T>
struct _ToHex {
public:
_ToHex(T v, int width, bool prefix)
: width(width),
value(v),
prefix(prefix) {
}

int width; // setw takes an int
T value;
bool prefix;
};

inline uint32_t _HexCast(uint8_t v) { return v; }
inline uint16_t _HexCast(uint16_t v) { return v; }
inline uint32_t _HexCast(uint32_t v) { return v; }

/**
* Convert a value to a hex string.
*
* Automatic constructor for _ToHex that deals with widths
* @tparam T the type of value to convert
* @param v the value to convert
* @param prefix show the 0x prefix
* @return A _ToHex struct representing the value, output it to an ostream to
* use it.
* @note We only currently support unsigned ints due to a lack of requirement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow this comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I've played around and have a way to make it work for signed values, see http://pastebin.com/j4zz7P6i

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get "./include/ola/StringUtils.h:135: multiple definition of `ola::_HexCast(unsigned int)" and all the other types with that code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think this may be where we have a difference of opinion and the easiest thing may be to continue not supporting it as we did with IntToHexString:
equality assertion failed

  • Expected: -0x42
  • Actual : 0xffffffbe

* for anything else
*/
template<typename T>
_ToHex<T> ToHex(T v, bool prefix = true) {
return _ToHex<T>(v,
(std::numeric_limits<T>::digits / HEX_BIT_WIDTH),
prefix);
}

/**
* Output the _ToHex type to an ostream
*/
template <typename T>
std::ostream& operator<<(std::ostream &out, const _ToHex<T> &i) {
// In C++, you only get the 0x on non-zero values, so we have to explicitly
// add it for all values if we want it
if (i.prefix) {
out << "0x";
}
return out << std::setw(i.width) << std::hex << std::setfill('0')
<< _HexCast(i.value) << std::dec;
}

/**
* Convert an unsigned int to a hex string.
* @param i the unsigned int to convert
* @param width the width to zero pad to
* @return The hex string representation of the unsigned int
* @note We don't currently support signed ints due to a lack of requirement
* for it and issues with negative handling and hex in C++
* @deprecated ola::ToHex() instead, unless you really want a string rather
* than use in an ostream (30 Dec 2014)
*/
std::string IntToHexString(unsigned int i, unsigned int width);

/**
* Convert a uint8_t to a hex string.
* @param i the number to convert
* @return The string representation of the number
* @deprecated ola::ToHex() instead, unless you really want a string rather
* than use in an ostream (30 Dec 2014)
*/
inline std::string IntToHexString(uint8_t i) {
return IntToHexString(i, (std::numeric_limits<uint8_t>::digits /
HEX_BIT_WIDTH));
std::ostringstream str;
str << ToHex(i);
return str.str();
}

/**
* Convert a uint16_t to a hex string.
* @param i the number to convert
* @return The string representation of the number
* @deprecated ola::ToHex() instead, unless you really want a string rather
* than use in an ostream (30 Dec 2014)
*/
inline std::string IntToHexString(uint16_t i) {
return IntToHexString(i, (std::numeric_limits<uint16_t>::digits /
HEX_BIT_WIDTH));
std::ostringstream str;
str << ToHex(i);
return str.str();
}

/**
* Convert a uint32_t to a hex string.
* @param i the number to convert
* @return The string representation of the number
* @deprecated ola::ToHex() instead, unless you really want a string rather
* than use in an ostream (30 Dec 2014)
*/
inline std::string IntToHexString(uint32_t i) {
return IntToHexString(i, (std::numeric_limits<uint32_t>::digits /
HEX_BIT_WIDTH));
std::ostringstream str;
str << ToHex(i);
return str.str();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions plugins/pathport/PathportPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class PathportOutputPort: public BasicOutputPort {
return m_helper.Description(GetUniverse());
}
bool WriteDMX(const DmxBuffer &buffer, uint8_t priority);
bool PreSetUniverse(Universe *old_universe, Universe *new_universe) {
bool PreSetUniverse(OLA_UNUSED Universe *old_universe,
Universe *new_universe) {
return m_helper.PreSetUniverse(new_universe);
(void) old_universe;
}

private:
Expand Down
4 changes: 2 additions & 2 deletions plugins/usbdmx/AsyncPluginImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ bool AsyncPluginImpl::USBDeviceAdded(libusb_device *usb_device) {
libusb_get_device_descriptor(usb_device, &descriptor);

OLA_DEBUG << "USB device added, checking for widget support, vendor "
<< IntToHexString(descriptor.idVendor) << ", product "
<< IntToHexString(descriptor.idProduct);
<< ToHex(descriptor.idVendor) << ", product "
<< ToHex(descriptor.idProduct);

WidgetFactories::iterator iter = m_widget_factories.begin();
for (; iter != m_widget_factories.end(); ++iter) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/usbdmx/ScanlimeFadecandy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ bool InitializeWidget(LibUsbAdaptor *adaptor,
unsigned int entry = (channel * 257) + value;
unsigned int packet_entry = entry % 31;
OLA_DEBUG << "Working on channel " << channel << " value " << value
<< " (" << IntToHexString(value) << ") with entry " << entry
<< " (" << ToHex(value) << ") with entry " << entry
<< ", packet entry " << packet_entry << " with val "
<< IntToHexString(lut[channel][value]);
<< ToHex(lut[channel][value]);
ola::utils::SplitUInt16(lut[channel][value],
&packet.data[packet_entry + 1],
&packet.data[packet_entry]);
Expand Down
4 changes: 2 additions & 2 deletions plugins/usbdmx/SyncPluginImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ bool SyncPluginImpl::CheckDevice(libusb_device *usb_device) {
libusb_get_device_descriptor(usb_device, &device_descriptor);

OLA_DEBUG << "USB device found, checking for widget support, vendor "
<< IntToHexString(device_descriptor.idVendor) << ", product "
<< IntToHexString(device_descriptor.idProduct);
<< ToHex(device_descriptor.idVendor) << ", product "
<< ToHex(device_descriptor.idProduct);

pair<uint8_t, uint8_t> bus_dev_id(libusb_get_bus_number(usb_device),
libusb_get_device_address(usb_device));
Expand Down
2 changes: 1 addition & 1 deletion plugins/usbpro/ArduinoRGBDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ArduinoRGBOutputPort::ArduinoRGBOutputPort(ArduinoRGBDevice *parent,
m_bucket(initial_count, rate, rate, *wake_time),
m_wake_time(wake_time) {
std::ostringstream str;
str << "Serial #: " << ola::IntToHexString(serial);
str << "Serial #: " << ola::ToHex(serial);
m_description = str.str();
}
} // namespace usbpro
Expand Down
Loading