[seiscomp, scanloc] Install, add .gitignore
This commit is contained in:
65
include/seiscomp/utils/base64.h
Normal file
65
include/seiscomp/utils/base64.h
Normal file
@ -0,0 +1,65 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_BASE64_H
|
||||
#define SEISCOMP_UTILS_BASE64_H
|
||||
|
||||
#include <seiscomp/core.h>
|
||||
#include <string>
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
/**
|
||||
* Converts a datastream into a base64 encoded string.
|
||||
* @param target The target string
|
||||
* @param data The source data stream
|
||||
* @param data_size The source data streamsize (number of elements, not bytes!)
|
||||
*/
|
||||
SC_SYSTEM_CORE_API void encodeBase64(std::string &target, const char *data, size_t data_size);
|
||||
|
||||
/**
|
||||
* Converts a datastream into a base64 encoded string.
|
||||
* @param target The target string
|
||||
* @param data The source data string
|
||||
*/
|
||||
SC_SYSTEM_CORE_API void encodeBase64(std::string &target, const std::string &data);
|
||||
|
||||
/**
|
||||
* Decodes a base64 encoded string.
|
||||
* @param target The container for the decoded data stream
|
||||
* @param data The base64 encoded string
|
||||
* @param data_size The base64 encoded string size (number of elements, not bytes!)
|
||||
|
||||
*/
|
||||
SC_SYSTEM_CORE_API void decodeBase64(std::string &target, const char *data, size_t data_size);
|
||||
|
||||
/**
|
||||
* Decodes a base64 encoded string.
|
||||
* @param target The container for the decoded data stream
|
||||
* @param data The base64 encoded string
|
||||
*/
|
||||
SC_SYSTEM_CORE_API void decodeBase64(std::string &target, const std::string &data);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
103
include/seiscomp/utils/base64.ipp
Normal file
103
include/seiscomp/utils/base64.ipp
Normal file
@ -0,0 +1,103 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include <boost/archive/iterators/base64_from_binary.hpp>
|
||||
#include <boost/archive/iterators/binary_from_base64.hpp>
|
||||
#include <boost/archive/iterators/insert_linebreaks.hpp>
|
||||
#include <boost/archive/iterators/remove_whitespace.hpp>
|
||||
#include <boost/archive/iterators/transform_width.hpp>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
void encodeBase64(std::string& target, const CharType *data, size_t data_size) {
|
||||
typedef
|
||||
boost::archive::iterators::insert_linebreaks<
|
||||
boost::archive::iterators::base64_from_binary<
|
||||
boost::archive::iterators::transform_width<
|
||||
CharType *
|
||||
,6
|
||||
,sizeof(CharType) * 8
|
||||
>
|
||||
>
|
||||
,72
|
||||
>
|
||||
translate_out;
|
||||
|
||||
std::copy(
|
||||
translate_out(BOOST_MAKE_PFTO_WRAPPER(static_cast<const CharType*>(data))),
|
||||
translate_out(BOOST_MAKE_PFTO_WRAPPER(data + data_size)),
|
||||
std::back_inserter(target)
|
||||
);
|
||||
}
|
||||
|
||||
template<typename Container>
|
||||
void encodeBase64(std::string& target, const Container& data) {
|
||||
typedef
|
||||
boost::archive::iterators::insert_linebreaks<
|
||||
boost::archive::iterators::base64_from_binary<
|
||||
boost::archive::iterators::transform_width<
|
||||
typename Container::value_type *
|
||||
,6
|
||||
,sizeof(typename Container::value_type) * 8
|
||||
>
|
||||
>
|
||||
,72
|
||||
>
|
||||
translate_out;
|
||||
|
||||
std::copy(
|
||||
translate_out(BOOST_MAKE_PFTO_WRAPPER(static_cast<const typename Container::value_type*>(&data[0]))),
|
||||
translate_out(BOOST_MAKE_PFTO_WRAPPER(static_cast<const typename Container::value_type*>(&data[0] + data.size()))),
|
||||
std::back_inserter(target)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
template<typename Container>
|
||||
void decodeBase64(Container& target, std::string& data) {
|
||||
// convert from base64 to binary and compare with the original
|
||||
typedef
|
||||
boost::archive::iterators::transform_width<
|
||||
boost::archive::iterators::binary_from_base64<
|
||||
boost::archive::iterators::remove_whitespace<
|
||||
BOOST_DEDUCED_TYPENAME std::string::iterator
|
||||
>
|
||||
>,
|
||||
sizeof(typename Container::value_type) * 8,
|
||||
6
|
||||
> translate_in;
|
||||
translate_in it(data.begin());
|
||||
try {
|
||||
while ( true )
|
||||
target.push_back(*it++);
|
||||
}
|
||||
catch (...) {}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
292
include/seiscomp/utils/bindings.h
Normal file
292
include/seiscomp/utils/bindings.h
Normal file
@ -0,0 +1,292 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_BINDINGS_H
|
||||
#define SEISCOMP_UTILS_BINDINGS_H
|
||||
|
||||
|
||||
#include <seiscomp/core/metaobject.h>
|
||||
#include <seiscomp/datamodel/station.h>
|
||||
#include <seiscomp/datamodel/configmodule.h>
|
||||
#include <seiscomp/utils/keyvalues.h>
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
DEFINE_SMARTPOINTER(Bindings);
|
||||
|
||||
/**
|
||||
* @brief The Bindings class holds a map of all stations and its corresponding
|
||||
* parameter set.
|
||||
*
|
||||
* The bindings are initialized from a Seiscomp::DataModel::ConfigModule object.
|
||||
* It provides a simple mechanims to iterate over all bound stations with its
|
||||
* parameters.
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API Bindings : public Core::BaseObject {
|
||||
// ----------------------------------------------------------------------
|
||||
// Public types
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
struct Binding {
|
||||
KeyValuesPtr keys;
|
||||
Core::MetaValue data;
|
||||
};
|
||||
|
||||
typedef std::map<std::string, Binding> StationMap;
|
||||
typedef std::map<std::string, StationMap> NetworkMap;
|
||||
|
||||
/**
|
||||
* @brief The const_iterator class to iterate through bindings.
|
||||
*
|
||||
* It provides access to the current station code, network code,
|
||||
* parameter list and user data.
|
||||
*/
|
||||
class const_iterator {
|
||||
public:
|
||||
const_iterator() {}
|
||||
|
||||
public:
|
||||
// Prefix ++
|
||||
const_iterator &operator++() {
|
||||
++_sIt;
|
||||
if ( _sIt == _stations->end() ) {
|
||||
++_nIt;
|
||||
if ( _nIt != _networks->end() ) {
|
||||
_stations = &_nIt->second;
|
||||
_sIt = _stations->begin();
|
||||
}
|
||||
else {
|
||||
*this = const_iterator();
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Postfix ++
|
||||
const_iterator operator++(int) {
|
||||
const_iterator res(*this);
|
||||
++(*this);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool operator==(const const_iterator &other) const {
|
||||
return _nIt == other._nIt && _sIt == other._sIt;
|
||||
}
|
||||
|
||||
bool operator!=(const const_iterator &other) const {
|
||||
return _nIt != other._nIt || _sIt != other._sIt;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Returns the network code of the current element.
|
||||
*
|
||||
* If calling this function on a singular iterator the behaviour
|
||||
* is undefined.
|
||||
* @return The network code.
|
||||
*/
|
||||
const std::string &networkCode() const {
|
||||
return _nIt->first;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the station code of the current element.
|
||||
*
|
||||
* If calling this function on a singular iterator the behaviour
|
||||
* is undefined.
|
||||
* @return The station code.
|
||||
*/
|
||||
const std::string &stationCode() const {
|
||||
return _sIt->first;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the parameter list of the current element.
|
||||
*
|
||||
* If calling this function on a singular iterator the behaviour
|
||||
* is undefined.
|
||||
* @return A pointer to the parameter list.
|
||||
*/
|
||||
const KeyValues *keys() const {
|
||||
return _sIt->second.keys.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the user data of the current element.
|
||||
*
|
||||
* If calling this function on a singular iterator the behaviour
|
||||
* is undefined.
|
||||
* @return The user data.
|
||||
*/
|
||||
const Core::MetaValue &data() const {
|
||||
return _sIt->second.data;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
const NetworkMap *_networks;
|
||||
const StationMap *_stations;
|
||||
|
||||
NetworkMap::const_iterator _nIt;
|
||||
StationMap::const_iterator _sIt;
|
||||
|
||||
|
||||
friend class Bindings;
|
||||
};
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// X'truction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
Bindings();
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public methods
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
/**
|
||||
* @brief Builds the bindings map from a ConfigModule and compiles
|
||||
* all inherited ParameterSets into a single list of key-value
|
||||
* pairs per station.
|
||||
* @param mod The ConfigModule, e.g. Application::configModule()
|
||||
* @param setupName The name of the setup to be read, e.g. Application::name()
|
||||
* @param allowGlobal Whether global bindings are allowed to be read if
|
||||
* an application (setup) specific bindings has not been found.
|
||||
* E.g. if scautopick reads its bindings, it sets allowGlobal to
|
||||
* false. That means, even if a global binding for a station
|
||||
* exists, it won't be read unless an scautopick binding exists
|
||||
* as well.
|
||||
* @return true if everything went smoothly, false otherwise.
|
||||
*/
|
||||
bool init(const DataModel::ConfigModule *mod,
|
||||
const std::string &setupName,
|
||||
bool allowGlobal);
|
||||
|
||||
/**
|
||||
* @brief Returns the associated parameter list for a station.
|
||||
* @param networkCode The stations network code
|
||||
* @param stationCode The station code
|
||||
* @return A pointer to the parameter list if available, nullptr otherwise.
|
||||
*/
|
||||
const KeyValues *getKeys(const std::string &networkCode,
|
||||
const std::string &stationCode) const;
|
||||
|
||||
/**
|
||||
* @brief Convenience function for getKeys(netCode, staCode)
|
||||
* @param station The station object
|
||||
* @return A pointer to the parameter list if available, nullptr otherwise.
|
||||
*/
|
||||
const KeyValues *getKeys(const DataModel::Station *station) const;
|
||||
|
||||
/**
|
||||
* @brief Removes a binding for a station.
|
||||
* @param networkCode The network code of the station
|
||||
* @param stationCode The code of the station
|
||||
* @return true if removed, false otherwise
|
||||
*/
|
||||
bool remove(const std::string &networkCode,
|
||||
const std::string &stationCode);
|
||||
|
||||
/**
|
||||
* @brief Convenience function that takes a station object.
|
||||
* @param station The station object
|
||||
* @return true if removed, false otherwise
|
||||
*/
|
||||
bool remove(const DataModel::Station *station);
|
||||
|
||||
/**
|
||||
* @brief Binds a user defined object to a station. That can
|
||||
* be used for application specific extensions.
|
||||
* @param networkCode The network code
|
||||
* @param stationCode The station code
|
||||
* @param value The MetaValue to be bound.
|
||||
* @return True if station is bound already, false otherwise.
|
||||
*/
|
||||
bool setData(const std::string &networkCode,
|
||||
const std::string &stationCode,
|
||||
const Core::MetaValue &value);
|
||||
|
||||
/**
|
||||
* @brief Convenience function that takes a station object.
|
||||
* @param station The station the value should be associated with.
|
||||
* @param value The MetaValue to be bound.
|
||||
* @return True if station is bound already, false otherwise.
|
||||
*/
|
||||
bool setData(const DataModel::Station *station,
|
||||
const Core::MetaValue &value);
|
||||
|
||||
/**
|
||||
* @brief Returns the associated user data of a station.
|
||||
* @param networkCode The network code
|
||||
* @param stationCode The station code
|
||||
* @return The associated meta value if found, an empty meta value
|
||||
* otherwise.
|
||||
*/
|
||||
const Core::MetaValue &data(const std::string &networkCode,
|
||||
const std::string &stationCode);
|
||||
|
||||
/**
|
||||
* @brief Convenience function that takes a station object.
|
||||
* @param station The network code
|
||||
* @return The associated meta value if found, an empty meta value
|
||||
* otherwise.
|
||||
*/
|
||||
const Core::MetaValue &data(const DataModel::Station *station);
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the first element of the bindings.
|
||||
* If the bindings are empty, the returned iterator will be
|
||||
* equal to end.
|
||||
* @return The iterator pointing to the first element or end().
|
||||
*/
|
||||
const_iterator begin() const;
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the element following the last
|
||||
* element of the bindings. Attempting to acces it results
|
||||
* in undefined behaviour.
|
||||
* @return The iterator pointing past the last element.
|
||||
*/
|
||||
const_iterator end() const;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private members
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
NetworkMap _bindings;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
260
include/seiscomp/utils/certstore.h
Normal file
260
include/seiscomp/utils/certstore.h
Normal file
@ -0,0 +1,260 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_CERTIFICATESTORE_H
|
||||
#define SEISCOMP_UTILS_CERTIFICATESTORE_H
|
||||
|
||||
|
||||
#include <seiscomp/core.h>
|
||||
#include <seiscomp/core/baseobject.h>
|
||||
#include <seiscomp/core/datetime.h>
|
||||
|
||||
#include <openssl/x509.h>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
DEFINE_SMARTPOINTER(CertificateContext);
|
||||
|
||||
/**
|
||||
* @brief An agency related certificate context.
|
||||
* This class manages information for a particular subject hash.
|
||||
* It saves the last accessed certificate for caching, the list of available
|
||||
* certificates and the certificate revocation list.
|
||||
*
|
||||
* It provides functions to lookup certificates based on a reference time
|
||||
* or a given digest and a reference signature.
|
||||
*
|
||||
* While looking up certificates the revocation list is considered as well.
|
||||
* Revoked certificates will be ignored.
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API CertificateContext : public Core::BaseObject {
|
||||
// ----------------------------------------------------------------------
|
||||
// X'truction
|
||||
// ----------------------------------------------------------------------
|
||||
protected:
|
||||
//! C'tor
|
||||
CertificateContext();
|
||||
|
||||
public:
|
||||
//! D'tor
|
||||
~CertificateContext();
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief Returns a certficate valid for a given reference time.
|
||||
* @param referenceTime The reference time
|
||||
* @return An X509 certificate or null
|
||||
*/
|
||||
const X509 *findCertificate(const Core::Time &referenceTime) const;
|
||||
|
||||
/**
|
||||
* @brief Returns a certificate by signing the digest and comparing it
|
||||
* against the reference signature.
|
||||
* @param digest Address pointing to the digest
|
||||
* @param nDigest Number of digest bytes
|
||||
* @param signature OpenSSL ECDSA signature
|
||||
* @return A certficate or null
|
||||
*/
|
||||
const X509 *findCertificate(const char *digest, size_t nDigest,
|
||||
const ECDSA_SIG *signature) const;
|
||||
|
||||
// /**
|
||||
// * @brief Checks if an certificate has been revoked
|
||||
// * Omit the reference time to disable the time check.
|
||||
// * @param cert The X509 certificate
|
||||
// * @param referenceTime The reference time
|
||||
// * @return
|
||||
// */
|
||||
// bool isRevoked(const X509 *cert,
|
||||
// const Core::Time &referenceTime = Core::Time()) const;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Members
|
||||
// ----------------------------------------------------------------------
|
||||
protected:
|
||||
typedef std::map<std::string, X509*> Certs;
|
||||
typedef std::map<std::string, X509_CRL*> CRLs;
|
||||
|
||||
// Cache
|
||||
mutable X509 *_cert;
|
||||
mutable const ASN1_TIME *_begin;
|
||||
mutable const ASN1_TIME *_end;
|
||||
|
||||
// Certificates and revocation list
|
||||
Certs _certs;
|
||||
CRLs _crls;
|
||||
|
||||
friend class CertificateStore;
|
||||
};
|
||||
|
||||
|
||||
DEFINE_SMARTPOINTER(CertificateStore);
|
||||
|
||||
/**
|
||||
* @brief An OpenSSL certificate store.
|
||||
*
|
||||
* @code
|
||||
* CertificateStore store;
|
||||
* if ( !store.init("/path/to/store") ) {
|
||||
* exit(1);
|
||||
* }
|
||||
*
|
||||
* // Get context for authority "gempa"
|
||||
* const CertificateContext *ctx = store.getContext("792241d4");
|
||||
* if ( !ctx ) {
|
||||
* exit(1);
|
||||
* }
|
||||
*
|
||||
* const X509 *cert = ctx.findCerticiate(Core::Time::GMT());
|
||||
* if ( !cert ) {
|
||||
* exit(1);
|
||||
* }
|
||||
*
|
||||
* // Do something with the certificate
|
||||
* @endcode
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API CertificateStore : public Core::BaseObject {
|
||||
// ----------------------------------------------------------------------
|
||||
// X'truction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
//! C'tor
|
||||
CertificateStore();
|
||||
//! D'tor
|
||||
~CertificateStore();
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
/**
|
||||
* @brief Returns the global certificate store.
|
||||
* The global store is going to be used by components that do not
|
||||
* accept a certificate store as part of their interface.
|
||||
* @return The certificate store reference.
|
||||
*/
|
||||
static CertificateStore &global();
|
||||
|
||||
//! Initializes the store and opens the passed directory.
|
||||
bool init(const std::string &baseDirectory);
|
||||
|
||||
/**
|
||||
* @brief Checks if the certificate store is valid
|
||||
* @return True, if the store is valid
|
||||
*/
|
||||
bool isValid() const;
|
||||
|
||||
/**
|
||||
* @brief Returns a certificate context for a given authority.
|
||||
* @param hash Pointer to OpenSSL X509 name hash
|
||||
* @param len Number of bytes of authority string
|
||||
* @return The context or null.
|
||||
*/
|
||||
const CertificateContext *getContext(const char *hash, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Returns a certificate context for a given authority.
|
||||
* @param hash The given OpenSSL X509 name hash
|
||||
* @return The context or null.
|
||||
*/
|
||||
const CertificateContext *getContext(const std::string &hash);
|
||||
|
||||
/**
|
||||
* @brief Validates an ECDSA signature against the certificates in
|
||||
* the store.
|
||||
* @param authority The authority
|
||||
* @param len Then length of the authority string in bytes
|
||||
* @param digest The digest block
|
||||
* @param nDigest The length of the digest block in bytes
|
||||
* @param signature The ECDSA signature to check against
|
||||
* @param matchedCertificate The matched certificate if requested
|
||||
* @return Success flag
|
||||
*/
|
||||
bool validate(const char *authority, size_t len,
|
||||
const char *digest, size_t nDigest,
|
||||
const ECDSA_SIG *signature,
|
||||
const X509 **matchedCertificate = 0);
|
||||
|
||||
bool validate(const std::string &authority,
|
||||
const char *digest, size_t nDigest,
|
||||
const ECDSA_SIG *signature,
|
||||
const X509 **matchedCertificate = 0);
|
||||
|
||||
/**
|
||||
* @brief Loads certificates for a specific hash from directory
|
||||
* @param List in which matching certs should be added to
|
||||
* @param hash The given OpenSSL X509 name hash
|
||||
* @param baseDirectory The base directory used for the
|
||||
* recursive search
|
||||
* @return True on success.
|
||||
*/
|
||||
bool loadCerts(CertificateContext::Certs &certs, const std::string &hash,
|
||||
const std::string &baseDirectory);
|
||||
|
||||
/**
|
||||
* @brief Loads crls for a specific hash from directory
|
||||
* @param crls List in which matching crls should be added to
|
||||
* @param hash The given OpenSSL X509 name hash
|
||||
* @param baseDirectory The base directory used for the
|
||||
* recursive search
|
||||
* @return True on success
|
||||
*/
|
||||
bool loadCRLs(CertificateContext::CRLs &crls, const std::string &hash,
|
||||
const std::string &baseDirectory);
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Members
|
||||
// ----------------------------------------------------------------------
|
||||
protected:
|
||||
typedef std::map<std::string, CertificateContextPtr> Lookup;
|
||||
static CertificateStore _global;
|
||||
Lookup _lookup;
|
||||
std::mutex _storeMutex;
|
||||
std::string _baseDirectory;
|
||||
};
|
||||
|
||||
|
||||
inline CertificateStore &CertificateStore:: global() {
|
||||
return _global;
|
||||
}
|
||||
|
||||
inline bool CertificateStore::isValid() const {
|
||||
return !_baseDirectory.empty();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
68
include/seiscomp/utils/datetime.h
Normal file
68
include/seiscomp/utils/datetime.h
Normal file
@ -0,0 +1,68 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_DATETIME_H
|
||||
#define SEISCOMP_UTILS_DATETIME_H
|
||||
|
||||
|
||||
#include <seiscomp/core/datetime.h>
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of seconds passed since the
|
||||
* last day of a given time has been started.
|
||||
* @param time The given time
|
||||
* @return The number of seconds including microseconds
|
||||
*/
|
||||
SC_SYSTEM_CORE_API double timeOfDay(const Seiscomp::Core::Time& time);
|
||||
|
||||
/**
|
||||
* Returns the number of seconds passed since the current
|
||||
* day has been started.
|
||||
* @return The number of seconds including microseconds
|
||||
*/
|
||||
SC_SYSTEM_CORE_API double timeOfDay();
|
||||
|
||||
|
||||
struct TimeVal {
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
int hour;
|
||||
int minute;
|
||||
int second;
|
||||
int usec;
|
||||
TimeVal() :
|
||||
year(0), month(0), day(0), hour(0), minute(0), second(), usec(0) {
|
||||
}
|
||||
};
|
||||
|
||||
SC_SYSTEM_CORE_API bool getTime(const Core::Time& time, TimeVal& tv);
|
||||
|
||||
SC_SYSTEM_CORE_API void setTime(Core::Time& time, const TimeVal& tv);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
77
include/seiscomp/utils/files.h
Normal file
77
include/seiscomp/utils/files.h
Normal file
@ -0,0 +1,77 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SEISCOMP_UTILS_FILES
|
||||
#define SEISCOMP_UTILS_FILES
|
||||
|
||||
#include <string>
|
||||
#include <streambuf>
|
||||
#include <iostream>
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
/**
|
||||
* Removes the path portion from a given path:
|
||||
* /dir/file.cpp -> file.cpp
|
||||
* @return basename */
|
||||
SC_SYSTEM_CORE_API std::string basename(const std::string& name);
|
||||
|
||||
//! Checks if a file is exists
|
||||
SC_SYSTEM_CORE_API bool fileExists(const std::string& file);
|
||||
|
||||
//! Checks if a path (directory) exists
|
||||
SC_SYSTEM_CORE_API bool pathExists(const std::string& path);
|
||||
|
||||
//! Creates a new directory inclusive all unavailable
|
||||
//! parent directories
|
||||
SC_SYSTEM_CORE_API bool createPath(const std::string& path);
|
||||
|
||||
/** Removes the extension from the filename
|
||||
* test.xyz -> test
|
||||
* @return name without extension */
|
||||
SC_SYSTEM_CORE_API std::string removeExtension(const std::string& name);
|
||||
|
||||
/**
|
||||
* Converts a string or char array to a streambuf object.
|
||||
* @return The streambuf object. The caller is responsible to delete the object
|
||||
*/
|
||||
SC_SYSTEM_CORE_API std::streambuf *bytesToStreambuf(char *data, size_t n);
|
||||
SC_SYSTEM_CORE_API std::streambuf *stringToStreambuf(const std::string &str);
|
||||
|
||||
/**
|
||||
* Tries to open a file and returns a corresponding output stream.
|
||||
* The special name '-' refers to stdout.
|
||||
* @return The ostream object. The caller is responsible to delete the object
|
||||
*/
|
||||
SC_SYSTEM_CORE_API std::ostream *file2ostream(const char *fn);
|
||||
|
||||
/**
|
||||
* Tries to open a file and returns a corresponding input stream.
|
||||
* The special name '-' refers to stdin.
|
||||
* @return The istream object. The caller is responsible to delete the object
|
||||
*/
|
||||
SC_SYSTEM_CORE_API std::istream *file2istream(const char *fn);
|
||||
|
||||
|
||||
} // namespace Util
|
||||
} // namespace Seiscomp
|
||||
|
||||
#endif
|
192
include/seiscomp/utils/keyvalues.h
Normal file
192
include/seiscomp/utils/keyvalues.h
Normal file
@ -0,0 +1,192 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_KEYVALUES_H
|
||||
#define SEISCOMP_UTILS_KEYVALUES_H
|
||||
|
||||
|
||||
#include <seiscomp/core/baseobject.h>
|
||||
#include <seiscomp/datamodel/parameterset.h>
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
DEFINE_SMARTPOINTER(KeyValues);
|
||||
|
||||
/**
|
||||
* @brief The KeyValues class manages a list of key value pairs
|
||||
*
|
||||
* Key value pairs are saved as strings and convenience functions exist
|
||||
* to convert to and from integer, doubles and bools.
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API KeyValues : public Core::BaseObject {
|
||||
// ----------------------------------------------------------------------
|
||||
// Public types
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
typedef std::map<std::string, std::string> NameValueMap;
|
||||
typedef NameValueMap::const_iterator iterator;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// X'truction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
KeyValues();
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
/**
|
||||
* @brief Returns an iterator to the first element in the container.
|
||||
* If the container is empty, the returned iterator will be
|
||||
* equal to end.
|
||||
* @return An iterator
|
||||
*/
|
||||
iterator begin() const;
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the element following the last
|
||||
* element of the container. Attempting to acces it results
|
||||
* in undefined behaviour.
|
||||
* @return The iterator pointing past the last element.
|
||||
*/
|
||||
iterator end() const;
|
||||
|
||||
/**
|
||||
* @brief Sets parameter _name_ to _value_.
|
||||
*
|
||||
* If no parameter with _name_ exists it is created. Otherwise it is
|
||||
* overwritten by _value_.
|
||||
* @param name The parameters name
|
||||
* @param value The parameters value
|
||||
* @return Always true
|
||||
*/
|
||||
bool setString(const std::string &name, const std::string &value);
|
||||
|
||||
/**
|
||||
* @brief Sets parameter _name_ to _value_.
|
||||
*
|
||||
* If no parameter with _name_ exists it is created. Otherwise it is
|
||||
* overwritten by _value_.
|
||||
* @param name The parameters name
|
||||
* @param value The parameters value that is being converted to string.
|
||||
* @return Always true
|
||||
*/
|
||||
bool setInt(const std::string &name, int value);
|
||||
|
||||
/**
|
||||
* @brief Sets parameter _name_ to _value_.
|
||||
*
|
||||
* If no parameter with _name_ exists it is created. Otherwise it is
|
||||
* overwritten by _value_.
|
||||
* @param name The parameters name
|
||||
* @param value The parameters value that is being converted to string.
|
||||
* @return Always true
|
||||
*/
|
||||
bool setDouble(const std::string &name, double value);
|
||||
|
||||
/**
|
||||
* @brief Sets parameter _name_ to _value_.
|
||||
*
|
||||
* If no parameter with _name_ exists it is created. Otherwise it is
|
||||
* overwritten by _value_.
|
||||
* @param name The parameters name
|
||||
* @param value The parameters value that is being converted to string.
|
||||
* @return Always true
|
||||
*/
|
||||
bool setBool(const std::string &name, bool value);
|
||||
|
||||
/**
|
||||
* @brief Unsets the parameter _name_ and removes it from the list.
|
||||
*
|
||||
* Post condition: get(tmp, "_name_") == false
|
||||
* @param name The parameters name
|
||||
* @return True if it has been unset, false if the parameter does not exist.
|
||||
*/
|
||||
bool unset(const std::string &name);
|
||||
|
||||
/**
|
||||
* @brief Retrieves the string value of parameter _name_.
|
||||
* @param value The output value. It is only touched if true is returned.
|
||||
* @param name The parameters name
|
||||
* @return False if the parameter does not exist, true otherwise
|
||||
*/
|
||||
bool getString(std::string &value, const std::string &name) const;
|
||||
|
||||
/**
|
||||
* @brief Retrieves the int value of parameter _name_.
|
||||
* @param value The output value. It is only touched if true is returned.
|
||||
* @param name The parameters name
|
||||
* @return False if the parameter does not exist or if it could not be
|
||||
* converted into an integer, true otherwise
|
||||
*/
|
||||
bool getInt(int &value, const std::string &name) const;
|
||||
|
||||
/**
|
||||
* @brief Retrieves the double value of parameter _name_.
|
||||
* @param value The output value. It is only touched if true is returned.
|
||||
* @param name The parameters name
|
||||
* @return False if the parameter does not exist or if it could not be
|
||||
* converted into a double, true otherwise
|
||||
*/
|
||||
bool getDouble(double &value, const std::string &name) const;
|
||||
|
||||
/**
|
||||
* @brief Retrieves the bool value of parameter _name_.
|
||||
* @param value The output value. It is only touched if true is returned.
|
||||
* @param name The parameters name
|
||||
* @return False if the parameter does not exist or if it could not be
|
||||
* converted into a bool, true otherwise
|
||||
*/
|
||||
bool getBool(bool &value, const std::string &name) const;
|
||||
|
||||
/**
|
||||
* @brief Reads all parameters from a ParameterSet recursively.
|
||||
*
|
||||
* Each ParameterSet can refer to a base parameter set which is being
|
||||
* read in bottom top order. First the base parameter set is being read
|
||||
* and then the current. As a result, parameters of the input parameter
|
||||
* set have higher priority than the parameters of the base parameter set.
|
||||
* @param ps The ParameterSet
|
||||
*/
|
||||
void init(DataModel::ParameterSet *ps);
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Members
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
NameValueMap _nameValueMap;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
270
include/seiscomp/utils/leparser.h
Normal file
270
include/seiscomp/utils/leparser.h
Normal file
@ -0,0 +1,270 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SEISCOMP_UTILSLEPARSER_H
|
||||
#define SEISCOMP_UTILSLEPARSER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stack>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Utils {
|
||||
|
||||
|
||||
|
||||
/** LeExpression can be used as baseclass for custom expression classes,
|
||||
* if a unified error handling mechanism is needed. Note: This is optional
|
||||
* as the LeClasses also work with classes not derived from LeExpression */
|
||||
class SC_SYSTEM_CORE_API LeExpression {
|
||||
protected:
|
||||
LeExpression() : _error(false) {}
|
||||
virtual ~LeExpression() {}
|
||||
|
||||
public:
|
||||
std::string what(bool clear = true) {
|
||||
if (clear) {
|
||||
std::string str = _ss.str();
|
||||
clearError();
|
||||
return str;
|
||||
}
|
||||
return _ss.str();
|
||||
}
|
||||
|
||||
bool error() const {
|
||||
return _error;
|
||||
}
|
||||
|
||||
void clearError() {
|
||||
_ss.str(std::string());
|
||||
_error = false;
|
||||
}
|
||||
|
||||
protected:
|
||||
void append(const std::string& str) {
|
||||
if (_ss.str().size() > 0)
|
||||
_ss << "\n";
|
||||
_ss << str;
|
||||
if (!_error)
|
||||
_error = !_error;
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostringstream _ss;
|
||||
bool _error;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/** BinaryOperator implements AND and OR for a given Expression */
|
||||
template <typename Expression>
|
||||
class BinaryOperator : public Expression {
|
||||
public:
|
||||
BinaryOperator() : _lhs(nullptr), _rhs(nullptr) {}
|
||||
|
||||
void setOperants(Expression* lhs, Expression* rhs) {
|
||||
_lhs = lhs;
|
||||
_rhs = rhs;
|
||||
}
|
||||
|
||||
virtual ~BinaryOperator() {
|
||||
if (_lhs) delete _lhs;
|
||||
if (_rhs) delete _rhs;
|
||||
}
|
||||
|
||||
private:
|
||||
Expression* _lhs;
|
||||
Expression* _rhs;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/** UnaryOperator implements NOT for a given Expression */
|
||||
template <typename Expression>
|
||||
class UnaryOperator : public Expression {
|
||||
public:
|
||||
UnaryOperator() : _rhs(nullptr) {}
|
||||
|
||||
void setOperant(Expression* rhs) {
|
||||
_rhs = rhs;
|
||||
}
|
||||
|
||||
virtual ~UnaryOperator() {
|
||||
if (_rhs) delete _rhs;
|
||||
}
|
||||
|
||||
private:
|
||||
Expression* _rhs;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/** ExpressionFactoryInterface is a abstract class that needs to be
|
||||
* implemented by the factory passed to LeParser */
|
||||
template <typename Expression>
|
||||
class ExpressionFactoryInterface {
|
||||
|
||||
public:
|
||||
virtual ~ExpressionFactoryInterface () {}
|
||||
|
||||
virtual Expression* createAndExpression() = 0;
|
||||
virtual Expression* createOrExpression() = 0;
|
||||
virtual Expression* createNotExpression() = 0;
|
||||
virtual Expression* createExpression(const std::string& name) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/** Types used by the Le classes */
|
||||
struct SC_SYSTEM_CORE_API LeParserTypes {
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Nested types
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
enum Operator { AND = 0x0, OR, NOT, POPEN, PCLOSE, OperatorCount };
|
||||
typedef std::vector<std::string> Tokens;
|
||||
|
||||
LeParserTypes() {
|
||||
OperatorMap.insert(std::make_pair(AND, "&"));
|
||||
OperatorMap.insert(std::make_pair(OR, "|"));
|
||||
OperatorMap.insert(std::make_pair(NOT, "!"));
|
||||
OperatorMap.insert(std::make_pair(POPEN, "("));
|
||||
OperatorMap.insert(std::make_pair(PCLOSE, ")"));
|
||||
}
|
||||
|
||||
static std::map<Operator, std::string> OperatorMap;
|
||||
static LeParserTypes __parserTypes__;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/** Tokenizes strings representing logical expressions of the form a & b & !(c | d) ...
|
||||
* The result is a vector containing the operands and terminals.
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API LeTokenizer {
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// X'struction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
LeTokenizer(const std::string& expr);
|
||||
~LeTokenizer() {}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
const LeParserTypes::Tokens& tokens() const;
|
||||
bool error() const;
|
||||
const std::string& what() const;
|
||||
bool tokenize(const std::string& expr);
|
||||
bool tokenize();
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private interface
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
void init();
|
||||
void pushToken(std::string& token);
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private data member
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
std::string _expr;
|
||||
LeParserTypes::Tokens _tokens;
|
||||
|
||||
bool _error;
|
||||
std::string _errorStr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/** Parses the given tokens from LeTokenizer and creates a logical expression */
|
||||
template <typename Expression>
|
||||
class LeParser {
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// X'struction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
LeParser(
|
||||
const LeParserTypes::Tokens& tokens,
|
||||
ExpressionFactoryInterface<Expression>* factory);
|
||||
~LeParser();
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
Expression* parse();
|
||||
bool error() const;
|
||||
const char* what() const;
|
||||
Expression* expression() const;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private interface
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
void init();
|
||||
void applyBinaryOperator();
|
||||
void applyUnaryOperator();
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private data members
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
bool _error;
|
||||
std::string _errorStr;
|
||||
|
||||
const LeParserTypes::Tokens& _tokens;
|
||||
LeParserTypes::Tokens::const_iterator _it;
|
||||
ExpressionFactoryInterface<Expression>* _factory;
|
||||
|
||||
std::stack<Expression*> _stack;
|
||||
std::stack<LeParserTypes::Operator> _modeStack;
|
||||
};
|
||||
|
||||
#include "leparser.ipp"
|
||||
|
||||
|
||||
} // namespace Utils
|
||||
} // namespace Seiscomp
|
||||
|
||||
|
||||
#endif
|
97
include/seiscomp/utils/misc.h
Normal file
97
include/seiscomp/utils/misc.h
Normal file
@ -0,0 +1,97 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SEISCOMP_UTILS_MISC
|
||||
#define SEISCOMP_UTILS_MISC
|
||||
|
||||
#include <string>
|
||||
#include <seiscomp/core.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
SC_SYSTEM_CORE_API char getShortPhaseName(const std::string &phase);
|
||||
|
||||
template <class T, class A>
|
||||
T join(const A &begin, const A &end, const T &glue);
|
||||
|
||||
template <typename T>
|
||||
void toHex(std::string &out, T v);
|
||||
|
||||
template <typename R, class... Types>
|
||||
/**
|
||||
* @brief A wrapper around a function that returns a results and throws
|
||||
* an exception in case of an error.
|
||||
*
|
||||
* This wrapper instead returns true if no exception has been thrown and false
|
||||
* otherwise. The result is passed as reference as 3rd parameter in \p ret.
|
||||
*
|
||||
* @code
|
||||
* int getSomeAttribute(const Object *obj) {
|
||||
* if ( !obj->attribute() ) {
|
||||
* throw runtime_error("Attribute is not set");
|
||||
* }
|
||||
* return obj->attribute();
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* To use this function it should be wrapped in catch-except
|
||||
* and the return value must be stored somewhere, which might
|
||||
* be inconvenient.
|
||||
*
|
||||
* @code
|
||||
* cout << "Attribute is ";
|
||||
*
|
||||
* int attribute;
|
||||
*
|
||||
* if ( catchBool(getSomeAttribute, attribute, obj) ) {
|
||||
* cout << attribute
|
||||
* }
|
||||
* else {
|
||||
* cout << "not set";
|
||||
* }
|
||||
*
|
||||
* cout << endl;
|
||||
* @endcode
|
||||
*
|
||||
* Which version is preferred is up to the user. With this wrapper both
|
||||
* versions are possible.
|
||||
*
|
||||
* @param[in] func The function to be wrapped.
|
||||
* @param[out] ret The return value of the function is stored here.
|
||||
* @param[in] args
|
||||
* @return True if no exception was thrown the the input function,
|
||||
* false otherwise.
|
||||
*/
|
||||
bool catchBool(R func(Types...), R &ret, Types... args);
|
||||
|
||||
extern char HEXCHARS[];
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#include <seiscomp/utils/misc.ipp>
|
||||
|
||||
|
||||
#endif
|
93
include/seiscomp/utils/replace.h
Normal file
93
include/seiscomp/utils/replace.h
Normal file
@ -0,0 +1,93 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_REPLACE_H
|
||||
#define SEISCOMP_UTILS_REPLACE_H
|
||||
|
||||
#include <string>
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
/**
|
||||
* Functor structur to resolve a variable and return
|
||||
* the content of it.
|
||||
*/
|
||||
struct SC_SYSTEM_CORE_API VariableResolver {
|
||||
/**
|
||||
* Resolves a variable. The default implementation replaces:
|
||||
* - hostname: The name of the host (uname -n)
|
||||
* - user: The name of the user (echo $USER)
|
||||
* - more to come...
|
||||
* When inheriting this class, call this method in the
|
||||
* new implementation to enable the build-in variables if this
|
||||
* behaviour in intended.
|
||||
* @param variable The variable name that has to be resolved. The result
|
||||
* will be written into it as well (inout).
|
||||
* @return Whether the variable could be resolved or not
|
||||
*/
|
||||
virtual bool resolve(std::string& variable) const;
|
||||
virtual ~VariableResolver() {};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Replaces variables of a string by their content.
|
||||
* Variables have to be enclosed by '@'. Two '@' will be
|
||||
* replaced by one '@'.
|
||||
* Replacing the variable "host" results when feeding the
|
||||
* input string "hostname: @host@".
|
||||
* This function used the VariableResolver class and replaces
|
||||
* beside the default variables:
|
||||
* - [empty] => '@'
|
||||
* @param input The string containing variables to be replaced
|
||||
* @return The replaced input string
|
||||
*/
|
||||
SC_SYSTEM_CORE_API
|
||||
std::string replace(const std::string& input);
|
||||
|
||||
|
||||
/**
|
||||
* See: string replace(string)
|
||||
* @param input The string containing variables to be replaced
|
||||
* @param resolver A resolver functor that resolves the variable name
|
||||
* to its content.
|
||||
* @return The replaced input string
|
||||
*/
|
||||
SC_SYSTEM_CORE_API
|
||||
std::string replace(const std::string& input,
|
||||
const VariableResolver& resolver);
|
||||
|
||||
SC_SYSTEM_CORE_API
|
||||
std::string replace(const char* input,
|
||||
const VariableResolver& resolver);
|
||||
|
||||
SC_SYSTEM_CORE_API
|
||||
std::string replace(const std::string &input,
|
||||
const VariableResolver &resolver,
|
||||
const std::string &prefix, const std::string &postfix,
|
||||
const std::string &emptyValue);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
142
include/seiscomp/utils/stringfirewall.h
Normal file
142
include/seiscomp/utils/stringfirewall.h
Normal file
@ -0,0 +1,142 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_STRINGFIREWALL_H
|
||||
#define SEISCOMP_UTILS_STRINGFIREWALL_H
|
||||
|
||||
|
||||
#include <seiscomp/core/baseobject.h>
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
DEFINE_SMARTPOINTER(StringFirewall);
|
||||
|
||||
|
||||
/**
|
||||
* @brief The StringFirewall class implements a "firewall" for strings.
|
||||
*
|
||||
* It manages a blacklist and a whitelist of strings. Whether a string is
|
||||
* accepted is checked with the following algorithm:
|
||||
* - Accept if both lists are empty
|
||||
* - If either list is empty the partial result (accepted1 or accepted2) is true
|
||||
* - If the input string matches any item in the whitelist, accepted1 is true,
|
||||
* false otherwise
|
||||
* - If the input string matches any item in the blacklist, accepted2 is false,
|
||||
* true otherwise
|
||||
* - The result is accepted1 && accepted2
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API StringFirewall : public Core::BaseObject {
|
||||
// ----------------------------------------------------------------------
|
||||
// Public typedefs and variables
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
typedef std::set<std::string> StringSet;
|
||||
|
||||
StringSet allow; //!< The whitelist
|
||||
StringSet deny; //!< The blacklist
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
/**
|
||||
* @brief isAllowed evaluates if a string may pass the firewall.
|
||||
* @param s The input string
|
||||
* @return true if it may pass, false otherwise
|
||||
*/
|
||||
virtual bool isAllowed(const std::string &s) const;
|
||||
|
||||
/**
|
||||
* @brief isDenied evaluates if a string is blocked by the firewall.
|
||||
*
|
||||
* It holds that isDenied(s) == !isAllowed(s).
|
||||
*
|
||||
* @param s The input string
|
||||
* @return true if it is blocked, false otherwise
|
||||
*/
|
||||
bool isDenied(const std::string &s) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief The WildcardStringFirewall class extends the StringFirewall by
|
||||
* allowing wildcard ('*' or '?') matches for strings.
|
||||
*
|
||||
* Each call to either isAllowed or isDenied is cached by default to reduce
|
||||
* evaluation time for repeating queries. If queries do not repeat or are
|
||||
* more or less random then caching should be disabled.
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API WildcardStringFirewall : public StringFirewall {
|
||||
// ----------------------------------------------------------------------
|
||||
// X'truction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
//! C'tor
|
||||
WildcardStringFirewall();
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
/**
|
||||
* Clears the internal evaluation cache. This should be called when
|
||||
* either #allow or #deny have changed.
|
||||
*/
|
||||
void clearCache() const;
|
||||
|
||||
/**
|
||||
* @brief Enables query caching. The default is true.
|
||||
* @param enable Whether to cache queries.
|
||||
*/
|
||||
void setCachingEnabled(bool enable);
|
||||
|
||||
/**
|
||||
* @brief isAllowed evaluates if a string may pass the firewall.
|
||||
* @param s The input string
|
||||
* @return true if it may pass, false otherwise
|
||||
*/
|
||||
virtual bool isAllowed(const std::string &s) const;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private members and typedefs
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
typedef std::map<std::string, bool> StringPassMap;
|
||||
mutable StringPassMap _cache;
|
||||
bool _enableCaching;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
124
include/seiscomp/utils/tabvalues.h
Normal file
124
include/seiscomp/utils/tabvalues.h
Normal file
@ -0,0 +1,124 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_TABVALUES_H
|
||||
#define SEISCOMP_UTILS_TABVALUES_H
|
||||
|
||||
|
||||
#include <seiscomp/core/baseobject.h>
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
DEFINE_SMARTPOINTER(TabValues);
|
||||
|
||||
/**
|
||||
* @brief The TabValues class manages a table of values which can be
|
||||
* extracted with interpolation.
|
||||
*
|
||||
* A table consists of an X and a Y axis each with a set of given nodes.
|
||||
* The table format on disc is compatible to the phase tables of LOCSAT.
|
||||
*
|
||||
* \code
|
||||
* n # Something
|
||||
* yn # Number of y samples
|
||||
* y1, y2, y3, ..., yn
|
||||
* xn # Number of x samples
|
||||
* x1, x2, x3, ..., xn
|
||||
* xn samples for y1
|
||||
* xn samples for y2
|
||||
* ...
|
||||
* xn samples for yn
|
||||
* \endcode
|
||||
*
|
||||
* Samples are separated with spaces.
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API TabValues : public Core::BaseObject {
|
||||
// ----------------------------------------------------------------------
|
||||
// Public types
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
using ValueList = std::vector<double>;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// X'truction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
TabValues();
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Reads a value table in LOCSAT format.
|
||||
* @param filename The path to the file to be read.
|
||||
* @return Success flag
|
||||
*/
|
||||
bool read(const std::string &filename);
|
||||
|
||||
bool validValue(size_t ix, size_t iy) const;
|
||||
double value(size_t ix, size_t iy) const;
|
||||
|
||||
bool interpolate(double &interpolatedValue,
|
||||
bool extrapolation, bool inHole, bool returnYDerivs,
|
||||
double xPos, double yPos,
|
||||
double *x1stDeriv = 0, double *y1stDeriv = 0,
|
||||
double *x2ndDeriv = 0, double *y2ndDeriv = 0,
|
||||
int *error = 0) const;
|
||||
|
||||
bool interpolate(double &interpolatedValue,
|
||||
bool extrapolation, bool returnYDerivs,
|
||||
double xPos, double yPos,
|
||||
double *x1stDeriv = 0, double *y1stDeriv = 0,
|
||||
double *x2ndDeriv = 0, double *y2ndDeriv = 0,
|
||||
int *error = 0) const;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public members
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
// X,Y value range
|
||||
std::string header;
|
||||
ValueList x, y;
|
||||
OPT(double) lowerUndefinedX, upperUndefinedX;
|
||||
std::vector<ValueList> values; // y-major
|
||||
};
|
||||
|
||||
|
||||
inline bool TabValues::validValue(size_t ix, size_t iy) const {
|
||||
return values[iy][ix] != -1;
|
||||
}
|
||||
|
||||
inline double TabValues::value(size_t ix, size_t iy) const {
|
||||
return values[iy][ix];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
214
include/seiscomp/utils/timer.h
Normal file
214
include/seiscomp/utils/timer.h
Normal file
@ -0,0 +1,214 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_TIMER_H
|
||||
#define SEISCOMP_UTILS_TIMER_H
|
||||
|
||||
|
||||
#include <seiscomp/core/datetime.h>
|
||||
#include <seiscomp/core/optional.h>
|
||||
#include <seiscomp/core/platform/platform.h>
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
#if defined(SC_HAS_TIMER_CREATE)
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#else
|
||||
#include <list>
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
/** \brief A stopwatch to measure a timespan
|
||||
* The stopwatch substracts the timestamps at the time of a
|
||||
* call to StopWatch::elapsed and a previous call to Timer::restart
|
||||
* or the instantiation of a StopWatch object.
|
||||
* \code
|
||||
* StopWatch aStopWatch;
|
||||
* // do some calculations
|
||||
* cout << aStopWatch.elapsed() << " seconds later" << endl;
|
||||
* \endcode
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API StopWatch {
|
||||
public:
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
StopWatch();
|
||||
|
||||
//! Constructor
|
||||
StopWatch(bool autorun);
|
||||
|
||||
public:
|
||||
//! Restarts the timer, isActive() == true.
|
||||
void restart();
|
||||
|
||||
//! Resets the timer, isActive() == false.
|
||||
void reset();
|
||||
|
||||
//! Returns whether the timer is active.
|
||||
bool isActive() const;
|
||||
|
||||
//! Returns the elapsed time as TimeSpan.
|
||||
Seiscomp::Core::TimeSpan elapsed() const;
|
||||
|
||||
//! Returns the elapsed microseconds or 0 if not active.
|
||||
uint64_t microseconds() const;
|
||||
|
||||
//! Returns the elapsed nanoseconds or 0 if not active.
|
||||
uint64_t nanoseconds() const;
|
||||
|
||||
private:
|
||||
OPT(Clock::time_point) _start;
|
||||
};
|
||||
|
||||
|
||||
/** \brief A timer class that provides repetitive and single-shot timers.
|
||||
* This class provides an interface for timers.
|
||||
* Usage:
|
||||
* \code
|
||||
* Timer timer;
|
||||
* timer.setTimeout(5);
|
||||
* timer.setCallback(myCallbackFunction);
|
||||
* timer.start()
|
||||
* \endcode
|
||||
*
|
||||
* Timing: The timer class emits timeouts in one second intervals.
|
||||
* A timer can only set its timeout to a multiple of one second
|
||||
* The timer itself starts immediatly unless another time has
|
||||
* been started already. The the second timer starts at the multiple
|
||||
* of one second + the start time of the first timer
|
||||
* Callback: The callback that is called when the timer timeouts runs
|
||||
* within the timer thread and not the one start() has been
|
||||
* called.
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API Timer {
|
||||
public:
|
||||
using Callback = std::function<void ()>;
|
||||
|
||||
public:
|
||||
//! C'tor
|
||||
Timer(unsigned int timeoutseconds = 0);
|
||||
|
||||
//! D'tor
|
||||
~Timer();
|
||||
|
||||
|
||||
public:
|
||||
//! Sets the timeout in seconds
|
||||
void setTimeout(unsigned int seconds);
|
||||
|
||||
//! Sets the timeout with possible nanosecond precision.
|
||||
//! @return Success flag. Systems that do not support nanosecond
|
||||
//! timers might fail.
|
||||
bool setTimeout2(unsigned int seconds, unsigned int nanoseconds);
|
||||
|
||||
//! Sets the callback for the timeout
|
||||
void setCallback(const Callback &);
|
||||
|
||||
//! Sets whether the timer is a single-shot timer.
|
||||
//! Single-shot timers stop after the first timeout.
|
||||
void setSingleShot(bool);
|
||||
|
||||
//! Starts the timer.
|
||||
bool start();
|
||||
|
||||
//! Stops the timer.
|
||||
bool stop();
|
||||
|
||||
//! Stops the timer if active and waits until it is removed from
|
||||
//! the timer list. After this call no callback will be executed.
|
||||
bool disable();
|
||||
|
||||
//! Returns the current timer state
|
||||
bool isActive() const;
|
||||
|
||||
|
||||
private:
|
||||
#if defined(SC_HAS_TIMER_CREATE)
|
||||
bool destroy();
|
||||
|
||||
static void handleTimeout(sigval_t self);
|
||||
#else
|
||||
bool deactivate(bool remove);
|
||||
|
||||
static void Loop();
|
||||
static bool Update();
|
||||
#endif
|
||||
|
||||
private:
|
||||
#if defined(SC_HAS_TIMER_CREATE)
|
||||
timer_t _timerID;
|
||||
#else
|
||||
typedef std::list<Timer*> TimerList;
|
||||
static TimerList _timers;
|
||||
static std::thread *_thread;
|
||||
static std::mutex _mutex;
|
||||
|
||||
bool _isActive;
|
||||
unsigned int _value;
|
||||
#endif
|
||||
|
||||
Callback _callback;
|
||||
std::mutex _callbackMutex;
|
||||
unsigned int _timeout;
|
||||
#if defined(SC_HAS_TIMER_CREATE)
|
||||
unsigned int _timeoutNs;
|
||||
#endif
|
||||
bool _singleShot;
|
||||
};
|
||||
|
||||
|
||||
inline StopWatch::StopWatch() {
|
||||
restart();
|
||||
}
|
||||
|
||||
inline void StopWatch::restart() {
|
||||
_start = Clock::now();
|
||||
}
|
||||
|
||||
inline uint64_t StopWatch::microseconds() const {
|
||||
return _start ? std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
Clock::now() - *_start
|
||||
).count() : 0;
|
||||
}
|
||||
|
||||
inline uint64_t StopWatch::nanoseconds() const {
|
||||
return _start ? std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
Clock::now() - *_start
|
||||
).count() : 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
125
include/seiscomp/utils/units.h
Normal file
125
include/seiscomp/utils/units.h
Normal file
@ -0,0 +1,125 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_UNITS_H
|
||||
#define SEISCOMP_UTILS_UNITS_H
|
||||
|
||||
|
||||
#include <seiscomp/core.h>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
struct SC_SYSTEM_CORE_API UnitConversion {
|
||||
UnitConversion() {}
|
||||
UnitConversion(const std::string &fromUnit_, const std::string &toUnit_,
|
||||
const std::string &toQMLUnit_, const std::string &toSEEDUnit_,
|
||||
double s)
|
||||
: fromUnit(fromUnit_)
|
||||
, toUnit(toUnit_)
|
||||
, toQMLUnit(toQMLUnit_)
|
||||
, toSEEDUnit(toSEEDUnit_)
|
||||
, scale(s) {}
|
||||
|
||||
std::string fromUnit;
|
||||
std::string toUnit;
|
||||
std::string toQMLUnit;
|
||||
std::string toSEEDUnit;
|
||||
double scale;
|
||||
|
||||
template <typename T>
|
||||
T operator()(T value) const;
|
||||
|
||||
//! Convert from input unit to SI unit
|
||||
template <typename T>
|
||||
T convert(T value) const;
|
||||
|
||||
//! Convert from SI unit back to input unit
|
||||
template <typename T>
|
||||
T revert(T value) const;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief The UnitConverter class provides a simple interface to retrieve
|
||||
* information to convert a value from one unit to another.
|
||||
*
|
||||
* The class supports variants of displacement and motion units such as
|
||||
* velocity and acceleration.
|
||||
*
|
||||
* @code
|
||||
* double value = 123;
|
||||
* double convertedValue;
|
||||
* // Convert from m/s
|
||||
* const UnitConversion *uc = UnitConverter::get('cm/s');
|
||||
* if ( uc != nullptr )
|
||||
* convertedValue = uc->convert(value);
|
||||
* @endcode
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API UnitConverter {
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
/**
|
||||
* @brief Returns a conversion object for a particular input unit.
|
||||
* @param fromUnit The unit to convert from.
|
||||
* @return A pointer to the conversion object. If no conversion is
|
||||
* available then nullptr is returned.
|
||||
*/
|
||||
static const UnitConversion *get(const std::string &fromUnit);
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private members
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
typedef std::map<std::string, UnitConversion> ConversionMap;
|
||||
static ConversionMap _conversionMap;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline T UnitConversion::convert(T value) const {
|
||||
return value*scale;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline T UnitConversion::revert(T value) const {
|
||||
return value/scale;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline T UnitConversion::operator()(T value) const {
|
||||
return convert(value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
158
include/seiscomp/utils/url.h
Normal file
158
include/seiscomp/utils/url.h
Normal file
@ -0,0 +1,158 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) gempa GmbH *
|
||||
* All rights reserved. *
|
||||
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
|
||||
* *
|
||||
* GNU Affero General Public License Usage *
|
||||
* This file may be used under the terms of the GNU Affero *
|
||||
* Public License version 3.0 as published by the Free Software Foundation *
|
||||
* and appearing in the file LICENSE included in the packaging of this *
|
||||
* file. Please review the following information to ensure the GNU Affero *
|
||||
* Public License version 3.0 requirements will be met: *
|
||||
* https://www.gnu.org/licenses/agpl-3.0.html. *
|
||||
* *
|
||||
* Other Usage *
|
||||
* Alternatively, this file may be used in accordance with the terms and *
|
||||
* conditions contained in a signed written agreement between you and *
|
||||
* gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef SEISCOMP_UTILS_URL_H
|
||||
#define SEISCOMP_UTILS_URL_H
|
||||
|
||||
|
||||
#include <seiscomp/core.h>
|
||||
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace Seiscomp {
|
||||
namespace Util {
|
||||
|
||||
|
||||
/**
|
||||
* @brief The Url class provides an interface to parse an URL.
|
||||
*
|
||||
* The general layout of an URL is:
|
||||
* scheme://username:password@host:port/path?query#fragment
|
||||
*/
|
||||
class SC_SYSTEM_CORE_API Url {
|
||||
// ----------------------------------------------------------------------
|
||||
// Public types
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
typedef std::map<std::string, std::string> QueryItems;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// X'truction
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
Url(const std::string &url = std::string());
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief Sets a URL from string and decomposes it into its parts.
|
||||
* @param url The URL string
|
||||
* @return true if the URL could be parsed, false in case of an
|
||||
* invalid URL.
|
||||
*/
|
||||
bool setUrl(const std::string &url);
|
||||
|
||||
const std::string &scheme() const;
|
||||
const std::string &username() const;
|
||||
const std::string &password() const;
|
||||
const std::string &host() const;
|
||||
size_t port() const;
|
||||
const std::string &path() const;
|
||||
const std::string &query() const;
|
||||
|
||||
bool hasQuery() const;
|
||||
bool hasQueryItem (const std::string&) const;
|
||||
std::string queryItemValue(const std::string&) const;
|
||||
const QueryItems &queryItems() const;
|
||||
|
||||
const std::string &fragment() const;
|
||||
|
||||
bool isValid() const;
|
||||
void debug() const;
|
||||
|
||||
/**
|
||||
* @brief Returns the current URL without scheme, e.g. turning
|
||||
* http://localhost into localhost.
|
||||
* @return The URL without scheme.
|
||||
*/
|
||||
std::string withoutScheme() const;
|
||||
|
||||
/**
|
||||
* @brief Decodes STL string based as defined in RFC 3986:
|
||||
* RFC 3986 section 2.2 Reserved Characters (January 2005) and
|
||||
* RFC 3986 section 2.3 Unreserved Characters (January 2005)
|
||||
* @param s C string
|
||||
* @return Encoded STL string
|
||||
*/
|
||||
static std::string Decoded(const std::string &s);
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Operators
|
||||
// ----------------------------------------------------------------------
|
||||
public:
|
||||
operator bool() const;
|
||||
operator const char*() const;
|
||||
operator const std::string &() const;
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private methods
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
bool parse(const std::string&);
|
||||
std::string subString(const std::string&, size_t&, const std::string& = "",
|
||||
bool required = false);
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Private members
|
||||
// ----------------------------------------------------------------------
|
||||
private:
|
||||
std::string _url;
|
||||
std::string _scheme;
|
||||
std::string _username;
|
||||
std::string _password;
|
||||
std::string _host;
|
||||
size_t _port;
|
||||
std::string _path;
|
||||
std::string _query;
|
||||
std::string _fragment;
|
||||
QueryItems _queryItems;
|
||||
bool _isValid;
|
||||
};
|
||||
|
||||
|
||||
inline Url::operator bool() const {
|
||||
return _isValid;
|
||||
}
|
||||
|
||||
inline Url::operator const char*() const {
|
||||
return _url.c_str();
|
||||
}
|
||||
|
||||
inline Url::operator const std::string &() const {
|
||||
return _url;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user