Created
May 5, 2024 16:36
-
-
Save vittorioromeo/74cf2a5bbed42099418810be7ad6a07c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using mode_t = unsigned int; | |
using size_t = decltype(sizeof(int)); | |
namespace detail | |
{ | |
struct invalid_permission_character {}; | |
struct invalid_permission_string_length {}; | |
consteval mode_t from_readable_mode_string(const char * const s) | |
{ | |
mode_t result{}; | |
#define STEP(position, accept) \ | |
{ \ | |
if(s[position] == accept) { result |= 1 << (8-position); } \ | |
else if(s[position] == '-') { result |= 0; } \ | |
else throw invalid_permission_character{}; \ | |
} | |
STEP(0, 'r'); STEP(3, 'r'); STEP(6, 'r'); | |
STEP(1, 'w'); STEP(4, 'w'); STEP(7, 'w'); | |
STEP(2, 'x'); STEP(5, 'x'); STEP(8, 'x'); | |
#undef STEP | |
return result; | |
}} | |
consteval mode_t from_readable_mode(const char (&permission_string)[10]) | |
{ | |
return detail::from_readable_mode_string(permission_string); | |
} | |
consteval mode_t operator""_mode(const char *s, size_t len) | |
{ | |
return len == 9 ? detail::from_readable_mode_string(s) | |
: throw detail::invalid_permission_string_length{}; | |
} | |
void set_permission(mode_t) { } | |
int main() | |
{ | |
set_permission("rwx---rwx"_mode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment