Skip to content

Commit 548a832

Browse files
committed
Use strtok_s for MSVC in class SVNetwork
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 <[email protected]>
1 parent 636c37f commit 548a832

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/viewer/svutil.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#include <unistd.h>
5353
#endif
5454

55+
#if defined(_WIN32) && !defined(__GNUC__)
56+
#define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr)
57+
#endif /* _WIN32 && !__GNUC__ */
58+
5559
#ifndef GRAPHICS_DISABLED
5660

5761
const int kMaxMsgSize = 4096;
@@ -164,18 +168,15 @@ void SVNetwork::Flush() {
164168
// This will always return one line of char* (denoted by \n).
165169
char* SVNetwork::Receive() {
166170
char* result = nullptr;
167-
#if defined(_WIN32) || defined(__CYGWIN__)
168-
if (has_content) { result = strtok (nullptr, "\n"); }
169-
#else
170-
if (buffer_ptr_ != nullptr) { result = strtok_r(nullptr, "\n", &buffer_ptr_); }
171-
#endif
171+
if (buffer_ptr_ != nullptr) {
172+
result = strtok_r(nullptr, "\n", &buffer_ptr_);
173+
}
172174

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

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

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

268-
has_content = false;
269264
buffer_ptr_ = nullptr;
270265

271266
struct addrinfo *addr_info = nullptr;

src/viewer/svutil.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ class SVNetwork {
9595
/// Stores the messages which are supposed to go out.
9696
std::string msg_buffer_out_;
9797

98-
bool has_content; // Win32 (strtok)
9998
/// Where we are at in our msg_buffer_in_
100-
char* buffer_ptr_; // Unix (strtok_r)
99+
char* buffer_ptr_; // strtok_r, strtok_s
101100
};
102101

103102
#endif // TESSERACT_VIEWER_SVUTIL_H_

0 commit comments

Comments
 (0)