Skip to content

Commit

Permalink
Use strtok_s for MSVC in class SVNetwork
Browse files Browse the repository at this point in the history
strtok_s can be used with MSVC as a replacement for strtok_r, so less
special handling is needed in the code and class SVNetwork can be
made smaller by removing member has_content.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Jul 10, 2020
1 parent 636c37f commit 548a832
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
19 changes: 7 additions & 12 deletions src/viewer/svutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#include <unistd.h>
#endif

#if defined(_WIN32) && !defined(__GNUC__)
#define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr)
#endif /* _WIN32 && !__GNUC__ */

#ifndef GRAPHICS_DISABLED

const int kMaxMsgSize = 4096;
Expand Down Expand Up @@ -164,18 +168,15 @@ void SVNetwork::Flush() {
// This will always return one line of char* (denoted by \n).
char* SVNetwork::Receive() {
char* result = nullptr;
#if defined(_WIN32) || defined(__CYGWIN__)
if (has_content) { result = strtok (nullptr, "\n"); }
#else
if (buffer_ptr_ != nullptr) { result = strtok_r(nullptr, "\n", &buffer_ptr_); }
#endif
if (buffer_ptr_ != nullptr) {
result = strtok_r(nullptr, "\n", &buffer_ptr_);
}

// This means there is something left in the buffer and we return it.
if (result != nullptr) { return result;
// Otherwise, we read from the stream_.
} else {
buffer_ptr_ = nullptr;
has_content = false;

// The timeout length is not really important since we are looping anyway
// until a new message is delivered.
Expand All @@ -199,13 +200,8 @@ char* SVNetwork::Receive() {
// Server quit (0) or error (-1).
if (i <= 0) { return nullptr; }
msg_buffer_in_[i] = '\0';
has_content = true;
#ifdef _WIN32
return strtok(msg_buffer_in_, "\n");
#else
// Setup a new string tokenizer.
return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_);
#endif
}
}

Expand Down Expand Up @@ -265,7 +261,6 @@ SVNetwork::SVNetwork(const char* hostname, int port) {
msg_buffer_in_ = new char[kMaxMsgSize + 1];
msg_buffer_in_[0] = '\0';

has_content = false;
buffer_ptr_ = nullptr;

struct addrinfo *addr_info = nullptr;
Expand Down
3 changes: 1 addition & 2 deletions src/viewer/svutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ class SVNetwork {
/// Stores the messages which are supposed to go out.
std::string msg_buffer_out_;

bool has_content; // Win32 (strtok)
/// Where we are at in our msg_buffer_in_
char* buffer_ptr_; // Unix (strtok_r)
char* buffer_ptr_; // strtok_r, strtok_s
};

#endif // TESSERACT_VIEWER_SVUTIL_H_

0 comments on commit 548a832

Please sign in to comment.