1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Fixing bad C++ constructors.

I am not sure if this is related to the random crashes on Linux, but the wrong C++ std::string constructors were being called because of bad use of c-style array dereferencing. The correct C++ std::string constructors are being called now.
This commit is contained in:
John Harrison 2012-02-06 18:22:14 -06:00
parent 61c193f5d8
commit 9734f943cb
3 changed files with 25 additions and 18 deletions

View File

@ -78,7 +78,7 @@ typedef enum {
*/ */
typedef enum { typedef enum {
flowcontrol_none = 0, flowcontrol_none = 0,
flowcontrol_software, flowcontrol_software
} flowcontrol_t; } flowcontrol_t;
/*! /*!

View File

@ -420,7 +420,7 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size)
// Calculate the time select took // Calculate the time select took
struct timeval diff; struct timeval diff;
diff.tv_sec = end.tv_sec-start.tv_sec; diff.tv_sec = end.tv_sec-start.tv_sec;
diff.tv_usec = (end.tv_nsec-start.tv_nsec)/1000; diff.tv_usec = static_cast<int> ((end.tv_nsec-start.tv_nsec)/1000);
// Update the timeout // Update the timeout
if (timeout.tv_sec <= diff.tv_sec) { if (timeout.tv_sec <= diff.tv_sec) {
timeout.tv_sec = 0; timeout.tv_sec = 0;

View File

@ -133,7 +133,8 @@ Serial::readline (string &buffer, size_t size, string eol)
{ {
ScopedReadLock (this->pimpl_); ScopedReadLock (this->pimpl_);
size_t eol_len = eol.length (); size_t eol_len = eol.length ();
unsigned char buffer_[size]; unsigned char *buffer_ = static_cast<unsigned char*>
(alloca (size * sizeof (unsigned char)));
size_t read_so_far = 0; size_t read_so_far = 0;
while (true) while (true)
{ {
@ -142,7 +143,8 @@ Serial::readline (string &buffer, size_t size, string eol)
if (bytes_read == 0) { if (bytes_read == 0) {
break; // Timeout occured on reading 1 byte break; // Timeout occured on reading 1 byte
} }
if (string(buffer_[read_so_far-eol_len], eol_len) == eol) { if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
break; // EOL found break; // EOL found
} }
if (read_so_far == size) { if (read_so_far == size) {
@ -166,7 +168,8 @@ Serial::readlines (size_t size, string eol)
ScopedReadLock (this->pimpl_); ScopedReadLock (this->pimpl_);
std::vector<std::string> lines; std::vector<std::string> lines;
size_t eol_len = eol.length (); size_t eol_len = eol.length ();
unsigned char buffer_[size]; unsigned char *buffer_ = static_cast<unsigned char*>
(alloca (size * sizeof (unsigned char)));
size_t read_so_far = 0; size_t read_so_far = 0;
size_t start_of_line = 0; size_t start_of_line = 0;
while (read_so_far < size) { while (read_so_far < size) {
@ -175,20 +178,24 @@ Serial::readlines (size_t size, string eol)
if (bytes_read == 0) { if (bytes_read == 0) {
if (start_of_line != read_so_far) { if (start_of_line != read_so_far) {
lines.push_back ( lines.push_back (
std::string(buffer_[start_of_line], read_so_far-start_of_line)); string (reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
} }
break; // Timeout occured on reading 1 byte break; // Timeout occured on reading 1 byte
} }
if (string(buffer_[read_so_far-eol_len], eol_len) == eol) { if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
// EOL found // EOL found
lines.push_back( lines.push_back(
std::string(buffer_[start_of_line], read_so_far-start_of_line)); string(reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
start_of_line = read_so_far; start_of_line = read_so_far;
} }
if (read_so_far == size) { if (read_so_far == size) {
if (start_of_line != read_so_far) { if (start_of_line != read_so_far) {
lines.push_back( lines.push_back(
std::string(buffer_[start_of_line], read_so_far-start_of_line)); string(reinterpret_cast<const char*> (buffer_ + start_of_line),
read_so_far - start_of_line));
} }
break; // Reached the maximum read length break; // Reached the maximum read length
} }