diff options
author | Max Kellermann <max@duempel.org> | 2013-10-19 16:46:07 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-10-19 16:47:41 +0200 |
commit | daa47546c78a535e1fd0cc6dbf97841f74c7acde (patch) | |
tree | 6b26e14d95cc6948856f25a0feea8074111f06f6 /src | |
parent | c5194d0a8f5bb34cd07ed96dde70eec6cda8f963 (diff) | |
download | mpd-daa47546c78a535e1fd0cc6dbf97841f74c7acde.tar.gz mpd-daa47546c78a535e1fd0cc6dbf97841f74c7acde.tar.xz mpd-daa47546c78a535e1fd0cc6dbf97841f74c7acde.zip |
Permission: move code to ParsePermission() and a table
Diffstat (limited to 'src')
-rw-r--r-- | src/Permission.cxx | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/Permission.cxx b/src/Permission.cxx index 95a164fc4..a703a9465 100644 --- a/src/Permission.cxx +++ b/src/Permission.cxx @@ -35,15 +35,32 @@ static constexpr char PERMISSION_PASSWORD_CHAR = '@'; #define PERMISSION_SEPERATOR "," -#define PERMISSION_READ_STRING "read" -#define PERMISSION_ADD_STRING "add" -#define PERMISSION_CONTROL_STRING "control" -#define PERMISSION_ADMIN_STRING "admin" +static constexpr struct { + const char *name; + unsigned value; +} permission_names[] = { + { "read", PERMISSION_READ }, + { "add", PERMISSION_ADD }, + { "control", PERMISSION_CONTROL }, + { "admin", PERMISSION_ADMIN }, + { nullptr, 0 }, +}; static std::map<std::string, unsigned> permission_passwords; static unsigned permission_default; +gcc_pure +static unsigned +ParsePermission(const char *p) +{ + for (auto i = permission_names; i->name != nullptr; ++i) + if (strcmp(p, i->name) == 0) + return i->value; + + FormatFatalError("unknown permission \"%s\"", p); +} + static unsigned parsePermissions(const char *string) { assert(string != nullptr); @@ -54,18 +71,7 @@ static unsigned parsePermissions(const char *string) tokens = g_strsplit(string, PERMISSION_SEPERATOR, 0); for (unsigned i = 0; tokens[i] != NULL; ++i) { char *temp = tokens[i]; - - if (strcmp(temp, PERMISSION_READ_STRING) == 0) { - permission |= PERMISSION_READ; - } else if (strcmp(temp, PERMISSION_ADD_STRING) == 0) { - permission |= PERMISSION_ADD; - } else if (strcmp(temp, PERMISSION_CONTROL_STRING) == 0) { - permission |= PERMISSION_CONTROL; - } else if (strcmp(temp, PERMISSION_ADMIN_STRING) == 0) { - permission |= PERMISSION_ADMIN; - } else { - FormatFatalError("unknown permission \"%s\"", temp); - } + permission |= ParsePermission(temp); } g_strfreev(tokens); |