Skip to content

Commit

Permalink
Merge pull request #127 from EmperorYP7/config-cleanup
Browse files Browse the repository at this point in the history
chore: Config and enforcer cleanup
  • Loading branch information
hsluoyz authored Aug 10, 2021
2 parents 77dccf5 + de809c6 commit 32bfa27
Show file tree
Hide file tree
Showing 55 changed files with 221 additions and 432 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,4 @@ MigrationBackup/
# CMake work directory
cmake-build/
xcode-build/
cmake-build*/
2 changes: 1 addition & 1 deletion bindings/python/py_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "py_casbin.h"

void bindPyModel(py::module &m) {
py::class_<casbin::Model>(m, "Model")
py::class_<casbin::Model, std::shared_ptr<casbin::Model>>(m, "Model")
.def(py::init<>())
.def(py::init<const std::string &>())

Expand Down
59 changes: 26 additions & 33 deletions casbin/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
#ifndef CONFIG_CPP
#define CONFIG_CPP


#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdio>

#include "./config.h"
#include "../exception/io_exception.h"
#include "../exception/illegal_argument_exception.h"
Expand All @@ -40,15 +34,15 @@ std::mutex Config::mtx_lock;
/**
* addConfig adds a new section->key:value to the configuration.
*/
bool Config :: AddConfig(std::string section, std::string option, std::string value) {
bool Config::AddConfig(std::string section, const std::string& option, const std::string& value) {
if (!section.compare(""))
section = DEFAULT_SECTION;
bool ok = data[section].find(option) != data[section].end();
data[section][option] = value;
return !ok;
}

void Config :: Parse(std::string f_name) {
void Config::Parse(const std::string& f_name) {
mtx_lock.lock();
std::ifstream infile;
try {
Expand All @@ -62,13 +56,13 @@ void Config :: Parse(std::string f_name) {
infile.close();
}

void Config ::ParseBuffer(std::istream * buf) {
void Config::ParseBuffer(std::istream * buf) {
std::string section = "";
int line_num = 0;
std::string line;
while (true) {
line_num++;
if (getline(*buf, line, '\n')){
if (getline(*buf, line, '\n')) {
if (!line.compare(""))
continue;
}
Expand All @@ -79,10 +73,10 @@ void Config ::ParseBuffer(std::istream * buf) {
continue;
else if (line.find(DEFAULT_COMMENT_SEM)==0)
continue;
else if (line.find("[")==0 && EndsWith(line, std::string("]")))
else if (line.find("[") == 0 && EndsWith(line, "]"))
section = line.substr(1, line.length() - 2);
else {
std::vector<std::string> option_val = Split(line, std::string("="), 2);
std::vector<std::string> option_val = Split(line, "=", 2);
if (option_val.size() != 2) {
char* error = new char;
sprintf(error, "parse the content error : line %d , %s = ? ", line_num, option_val[0].c_str());
Expand All @@ -101,8 +95,8 @@ void Config ::ParseBuffer(std::istream * buf) {
* @param confName the path of the model file.
* @return the constructor of Config.
*/
std::shared_ptr<Config> Config :: NewConfig(std::string conf_name) {
std::shared_ptr<Config> c(new Config);
std::shared_ptr<Config> Config::NewConfig(const std::string& conf_name) {
std::shared_ptr<Config> c = std::make_shared<Config>();
c->Parse(conf_name);
return c;
}
Expand All @@ -113,39 +107,39 @@ std::shared_ptr<Config> Config :: NewConfig(std::string conf_name) {
* @param text the model text.
* @return the constructor of Config.
*/
std::shared_ptr<Config> Config :: NewConfigFromText(std::string text) {
std::shared_ptr<Config> c(new Config);
std::shared_ptr<Config> Config::NewConfigFromText(const std::string& text) {
std::shared_ptr<Config> c = std::make_shared<Config>();
std::stringstream stream(text);
c->ParseBuffer(&stream);
return c;
}

bool Config :: GetBool(std::string key) {
bool Config::GetBool(std::string_view key) {
return Get(key).compare("true")==0;
}

int Config :: GetInt(std::string key) {
int Config::GetInt(std::string_view key) {
return atoi(Get(key).c_str());
}

float Config :: GetFloat(std::string key) {
float Config::GetFloat(std::string_view key) {
return float(atof(Get(key).c_str()));
}

std::string Config :: GetString(std::string key) {
std::string Config::GetString(std::string_view key) {
return Get(key);
}

std::vector<std::string> Config :: GetStrings(std::string key) {
std::vector<std::string> Config::GetStrings(std::string_view key) {
std::string v = Get(key);
if (!v.compare("")) {
std::vector<std::string> empty;
return empty;
}
return Split(v,std::string(","));

if (!v.compare(""))
return {};

return Split(v, ",");
}

void Config :: Set(std::string key, std::string value) {
void Config::Set(std::string_view key, const std::string& value) {
mtx_lock.lock();
if (key.length() == 0) {
mtx_lock.unlock();
Expand All @@ -155,8 +149,7 @@ void Config :: Set(std::string key, std::string value) {
std::string section = "";
std::string option;

transform(key.begin(), key.end(), key.begin(), ::tolower);
std::vector<std::string> keys = Split(key, std::string("::"));
std::vector<std::string> keys = Split(std::string(key), "::");
if (keys.size() >= 2) {
section = keys[0];
option = keys[1];
Expand All @@ -168,19 +161,19 @@ void Config :: Set(std::string key, std::string value) {
mtx_lock.unlock();
}

std::string Config :: Get(std::string key) {
std::string Config::Get(std::string_view key) {
std::string section;
std::string option;
transform(key.begin(), key.end(), key.begin(), ::tolower);
std::vector<std::string> keys = Split(key, std::string("::"));

std::vector<std::string> keys = Split(std::string(key), "::");
if (keys.size() >= 2) {
section = keys[0];
option = keys[1];
} else {
section = DEFAULT_SECTION;
option = keys[0];
}
bool ok = data.find(section)!=data.end() && data[section].find(option) != data[section].end();
bool ok = data.find(section) != data.end() && data[section].find(option) != data[section].end();
if (ok)
return data[section][option];
return "";
Expand Down
22 changes: 11 additions & 11 deletions casbin/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Config : public ConfigInterface {
/**
* addConfig adds a new section->key:value to the configuration.
*/
bool AddConfig(std::string section, std::string option, std::string value);
bool AddConfig(std::string section, const std::string& option, const std::string& value);

void Parse(std::string f_name);
void Parse(const std::string& f_name);

void ParseBuffer(std::istream* buf);

Expand All @@ -52,29 +52,29 @@ class Config : public ConfigInterface {
* @param confName the path of the model file.
* @return the constructor of Config.
*/
static std::shared_ptr<Config> NewConfig(std::string conf_name);
static std::shared_ptr<Config> NewConfig(const std::string& conf_name);

/**
* newConfigFromText create an empty configuration representation from text.
*
* @param text the model text.
* @return the constructor of Config.
*/
static std::shared_ptr<Config> NewConfigFromText(std::string text);
static std::shared_ptr<Config> NewConfigFromText(const std::string& text);

bool GetBool(std::string key);
bool GetBool(std::string_view key);

int GetInt(std::string key);
int GetInt(std::string_view key);

float GetFloat(std::string key);
float GetFloat(std::string_view key);

std::string GetString(std::string key);
std::string GetString(std::string_view key);

std::vector<std::string> GetStrings(std::string key);
std::vector<std::string> GetStrings(std::string_view key);

void Set(std::string key, std::string value);
void Set(std::string_view key, const std::string& value);

std::string Get(std::string key);
std::string Get(std::string_view key);
};

} // namespace casbin
Expand Down
12 changes: 6 additions & 6 deletions casbin/config/config_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ namespace casbin {
class ConfigInterface {
public:

virtual std::string GetString(std::string key) = 0;
virtual std::vector<std::string> GetStrings(std::string key) = 0;
virtual bool GetBool(std::string key) = 0;
virtual int GetInt(std::string key) = 0;
virtual float GetFloat(std::string key) = 0;
virtual void Set(std::string key, std::string value) = 0;
virtual std::string GetString(std::string_view key) = 0;
virtual std::vector<std::string> GetStrings(std::string_view key) = 0;
virtual bool GetBool(std::string_view key) = 0;
virtual int GetInt(std::string_view key) = 0;
virtual float GetFloat(std::string_view key) = 0;
virtual void Set(std::string_view key, const std::string& value) = 0;

};

Expand Down
29 changes: 0 additions & 29 deletions casbin/config/pch.h

This file was deleted.

29 changes: 0 additions & 29 deletions casbin/duktape/pch.h

This file was deleted.

29 changes: 0 additions & 29 deletions casbin/effect/pch.h

This file was deleted.

Loading

0 comments on commit 32bfa27

Please sign in to comment.