Skip to content

Instantly share code, notes, and snippets.

Thiago Massari Guedes thiagomg

Block or report user

Report or block thiagomg

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
struct my_value {
std::string sval;
using const_iterator = std::string::const_iterator;
};
//...
auto cfg = configuration::load<std::string, my_value>(fname,
[](auto &config, iter_type b, iter_type m, iter_type e) {
std::string k(b, m);
my_value mv;
mv.sval = std::string(m+1, e);
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
//Leitura de uma chave
const std::string &log_dir = cfg.get("log.base_dir");
//Agora vamos pegar todas as configurações relativas a log
auto plugins = cfg.prefix("log.");
for(auto p : plugins)
std::cout << p.first << "=>" << p.second << std::endl;
//Lista de plugins na configuração
auto tok = cfg.next_token("plugin.", '.');
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
auto after(const Key &prefix) const -> std::unordered_set<Key> {
std::unordered_set<Key> values;
iterate_and_check(prefix, [&values, &prefix](const std::pair<Key,Value> &pair) {
values.insert( pair.first.substr( prefix.size() ) );
});
return values;
}
auto next_token(const Key &prefix, const typename Key::value_type separator) const -> std::unordered_set<Key> {
std::unordered_set<Key> values;
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
auto prefix(const Key &prefix) const -> std::vector<map_value> {
std::vector<map_value> values;
iterate_and_check(prefix, [&values](const std::pair<Key,Value> &pair) {
values.push_back(pair);
});
return values;
}
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
using adder_func = std::function<void(const std::pair<Key,Value> &pair)>;
void iterate_and_check(const Key &prefix, adder_func f_adder) const {
std::for_each(begin(_items), end(_items), [&prefix, &f_adder](const std::pair<Key,Value> &pair) {
//let's check prefix
if( std::equal( begin(prefix), end(prefix), begin(pair.first) ) )
f_adder(pair);
});
}
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
auto get(const Key &key) const -> const Value & {
auto it = _items.find(key);
if( it == _items.end() )
return _empty;
return it->second;
}
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
template<typename Key, typename Value, typename AdderFunc>
void crack(config_holder<Key, Value> &config, const std::string &line, char delim, AdderFunc f) {
auto b = std::begin(line);
auto e = std::end(line);
auto pos = line.find(delim);
if( pos != std::string::npos ) {
f(config, b, b+pos, e);
}
}
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
std::ifstream istr;
istr.open(file_name);
while( istr.good() ) {
std::string line;
std::getline(istr, line);
std::cout << line << std::endl;
}
@thiagomg
thiagomg / config.h
Created Feb 4, 2016
Leitura de configuração em C++
View config.h
template<typename Key = std::string, typename Value = std::string, typename Store = std::unordered_map<Key, Value>>
struct config_holder {
using key_iter = typename Key::const_iterator;
using val_iter = typename Value::const_iterator;
using key_pair = std::pair<key_iter, key_iter>;
using val_pair = std::pair<val_iter, val_iter>;
using range = std::pair<key_pair, val_pair>;
using map_value = typename Store::value_type;
};
@thiagomg
thiagomg / numeric.cpp
Last active Dec 17, 2015
Legibilidade com C++14 - get_numeric
View numeric.cpp
template<typename T>
T get_numeric(const T &input)
{
T output;
std::copy_if(begin(input), end(input), std::back_inserter(output), [](auto c) {
return ( c >= '0' && c <= '9' );
});
return output;
}
You can’t perform that action at this time.