Skip to content

Commit

Permalink
cli: move str_to_uint to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jun 16, 2024
1 parent 32d16a1 commit a3d0a32
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ list(APPEND TEST_ENV "TEST_RUNTIME_EXE=${TOYWASM_CLI}")
set(cli_sources
"cli/main.c"
"cli/repl.c"
"cli/str_to_uint.c"
)

add_executable(toywasm-cli ${cli_sources})
Expand Down
21 changes: 1 addition & 20 deletions cli/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "nbio.h"
#include "repl.h"
#include "report.h"
#include "str_to_uint.h"
#include "suspend.h"
#include "timeutil.h"
#include "toywasm_version.h"
Expand Down Expand Up @@ -66,26 +67,6 @@
*/
#define EXTERNREF_0 ((uintptr_t)(-1))

int
str_to_uint(const char *s, int base, uintmax_t *resultp)
{
uintmax_t v;
char *ep;
errno = 0;
v = strtoumax(s, &ep, base);
if (s == ep) {
return EINVAL;
}
if (*ep != 0) {
return EINVAL;
}
if (errno != 0) {
return errno;
}
*resultp = v;
return 0;
}

int
str_to_ptr(const char *s, int base, uintmax_t *resultp)
{
Expand Down
24 changes: 24 additions & 0 deletions cli/str_to_uint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <errno.h>
#include <inttypes.h>

#include "str_to_uint.h"

int
str_to_uint(const char *s, int base, uintmax_t *resultp)
{
uintmax_t v;
char *ep;
errno = 0;
v = strtoumax(s, &ep, base);
if (s == ep) {
return EINVAL;
}
if (*ep != 0) {
return EINVAL;
}
if (errno != 0) {
return errno;
}
*resultp = v;
return 0;
}
3 changes: 3 additions & 0 deletions cli/str_to_uint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdint.h>

int str_to_uint(const char *s, int base, uintmax_t *resultp);

0 comments on commit a3d0a32

Please sign in to comment.