NeKernel dev
Loading...
Searching...
No Matches
mkfs.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6
7#pragma once
8
9#include <tools/rang.h>
10#include <iostream>
11#include <string>
12
13#define kMkFsSectorSz (512U)
14#define kMkFsMaxBadSectors (128U)
15
17namespace mkfs {
18
19namespace detail {
22 inline constexpr size_t gib_cast(uint32_t gb) {
23 return static_cast<size_t>(gb) * 1024ULL * 1024ULL * 1024ULL;
24 }
25
26 inline bool parse_decimal(const std::string& opt, unsigned long long& out) {
27 if (opt.empty()) return false;
28 char* endptr = nullptr;
29 unsigned long long val = std::strtoull(opt.c_str(), &endptr, 10);
30 if (endptr == opt.c_str() || *endptr != '\0') return false;
31 out = val;
32 return true;
33 }
34
35 inline bool parse_signed(const std::string& opt, long& out, int base = 10) {
36 out = 0L;
37
38 if (opt.empty()) return true;
39
40 char* endptr = nullptr;
41 long val = std::strtol(opt.c_str(), &endptr, base);
42 auto err = errno;
43
44 if (err == ERANGE || err == EINVAL) return false;
45 if (endptr == opt.c_str() || *endptr != '\0') return false;
46
47 out = val;
48 return true;
49 }
50
51 inline std::string build_args(int argc, char** argv) {
52 std::string combined;
53 for (int i = 1; i < argc; ++i) {
54 combined += argv[i];
55 combined += ' ';
56 }
57 return combined;
58 }
59} // namespace detail
60
62template <typename CharType>
63inline std::basic_string<CharType> get_option(const std::basic_string<CharType>& args,
64 const std::basic_string<CharType>& option) {
65 size_t pos = args.find(CharType('-') + option + CharType('='));
66
67 if (pos != std::string::npos) {
68 size_t start = pos + option.length() + 2;
69 size_t end = args.find(' ', start);
70 return args.substr(start, end - start);
71 }
72
73 return std::basic_string<CharType>{};
74}
75
76inline auto console_out() -> std::ostream& {
77 std::ostream& conout = std::cout;
78 conout << rang::fg::red << "mkfs: " << rang::style::reset;
79
80 return conout;
81}
82} // namespace mkfs
#define gib_cast(X)
Definition mkfs.h:19
std::string build_args(int argc, char **argv)
Definition mkfs.h:51
bool parse_signed(const std::string &opt, long &out, int base=10)
Definition mkfs.h:35
bool parse_decimal(const std::string &opt, unsigned long long &out)
Definition mkfs.h:26
Definition mkfs.h:17
auto console_out() -> std::ostream &
Definition mkfs.h:76
std::basic_string< CharType > get_option(const std::basic_string< CharType > &args, const std::basic_string< CharType > &option)
Helper function to get the option value from command line arguments.
Definition mkfs.h:63
@ red
Definition rang.h:66
@ reset
Definition rang.h:52