Added mod_khomp Endpoint.

This module has been developed to make a nice,
affordable brazilian board called Khomp
(http://www.khomp.com.br) compatible with FreeSWITCH.

Supported boards:
- FXS
- FXO
- E1
- GSM (boards and usb devices)
- Passive record
- kommuter
上级 f8d8a395
MODNAME := mod_khomp
VERBOSE := 1
ifeq ($(strip $(FREESWITCH_PATH)),)
BASE := ../../../../
else
BASE := $(FREESWITCH_PATH)
endif
curr_dir := $(shell pwd)
versions := -DFS_VERSION_MAJOR=$(shell bash $(curr_dir)/tools/getversion.sh "SWITCH_VERSION_MAJOR" $(BASE)) -DFS_VERSION_MINOR=$(shell bash $(curr_dir)/tools/getversion.sh "SWITCH_VERSION_MINOR" $(BASE)) -DFS_VERSION_MICRO=$(shell bash $(curr_dir)/tools/getversion.sh "SWITCH_VERSION_MICRO" $(BASE))
LOCAL_CFLAGS = -I./ -I./include -I./commons -I./support -D_REENTRANT -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DK3L_HOSTSYSTEM -DCOMMONS_LIBRARY_USING_FREESWITCH -g -ggdb #-DDEBUG_FLAGS
LOCAL_CFLAGS += $(versions)
LOCAL_LDFLAGS = -lk3l
LOCAL_OBJS = ./commons/k3lapi.o ./commons/k3lutil.o ./commons/config_options.o ./commons/format.o ./commons/strings.o ./commons/ringbuffer.o ./commons/verbose.o ./commons/saved_condition.o ./commons/regex.o ./commons/timer.o ./commons/configurator/configfile.o ./commons/configurator/option.o ./commons/configurator/section.o ./commons/configurator/restriction.o
LOCAL_OBJS += ./support/klog-config.o ./support/klog-options.o ./support/config_defaults.o
LOCAL_OBJS += ./src/globals.o ./src/opt.o ./src/frame.o ./src/utils.o ./src/lock.o ./src/spec.o ./src/applications.o ./src/khomp_pvt_fxo.o ./src/khomp_pvt_gsm.o ./src/khomp_pvt_kxe1.o ./src/khomp_pvt_passive.o ./src/khomp_pvt.o ./src/logger.o ./src/cli.o
conf_file_name := khomp.conf.xml
conf_file_dir := $(curr_dir)/Install/files
conf_file_dir_alt := $(curr_dir)/conf
conf_file_install = $(sysconfdir)/autoload_configs
include $(BASE)/build/modmake.rules
depend_install:
@echo "Copy $(conf_file_name)"
@if test -d $(conf_file_install) ; then \
if test -f $(conf_file_dir)/$(conf_file_name) ; then \
cp $(conf_file_dir)/$(conf_file_name) $(conf_file_install)/$(conf_file_name).new ;\
else \
cp $(conf_file_dir_alt)/$(conf_file_name) $(conf_file_install)/$(conf_file_name).new ;\
fi; \
if test ! -f "$(conf_file_install)/$(conf_file_name)" ; then \
mv $(conf_file_install)/$(conf_file_name).new $(conf_file_install)/$(conf_file_name) ;\
fi; \
fi;
/*
KHOMP generic endpoint/channel library.
This code was based on FreeBSD 7.X SVN (sys/i386/include/atomic.h),
with changes regarding optimizations and generalizations, and a
remake of the interface to fit use C++ features.
Code is distributed under original license.
Original copyright follows:
* Copyright (c) 1998 Doug Rabson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _ATOMIC_HPP_
#define _ATOMIC_HPP_
namespace Atomic
{
// Macros used to insert compare and exchange instructions easily into functions.
#define MAKE_CMPXCHG_FUNCTION(INS, PTR, EXP, VAL, TYPE) \
PunnedType pexp; pexp.valtype = EXP; \
PunnedType pval; pval.valtype = VAL; \
TYPE vexp = *(pexp.podtype); \
TYPE vval = *(pval.podtype); \
TYPE res; \
unsigned char chg = 0; \
asm volatile("lock;" INS "sete %1;" \
: "=a" (res), /* 0 */ \
"=q" (chg), /* 1 */ \
"=m" (*(unsigned char **)(PTR)) /* 2 */ \
: "r" (vval), /* 3 */ \
"a" (vexp), /* 4 */ \
"m" (*(unsigned char **)(PTR)) /* 5 */ \
: "memory"); \
*(pexp.podtype) = res; \
return (chg != 0 ? true : false);
#define MAKE_CMPXCHG8B_FUNCTION(PTR,EXP,VAL) \
PunnedType pexp; pexp.valtype = EXP; \
PunnedType pval; pval.valtype = VAL; \
unsigned long long vexp = *(pexp.podtype); \
unsigned long long vval = *(pval.podtype); \
unsigned long long res = (unsigned long long)exp; \
unsigned char chg = 0; \
asm volatile("lock; cmpxchg8b %2; sete %1;" \
: "+A" (vexp), /* 0 (result) */ \
"=q" (chg) /* 1 */ \
: "m" (*(unsigned char**)(PTR)), /* 2 */ \
"b" ((unsigned long)(vval)), \
"c" ((unsigned long)(vval >> 32))); \
*(pexp.podtype) = vexp; \
return (chg != 0 ? true : false);
// Types used for making CMPXCHG instructions independent from base type.
template < typename ValType, typename PodType >
union PunnedTypeTemplate
{
ValType * valtype;
PodType * podtype;
};
template < int SizeOfType, typename ReturnType >
struct HelperCreateCAS;
template < typename ValType >
struct HelperCreateCAS<4, ValType>
{
#if !defined(__LP64__) && !defined(__LP64)
typedef unsigned long BaseType;
#else
typedef unsigned int BaseType;
#endif
typedef PunnedTypeTemplate< ValType, BaseType > PunnedType;
inline static bool apply(volatile void *p, ValType * exp, ValType now)
{
#if !defined(__LP64__) && !defined(__LP64)
MAKE_CMPXCHG_FUNCTION("cmpxchgl %3,%5;", p, exp, &now, BaseType);
#else
MAKE_CMPXCHG_FUNCTION("cmpxchgl %k3,%5;", p, exp, &now, BaseType);
#endif
}
};
template < typename ValType >
struct HelperCreateCAS<8, ValType>
{
#if !defined(__LP64__) && !defined(__LP64)
typedef unsigned long long BaseType;
#else
typedef unsigned long BaseType;
#endif
typedef PunnedTypeTemplate< ValType, BaseType > PunnedType;
inline static volatile ValType apply(volatile void *p, ValType * exp, ValType now)
{
#if !defined(__LP64__) && !defined(__LP64)
MAKE_CMPXCHG8B_FUNCTION(p, exp, &now);
#else
MAKE_CMPXCHG_FUNCTION("cmpxchgq %3,%5;", p, exp, &now, BaseType);
#endif
}
};
// The CAS function itself.
template < typename ValType >
inline bool doCAS(volatile ValType * p, ValType * o, ValType n)
{
return HelperCreateCAS<sizeof(ValType), ValType>::apply(static_cast<volatile void *>(p), o, n);
};
template < typename ValType >
inline bool doCAS(volatile ValType * p, ValType o, ValType n)
{
return HelperCreateCAS<sizeof(ValType), ValType>::apply(static_cast<volatile void *>(p), &o, n);
};
#undef MAKE_CMPXCHG_32_FUNCTION
#undef MAKE_CMPXCHG_64_FUNCTION
#define MAKE_LOCKED_TEMPLATE(NAME) \
template < typename ValType > inline void do##NAME(volatile ValType * p, ValType v); \
template < typename ValType > inline void do##NAME(volatile ValType * p);
#define MAKE_LOCKED_FUNCTION(NAME, TYPE, INS, CONS, VAL) \
template < > inline void do##NAME < TYPE > (volatile TYPE * p, TYPE v){ asm volatile("lock;" INS : "=m" (*p) : CONS (VAL), "m" (*p)); } \
template < > inline void do##NAME < TYPE > (volatile TYPE * p) { asm volatile("lock;" INS : "=m" (*p) : CONS (1), "m" (*p)); }
#define MAKE_LOCKED_FUNCTIONS(NAME, TYPE, INS, CONS, VAL) \
MAKE_LOCKED_FUNCTION(NAME, TYPE, INS, CONS, VAL) \
MAKE_LOCKED_FUNCTION(NAME, unsigned TYPE, INS, CONS, VAL)
MAKE_LOCKED_TEMPLATE(Add);
MAKE_LOCKED_TEMPLATE(Sub);
MAKE_LOCKED_TEMPLATE(SetBits);
MAKE_LOCKED_TEMPLATE(ClearBits);
MAKE_LOCKED_FUNCTIONS(Add, int, "addl %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(Sub, int, "subl %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(SetBits, int, "orl %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(ClearBits, int, "andl %1,%0", "ir", ~v);
#if !defined(__LP64__) && !defined(__LP64)
MAKE_LOCKED_FUNCTIONS(Add, long, "addl %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(Sub, long, "subl %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(SetBits, long, "orl %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(ClearBits, long, "andl %1,%0", "ir", ~v);
#else
MAKE_LOCKED_FUNCTIONS(Add, long, "addq %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(Sub, long, "subq %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(SetBits, long, "orq %1,%0", "ir", v);
MAKE_LOCKED_FUNCTIONS(ClearBits, long, "andq %1,%0", "ir", ~v);
#endif
};
#endif /* _ATOMIC_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _CONFIG_COMMONS_HPP_
#define _CONFIG_COMMONS_HPP_
/****************************************************************************/
/* ASTERISK */
#if defined(COMMONS_LIBRARY_USING_ASTERISK)
#define COMMONS_IMPLEMENTATION asterisk
/****************************************************************************/
/* CALLWEAVER */
#elif defined(COMMONS_LIBRARY_USING_CALLWEAVER)
#define COMMONS_IMPLEMENTATION callweaver
/****************************************************************************/
/* FREESWITCH */
#elif defined(COMMONS_LIBRARY_USING_FREESWITCH)
#define COMMONS_IMPLEMENTATION freeswitch
/****************************************************************************/
/* GNU/LINUX (generic) */
#elif defined(COMMONS_LIBRARY_USING_GNU_LINUX)
#define COMMONS_IMPLEMENTATION gnulinux
/****************************************************************************/
#else
#error Unknown implementation selected. Please define COMMONS_LIBRARY_USING_* correctly.
#endif
#define COMMONS_INCLUDE(file) <COMMONS_IMPLEMENTATION/file>
#endif /* _CONFIG_COMMONS_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <set>
#include <map>
#include <vector>
#include <strings.hpp>
#include <function.hpp>
#include <variant.hpp>
#include <format.hpp>
#ifndef _CONFIG_OPTIONS_HPP_
#define _CONFIG_OPTIONS_HPP_
struct ConfigProcessFailure
{
ConfigProcessFailure(std::string _msg): msg(_msg) {};
std::string msg;
};
struct ConfigOption
{
typedef int SignedIntType;
typedef unsigned int UnsignedIntType;
typedef bool BooleanType;
typedef std::string StringType;
typedef Function::Function1 < void, std::string > FunctionType;
typedef std::set < StringType > string_allowed_type;
/* this should reflect 'variant.which()'! */
typedef enum
{
ID_SINT = 0,
ID_UINT = 1,
ID_BOOL = 2,
ID_STRING = 3,
ID_FUN = 4,
}
value_id_type;
template < typename number_type >
struct Range
{
Range(number_type _minimum, number_type _maximum, number_type _step)
: minimum(_minimum), maximum(_maximum), step(_step) {};
number_type minimum, maximum, step;
};
struct SignedIntData : public VariantBaseType < void >
{
SignedIntData(SignedIntType & _sint_val, SignedIntType _sint_default, Range< SignedIntType > _sint_Range)
: sint_val(_sint_val), sint_default(_sint_default), sint_Range(_sint_Range) {};
int which()
{
return ID_SINT;
}
SignedIntType & sint_val;
SignedIntType sint_default;
Range<SignedIntType> sint_Range;
};
struct UnsignedIntData : public VariantBaseType < void >
{
UnsignedIntData(UnsignedIntType & _uint_val, UnsignedIntType _uint_default, Range< UnsignedIntType > _uint_Range)
: uint_val(_uint_val), uint_default(_uint_default), uint_Range(_uint_Range) {};
int which()
{
return ID_UINT;
}
UnsignedIntType & uint_val;
UnsignedIntType uint_default;
Range<UnsignedIntType> uint_Range;
};
struct BooleanData : public VariantBaseType < void >
{
BooleanData(BooleanType & _bool_val, BooleanType _bool_default)
: bool_val(_bool_val), bool_default(_bool_default) {};
int which()
{
return ID_BOOL;
}
BooleanType & bool_val;
BooleanType bool_default;
};
struct StringData : public VariantBaseType < void >
{
StringData(std::string & _string_val, std::string _string_default, string_allowed_type _string_allowed)
: string_val(_string_val), string_default(_string_default), string_allowed(_string_allowed) {};
int which()
{
return ID_STRING;
}
std::string & string_val;
std::string string_default;
string_allowed_type string_allowed;
};
struct FunctionData : public VariantBaseType < void >
{
FunctionData(FunctionType _fun_val, std::string _fun_default, string_allowed_type _fun_allowed)
: fun_val(_fun_val), fun_default(_fun_default), fun_allowed(_fun_allowed) {};
int which()
{
return ID_FUN;
}
FunctionType fun_val;
std::string fun_default;
string_allowed_type fun_allowed;
};
typedef Variant < VariantBaseType < void > > ValueType;
ConfigOption(std::string, const StringType &, const StringType, string_allowed_type allowed, bool list_me = true);
ConfigOption(std::string, const StringType &, const StringType = "", bool list_me = true);
ConfigOption(std::string, const SignedIntType &, const SignedIntType = 0, SignedIntType min = -INT_MAX, SignedIntType max = INT_MAX, SignedIntType step = 1, bool list_me = true);
ConfigOption(std::string, const UnsignedIntType &, const UnsignedIntType = 0, UnsignedIntType min = 0, UnsignedIntType max = UINT_MAX, UnsignedIntType step = 1, bool list_me = true);
ConfigOption(std::string, const BooleanType &, const BooleanType = false, bool list_me = true);
ConfigOption(std::string, FunctionType, std::string defvalue, string_allowed_type allowed, bool list_me = true);
ConfigOption(std::string, FunctionType, std::string defvalue = "", bool list_me = true);
ConfigOption(const ConfigOption & o);
~ConfigOption(void);
void set(StringType value);
void set(SignedIntType value);
void set(UnsignedIntType value);
void set(BooleanType value);
BooleanType get_bool(){ return _value_data.get<BooleanData>().bool_val; }
std::string get_str(){ return _value_data.get<StringData>().string_val; }
SignedIntType get_sint(){ return _value_data.get<SignedIntData>().sint_val; }
UnsignedIntType get_uint(){ return _value_data.get<UnsignedIntData>().uint_val; }
std::string & name(void);
value_id_type type(void);
const char ** values(void);
void reset(void);
void commit(void);
bool list_me(void) { return _list_me; };
bool loaded(void) { return _loaded; };
void copy_from(ConfigOption &);
protected:
std::string _my_name;
ValueType _value_data;
bool _list_me;
const char ** _values;
bool _loaded;
};
struct ConfigOptions
{
typedef std::vector < std::string > messages_type;
ConfigOptions(void): _values(NULL) {};
typedef std::set < std::string > string_set;
typedef std::map < std::string, ConfigOption > option_map_type;
typedef std::pair < std::string, ConfigOption > option_pair_type;
typedef std::map < std::string, std::string > syn_option_map_type;
typedef std::pair < std::string, std::string > syn_option_pair_type;
bool add(ConfigOption option);
/* only valid in "process" (for backwards compatibility config files) */
bool synonym(std::string, std::string);
template <typename ValueType>
void set(std::string name, ValueType value)
{
option_map_type::iterator iter = _map.find(name);
if (iter == _map.end())
throw ConfigProcessFailure(STG(FMT("unknown option: %s") % name));
(*iter).second.set(value);
}
std::string get(std::string name)
{
option_map_type::iterator iter = _map.find(name);
if (iter == _map.end())
throw ConfigProcessFailure(STG(FMT("unknown option: %s") % name));
switch((*iter).second.type())
{
case ConfigOption::ID_BOOL: return (*iter).second.get_bool() ? "yes" : "no";
case ConfigOption::ID_STRING: return (*iter).second.get_str();
case ConfigOption::ID_UINT: return STG(FMT("%d") % (*iter).second.get_uint());
case ConfigOption::ID_SINT: return STG(FMT("%d") % (*iter).second.get_sint());
case ConfigOption::ID_FUN: return "";
}
}
string_set options(void);
void process(const char *, const char *); /* process option from file */
void reset(void); /* reset loaded opts */
messages_type commit(void); /* set defaults */
const char ** values(const char *); /* option value */
const char ** values(void); /* values from options */
bool loaded(std::string); /* return if config was loaded */
void copy_from(ConfigOptions &, std::string);
protected:
option_map_type::iterator find_option(std::string);
protected:
option_map_type _map;
syn_option_map_type _syn_map;
const char ** _values;
};
#endif /* _CONFIG_OPTIONS_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <configurator/configfile.hpp>
#if _MSC_VER >= 1400
#undef close
#endif
void Configfile::ignore(std::string str)
{
_ignores.insert(str);
};
bool Configfile::select(Section **ptr, std::string str)
{
/* default section == this! */
*ptr = this;
/* always success by default */
return true;
};
bool Configfile::adjust(Section * section, std::string & opt, std::string & val)
{
return section->load(opt, val);
};
bool Configfile::deserialize(std::ifstream& fd)
{
Section * section = NULL;
/* default selection! */
if (!select(&section))
{
_errors.push_back("default selection has failed!");
return false;
}
size_t count = 0;
while (fd.good())
{
std::string str;
/* read one line! */
std::getline(fd, str);
size_t lst = str.size() - 1;
if (str.size() >= 1 && str[lst] == '\r') //cuida das quebras de linha do tipo \r\n
{
str.erase(lst,1);
--lst;
}
/* empty line! */
if (str.size() == 0)
continue;
/* comment! */
if (str[0] == '#')
continue;
++count;
if (str[0] == '[' && str[lst] == ']')
{
str.erase(0,1); --lst;
str.erase(lst,1); --lst;
if (!select(&section, str))
{
_errors.push_back(STG(FMT("erroneous section '%s'") % str));
/* ignore this section */
section = NULL;
continue;
}
}
else
{
std::string::size_type pos = str.find('=');
if (pos == std::string::npos)
{
_errors.push_back(STG(FMT("erroneous separator '%s'") % str));
continue;
};
if (section == NULL)
{
_errors.push_back(STG(FMT("no section for option '%s'") % str));
continue;
}
std::string opt(str.substr(0,pos));
std::string val(str.substr(pos+1));
if (_ignores.find(opt) != _ignores.end())
continue;
if (val == "@") val = "";
if (adjust(section, opt, val))
continue;
_errors.push_back(STG(FMT("option '%s' does "
"not exist or '%s' is not a valid value (at section '%s')")
% opt % val % section->name()));
}
}
// retorna 'true' se arquivo tinha alguma coisa valida.
return (count != 0);
}
bool Configfile::obtain()
{
std::ifstream fd(_filename.c_str());
if (!fd.is_open())
{
_errors.push_back(STG(FMT("unable to open file '%s': %s")
% _filename % strerror(errno)));
return false;
};
if (!deserialize(fd))
{
fd.close();
return false;
}
fd.close();
return true;
};
void Configfile::recurse(std::ofstream& fd, Section * section)
{
typedef Section::SectionMap::iterator section_iter;
typedef Section::OptionMap::iterator option_iter;
for (option_iter i = section->option_begin(); i != section->option_end(); i++)
{
std::string res;
if ((*i).second.store(res))
{
if (res == "") res = "@";
fd << (*i).first << "=" << res << std::endl;
}
}
if (!section->recursive())
return;
for (section_iter j = section->section_begin(); j != section->section_end(); j++)
recurse(fd, (*j).second);
}
bool Configfile::serialize(std::ofstream& fd)
{
recurse(fd, this);
return true;
}
bool Configfile::provide()
{
std::string tmp(_filename);
tmp += ".new";
std::ofstream fd(tmp.c_str());
if (!fd.good())
{
_errors.push_back(STG(FMT("unable to open file '%s': %s")
% tmp % strerror(errno)));
return false;
}
if (!serialize(fd))
{
fd.close();
return false;
}
fd.close();
if (rename(tmp.c_str(), _filename.c_str()) != 0)
{
_errors.push_back(STG(FMT("unable to replace config file '%s': %s")
% _filename % strerror(errno)));
return false;
}
return true;
}
#if _MSC_VER >= 1400
#define close _close
#endif
\ No newline at end of file
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vector>
#include <fstream>
#include <sstream>
#include <set>
#include <format.hpp>
#include <configurator/section.hpp>
#ifndef _CONFIG_CONFIGFILE_HPP_
#define _CONFIG_CONFIGFILE_HPP_
struct Configfile: public Section
{
typedef std::list < std::string > ErrorVector;
typedef std::set < std::string > NameSet;
Configfile(std::string name, std::string desc)
: Section(name, desc), _good(false) {};
virtual ~Configfile() {};
bool good() { return _good; };
std::string & filename() { return _filename; };
ErrorVector & errors() { return _errors; };
void ignore(std::string);
virtual bool obtain();
virtual bool provide();
protected:
virtual bool select(Section **, std::string str = "");
virtual bool adjust(Section *, std::string & opt, std::string & val);
virtual bool deserialize(std::ifstream &);
virtual bool serialize(std::ofstream &);
void recurse(std::ofstream &, Section *);
protected:
bool _good;
ErrorVector _errors;
NameSet _ignores;
std::string _filename;
};
#endif /* _CONFIG_CONFIGFILE_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <configurator/option.hpp>
bool Option::equals(std::string & value)
{
switch (_restriction.numeral())
{
case Restriction::N_UNIQUE:
{
Restriction::Value my_value;
if (!_restriction.get(Restriction::F_USER, my_value))
return false;
return (my_value == value);
}
case Restriction::N_MULTIPLE:
{
Restriction::Vector my_values;
if (!_restriction.get(Restriction::F_USER, my_values))
return false;
for (Restriction::Vector::iterator i = my_values.begin(); i != my_values.end(); i++)
{
if ((*i) == value)
return true;
}
return false;
}
}
return false;
}
bool Option::load(std::string & value)
{
bool ret = _restriction.set(Restriction::F_FILE, value);
if (ret) _modified = false;
return ret;
}
bool Option::change(std::string & value)
{
bool ret = _restriction.set(Restriction::F_FILE, value);
if (ret) _modified = true;
return ret;
}
bool Option::store(std::string & value)
{
switch (_restriction.numeral())
{
case Restriction::N_UNIQUE:
return _restriction.get(Restriction::F_FILE, value);
case Restriction::N_MULTIPLE:
{
Restriction::Vector values;
if (!_restriction.get(Restriction::F_FILE, values))
return false;
Strings::Merger strs;
for (Restriction::Vector::iterator i = values.begin(); i != values.end(); i++)
strs.add(*i);
value = strs.merge(",");
return true;
}
default:
return false;
}
}
Option::Flags Option::set(const char * value)
{
std::string str_value(value);
return set(str_value);
}
Option::Flags Option::set(Restriction::Value & value)
{
Restriction::Value last_value, curr_value;
Flags flags;
bool ret1 = _restriction.get(Restriction::F_USER, last_value);
if (!_restriction.set(Restriction::F_USER, value))
return flags;
flags[F_ADJUSTED] = true;
bool ret2 = _restriction.get(Restriction::F_USER, curr_value);
if (!ret1 || (ret2 && (last_value != curr_value)))
{
flags[F_MODIFIED] = true;
_modified = true;
}
return flags;
}
Option::Flags Option::set(Restriction::Vector & values)
{
Restriction::Vector last_values, curr_values;
Flags flags;
bool ret1 = _restriction.get(Restriction::F_USER, last_values);
if (!_restriction.set(Restriction::F_USER, values))
return flags;
flags[F_ADJUSTED] = true;
bool ret2 = _restriction.get(Restriction::F_USER, curr_values);
if (!ret1 || (ret2 && (last_values != curr_values)))
{
flags[F_MODIFIED] = true;
_modified = true;
}
return flags;
}
bool Option::get(Restriction::Value & value)
{
return _restriction.get(Restriction::F_USER, value);
}
bool Option::get(Restriction::Vector & values)
{
return _restriction.get(Restriction::F_USER, values);
}
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <strings.hpp>
#include <configurator/restriction.hpp>
#ifndef _CONFIG_OPTION_HPP_
#define _CONFIG_OPTION_HPP_
struct Option
{
enum FlagTypes
{
F_MODIFIED = 0x0, /* if option was modified */
F_ADJUSTED = 0x1, /* if option was correctly formated */
};
struct Flags: public std::vector<bool>
{
Flags(): std::vector<bool>(2) {};
};
typedef Restriction::Value Value;
typedef Restriction::Vector Vector;
/* exception */
struct InvalidDefaultValue
{
InvalidDefaultValue(std::string name, std::string value)
: _name(name), _value(value) {};
std::string & name() { return _name; };
std::string & value() { return _value; };
protected:
std::string _name;
std::string _value;
};
Option(std::string name, std::string desc, std::string defvalue, Restriction restriction)
: _name(name), _desc(desc), _restriction(restriction), _modified(true)
{
std::string value(defvalue);
if (!(set(value)[F_ADJUSTED]))
throw InvalidDefaultValue(name, defvalue);
}
const std::string & name() { return _name; };
const std::string & description() { return _desc; };
Restriction & restriction() { return _restriction; };
bool modified() { return _modified; };
public:
bool load(std::string &);
bool change(std::string &);
bool store(std::string &);
Flags set(const char *);
Flags set(Value &);
Flags set(Vector &);
bool get(Value &);
bool get(Vector &);
bool equals(std::string &);
protected:
std::string _name;
std::string _desc;
Restriction _restriction;
bool _modified;
};
#endif /* _CONFIG_OPTION_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <math.h>
#include <string.h>
#include <iostream>
#include <strings.hpp>
#include <configurator/restriction.hpp>
/* internal helper! */
bool Restriction::equalNumber(const double a, const double b)
{
char tmp1[64];
char tmp2[64];
snprintf(tmp1, sizeof(tmp1), "%.3f", a);
snprintf(tmp2, sizeof(tmp2), "%.3f", b);
if (strncmp(tmp1, tmp2, sizeof(tmp1)))
return false;
return true;
}
/* process value to our internal representation */
bool Restriction::process(Restriction::Format fmt,
const Restriction::Value & value, Restriction::Value & final)
{
switch (_bounds)
{
case B_RANGE:
{
if (_kind != K_NUMBER)
return false;
std::string tmpvalue;
Restriction::Value::const_iterator itr = value.begin();
Restriction::Value::const_iterator end = value.end();
tmpvalue.reserve(value.size());
// f*cking dot/comma notation!
for (; itr != end; ++itr)
tmpvalue += ((*itr) != ',' ? (*itr) : '.');
try
{
double newvalue = Strings::todouble(tmpvalue);
if (newvalue < _init && newvalue > _fini)
return false;
double res = (newvalue - _init) / _step;
if (!equalNumber(res, rint(res)))
return false;
final = value;
return true;
}
catch (...)
{
return false;
}
}
case B_LIST:
for (List::iterator i = _list.begin(); i != _list.end(); i++)
{
Value & tmp = (*i);
if (tmp == value)
{
final = value;
return true;
}
}
return false;
case B_MAPS:
switch (fmt)
{
case F_USER:
{
Map::iterator i = _map_from_usr.find(std::string(value));
if (i == _map_from_usr.end())
return false;
Value & tmp = (*i).second;
final = tmp;
return true;
}
case F_FILE:
{
Map::iterator i = _map_from_cfg.find(std::string(value));
if (i == _map_from_cfg.end())
return false;
final = value;
return true;
}
default:
break;
}
return false;
case B_FREE:
final = value;
return true;
default:
break;
}
return false;
}
/* unprocess the value (outputs the external representation) */
bool Restriction::unprocess(Restriction::Format fmt,
const Restriction::Value & value, Restriction::Value & final)
{
switch (_bounds)
{
case B_MAPS:
switch (fmt)
{
case F_USER:
{
Map::iterator i = _map_from_cfg.find(std::string(value));
if (i == _map_from_cfg.end())
return false;
final = (*i).second;
return true;
}
default:
break;
}
default:
final = value;
return true;
}
}
/***************************** *****************************/
bool Restriction::get(Restriction::Format fmt, Restriction::Value & value)
{
if (_numeral != N_UNIQUE)
return false;
if (!unprocess(fmt, _value._unique, value))
return false;
return true;
}
bool Restriction::get(Restriction::Format fmt, Restriction::Vector & values)
{
if (_numeral != N_MULTIPLE)
return false;
List & my_values = _value._multiple;
for (List::iterator i = my_values.begin(); i != my_values.end(); i++)
{
Value & value = (*i);
Value final;
if (!unprocess(fmt, value, final))
return false;
values.push_back(final);
};
return true;
}
/***************************** *****************************/
bool Restriction::set(Restriction::Format fmt, Restriction::Value &value)
{
switch (_numeral)
{
case N_UNIQUE:
{
Value final;
if (!process(fmt, value, final))
return false;
_value._unique = final;
return true;
}
case N_MULTIPLE:
{
if (value == "@" || value == "#" || value == "")
{
_value._multiple.clear();
return true;
}
Strings::vector_type values;
Strings::tokenize(value, values, ",");
return set(fmt, values);
}
default:
return false;
}
}
bool Restriction::set(Restriction::Format fmt, Restriction::Vector & values)
{
if (_numeral != N_MULTIPLE)
return false;
if (values.empty())
{
_value._multiple.clear();
}
else
{
/* list needed to store temporary values */
List finals;
for (Vector::iterator i = values.begin(); i != values.end(); i++)
{
Value & value = (*i);
Value final;
if (!process(fmt, value, final))
return false;
finals.push_back(final);
}
List & lst = _value._multiple;
/* need to clear values set before */
lst.clear();
for (List::iterator i = finals.begin(); i != finals.end(); i++)
{
Value value = (*i);
lst.push_back(value);
}
};
return true;
}
/***************************** *****************************/
void Restriction::allowed(Restriction::Vector &vals)
{
switch (_bounds)
{
case B_FREE:
return;
case B_LIST:
for (List::iterator i = _list.begin(); i != _list.end(); i++)
vals.push_back((*i));
break;
case B_MAPS:
for (Map::iterator i = _map_from_usr.begin(); i != _map_from_usr.end(); i++)
vals.push_back((*i).first);
break;
case B_RANGE:
{
if (_kind != K_NUMBER)
return;
// is there any fraction?
bool has_fraction = (!equalNumber(_init, rint(_init))) || (!equalNumber(_fini, rint(_fini))) || (!equalNumber(_step, rint(_step)));
const char * format = (has_fraction ? "%.2f" : "%02.0f");
for (double i = _init; i <= _fini; i += _step)
{
char tmp[32];
snprintf(tmp, sizeof(tmp), format, i);
vals.push_back(std::string(tmp));
}
break;
}
default:
break;
}
}
void Restriction::init_class()
{
_value._unique.clear();
_value._multiple.clear();
}
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdarg.h>
#include <string>
#include <list>
#include <vector>
#include <map>
#ifndef _CONFIG_RESTRICTION_HPP_
#define _CONFIG_RESTRICTION_HPP_
struct Restriction
{
/* generic types */
enum Format
{
F_USER,
F_FILE
};
enum Kind
{
// K_INTEGER,
// K_FLOAT,
K_STRING,
K_NUMBER // = K_INTEGER // compatibility
};
enum Bounds
{
B_FREE,
B_RANGE,
B_LIST,
B_MAPS
};
enum Numeral
{
N_UNIQUE,
N_MULTIPLE
};
typedef std::string Value;
/* types used for data input */
struct Pair
{
const char *pretty;
const char *value;
};
typedef std::pair < Value, Value > PairMap;
typedef std::list < PairMap > ListMap;
/* types used internally */
typedef std::map < Value, Value > Map;
typedef std::vector < Value > Vector;
typedef std::list < Value > List;
typedef std::pair < Value, Value > MapPair;
struct Generic
{
Value _unique;
List _multiple;
};
Restriction(Kind kind, Numeral num)
: _kind(kind), _bounds(B_FREE), _numeral(num), _unit(""),
_init(-1), _fini(-1), _step(-1)
{
init_class();
}
/*
Restriction(Kind kind, Numeral num,
int init, int fini, int step = 1)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_iinit(init), _ifini(fini), _istep(step),
_finit(-1), _ffini(-1), _fstep(-1)
{
init_class();
}
Restriction(Kind kind, Numeral num,
const char *unit, int init, int fini, int step = 1)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_iinit(init), _ifini(fini), _istep(step),
_finit(-1), _ffini(-1), _fstep(-1)
{
init_class();
}
Restriction(Kind kind, Numeral num,
std::string unit, int init, int fini, int step = 1)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_iinit(init), _ifini(fini), _istep(step),
_finit(-1), _ffini(-1), _fstep(-1)
{
init_class();
}
Restriction(Kind kind, Numeral num,
float init, float fini, float step = 1)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_iinit(-1), _ifini(-1), _istep(-1)
_finit(init), _ffini(fini), _fstep(step),
{
init_class();
}
Restriction(Kind kind, Numeral num,
const char *unit, float init, float fini, float step = 1.0)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_iinit(-1), _ifini(-1), _istep(-1)
_finit(init), _ffini(fini), _fstep(step),
{
init_class();
}
Restriction(Kind kind, Numeral num,
std::string unit, float init, float fini, float step = 1.0)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_iinit(-1), _ifini(-1), _istep(-1)
_finit(init), _ffini(fini), _fstep(step),
{
init_class();
}
*/
Restriction(Kind kind, Numeral num,
double init, double fini, double step = 1)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(""),
_init(init), _fini(fini), _step(step)
{
init_class();
}
Restriction(Kind kind, Numeral num,
const char *unit, double init, double fini, double step = 1.0)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_init(init), _fini(fini), _step(step)
{
init_class();
}
Restriction(Kind kind, Numeral num,
std::string unit, double init, double fini, double step = 1.0)
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
_init(init), _fini(fini), _step(step)
{
init_class();
}
Restriction(Kind kind, Numeral num,
const char *first, ...)
: _kind(kind), _bounds(B_LIST), _numeral(num), _unit(""),
_init(-1), _fini(-1), _step(-1)
{
_list.push_back(std::string(first));
va_list ap;
va_start(ap, first);
while (true)
{
const char *arg = va_arg(ap, const char *);
if (arg == NULL) break;
_list.push_back(std::string(arg));
}
init_class();
}
Restriction(Kind kind, const char *unit, Numeral num,
const char *first, ...)
: _kind(kind), _bounds(B_LIST), _numeral(num), _unit(unit),
_init(-1), _fini(-1), _step(-1)
{
_list.push_back(std::string(first));
va_list ap;
va_start(ap, first);
while (true)
{
const char *arg = va_arg(ap, const char *);
if (arg == NULL) break;
_list.push_back(std::string(arg));
}
init_class();
}
Restriction(Kind kind, Numeral num,
const struct Pair first, ...)
: _kind(kind), _bounds(B_MAPS), _numeral(num), _unit(""),
_init(-1), _fini(-1), _step(-1)
{
_map_from_usr.insert(MapPair(Value(first.pretty), Value(first.value)));
_map_from_cfg.insert(MapPair(Value(first.value), Value(first.pretty)));
va_list ap;
va_start(ap, first);
while (true)
{
Pair arg = va_arg(ap, Pair);
if (arg.pretty == NULL) break;
_map_from_usr.insert(MapPair(Value(arg.pretty), Value(arg.value)));
_map_from_cfg.insert(MapPair(Value(arg.value), Value(arg.pretty)));
}
init_class();
}
Restriction(Kind kind, Numeral num, List list)
: _kind(kind), _bounds(B_LIST), _numeral(num), _unit(""),
_init(-1), _fini(-1), _step(-1), _list(list)
{
init_class();
}
Restriction(Kind kind, Numeral num, ListMap map)
: _kind(kind), _bounds(B_MAPS), _numeral(num), _unit(""),
_init(-1), _fini(-1), _step(-1)
{
for (ListMap::iterator i = map.begin(); i != map.end(); i++)
{
_map_from_usr.insert(MapPair(Value((*i).first), Value((*i).second)));
_map_from_cfg.insert(MapPair(Value((*i).second), Value((*i).first)));
}
init_class();
}
Kind kind() { return _kind; };
Bounds bounds() { return _bounds; };
Numeral numeral() { return _numeral; };
Value unit() { return _unit; };
bool set(Format, Vector &);
bool set(Format, Value &);
bool get(Format, Vector &);
bool get(Format, Value &);
void allowed(Vector &);
private:
bool process(Format, const Value &, Value &);
bool unprocess(Format, const Value &, Value &);
void init_class();
bool equalNumber(const double, const double);
protected:
Kind _kind;
Bounds _bounds;
Numeral _numeral;
Value _unit;
double _init, _fini, _step;
Map _map_from_usr,
_map_from_cfg;
List _list;
Generic _value;
};
#endif /* _CONFIG_RESTRICTION_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <configurator/section.hpp>
void Section::options(Section::OptionVector & vec)
{
for (OptionMap::iterator it = _options.begin(); it != _options.end();)
{
vec.push_back(&((*it).second));
++it;
}
}
void Section::sections(Section::SectionVector & vec)
{
for (SectionMap::iterator it = _sections.begin(); it != _sections.end();)
{
vec.push_back((*it).second);
++it;
}
}
/*********/
Option * Section::option_find(std::string & str, bool recurse)
{
OptionMap::iterator i = _options.find(str);
if (i == _options.end())
{
if (!recurse)
throw not_found();
for (SectionMap::iterator i = _sections.begin(); i != _sections.end(); i++)
{
try
{
return i->second->option_find(str, recurse);
}
catch (not_found & e)
{
/* keep looping! */
};
}
throw not_found();
}
return &((*i).second);
}
Option * Section::option_find(const char * str, bool recurse)
{
std::string sstr(str);
return option_find(sstr, recurse);
}
/*********/
Section * Section::section_find(std::string & str, bool recurse)
{
SectionMap::iterator i = _sections.find(str);
if (i == _sections.end())
{
if (!recurse)
throw not_found();
for (SectionMap::iterator i = _sections.begin(); i != _sections.end(); i++)
{
try
{
return i->second->section_find(str, recurse);
}
catch (not_found & e)
{
/* keep looping! */
};
}
throw not_found();
}
return ((*i).second);
}
Section * Section::section_find(const char * str, bool recurse)
{
std::string sstr(str);
return section_find(sstr, recurse);
}
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <configurator/option.hpp>
#ifndef _CONFIG_SECTION_HPP_
#define _CONFIG_SECTION_HPP_
struct Section
{
typedef std::map < std::string, Option > OptionMap;
typedef std::vector< Option * > OptionVector;
typedef std::map < std::string, Section * > SectionMap;
typedef std::vector < Section * > SectionVector;
struct not_found {}; /* exception */
// protected:
Section(std::string name, std::string desc, bool recursive = true)
: _name(name), _description(desc), _recursive(recursive) {};
void add(Option o)
{
_options.insert(std::pair<std::string,Option>(o.name(), o));
};
void del(std::string name)
{
_options.erase(name);
};
void add(Section *s)
{
_sections.insert(std::pair<std::string,Section*>(s->name(), s));
};
public:
const std::string & name() { return _name; };
const std::string & description() { return _description; };
const bool & recursive() { return _recursive; };
OptionMap::iterator option_begin() { return _options.begin(); };
OptionMap::iterator option_end() { return _options.end(); };
SectionMap::iterator section_begin() { return _sections.begin(); };
SectionMap::iterator section_end() { return _sections.end(); };
/**/
Option * option_find(const char *, bool recurse = false);
Section * section_find(const char *, bool recurse = false);
Option * option_find(std::string &, bool recurse = false);
Section * section_find(std::string &, bool recurse = false);
/**/
void options(OptionVector &);
void sections(SectionVector &);
/**/
template <typename F>
bool search_and_apply(std::string &key, std::string &value, F f)
{
OptionMap::iterator i = _options.find(key);
if (i != _options.end())
return f((*i).second);
if (!_recursive)
return false;
return (find_if(_sections.begin(), _sections.end(), f) != _sections.end());
}
private:
struct key_value
{
key_value(std::string &k, std::string &v): _k(k), _v(v) {};
std::string & _k, & _v;
};
struct load_section: protected key_value
{
load_section(std::string &k, std::string &v): key_value(k,v) {};
bool operator()(Option &o) { return o.load(_v); };
bool operator()(SectionMap::value_type &v) { return v.second->load(_k,_v); };
};
struct change_section: protected key_value
{
change_section(std::string &k, std::string &v): key_value(k,v) {};
bool operator()(Option &o) { return o.change(_v); };
bool operator()(SectionMap::value_type &v) { return v.second->change(_k,_v); };
};
struct store_section: protected key_value
{
store_section(std::string &k, std::string &v): key_value(k,v) {};
bool operator()(Option &o) { return o.store(_v); };
bool operator()(SectionMap::value_type &v) { return v.second->store(_k,_v); };
};
struct set_section: protected key_value
{
set_section(std::string &k, std::string &v): key_value(k,v) {};
bool operator()(Option &o) { return (o.set(_v))[Option::F_ADJUSTED]; };
bool operator()(SectionMap::value_type &v) { return v.second->set(_k,_v); };
};
struct get_section: protected key_value
{
get_section(std::string &k, std::string &v): key_value(k,v) {};
bool operator()(Option &o) { return o.get(_v); };
bool operator()(SectionMap::value_type &v) { return v.second->get(_k,_v); };
};
struct modified_section
{
bool operator()(OptionMap::value_type &v) { return v.second.modified(); };
bool operator()(SectionMap::value_type &v) { return v.second->modified(); };
};
public:
bool load(const char * key, std::string value)
{
std::string skey(key);
return search_and_apply(skey, value, load_section(skey, value));
}
bool load(std::string &key, std::string &value)
{
return search_and_apply(key, value, load_section(key, value));
}
bool change(std::string &key, std::string &value)
{
return search_and_apply(key, value, change_section(key, value));
}
bool store(std::string &key, std::string &value)
{
return search_and_apply(key, value, store_section(key, value));
}
bool set(std::string &key, std::string &value)
{
return search_and_apply(key, value, set_section(key, value));
}
bool get(std::string &key, std::string &value)
{
return search_and_apply(key, value, get_section(key, value));
}
bool modified()
{
return ((find_if(_options.begin(), _options.end(), modified_section()) != _options.end()) ||
(find_if(_sections.begin(), _sections.end(), modified_section()) != _sections.end()));
}
private:
Section() {};
protected:
std::string _name;
std::string _description;
OptionMap _options;
SectionMap _sections;
bool _recursive;
};
#endif /* _CONFIG_SECTION_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <map>
#include <initializer.hpp>
#ifndef _FLAGGER_HPP_
#define _FLAGGER_HPP_
template < typename Flag >
struct Flagger
{
protected:
struct Bool
{
Bool(): value(false) {};
Bool(bool &v): value(v) {};
bool value;
};
typedef std::map< Flag, Bool > Map;
public:
typedef Initializer< Flag > InitFlags;
Flagger() {};
Flagger(InitFlags flags)
{
for (typename InitFlags::iterator i = flags.begin(); i != flags.end(); i++)
{
Flag & flag = (*i);
_map[flag].value = true;
};
};
void set(Flag elt, bool value = true)
{
_map[elt].value = value;
}
bool is_set(Flag elt)
{
return _map[elt].value;
}
Flagger & operator&(Flag elt)
{
set(elt);
return *this;
};
bool operator[](Flag elt)
{
return is_set(elt);
};
protected:
Map _map;
};
#endif /* _FLAGGER_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "format.hpp"
//#include <iostream>
Format::Format(const char * format_string, bool raise_exception)
: _format(format_string), _valid(true), _raise(raise_exception)
{
initialize(format_string);
}
/*
Format::Format(std::string & format_string, bool raise_exception)
: _format(NULL), _valid(true), _raise(raise_exception)
{
initialize(format_string.c_str());
}
*/
Format::Format(std::string format_string, bool raise_exception)
: _format(format_string), _valid(true), _raise(raise_exception)
{
initialize(format_string.c_str());
}
void Format::initialize(const char * format_string)
{
std::string txt;
const char * ptr = format_string;
while (*ptr != '\0')
{
if (*ptr != '%')
{
txt += *ptr;
++ptr;
continue;
}
const char * ptr2 = ptr+1;
if (*ptr2 == '%')
{
txt += *ptr;
ptr += 2;
continue;
}
if (!txt.empty())
push_argument(txt, T_LITERAL);
std::string arg(1, *ptr);
++ptr;
bool finished = false;
short long_count = 0;
short short_count = 0;
while(*ptr != '\0' && !finished)
{
switch (*ptr)
{
case ' ':
// uncomplete format with ' ', make it a literal.
arg += *ptr;
push_argument(arg, T_LITERAL);
finished = true;
break;
case '%':
// uncomplete format with '%', make it a literal and start a new format.
push_argument(arg, T_LITERAL);
arg += *ptr;
break;
case 'h':
short_count = std::min<short>(short_count+1, 2);
long_count = 0;
arg += *ptr;
break;
case 'l':
long_count = std::min<short>(long_count+1, 2);
short_count = 0;
arg += *ptr;
break;
case 'd':
case 'i':
arg += *ptr;
switch (long_count - short_count)
{
case 2:
push_argument(arg, T_SIGNED_LONG_LONG);
break;
case 1:
push_argument(arg, T_SIGNED_LONG);
break;
case 0:
push_argument(arg, T_SIGNED_INT);
break;
case -1:
push_argument(arg, T_SIGNED_SHORT);
break;
case -2:
push_argument(arg, T_SIGNED_SHORT_SHORT);
break;
default:
break;
}
finished = true;
break;
case 'o':
case 'u':
case 'x':
case 'X':
arg += *ptr;
switch (long_count - short_count)
{
case 2:
push_argument(arg, T_UNSIGNED_LONG_LONG);
break;
case 1:
push_argument(arg, T_UNSIGNED_LONG);
break;
case 0:
push_argument(arg, T_UNSIGNED_INT);
break;
case -1:
push_argument(arg, T_UNSIGNED_SHORT);
break;
case -2:
push_argument(arg, T_UNSIGNED_SHORT_SHORT);
break;
default:
break;
}
finished = true;
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'a':
case 'A':
arg += *ptr;
push_argument(arg, T_FLOAT);
finished = true;
break;
case 'c':
arg += *ptr;
push_argument(arg, T_CHAR);
finished = true;
break;
case 's':
arg += *ptr;
push_argument(arg, T_STRING);
finished = true;
break;
case 'p':
arg += *ptr;
push_argument(arg, T_POINTER);
finished = true;
break;
case 'C':
case 'S':
case 'm':
case 'n': // unsupported for now.
arg += *ptr;
push_argument(arg, T_ANYTHING);
finished = true;
break;
default:
arg += *ptr;
break;
}
++ptr;
}
if (!arg.empty())
push_argument(arg, T_LITERAL);
}
if (!txt.empty())
push_argument(txt, T_LITERAL);
}
void Format::mark_invalid(std::string & msg)
{
if (_valid)
{
_valid = false;
std::string finalmsg;
finalmsg += "** INVALID FORMAT: ";
finalmsg += msg;
finalmsg += " **";
_result = finalmsg;
}
}
void Format::raise_check(void)
{
if (!_valid && _raise)
throw InvalidFormat(_result);
}
bool Format::validity_check(void)
{
raise_check();
return _valid;
}
const Format::Argument * Format::next_argument(void)
{
// std::cerr << "size: " << _args.size() << std::endl;
while (true)
{
// std::cerr << "loop size: " << _args.size() << std::endl;
if (_args.empty())
return NULL; // throw NoArgumentLeft();
const Argument & top = _args.front();
if (top.type() == T_LITERAL)
{
// std::cerr << "top type == LITERAL, looping..." << std::endl;
_result += top.fmts();
pop_argument();
}
else
{
// std::cerr << "top type: " << top.type() << std::endl;
return &top;
}
}
}
void Format::pop_argument(void)
{
_args.pop();
}
void Format::push_argument(std::string & data, Format::Type type)
{
// std::cerr << "pushing type (" << type << ") with format (" << data << ")" << std::endl;
_args.push(Argument(data, type));
data.clear();
}
std::string Format::str()
{
if (!validity_check())
return _result;
if (next_argument() == NULL)
return _result;
std::string msg;
msg += "too few arguments passed for format '";
msg += _format;
msg += "' (";
msg += _format;
msg += ")";
mark_invalid(msg);
return _result;
}
差异被折叠。
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "saved_condition.hpp"
bool SavedCondition::wait(unsigned int msec)
{
bool ret = true;
switch_mutex_lock(_mutex);
if (!_signaled)
{
/* msec * 1000 = The amount of time in microseconds to wait. */
if (switch_thread_cond_timedwait(_condition, _mutex, (switch_interval_time_t)msec * 1000) != 0)
ret = false;
}
_signaled = false;
switch_mutex_unlock(_mutex);
return ret;
}
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _SAVED_CONDITION_
#define _SAVED_CONDITION_
#include <saved_condition.hpp>
extern "C"
{
#include <switch.h>
}
struct SavedCondition : public SavedConditionCommon// : public RefCounter < SavedCondition >
{
typedef switch_thread_cond_t BaseConditionType;
typedef switch_mutex_t BaseMutexType;
SavedCondition(switch_memory_pool_t *pool=NULL):
_pool(pool),
_can_delete_pool(false)
{
if(!_pool)
{
switch_core_new_memory_pool(&_pool);
_can_delete_pool = true;
}
switch_thread_cond_create(&_condition, _pool);
switch_mutex_init(&_mutex, SWITCH_MUTEX_DEFAULT, _pool);
}
//SavedCondition(const SavedCondition &);
~SavedCondition()
{
switch_thread_cond_destroy(_condition);
switch_mutex_destroy(_mutex);
if(_can_delete_pool)
switch_core_destroy_memory_pool(&_pool);
}
void signal(void)
{
switch_mutex_lock(_mutex);
_signaled = true;
switch_thread_cond_signal(_condition);
switch_mutex_unlock(_mutex);
}
void broadcast(void)
{
switch_mutex_lock(_mutex);
_signaled = true;
switch_thread_cond_broadcast(_condition);
switch_mutex_unlock(_mutex);
}
void wait(void)
{
switch_mutex_lock(_mutex);
if (!_signaled)
switch_thread_cond_wait(_condition, _mutex);
_signaled = false;
switch_mutex_unlock(_mutex);
}
bool wait(unsigned int);
void reset(void)
{
switch_mutex_lock(_mutex);
_signaled = false;
switch_mutex_unlock(_mutex);
}
BaseMutexType * mutex() { return _mutex; };
BaseConditionType * condition() { return _condition; };
protected:
BaseConditionType *_condition;
BaseMutexType *_mutex;
switch_memory_pool_t *_pool;
bool _can_delete_pool;
};
#endif /* _SAVED_CONDITION_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _SIMPLE_LOCK_HPP_
#define _SIMPLE_LOCK_HPP_
#include <simple_lock.hpp>
extern "C"
{
#include <switch.h>
}
template < typename Implementor >
struct SimpleLockBasic: public SimpleLockCommon < Implementor >
{
typedef SimpleLockCommon < Implementor > Super;
typedef typename Super::Result Result;
typedef switch_mutex_t BaseMutexType;
SimpleLockBasic(switch_memory_pool_t *pool = NULL)
: _pool(pool), _can_delete_pool( (_pool == NULL) )
{
if(!_pool)
switch_core_new_memory_pool(&_pool);
//switch_mutex_init(&_mutex, SWITCH_MUTEX_DEFAULT, _pool);
switch_mutex_init(&_mutex, SWITCH_MUTEX_NESTED, _pool);
}
virtual ~SimpleLockBasic()
{
/* do nothing */
};
void unreference()
{
switch_mutex_destroy(_mutex);
if (_can_delete_pool)
switch_core_destroy_memory_pool(&_pool);
}
Result trylock()
{
switch (switch_mutex_trylock(_mutex))
{
case SWITCH_STATUS_SUCCESS:
return Super::SUCCESS;
case SWITCH_STATUS_FALSE:
case SWITCH_STATUS_TERM:
case SWITCH_STATUS_NOTIMPL:
case SWITCH_STATUS_MEMERR:
case SWITCH_STATUS_GENERR:
case SWITCH_STATUS_SOCKERR:
case SWITCH_STATUS_NOTFOUND:
case SWITCH_STATUS_UNLOAD:
case SWITCH_STATUS_NOUNLOAD:
case SWITCH_STATUS_NOT_INITALIZED:
return Super::FAILURE;
//case SWITCH_STATUS_INUSE:
default:
return Super::ISINUSE;
}
}
void unlock()
{
switch_mutex_unlock(_mutex);
}
BaseMutexType * mutex() { return _mutex; };
protected:
BaseMutexType *_mutex;
switch_memory_pool_t *_pool;
bool _can_delete_pool;
};
struct SimpleLock: public SimpleLockBasic < SimpleLock >
{
typedef SimpleLockBasic < SimpleLock > Super;
typedef Super::Result Result;
SimpleLock(switch_memory_pool_t *pool = NULL)
: Super(pool) {};
Result lock()
{
switch (switch_mutex_lock(_mutex))
{
case SWITCH_STATUS_SUCCESS:
return Super::SUCCESS;
case SWITCH_STATUS_FALSE:
case SWITCH_STATUS_TERM:
case SWITCH_STATUS_NOTIMPL:
case SWITCH_STATUS_MEMERR:
case SWITCH_STATUS_GENERR:
case SWITCH_STATUS_SOCKERR:
case SWITCH_STATUS_NOTFOUND:
case SWITCH_STATUS_UNLOAD:
case SWITCH_STATUS_NOUNLOAD:
case SWITCH_STATUS_NOT_INITALIZED:
return Super::FAILURE;
//case SWITCH_STATUS_INUSE:
default:
return Super::ISINUSE;
}
}
};
template < unsigned int Retries = 10, unsigned int Interval = 50 >
struct SimpleNonBlockLock: public SimpleLockBasic < SimpleNonBlockLock < Retries, Interval > >
{
typedef SimpleLockBasic < SimpleNonBlockLock < Retries, Interval > > Super;
typedef typename Super::Result Result;
SimpleNonBlockLock(switch_memory_pool_t *pool = NULL)
: Super(pool) {};
inline Result lock()
{
for (unsigned int i = 0; i < Retries; i++)
{
Result ret = Super::trylock();
if (ret != Super::ISINUSE)
return ret;
usleep(Interval * 1000);
}
return Super::ISINUSE;
}
};
#endif /* _SIMPLE_LOCK_HPP_ */
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vector>
#ifndef _INITIALIZER_HPP_
#define _INITIALIZER_HPP_
template < typename Type >
struct Initializer: public std::vector< Type >
{
typedef std::vector< Type > super;
Initializer(Type e) { push_back(e); };
Initializer(Type & e) { push_back(e); };
Initializer & operator&(Initializer v)
{
insert(super::end(), v.begin(), v.end());
return *this;
};
Initializer & operator&(Initializer & v)
{
insert(super::end(), v.begin(), v.end());
return *this;
};
Initializer & operator&(Type v)
{
insert(super::end(), v);
return *this;
};
Initializer & operator&(Type & v)
{
insert(super::end(), v);
return *this;
};
};
#endif /* _INITIALIZER_HPP_ */
差异被折叠。
差异被折叠。
/*
KHOMP generic endpoint/channel library.
Copyright (C) 2007-2009 Khomp Ind. & Com.
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
Alternatively, the contents of this file may be used under the terms of the
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
case the provisions of "LGPL License" are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of
the LGPL License and not to allow others to use your version of this file under
the MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the LGPL License. If you do not
delete the provisions above, a recipient may use your version of this file under
either the MPL or the LGPL License.
The LGPL header follows below:
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string>
#include <utility>
#include <list>
#include <k3lapi.hpp>
#include <verbose.hpp>
#ifndef _K3LUTIL_HPP_
#define _K3LUTIL_HPP_
struct K3LUtil
{
typedef std::pair < std::string, unsigned int > ErrorCountPairType;
typedef std::list < ErrorCountPairType > ErrorCountType;
K3LUtil(K3LAPI & k3lapi): _k3lapi(k3lapi) {};
std::string callStatus(int32 dev, int32 channel,
Verbose::Presentation fmt = Verbose::HUMAN);
std::string channelStatus(int32, int32,
Verbose::Presentation fmt = Verbose::HUMAN);
std::string linkStatus(int32, int32,
Verbose::Presentation fmt = Verbose::HUMAN,
KSignaling sig = ksigInactive);
std::string getLinkStatus(int32, int32,
Verbose::Presentation fmt = Verbose::HUMAN);
unsigned int physicalLinkCount(int32 dev, bool count_virtual = false);
ErrorCountType linkErrorCount(int32, int32,
Verbose::Presentation fmt = Verbose::HUMAN);
protected:
K3LAPI & _k3lapi;
};
#endif /* _K3LUTIL_HPP_ */
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
#define MOD_KHOMP_VERSION "1.0 - (rev: 5796)"
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论