Commit e1a7114c authored by louiz’'s avatar louiz’

louloulibs is directly included, instead of being a submodule

Because this is a nightmare to manage
parent 897b281e
[submodule "louloulibs"]
path = louloulibs
url = git://git.louiz.org/louloulibs
louloulibs @ 0f3c1183
Subproject commit 0f3c1183e2bf0941ae2bffd3f31577bce4f3001c
cmake_minimum_required(VERSION 2.6)
set(${PROJECT_NAME}_VERSION_MAJOR 1)
set(${PROJECT_NAME}_VERSION_MINOR 0)
set(${PROJECT_NAME}_VERSION_SUFFIX "~dev")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -pedantic -Wall -Wextra")
# Define a __FILENAME__ macro to get the filename of each file, instead of
# the full path as in __FILE__
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
#
## Look for external libraries
#
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
include(FindEXPAT)
find_package(EXPAT REQUIRED)
find_package(ICONV REQUIRED)
find_package(LIBUUID REQUIRED)
if(WITH_LIBIDN)
find_package(LIBIDN REQUIRED)
elseif(NOT WITHOUT_LIBIDN)
find_package(LIBIDN)
endif()
if(WITH_SYSTEMD)
find_package(SYSTEMD REQUIRED)
elseif(NOT WITHOUT_SYSTEMD)
find_package(SYSTEMD)
endif()
if(WITH_BOTAN)
find_package(BOTAN REQUIRED)
elseif(NOT WITHOUT_BOTAN)
find_package(BOTAN)
endif()
if(WITH_CARES)
find_package(CARES REQUIRED)
elseif(NOT WITHOUT_CARES)
find_package(CARES)
endif()
# To be able to include the config.h file generated by cmake
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
include_directories(${EXPAT_INCLUDE_DIRS})
include_directories(${ICONV_INCLUDE_DIRS})
include_directories(${LIBUUID_INCLUDE_DIRS})
set(EXPAT_INCLUDE_DIRS ${EXPAT_INCLUDE_DIRS} PARENT_SCOPE)
set(ICONV_INCLUDE_DIRS ${ICONV_INCLUDE_DIRS} PARENT_SCOPE)
set(LIBUUID_INCLUDE_DIRS ${LIBUUID_INCLUDE_DIRS} PARENT_SCOPE)
if(LIBIDN_FOUND)
include_directories(${LIBIDN_INCLUDE_DIRS})
set(LIBDIN_FOUND ${LIBDIN_FOUND} PARENT_SCOPE)
set(LIBDIN_INCLUDE_DIRS ${LIBDIN_INCLUDE_DIRS} PARENT_SCOPE)
endif()
if(SYSTEMD_FOUND)
include_directories(${SYSTEMD_INCLUDE_DIRS})
set(SYSTEMD_FOUND ${SYSTEMD_FOUND} PARENT_SCOPE)
set(SYSTEMD_INCLUDE_DIRS ${SYSTEMD_INCLUDE_DIRS} PARENT_SCOPE)
endif()
if(BOTAN_FOUND)
include_directories(SYSTEM ${BOTAN_INCLUDE_DIRS})
set(BOTAN_FOUND ${BOTAN_FOUND} PARENT_SCOPE)
set(BOTAN_INCLUDE_DIRS ${BOTAN_INCLUDE_DIRS} PARENT_SCOPE)
endif()
if(CARES_FOUND)
include_directories(${CARES_INCLUDE_DIRS})
set(CARES_FOUND ${CARES_FOUND} PARENT_SCOPE)
set(CARES_INCLUDE_DIRS ${CARES_INCLUDE_DIRS} PARENT_SCOPE)
endif()
set(POLLER_DOCSTRING "Choose the poller between POLL and EPOLL (Linux-only)")
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(POLLER "EPOLL" CACHE STRING ${POLLER_DOCSTRING})
else()
set(POLLER "POLL" CACHE STRING ${POLLER_DOCSTRING})
endif()
if((NOT ${POLLER} MATCHES "POLL") AND
(NOT ${POLLER} MATCHES "EPOLL"))
message(FATAL_ERROR "POLLER must be either POLL or EPOLL")
endif()
#
## utils
#
file(GLOB source_utils
utils/*.[hc]pp)
add_library(utils STATIC ${source_utils})
target_link_libraries(utils ${ICONV_LIBRARIES})
#
## config
#
file(GLOB source_config
config/*.[hc]pp)
add_library(config STATIC ${source_config})
target_link_libraries(config utils)
#
## logger
#
file(GLOB source_logger
logger/*.[hc]pp)
add_library(logger STATIC ${source_logger})
target_link_libraries(logger config)
#
## network
#
file(GLOB source_network
network/*.[hc]pp)
add_library(network STATIC ${source_network})
target_link_libraries(network logger)
if(BOTAN_FOUND)
target_link_libraries(network ${BOTAN_LIBRARIES})
endif()
if(CARES_FOUND)
target_link_libraries(network ${CARES_LIBRARIES})
endif()
#
## xmpplib
#
file(GLOB source_xmpplib
xmpp/*.[hc]pp)
add_library(xmpplib STATIC ${source_xmpplib})
target_link_libraries(xmpplib network utils logger
${EXPAT_LIBRARIES}
${LIBUUID_LIBRARIES})
if(LIBIDN_FOUND)
target_link_libraries(xmpplib ${LIBIDN_LIBRARIES})
endif()
if(SYSTEMD_FOUND)
target_link_libraries(xmpplib ${SYSTEMD_LIBRARIES})
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/louloulibs.h.cmake ${CMAKE_BINARY_DIR}/src/louloulibs.h)
# - Find botan
# Find the botan cryptographic library
#
# This module defines the following variables:
# BOTAN_FOUND - True if library and include directory are found
# If set to TRUE, the following are also defined:
# BOTAN_INCLUDE_DIRS - The directory where to find the header file
# BOTAN_LIBRARIES - Where to find the library file
#
# For conveniance, these variables are also set. They have the same values
# than the variables above. The user can thus choose his/her prefered way
# to write them.
# BOTAN_LIBRARY
# BOTAN_INCLUDE_DIR
#
# This file is in the public domain
find_path(BOTAN_INCLUDE_DIRS NAMES botan/botan.h
PATH_SUFFIXES botan-1.11
DOC "The botan include directory")
find_library(BOTAN_LIBRARIES NAMES botan botan-1.11
DOC "The botan library")
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
# set BOTAN_FOUND to TRUE if these two variables are set.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(BOTAN REQUIRED_VARS BOTAN_LIBRARIES BOTAN_INCLUDE_DIRS)
if(BOTAN_FOUND)
set(BOTAN_LIBRARY ${BOTAN_LIBRARIES})
set(BOTAN_INCLUDE_DIR ${BOTAN_INCLUDE_DIRS})
endif()
mark_as_advanced(BOTAN_INCLUDE_DIRS BOTAN_LIBRARIES)
# - Find c-ares
# Find the c-ares library, and more particularly the stringprep header.
#
# This module defines the following variables:
# CARES_FOUND - True if library and include directory are found
# If set to TRUE, the following are also defined:
# CARES_INCLUDE_DIRS - The directory where to find the header file
# CARES_LIBRARIES - Where to find the library file
#
# For conveniance, these variables are also set. They have the same values
# than the variables above. The user can thus choose his/her prefered way
# to write them.
# CARES_INCLUDE_DIR
# CARES_LIBRARY
#
# This file is in the public domain
if(NOT CARES_FOUND)
find_path(CARES_INCLUDE_DIRS NAMES ares.h
DOC "The c-ares include directory")
find_library(CARES_LIBRARIES NAMES cares
DOC "The c-ares library")
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
# set CARES_FOUND to TRUE if these two variables are set.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CARES REQUIRED_VARS CARES_LIBRARIES CARES_INCLUDE_DIRS)
# Compatibility for all the ways of writing these variables
if(CARES_FOUND)
set(CARES_INCLUDE_DIR ${CARES_INCLUDE_DIRS})
set(CARES_LIBRARY ${CARES_LIBRARIES})
endif()
endif()
mark_as_advanced(CARES_INCLUDE_DIRS CARES_LIBRARIES)
# - Find iconv
# Find the iconv (character set conversion) library
#
# This module defines the following variables:
# ICONV_FOUND - True if library and include directory are found
# If set to TRUE, the following are also defined:
# ICONV_INCLUDE_DIRS - The directory where to find the header file
# ICONV_LIBRARIES - Where to find the library file
# ICONV_SECOND_ARGUMENT_IS_CONST - The second argument for iconv() is const
#
# For conveniance, these variables are also set. They have the same values
# than the variables above. The user can thus choose his/her prefered way
# to write them.
# ICONV_LIBRARY
# ICONV_INCLUDE_DIR
#
# This file is in the public domain
find_path(ICONV_INCLUDE_DIRS NAMES iconv.h
DOC "The iconv include directory")
find_library(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c
DOC "The iconv library")
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
# set ICONV_FOUND to TRUE if these two variables are set.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Iconv REQUIRED_VARS ICONV_LIBRARIES ICONV_INCLUDE_DIRS)
# Check if the prototype is
# size_t iconv(iconv_t cd, char** inbuf, size_t* inbytesleft,
# char** outbuf, size_t* outbytesleft);
# or
# size_t iconv (iconv_t cd, const char** inbuf, size_t* inbytesleft,
# char** outbuf, size_t* outbytesleft);
if(ICONV_FOUND)
include(CheckCXXSourceCompiles)
# Set the parameters needed to compile the following code.
set(CMAKE_REQUIRED_INCLUDES ${ICONV_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARIES})
check_cxx_source_compiles("
#include <iconv.h>
int main(){
iconv_t conv = 0;
const char* in = 0;
size_t ilen = 0;
char* out = 0;
size_t olen = 0;
iconv(conv, &in, &ilen, &out, &olen);
return 0;}"
ICONV_SECOND_ARGUMENT_IS_CONST)
# Compatibility for all the ways of writing these variables
set(ICONV_LIBRARY ${ICONV_LIBRARIES})
set(ICONV_INCLUDE_DIR ${ICONV_INCLUDE_DIRS})
endif()
mark_as_advanced(ICONV_INCLUDE_DIRS ICONV_LIBRARIES ICONV_SECOND_ARGUMENT_IS_CONST)
\ No newline at end of file
# - Find libidn
# Find the libidn library, and more particularly the stringprep header.
#
# This module defines the following variables:
# LIBIDN_FOUND - True if library and include directory are found
# If set to TRUE, the following are also defined:
# LIBIDN_INCLUDE_DIRS - The directory where to find the header file
# LIBIDN_LIBRARIES - Where to find the library file
#
# For conveniance, these variables are also set. They have the same values
# than the variables above. The user can thus choose his/her prefered way
# to write them.
# LIBIDN_INCLUDE_DIR
# LIBIDN_LIBRARY
#
# This file is in the public domain
include(FindPkgConfig)
pkg_check_modules(LIBIDN libidn)
if(NOT LIBIDN_FOUND)
find_path(LIBIDN_INCLUDE_DIRS NAMES stringprep.h
DOC "The libidn include directory")
# The library containing the stringprep module is libidn
find_library(LIBIDN_LIBRARIES NAMES idn
DOC "The libidn library")
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
# set LIBIDN_FOUND to TRUE if these two variables are set.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LIBIDN REQUIRED_VARS LIBIDN_LIBRARIES LIBIDN_INCLUDE_DIRS)
# Compatibility for all the ways of writing these variables
if(LIBIDN_FOUND)
set(LIBIDN_INCLUDE_DIR ${LIBIDN_INCLUDE_DIRS})
set(LIBIDN_LIBRARY ${LIBIDN_LIBRARIES})
endif()
endif()
mark_as_advanced(LIBIDN_INCLUDE_DIRS LIBIDN_LIBRARIES)
\ No newline at end of file
# - Find libuuid
# Find the libuuid library
#
# This module defines the following variables:
# LIBUUID_FOUND - True if library and include directory are found
# If set to TRUE, the following are also defined:
# LIBUUID_INCLUDE_DIRS - The directory where to find the header file
# LIBUUID_LIBRARIES - Where to find the library file
#
# For conveniance, these variables are also set. They have the same values
# than the variables above. The user can thus choose his/her prefered way
# to write them.
# LIBUUID_INCLUDE_DIR
# LIBUUID_LIBRARY
#
# This file is in the public domain
include(FindPkgConfig)
pkg_check_modules(LIBUUID uuid)
if(NOT LIBUUID_FOUND)
find_path(LIBUUID_INCLUDE_DIRS NAMES uuid.h
PATH_SUFFIXES uuid
DOC "The libuuid include directory")
find_library(LIBUUID_LIBRARIES NAMES uuid
DOC "The libuuid library")
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
# set LIBUUID_FOUND to TRUE if these two variables are set.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LIBUUID REQUIRED_VARS LIBUUID_LIBRARIES LIBUUID_INCLUDE_DIRS)
# Compatibility for all the ways of writing these variables
if(LIBUUID_FOUND)
set(LIBUUID_INCLUDE_DIR ${LIBUUID_INCLUDE_DIRS})
set(LIBUUID_LIBRARY ${LIBUUID_LIBRARIES})
endif()
endif()
mark_as_advanced(LIBUUID_INCLUDE_DIRS LIBUUID_LIBRARIES)
# - Find SystemdDaemon
# Find the systemd daemon library
#
# This module defines the following variables:
# SYSTEMD_FOUND - True if library and include directory are found
# If set to TRUE, the following are also defined:
# SYSTEMD_INCLUDE_DIRS - The directory where to find the header file
# SYSTEMD_LIBRARIES - Where to find the library file
#
# For conveniance, these variables are also set. They have the same values
# than the variables above. The user can thus choose his/her prefered way
# to write them.
# SYSTEMD_LIBRARY
# SYSTEMD_INCLUDE_DIR
#
# This file is in the public domain
include(FindPkgConfig)
pkg_check_modules(SYSTEMD libsystemd)
if(NOT SYSTEMD_FOUND)
find_path(SYSTEMD_INCLUDE_DIRS NAMES systemd/sd-daemon.h
DOC "The Systemd include directory")
find_library(SYSTEMD_LIBRARIES NAMES systemd
DOC "The Systemd library")
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
# set SYSTEMD_FOUND to TRUE if these two variables are set.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SYSTEMD REQUIRED_VARS SYSTEMD_LIBRARIES SYSTEMD_INCLUDE_DIRS)
if(SYSTEMD_FOUND)
set(SYSTEMD_LIBRARY ${SYSTEMD_LIBRARIES})
set(SYSTEMD_INCLUDE_DIR ${SYSTEMD_INCLUDE_DIRS})
endif()
endif()
mark_as_advanced(SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES)
\ No newline at end of file
#include <config/config.hpp>
#include <iostream>
#include <sstream>
#include <stdlib.h>
std::string Config::filename{};
bool Config::file_must_exist = false;
std::string Config::get(const std::string& option, const std::string& def)
{
Config* self = Config::instance().get();
auto it = self->values.find(option);
if (it == self->values.end())
return def;
return it->second;
}
int Config::get_int(const std::string& option, const int& def)
{
Config* self = Config::instance().get();
std::string res = self->get(option, "");
if (!res.empty())
return atoi(res.c_str());
else
return def;
}
void Config::set(const std::string& option, const std::string& value, bool save)
{
Config* self = Config::instance().get();
self->values[option] = value;
if (save)
{
self->save_to_file();
self->trigger_configuration_change();
}
}
void Config::connect(t_config_changed_callback callback)
{
Config* self = Config::instance().get();
self->callbacks.push_back(callback);
}
void Config::close()
{
Config* self = Config::instance().get();
self->values.clear();
Config::instance().reset();
}
/**
* Private methods
*/
void Config::trigger_configuration_change()
{
std::vector<t_config_changed_callback>::iterator it;
for (it = this->callbacks.begin(); it < this->callbacks.end(); ++it)
(*it)();
}
std::unique_ptr<Config>& Config::instance()
{
static std::unique_ptr<Config> instance;
if (!instance)
{
instance = std::make_unique<Config>();
instance->read_conf();
}
return instance;
}
bool Config::read_conf()
{
std::ifstream file;
file.open(filename.data());
if (!file.is_open())
{
if (Config::file_must_exist)
{
perror(("Error while opening file " + filename + " for reading.").c_str());
file.exceptions(std::ifstream::failbit);
}
return false;
}
std::string line;
size_t pos;
std::string option;
std::string value;
while (file.good())
{
std::getline(file, line);
if (line == "" || line[0] == '#')
continue ;
pos = line.find('=');
if (pos == std::string::npos)
continue ;
option = line.substr(0, pos);
value = line.substr(pos+1);
this->values[option] = value;
}
return true;
}
void Config::save_to_file() const
{
std::ofstream file(this->filename.data());
if (file.fail())
{
std::cerr << "Could not save config file." << std::endl;
return ;
}
for (auto& it: this->values)
file << it.first << "=" << it.second << std::endl;
file.close();
}
/**
* Read the config file and save all the values in a map.
* Also, a singleton.
*
* Use Config::filename = "bla" to set the filename you want to use.
*
* If you want to exit if the file does not exist when it is open for
* reading, set Config::file_must_exist = true.
*
* Config::get() can the be used to access the values in the conf.
*
* Use Config::close() when you're done getting/setting value. This will
* save the config into the file.
*/
#ifndef CONFIG_INCLUDED
# define CONFIG_INCLUDED
#include <functional>
#include <fstream>
#include <memory>
#include <vector>
#include <string>
#include <map>
typedef std::function<void()> t_config_changed_callback;
class Config
{
public:
Config(){};
~Config(){};
/**
* returns a value from the config. If it doesn’t exist, use
* the second argument as the default.
* @param option The option we want
* @param def The default value in case the option does not exist
*/
static std::string get(const std::string&, const std::string&);
/**
* returns a value from the config. If it doesn’t exist, use
* the second argument as the default.
* @param option The option we want
* @param def The default value in case the option does not exist
*/
static int get_int(const std::string&, const int&);
/**
* Set a value for the given option. And write all the config
* in the file from which it was read if boolean is set.
* @param option The option to set
* @param value The value to use
* @param save if true, save the config file
*/
static void set(const std::string&, const std::string&, bool save = false);
/**
* Adds a function to a list. This function will be called whenever a
* configuration change occurs.
*/
static void connect(t_config_changed_callback);
/**
* Close the config file, saving it to the file is save == true.
*/
static void close();
/**
* Set the value of the filename to use, before calling any method.
*/
static std::string filename;
/**
* Set to true if you want an exception to be raised if the file does not
* exist when reading it.
*/
static bool file_must_exist;
private:
/**
* Get the singleton instance
*/
static std::unique_ptr<Config>& instance();
/**
* Read the configuration file at the given path.
*/
bool read_conf();
/**
* Write all the config values into the configuration file
*/
void save_to_file() const;
/**
* Call all the callbacks previously registered using connect().
* This is used to notify any class that a configuration change occured.
*/
void trigger_configuration_change();
std::map<std::string, std::string> values;
std::vector<t_config_changed_callback> callbacks;