mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 04:57:45 +00:00
AK: Make string-to-number conversion helpers return Optional
Get rid of the weird old signature: - int StringType::to_int(bool& ok) const And replace it with sensible new signature: - Optional<int> StringType::to_int() const
This commit is contained in:
parent
15f4043a7a
commit
fdfda6dec2
55 changed files with 354 additions and 455 deletions
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/ElapsedTimer.h>
|
||||
#include <stdio.h>
|
||||
|
@ -36,7 +37,9 @@ void usage(void)
|
|||
exit(1);
|
||||
}
|
||||
|
||||
enum Unit { Bytes, KiloBytes, MegaBytes };
|
||||
enum Unit { Bytes,
|
||||
KiloBytes,
|
||||
MegaBytes };
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
|
@ -44,11 +47,11 @@ int main(int argc, char** argv)
|
|||
Unit unit = MegaBytes;
|
||||
|
||||
if (argc >= 2) {
|
||||
bool ok;
|
||||
count = String(argv[1]).to_uint(ok);
|
||||
if (!ok) {
|
||||
auto number = String(argv[1]).to_uint();
|
||||
if (!number.has_value()) {
|
||||
usage();
|
||||
}
|
||||
count = number.value();
|
||||
}
|
||||
|
||||
if (argc >= 3) {
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
|
@ -52,10 +53,11 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
new_gid = gid_arg.to_uint(ok);
|
||||
auto number = gid_arg.to_uint();
|
||||
|
||||
if (!ok) {
|
||||
if (number.has_value()) {
|
||||
new_gid = number.value();
|
||||
} else {
|
||||
auto* group = getgrnam(gid_arg.characters());
|
||||
if (!group) {
|
||||
fprintf(stderr, "Unknown group '%s'\n", gid_arg.characters());
|
||||
|
|
|
@ -54,9 +54,10 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
new_uid = parts[0].to_uint(ok);
|
||||
if (!ok) {
|
||||
auto number = parts[0].to_uint();
|
||||
if (number.has_value()) {
|
||||
new_uid = number.value();
|
||||
} else {
|
||||
auto* passwd = getpwnam(parts[0].characters());
|
||||
if (!passwd) {
|
||||
fprintf(stderr, "Unknown user '%s'\n", parts[0].characters());
|
||||
|
@ -66,8 +67,10 @@ int main(int argc, char** argv)
|
|||
}
|
||||
|
||||
if (parts.size() == 2) {
|
||||
new_gid = parts[1].to_uint(ok);
|
||||
if (!ok) {
|
||||
auto number = parts[1].to_uint();
|
||||
if (number.has_value()) {
|
||||
new_gid = number.value();
|
||||
} else {
|
||||
auto* group = getgrnam(parts[1].characters());
|
||||
if (!new_gid) {
|
||||
fprintf(stderr, "Unknown group '%s'\n", parts[1].characters());
|
||||
|
|
|
@ -89,74 +89,70 @@ static void expand_list(Vector<String>& tokens, Vector<Index>& indexes)
|
|||
}
|
||||
|
||||
if (token[0] == '-') {
|
||||
bool ok = true;
|
||||
ssize_t index = token.substring(1, token.length() - 1).to_int(ok);
|
||||
if (!ok) {
|
||||
auto index = token.substring(1, token.length() - 1).to_int();
|
||||
if (!index.has_value()) {
|
||||
fprintf(stderr, "cut: invalid byte/character position '%s'\n", token.characters());
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
if (index == 0) {
|
||||
if (index.value() == 0) {
|
||||
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
Index tmp = { 1, index, Index::Type::RangedIndex };
|
||||
Index tmp = { 1, index.value(), Index::Type::RangedIndex };
|
||||
add_if_not_exists(indexes, tmp);
|
||||
} else if (token[token.length() - 1] == '-') {
|
||||
bool ok = true;
|
||||
ssize_t index = token.substring(0, token.length() - 1).to_int(ok);
|
||||
if (!ok) {
|
||||
auto index = token.substring(0, token.length() - 1).to_int();
|
||||
if (!index.has_value()) {
|
||||
fprintf(stderr, "cut: invalid byte/character position '%s'\n", token.characters());
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
if (index == 0) {
|
||||
if (index.value() == 0) {
|
||||
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
Index tmp = { index, -1, Index::Type::SliceIndex };
|
||||
Index tmp = { index.value(), -1, Index::Type::SliceIndex };
|
||||
add_if_not_exists(indexes, tmp);
|
||||
} else {
|
||||
auto range = token.split('-');
|
||||
if (range.size() == 2) {
|
||||
bool ok = true;
|
||||
ssize_t index1 = range[0].to_int(ok);
|
||||
if (!ok) {
|
||||
auto index1 = range[0].to_int();
|
||||
if (!index1.has_value()) {
|
||||
fprintf(stderr, "cut: invalid byte/character position '%s'\n", range[0].characters());
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
ssize_t index2 = range[1].to_int(ok);
|
||||
if (!ok) {
|
||||
auto index2 = range[1].to_int();
|
||||
if (!index2.has_value()) {
|
||||
fprintf(stderr, "cut: invalid byte/character position '%s'\n", range[1].characters());
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
if (index1 > index2) {
|
||||
if (index1.value() > index2.value()) {
|
||||
fprintf(stderr, "cut: invalid decreasing range\n");
|
||||
print_usage_and_exit(1);
|
||||
} else if (index1 == 0 || index2 == 0) {
|
||||
} else if (index1.value() == 0 || index2.value() == 0) {
|
||||
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
Index tmp = { index1, index2, Index::Type::RangedIndex };
|
||||
Index tmp = { index1.value(), index2.value(), Index::Type::RangedIndex };
|
||||
add_if_not_exists(indexes, tmp);
|
||||
} else if (range.size() == 1) {
|
||||
bool ok = true;
|
||||
ssize_t index = range[0].to_int(ok);
|
||||
if (!ok) {
|
||||
auto index = range[0].to_int();
|
||||
if (!index.has_value()) {
|
||||
fprintf(stderr, "cut: invalid byte/character position '%s'\n", range[0].characters());
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
if (index == 0) {
|
||||
if (index.value() == 0) {
|
||||
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
|
||||
print_usage_and_exit(1);
|
||||
}
|
||||
|
||||
Index tmp = { index, index, Index::Type::SingleIndex };
|
||||
Index tmp = { index.value(), index.value(), Index::Type::SingleIndex };
|
||||
add_if_not_exists(indexes, tmp);
|
||||
} else {
|
||||
fprintf(stderr, "cut: invalid byte or character range\n");
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <stdio.h>
|
||||
|
@ -44,12 +45,12 @@ int main(int argc, char** argv)
|
|||
return 0;
|
||||
}
|
||||
if (argc == 3 && !strcmp(argv[1], "-s")) {
|
||||
bool ok;
|
||||
timespec ts = { String(argv[2]).to_uint(ok), 0 };
|
||||
if (!ok) {
|
||||
auto number = StringView(argv[2]).to_uint();
|
||||
if (!number.has_value()) {
|
||||
fprintf(stderr, "date: Invalid timestamp value");
|
||||
return 1;
|
||||
}
|
||||
timespec ts = { number.value(), 0 };
|
||||
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
|
||||
perror("clock_settime");
|
||||
return 1;
|
||||
|
|
|
@ -120,10 +120,10 @@ class LinksCommand final : public StatCommand {
|
|||
public:
|
||||
LinksCommand(const char* arg)
|
||||
{
|
||||
bool ok;
|
||||
m_links = StringView(arg).to_uint(ok);
|
||||
if (!ok)
|
||||
auto number = StringView(arg).to_uint();
|
||||
if (!number.has_value())
|
||||
fatal_error("Invalid number: \033[1m%s", arg);
|
||||
m_links = number.value();
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -143,10 +143,10 @@ public:
|
|||
m_uid = passwd->pw_uid;
|
||||
} else {
|
||||
// Attempt to parse it as decimal UID.
|
||||
bool ok;
|
||||
m_uid = StringView(arg).to_uint(ok);
|
||||
if (!ok)
|
||||
auto number = StringView(arg).to_uint();
|
||||
if (!number.has_value())
|
||||
fatal_error("Invalid user: \033[1m%s", arg);
|
||||
m_uid = number.value();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -167,10 +167,10 @@ public:
|
|||
m_gid = gr->gr_gid;
|
||||
} else {
|
||||
// Attempt to parse it as decimal GID.
|
||||
bool ok;
|
||||
m_gid = StringView(arg).to_int(ok);
|
||||
if (!ok)
|
||||
auto number = StringView(arg).to_int();
|
||||
if (!number.has_value())
|
||||
fatal_error("Invalid group: \033[1m%s", arg);
|
||||
m_gid = number.value();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -192,10 +192,10 @@ public:
|
|||
m_is_bytes = true;
|
||||
view = view.substring_view(0, view.length() - 1);
|
||||
}
|
||||
bool ok;
|
||||
m_size = view.to_uint(ok);
|
||||
if (!ok)
|
||||
auto number = view.to_uint();
|
||||
if (!number.has_value())
|
||||
fatal_error("Invalid size: \033[1m%s", arg);
|
||||
m_size = number.value();
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
|
@ -45,24 +46,25 @@ int main(int argc, char** argv)
|
|||
|
||||
if (argc != 2 && argc != 3)
|
||||
print_usage_and_exit();
|
||||
bool ok;
|
||||
unsigned signum = SIGTERM;
|
||||
int pid_argi = 1;
|
||||
if (argc == 3) {
|
||||
pid_argi = 2;
|
||||
if (argv[1][0] != '-')
|
||||
print_usage_and_exit();
|
||||
signum = String(&argv[1][1]).to_uint(ok);
|
||||
if (!ok) {
|
||||
auto number = StringView(&argv[1][1]).to_uint();
|
||||
if (!number.has_value()) {
|
||||
printf("'%s' is not a valid signal number\n", &argv[1][1]);
|
||||
return 2;
|
||||
}
|
||||
signum = number.value();
|
||||
}
|
||||
pid_t pid = String(argv[pid_argi]).to_int(ok);
|
||||
if (!ok) {
|
||||
auto pid_opt = String(argv[pid_argi]).to_int();
|
||||
if (!pid_opt.has_value()) {
|
||||
printf("'%s' is not a valid PID\n", argv[pid_argi]);
|
||||
return 3;
|
||||
}
|
||||
pid_t pid = pid_opt.value();
|
||||
|
||||
int rc = kill(pid, signum);
|
||||
if (rc < 0)
|
||||
|
|
|
@ -54,7 +54,6 @@ static int kill_all(const String& process_name, const unsigned signum)
|
|||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
bool ok;
|
||||
unsigned signum = SIGTERM;
|
||||
int name_argi = 1;
|
||||
|
||||
|
@ -67,11 +66,12 @@ int main(int argc, char** argv)
|
|||
if (argv[1][0] != '-')
|
||||
print_usage_and_exit();
|
||||
|
||||
signum = String(&argv[1][1]).to_uint(ok);
|
||||
if (!ok) {
|
||||
auto number = String(&argv[1][1]).to_uint();
|
||||
if (!number.has_value()) {
|
||||
printf("'%s' is not a valid signal number\n", &argv[1][1]);
|
||||
return 2;
|
||||
}
|
||||
signum = number.value();
|
||||
}
|
||||
|
||||
return kill_all(argv[name_argi], signum);
|
||||
|
|
|
@ -74,15 +74,16 @@ int main(int argc, char** argv)
|
|||
|
||||
pid_t pid_to_omit = 0;
|
||||
if (omit_pid_value) {
|
||||
bool ok = true;
|
||||
if (!strcmp(omit_pid_value, "%PPID"))
|
||||
if (!strcmp(omit_pid_value, "%PPID")) {
|
||||
pid_to_omit = getppid();
|
||||
else
|
||||
pid_to_omit = StringView(omit_pid_value).to_uint(ok);
|
||||
if (!ok) {
|
||||
fprintf(stderr, "Invalid value for -o\n");
|
||||
args_parser.print_usage(stderr, argv[0]);
|
||||
return 1;
|
||||
} else {
|
||||
auto number = StringView(omit_pid_value).to_uint();
|
||||
if (!number.has_value()) {
|
||||
fprintf(stderr, "Invalid value for -o\n");
|
||||
args_parser.print_usage(stderr, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
pid_to_omit = number.value();
|
||||
}
|
||||
}
|
||||
return pid_of(process_name, single_shot, omit_pid_value != nullptr, pid_to_omit);
|
||||
|
|
|
@ -75,12 +75,12 @@ int main(int argc, char** argv)
|
|||
break;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
size = str.to_int(ok);
|
||||
if (!ok) {
|
||||
auto size_opt = str.to_int();
|
||||
if (!size_opt.has_value()) {
|
||||
args_parser.print_usage(stderr, argv[0]);
|
||||
return 1;
|
||||
}
|
||||
size = size_opt.value();
|
||||
}
|
||||
|
||||
if (reference) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue