Skip to content

Commit d7da33e

Browse files
committed
Merge branch 'develop'
2 parents 8f6035e + d8d7ade commit d7da33e

20 files changed

+630
-40
lines changed

modules/base/catch_throw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void CatchType()
5252
}
5353

5454
bool CatchThrow(const std::function<void()> &func,
55-
bool print_backtrace, bool abort_process)
55+
bool print_backtrace, bool abort_process) noexcept
5656
{
5757
try {
5858
if (func)
@@ -93,7 +93,7 @@ bool CatchThrow(const std::function<void()> &func,
9393
return true;
9494
}
9595

96-
bool CatchThrowQuietly(const std::function<void()> &func)
96+
bool CatchThrowQuietly(const std::function<void()> &func) noexcept
9797
{
9898
try {
9999
if (func)

modules/base/catch_throw.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace tbox {
3535
*/
3636
bool CatchThrow(const std::function<void()> &func,
3737
bool print_backtrace = false,
38-
bool abort_process = false);
38+
bool abort_process = false) noexcept;
3939

4040
/// 同 CatchThrow,但不打印日志、堆栈,也不退出进程
4141
/**
@@ -44,7 +44,7 @@ bool CatchThrow(const std::function<void()> &func,
4444
* \return false 运行过程中没有出现异常
4545
* \return true 运行过程中捕获到了异常
4646
*/
47-
bool CatchThrowQuietly(const std::function<void()> &func);
47+
bool CatchThrowQuietly(const std::function<void()> &func) noexcept;
4848

4949
}
5050

modules/http/server/request_parser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <tbox/base/defines.h>
2323
#include <tbox/base/log.h>
2424
#include <tbox/util/string.h>
25+
#include <tbox/util/string_to.h>
2526

2627
namespace tbox {
2728
namespace http {
@@ -139,8 +140,13 @@ size_t RequestParser::parse(const void *data_ptr, size_t data_size)
139140
auto head_value = util::string::Strip(str.substr(head_value_start_pos, head_value_end_pos - head_value_start_pos));
140141
sp_request_->headers[head_key] = head_value;
141142

142-
if (head_key == "Content-Length")
143-
content_length_ = std::stoi(head_value);
143+
if (head_key == "Content-Length") {
144+
if (!util::StringTo(head_value, content_length_)) {
145+
LogNotice("Content-Length should be number");
146+
state_ = State::kFail;
147+
return pos;
148+
}
149+
}
144150

145151
} else {
146152
sp_request_->headers[head_key] = "";

modules/main/log.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <tbox/terminal/helper.h>
2727
#include <tbox/util/fs.h>
2828
#include <tbox/util/json.h>
29+
#include <tbox/util/string_to.h>
2930

3031
namespace tbox {
3132
namespace main {
@@ -388,7 +389,7 @@ void Log::installShellForSink(log::Sink &sink, terminal::NodeToken parent_node,
388389
do {
389390
auto &module_id = args[1];
390391
int level = 0;
391-
if (CatchThrowQuietly([&] { level = std::stoi(args[2]); })) {
392+
if (!util::StringTo(args[2], level)) {
392393
oss << "level must be number\r\n";
393394
break;
394395
}

modules/network/sockaddr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <arpa/inet.h>
2323

2424
#include <tbox/base/log.h>
25+
#include <tbox/util/string_to.h>
26+
2527
#include <sstream>
2628

2729
#include "sockaddr.h"
@@ -99,10 +101,10 @@ SockAddr SockAddr::FromString(const string &addr_str)
99101
auto ipv4_str = addr_str.substr(0, colon_pos) ;
100102
auto port_str = addr_str.substr(colon_pos + 1) ;
101103

102-
try {
103-
uint16_t port = stoi(port_str);
104+
uint16_t port = 0;
105+
if (util::StringTo(port_str, port)) {
104106
return SockAddr(IPAddress::FromString(ipv4_str), port);
105-
} catch (const std::exception &e) {
107+
} else {
106108
return SockAddr();
107109
}
108110
} else {

modules/network/uart.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <fcntl.h>
2424

2525
#include <tbox/base/log.h>
26+
#include <tbox/util/string_to.h>
2627

2728
namespace tbox {
2829
namespace network {
@@ -233,7 +234,10 @@ bool Uart::setMode(const std::string &mode_str)
233234
Mode mode;
234235

235236
//! 波特率
236-
mode.baudrate = stoi(baudrate_str);
237+
if (!util::StringTo(baudrate_str, mode.baudrate)) {
238+
LogErr("baudrate must be number");
239+
return false;
240+
}
237241

238242
//! 数据位
239243
if (data_bits_ch == '8')

modules/terminal/helper.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "helper.h"
2222

2323
#include <sstream>
24-
#include <tbox/base/catch_throw.h>
24+
#include <tbox/util/string_to.h>
2525

2626
namespace tbox {
2727
namespace terminal {
@@ -64,16 +64,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node,
6464

6565
} else if (a.size() == 2u) {
6666
auto &str = a[1];
67-
if (str == "true" || str == "True" || str == "TRUE" ||
68-
str == "on" || str == "On" || str == "ON") {
69-
value = true;
70-
is_ok = true;
71-
72-
} else if (str == "false" || str == "False" || str == "FALSE" ||
73-
str == "off" || str == "Off" || str == "OFF") {
74-
value = false;
75-
is_ok = true;
76-
}
67+
is_ok = util::StringTo(str, value);
7768
}
7869

7970
if (is_ok) {
@@ -136,7 +127,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node,
136127
} else if (a.size() == 2u) {
137128
auto &str = a[1];
138129
int new_value = 0;
139-
if (!CatchThrowQuietly([&] { new_value = std::stoi(str); })) {
130+
if (util::StringTo(str, new_value)) {
140131
if (new_value >= min_value && new_value <= max_value) {
141132
value = new_value;
142133
is_ok = true;
@@ -177,7 +168,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node,
177168
} else if (a.size() == 2u) {
178169
auto &str = a[1];
179170
double new_value = 0;
180-
if (!CatchThrowQuietly([&] { new_value = std::stod(str); })) {
171+
if (util::StringTo(str, new_value)) {
181172
if (new_value >= min_value && new_value <= max_value) {
182173
value = new_value;
183174
is_ok = true;
@@ -264,16 +255,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node, const std:
264255
} else if (a.size() == 2u && profile.set_func) {
265256
auto &str = a[1];
266257
bool new_value = false;
267-
if (str == "true" || str == "True" || str == "TRUE" ||
268-
str == "on" || str == "On" || str == "ON") {
269-
new_value = true;
270-
is_ok = true;
271-
272-
} else if (str == "false" || str == "False" || str == "FALSE" ||
273-
str == "off" || str == "Off" || str == "OFF") {
274-
new_value = false;
275-
is_ok = true;
276-
}
258+
is_ok = util::StringTo(str, new_value);
277259

278260
if (is_ok) {
279261
if (profile.set_func(new_value)) {
@@ -322,7 +304,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node, const std:
322304
} else if (a.size() == 2u && profile.set_func) {
323305
auto &str = a[1];
324306
int new_value = 0;
325-
CatchThrowQuietly([&] { new_value = std::stoi(str); is_ok = true; });
307+
is_ok = util::StringTo(str, new_value);
326308
if (is_ok) {
327309
if (new_value < profile.min_value || new_value > profile.max_value) {
328310
oss << "fail, out of range.\r\n";
@@ -387,7 +369,7 @@ NodeToken AddFuncNode(TerminalNodes &terminal, NodeToken parent_node, const std:
387369
} else if (a.size() == 2u && profile.set_func) {
388370
auto &str = a[1];
389371
double new_value = 0;
390-
CatchThrowQuietly([&] { new_value = std::stod(str); is_ok = true; });
372+
is_ok = util::StringTo(str, new_value);
391373
if (is_ok) {
392374
if (new_value < profile.min_value || new_value > profile.max_value) {
393375
oss << "fail, out of range.\r\n";

modules/terminal/impl/terminal_commands.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <tbox/base/log.h>
2626
#include <tbox/util/split_cmdline.h>
2727
#include <tbox/util/string.h>
28+
#include <tbox/util/string_to.h>
2829
#include <tbox/event/loop.h>
2930

3031
#include "session_context.h"
@@ -336,8 +337,9 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
336337
return execute(s);
337338
}
338339

339-
try {
340-
auto index = std::stoi(sub_cmd);
340+
size_t index = 0;
341+
342+
if (util::StringTo(sub_cmd, index)) {
341343
bool is_index_valid = false;
342344
if (index >= 0) {
343345
if (static_cast<size_t>(index) < s->history.size()) {
@@ -356,7 +358,8 @@ bool Terminal::Impl::executeRunHistoryCmd(SessionContext *s, const Args &args)
356358
return execute(s);
357359
} else
358360
s->wp_conn->send(s->token, "Error: index out of range.\r\n");
359-
} catch (const invalid_argument &e) {
361+
362+
} else {
360363
s->wp_conn->send(s->token, "Error: parse index fail.\r\n");
361364
}
362365

modules/util/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ set(TBOX_UTIL_HEADERS
4848
fd.h
4949
scalable_integer.h
5050
variables.h
51+
string_to.h
5152
)
5253

5354
set(TBOX_UTIL_SOURCES
@@ -69,6 +70,7 @@ set(TBOX_UTIL_SOURCES
6970
fd.cpp
7071
scalable_integer.cpp
7172
variables.cpp
73+
string_to.cpp
7274
)
7375

7476
set(TBOX_UTIL_TEST_SOURCES
@@ -88,6 +90,7 @@ set(TBOX_UTIL_TEST_SOURCES
8890
fd_test.cpp
8991
scalable_integer_test.cpp
9092
variables_test.cpp
93+
string_to_test.cpp
9194
)
9295

9396
add_library(${TBOX_LIBRARY_NAME} ${TBOX_BUILD_LIB_TYPE} ${TBOX_UTIL_SOURCES})

modules/util/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ HEAD_FILES = \
4343
fd.h \
4444
scalable_integer.h \
4545
variables.h \
46+
string_to.h \
4647

4748
CPP_SRC_FILES = \
4849
pid_file.cpp \
@@ -63,6 +64,7 @@ CPP_SRC_FILES = \
6364
fd.cpp \
6465
scalable_integer.cpp \
6566
variables.cpp \
67+
string_to.cpp \
6668

6769
CXXFLAGS := -DMODULE_ID='"tbox.util"' $(CXXFLAGS)
6870

@@ -79,12 +81,14 @@ TEST_CPP_SRC_FILES = \
7981
base64_test.cpp \
8082
checksum_test.cpp \
8183
crc_test.cpp \
84+
json_deep_loader_test.cpp \
8285
execute_cmd_test.cpp \
8386
timestamp_test.cpp \
8487
buffer_test.cpp \
8588
fd_test.cpp \
8689
scalable_integer_test.cpp \
8790
variables_test.cpp \
91+
string_to_test.cpp \
8892

8993
TEST_LDFLAGS := $(LDFLAGS) -ltbox_base -ldl
9094

0 commit comments

Comments
 (0)