diff options
author | Andreas Kling <kling@serenityos.org> | 2020-06-12 21:07:52 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-12 21:28:55 +0200 |
commit | fdfda6dec20101013bb33633e657f06ef2a1ea96 (patch) | |
tree | 2157f8281cd9bc33a6984455c4831c397d2bd30c /Kernel | |
parent | 15f4043a7a80f52c0fa05c4e69771e758464cd20 (diff) | |
download | serenity-fdfda6dec20101013bb33633e657f06ef2a1ea96.zip |
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
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/FileSystem/DevPtsFS.cpp | 7 | ||||
-rw-r--r-- | Kernel/FileSystem/ProcFS.cpp | 40 | ||||
-rw-r--r-- | Kernel/init.cpp | 21 |
3 files changed, 32 insertions, 36 deletions
diff --git a/Kernel/FileSystem/DevPtsFS.cpp b/Kernel/FileSystem/DevPtsFS.cpp index cf0237584e..4b86dc6a1d 100644 --- a/Kernel/FileSystem/DevPtsFS.cpp +++ b/Kernel/FileSystem/DevPtsFS.cpp @@ -177,10 +177,9 @@ RefPtr<Inode> DevPtsFSInode::lookup(StringView name) if (name == "." || name == "..") return fs().get_inode(identifier()); - bool ok; - unsigned pty_index = name.to_uint(ok); - if (ok && ptys->contains(pty_index)) { - return fs().get_inode({ fsid(), pty_index_to_inode_index(pty_index) }); + auto pty_index = name.to_uint(); + if (pty_index.has_value() && ptys->contains(pty_index.value())) { + return fs().get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) }); } return {}; diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 2f154871bd..8c5b3b9a01 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -1356,17 +1356,16 @@ RefPtr<Inode> ProcFSInode::lookup(StringView name) } } } - bool ok; - unsigned name_as_number = name.to_uint(ok); - if (ok) { - bool process_exists = false; - { - InterruptDisabler disabler; - process_exists = Process::from_pid(name_as_number); - } - if (process_exists) - return fs().get_inode(to_identifier(fsid(), PDI_Root, name_as_number, FI_PID)); + auto name_as_number = name.to_uint(); + if (!name_as_number.has_value()) + return {}; + bool process_exists = false; + { + InterruptDisabler disabler; + process_exists = Process::from_pid(name_as_number.value()); } + if (process_exists) + return fs().get_inode(to_identifier(fsid(), PDI_Root, name_as_number.value(), FI_PID)); return {}; } @@ -1413,18 +1412,17 @@ RefPtr<Inode> ProcFSInode::lookup(StringView name) } if (proc_file_type == FI_PID_fd) { - bool ok; - unsigned name_as_number = name.to_uint(ok); - if (ok) { - bool fd_exists = false; - { - InterruptDisabler disabler; - if (auto* process = Process::from_pid(to_pid(identifier()))) - fd_exists = process->file_description(name_as_number); - } - if (fd_exists) - return fs().get_inode(to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number)); + auto name_as_number = name.to_uint(); + if (!name_as_number.has_value()) + return {}; + bool fd_exists = false; + { + InterruptDisabler disabler; + if (auto* process = Process::from_pid(to_pid(identifier()))) + fd_exists = process->file_description(name_as_number.value()); } + if (fd_exists) + return fs().get_inode(to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number.value())); } return {}; } diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 0dacc83a6a..7e9006d8cd 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -251,10 +251,9 @@ void init_stage2() root = root.substring(strlen("/dev/hda"), root.length() - strlen("/dev/hda")); if (root.length()) { - bool ok; - unsigned partition_number = root.to_uint(ok); + auto partition_number = root.to_uint(); - if (!ok) { + if (!partition_number.has_value()) { klog() << "init_stage2: couldn't parse partition number from root kernel parameter"; hang(); } @@ -273,9 +272,9 @@ void init_stage2() klog() << "init_stage2: couldn't read GPT from disk"; hang(); } - auto partition = gpt.partition(partition_number); + auto partition = gpt.partition(partition_number.value()); if (!partition) { - klog() << "init_stage2: couldn't get partition " << partition_number; + klog() << "init_stage2: couldn't get partition " << partition_number.value(); hang(); } root_dev = *partition; @@ -287,20 +286,20 @@ void init_stage2() klog() << "init_stage2: couldn't read EBR from disk"; hang(); } - auto partition = ebr.partition(partition_number); + auto partition = ebr.partition(partition_number.value()); if (!partition) { - klog() << "init_stage2: couldn't get partition " << partition_number; + klog() << "init_stage2: couldn't get partition " << partition_number.value(); hang(); } root_dev = *partition; } else { - if (partition_number < 1 || partition_number > 4) { - klog() << "init_stage2: invalid partition number " << partition_number << "; expected 1 to 4"; + if (partition_number.value() < 1 || partition_number.value() > 4) { + klog() << "init_stage2: invalid partition number " << partition_number.value() << "; expected 1 to 4"; hang(); } - auto partition = mbr.partition(partition_number); + auto partition = mbr.partition(partition_number.value()); if (!partition) { - klog() << "init_stage2: couldn't get partition " << partition_number; + klog() << "init_stage2: couldn't get partition " << partition_number.value(); hang(); } root_dev = *partition; |