提交 f211064e authored 作者: Raul Fragoso's avatar Raul Fragoso

Merge of changes from Khomp plus some minor changes in the code structure

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/contrib@13989 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 cc666f42
MODNAME=mod_khomp
LOCAL_CFLAGS=-I./include -I./commons/include -lk3l -D_REENTRANT -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DK3L_HOSTSYSTEM
VERBOSE=1
LOCAL_CFLAGS=-I./include -I./commons -D_REENTRANT -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DK3L_HOSTSYSTEM
LOCAL_LDFLAGS=-lk3l
LOCAL_OBJS= ./commons/k3lapi.o ./commons/config_options.o ./commons/format.o ./commons/strings.o
LOCAL_OBJS+= ./src/globals.o ./src/opt.o ./src/khomp_pvt.o
LOCAL_OBJS= ./commons/k3lapi.o
include ../../../build/modmake.rules
include ../../../../build/modmake.rules
#include <set>
#include <map>
#include <vector>
#include <strings.hpp>
#include <format.hpp>
#ifndef INCLUDED_CONFIG_OPTIONS
#define INCLUDED_CONFIG_OPTIONS
template < typename base_type >
struct Variant
{
struct InvalidType {};
Variant(base_type * value) : _value(value)
{
_count = new int(1);
};
Variant(const Variant & v) : _value(v._value), _count(v._count)
{
(*_count)++;
}
~Variant()
{
(*_count)--;
if (_value && !(*_count))
{
delete _value;
delete _count;
}
};
template < typename return_type >
return_type & get(void)
{
try
{
return_type & ret = dynamic_cast < return_type & > (*_value);
return ret;
}
catch (std::bad_cast & e)
{
throw InvalidType();
}
};
int which()
{
return _value->which();
}
protected:
base_type * _value;
int *_count;
};
struct config_process_failure
{
config_process_failure(std::string _msg): msg(_msg) {};
std::string msg;
};
struct config_option
{
typedef int sint_type;
typedef unsigned int uint_type;
typedef bool bool_type;
typedef std::string string_type;
//typedef boost::function1<void, std::string>
typedef void (*fun_type)(std::string);
typedef std::set < string_type > 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 BaseType
{
BaseType() {}
virtual ~BaseType() {}
virtual int which() = 0;
};
struct sint_data_type : public BaseType
{
sint_data_type(sint_type & _sint_val, sint_type _sint_default, range<sint_type> _sint_range)
: sint_val(_sint_val), sint_default(_sint_default), sint_range(_sint_range) {};
int which()
{
return ID_SINT;
}
sint_type & sint_val;
sint_type sint_default;
range<sint_type> sint_range;
};
struct uint_data_type : public BaseType
{
uint_data_type(uint_type & _uint_val, uint_type _uint_default, range<uint_type> _uint_range)
: uint_val(_uint_val), uint_default(_uint_default), uint_range(_uint_range) {};
int which()
{
return ID_UINT;
}
uint_type & uint_val;
uint_type uint_default;
range<uint_type> uint_range;
};
struct bool_data_type : public BaseType
{
bool_data_type(bool_type & _bool_val, bool_type _bool_default)
: bool_val(_bool_val), bool_default(_bool_default) {};
int which()
{
return ID_BOOL;
}
bool_type & bool_val;
bool_type bool_default;
};
struct string_data_type : public BaseType
{
string_data_type(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 fun_data_type : public BaseType
{
fun_data_type(fun_type _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;
}
fun_type fun_val;
std::string fun_default;
string_allowed_type fun_allowed;
};
typedef Variant < BaseType > value_type;
//typedef boost::variant < sint_data_type , uint_data_type, bool_data_type, string_data_type, fun_data_type > value_type;
config_option(std::string, const string_type &, const string_type, string_allowed_type allowed, bool list_me = true);
config_option(std::string, const string_type &, const string_type = "", bool list_me = true);
config_option(std::string, const sint_type &, const sint_type = 0, sint_type min = -INT_MAX, sint_type max = INT_MAX, sint_type step = 1, bool list_me = true);
config_option(std::string, const uint_type &, const uint_type = 0, uint_type min = 0, uint_type max = UINT_MAX, uint_type step = 1, bool list_me = true);
config_option(std::string, const bool_type &, const bool_type = false, bool list_me = true);
config_option(std::string, fun_type, std::string defvalue, string_allowed_type allowed, bool list_me = true);
config_option(std::string, fun_type, std::string defvalue = "", bool list_me = true);
~config_option(void);
void set(string_type value);
void set(sint_type value);
void set(uint_type value);
void set(bool_type value);
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(config_option &);
protected:
std::string _my_name;
value_type _value_data;
bool _list_me;
const char ** _values;
bool _loaded;
};
struct config_options
{
typedef std::vector < std::string > messages_type;
config_options(void): _values(NULL) {};
typedef std::set < std::string > string_set;
typedef std::map < std::string, config_option > option_map_type;
typedef std::pair < std::string, config_option > 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(config_option option);
/* only valid in "process" (for backwards compatibility config files) */
bool synonym(std::string, std::string);
template <typename value_type>
void set(std::string name, value_type value)
{
option_map_type::iterator iter = _map.find(name);
if (iter == _map.end())
throw config_process_failure(STG(FMT("unknown option: %s") % name));
(*iter).second.set(value);
}
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(config_options &, 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 /* INCLUDED_CONFIG_OPTIONS */
#include "format.hpp"
const char * Format::INVALID_MSG = "** Invalid Format **";
Format::Format(const char * fmt, bool exception)
: _valid(true), _exception(exception)
{
std::string text;
std::string arg;
const char * ptr = fmt;
while(*ptr != '\0')
{
if (*ptr != '%')
{
text += *ptr;
ptr++;
continue;
}
else if(*(ptr+1) == '%')
{
text += *ptr;
ptr++;
text += *ptr;
ptr++;
continue;
}
_args.push(new Argument(text, "text"));
text.clear();
arg += *ptr;
bool finished = false;
ptr++;
while(*ptr != '\0' && !finished)
{
Argument * argument;
switch (*ptr)
{
case ' ':
arg += *ptr;
_args.push(new Argument(arg, "text"));
arg.clear();
finished = true;
break;
case '%':
_args.push(new Argument(arg, "text"));
arg.clear();
arg += *ptr;
break;
case 'h':
arg += *ptr;
break;
case 'l':
arg += *ptr;
break;
case 'd':
case 'i':
arg += *ptr;
argument = new Argument(arg, typeid(int).name());
argument->type(typeid(unsigned int).name());
_args.push(argument);
argument = NULL;
arg.clear();
finished = true;
break;
case 'o':
case 'u':
case 'x':
case 'X':
arg += *ptr;
argument = new Argument(arg, typeid(unsigned int).name());
argument->type(typeid(int).name());
_args.push(argument);
argument = NULL;
arg.clear();
finished = true;
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'a':
case 'A':
arg += *ptr;
argument = new Argument(arg, typeid(double).name());
argument->type(typeid(float).name());
_args.push(argument);
argument = NULL;
arg.clear();
finished = true;
break;
case 'c':
arg += *ptr;
argument = new Argument(arg, typeid(char).name());
argument->type(typeid(unsigned char).name());
_args.push(argument);
argument = NULL;
arg.clear();
finished = true;
break;
case 's':
arg += *ptr;
argument = new Argument(arg, typeid(const char *).name());
argument->type(typeid(char *).name());
argument->type(typeid(const unsigned char *).name());
argument->type(typeid(unsigned char *).name());
_args.push(argument);
argument = NULL;
arg.clear();
finished = true;
break;
case 'p':
case 'C':
case 'S':
case 'n':
case 'm':
arg += *ptr;
_args.push(new Argument(arg, ""));
arg.clear();
finished = true;
break;
default:
arg += *ptr;
break;
}
ptr++;
}
}
if(!text.empty())
_args.push(new Argument(text, "text"));
if(!arg.empty())
_args.push(new Argument(arg, "text"));
}
std::string Format::str()
{
if(!_valid)
{
if(_exception)
throw InvalidFormat(INVALID_MSG);
_result.clear();
_result += INVALID_MSG;
return _result;
}
Argument * top;
bool text;
do
{
text = false;
if(_args.empty())
return _result;
top = _args.front();
_args.pop();
if(top->compare("text"))
{
_result += top->fmts();
text = true;
free(top);
}
}
while(text);
free(top);
_valid = false;
if(_exception)
throw InvalidFormat(INVALID_MSG);
_result.clear();
_result += INVALID_MSG;
return _result;
}
#ifndef _FORMAT_H_
#define _FORMAT_H_
#include <string>
#include <queue>
#include <set>
#include <typeinfo>
#include <cstring>
#include <cstdlib>
/* macros used for shortening lines and making the code clearer */
#define STG(x) (x).str()
#define FMT(x) Format(x)
struct Format
{
static const char * INVALID_MSG;
struct Argument
{
typedef std::set < std::string > StringSet;
Argument(std::string init_fmts, std::string init_type)
: _fmts(init_fmts)
{
_types.insert(init_type);
};
void type(std::string t)
{
_types.insert(t);
}
bool compare(const char * type)
{
for(StringSet::iterator it = _types.begin(); it != _types.end(); it++)
{
if(!strcmp(type, (*it).c_str()))
return true;
}
return false;
}
std::string fmts()
{
return _fmts;
}
protected:
std::string _fmts;
StringSet _types;
};
struct InvalidFormat
{
InvalidFormat(std::string msg) : _msg(msg) {}
std::string _msg;
};
typedef std::queue < Argument *> ArgumentsQueue;
Format(const char * fmt, bool exception = false);
std::string str();
template < typename Type >
Format & operator % ( Type value )
{
Argument * top;
bool text;
do
{
text = false;
if(_args.empty()) {
_valid = false;
if(_exception)
throw InvalidFormat(INVALID_MSG);
return *this;
}
top = _args.front();
_args.pop();
if(top->compare("text"))
{
_result += top->fmts();
text = true;
free(top);
}
}
while(text);
char tmp[1000];
if(top->compare(""))
{
snprintf(tmp, 1000, top->fmts().c_str(), value);
_result += tmp;
}
else if (top->compare(typeid(Type).name()))
{
snprintf(tmp, 1000, top->fmts().c_str(), value);
_result += tmp;
}
else
{
free(top);
_valid = false;
if(_exception)
throw InvalidFormat(INVALID_MSG);
return *this;
}
free(top);
return *this;
}
Format & operator%( std::string & value )
{
Argument * top;
bool text;
do
{
text = false;
if(_args.empty()) {
_valid = false;
if(_exception)
throw InvalidFormat(INVALID_MSG);
return *this;
}
top = _args.front();
_args.pop();
if(top->compare("text"))
{
_result += top->fmts();
text = true;
free(top);
}
}
while(text);
char tmp[1000];
if (top->compare(typeid(char *).name()))
{
snprintf(tmp, 1000, top->fmts().c_str(), value.c_str());
_result += tmp;
}
else
{
free(top);
_valid = false;
if(_exception)
throw InvalidFormat(INVALID_MSG);
return *this;
}
free(top);
return *this;
}
private:
bool _valid;
bool _exception;
std::string _result;
ArgumentsQueue _args;
};
#endif /* _FORMAT_H_ */
......@@ -7,169 +7,165 @@ K3LAPI::K3LAPI()
K3LAPI::~K3LAPI()
{
_device_count = 0;
if (_device_type) { delete[] _device_type; _device_type = NULL; }
if (_device_config) { delete[] _device_config; _device_config = NULL; }
if (_channel_config) { delete[] _channel_config; _channel_config = NULL; }
if (_link_config) { delete[] _link_config; _link_config = NULL; }
if (_channel_count) { delete[] _channel_count; _channel_count = NULL; }
if (_link_count) { delete[] _link_count; _link_count = NULL; }
_device_count = 0;
if (_device_type) { delete[] _device_type; _device_type = NULL; }
if (_device_config) { delete[] _device_config; _device_config = NULL; }
if (_channel_config) { delete[] _channel_config; _channel_config = NULL; }
if (_link_config) { delete[] _link_config; _link_config = NULL; }
if (_channel_count) { delete[] _channel_count; _channel_count = NULL; }
if (_link_count) { delete[] _link_count; _link_count = NULL; }
}
/* initialize the whole thing! */
void K3LAPI::start(void)
{
/* tie the used k3l to the compiled k3l version */
char *ret = k3lStart(2, 0, 0); //k3lApiMajorVersion, k3lApiMinorVersion, 0); //k3lApiBuildVersion);
if (ret && *ret)
throw start_failed(ret);
/* call init automagically */
init();
/* tie the used k3l to the compiled k3l version */
char *ret = k3lStart(2, 0, 0); //k3lApiBuildVersion);
if (ret && *ret)
throw start_failed(ret);
/* call init automagically */
init();
}
void K3LAPI::stop(void)
{
k3lStop();
k3lStop();
}
/* envio de comandos para placa */
void K3LAPI::mixer(int32 dev, int32 obj, byte track, KMixerSource src, int32 index)
{
KMixerCommand mix;
KMixerCommand mix;
mix.Track = track;
mix.Source = src;
mix.Source = src;
mix.SourceIndex = index;
command(dev, obj, CM_MIXER, (const char *) &mix);
command(dev, obj, CM_MIXER, (const char *) &mix);
}
void K3LAPI::mixerCTbus(int32 dev, int32 obj, byte track, KMixerSource src, int32 index)
{
KMixerCommand mix;
KMixerCommand mix;
mix.Track = track;
mix.Source = src;
mix.Source = src;
mix.SourceIndex = index;
command(dev, obj, CM_MIXER_CTBUS, (const char *) &mix);
command(dev, obj, CM_MIXER_CTBUS, (const char *) &mix);
}
void K3LAPI::command(int32 dev, int32 obj, int32 code, std::string & str)
{
command(dev, obj, code, str.c_str());
command(dev, obj, code, str.c_str());
}
void K3LAPI::command (int32 dev, int32 obj, int32 code, const char * parms)
{
K3L_COMMAND cmd;
cmd.Cmd = code;
cmd.Cmd = code;
cmd.Object = obj;
cmd.Params = (byte *)parms;
cmd.Params = (byte *)parms;
int32 rc = k3lSendCommand(dev, &cmd);
if (rc != ksSuccess)
throw failed_command(code, dev, obj, rc);
int32 rc = k3lSendCommand(dev, &cmd);
if (rc != ksSuccess)
throw failed_command(code, dev, obj, rc);
}
void K3LAPI::raw_command(int32 dev, int32 dsp, std::string & str)
{
raw_command(dev, dsp, str.data(), str.size());
raw_command(dev, dsp, str.data(), str.size());
}
void K3LAPI::raw_command(int32 dev, int32 dsp, const char * cmds, int32 size)
{
std::string str(cmds, size);
int32 rc = k3lSendRawCommand(dev, dsp, (void *)cmds, size);
int32 rc = k3lSendRawCommand(dev, dsp, (void *)cmds, size);
if (rc != ksSuccess)
throw failed_raw_command(dev, dsp, rc);
if (rc != ksSuccess)
throw failed_raw_command(dev, dsp, rc);
}
KLibraryStatus K3LAPI::get_param(K3L_EVENT *ev, const char *name, std::string &res)
{
char tmp_param[256];
memset((void*)tmp_param, 0, sizeof(tmp_param));
char tmp_param[256];
memset((void*)tmp_param, 0, sizeof(tmp_param));
int32 rc = k3lGetEventParam (ev, (sbyte *)name, (sbyte *)tmp_param, sizeof(tmp_param)-1);
if (rc != ksSuccess)
return (KLibraryStatus)rc;
int32 rc = k3lGetEventParam (ev, (sbyte *)name, (sbyte *)tmp_param, sizeof(tmp_param)-1);
if (rc != ksSuccess)
return (KLibraryStatus)rc;
res.append(tmp_param, strlen(tmp_param));
return ksSuccess;
res.append(tmp_param, strlen(tmp_param));
return ksSuccess;
}
std::string K3LAPI::get_param(K3L_EVENT *ev, const char *name)
{
std::string res;
std::string res;
KLibraryStatus rc = get_param(ev, name, res);
KLibraryStatus rc = get_param(ev, name, res);
if (rc != ksSuccess)
throw get_param_failed(name, rc);
return res;
if (rc != ksSuccess)
throw get_param_failed(name, rc);
return res;
}
void K3LAPI::init(void)
{
if (_device_count != 0) return;
_device_count = k3lGetDeviceCount();
_device_type = new KDeviceType[_device_count];
_device_config = new device_conf_type[_device_count];
_channel_config = new channel_ptr_conf_type[_device_count];
_KChannel = new KChannel_ptr_t[_device_count];
_link_config = new link_ptr_conf_type[_device_count];
_channel_count = new unsigned int[_device_count];
_link_count = new unsigned int[_device_count];
for (unsigned int dev = 0; dev < _device_count; dev++)
{
_device_type[dev] = (KDeviceType) k3lGetDeviceType(dev);
/* caches each device config */
if (k3lGetDeviceConfig(dev, ksoDevice + dev, &(_device_config[dev]), sizeof(_device_config[dev])) != ksSuccess)
throw start_failed("k3lGetDeviceConfig(device)");
/* adjust channel/link count for device */
_channel_count[dev] = _device_config[dev].ChannelCount;
_link_count[dev] = _device_config[dev].LinkCount;
/* caches each channel config */
_channel_config[dev] = new channel_conf_type[_channel_count[dev]];
_KChannel[dev] = new KChannel_t[_channel_count[dev]];
for (unsigned int obj = 0; obj < _channel_count[dev]; obj++)
{
if (k3lGetDeviceConfig(dev, ksoChannel + obj, &(_channel_config[dev][obj]),
sizeof(_channel_config[dev][obj])) != ksSuccess)
{
throw start_failed("k3lGetDeviceConfig(channel)");
}
}
/* adjust link count for device */
_link_count[dev] = _device_config[dev].LinkCount;
/* caches each link config */
_link_config[dev] = new link_conf_type[_link_count[dev]];
for (unsigned int obj = 0; obj < _link_count[dev]; obj++)
{
if (k3lGetDeviceConfig(dev, ksoLink + obj, &(_link_config[dev][obj]),
sizeof(_link_config[dev][obj])) != ksSuccess)
throw start_failed("k3lGetDeviceConfig(link)");
}
}
if (_device_count != 0) return;
_device_count = k3lGetDeviceCount();
_device_type = new KDeviceType[_device_count];
_device_config = new device_conf_type[_device_count];
_channel_config = new channel_ptr_conf_type[_device_count];
_link_config = new link_ptr_conf_type[_device_count];
_channel_count = new unsigned int[_device_count];
_link_count = new unsigned int[_device_count];
for (unsigned int dev = 0; dev < _device_count; dev++)
{
_device_type[dev] = (KDeviceType) k3lGetDeviceType(dev);
/* caches each device config */
if (k3lGetDeviceConfig(dev, ksoDevice + dev, &(_device_config[dev]), sizeof(_device_config[dev])) != ksSuccess)
throw start_failed("k3lGetDeviceConfig(device)");
/* adjust channel/link count for device */
_channel_count[dev] = _device_config[dev].ChannelCount;
_link_count[dev] = _device_config[dev].LinkCount;
/* caches each channel config */
_channel_config[dev] = new channel_conf_type[_channel_count[dev]];
for (unsigned int obj = 0; obj < _channel_count[dev]; obj++)
{
if (k3lGetDeviceConfig(dev, ksoChannel + obj, &(_channel_config[dev][obj]),
sizeof(_channel_config[dev][obj])) != ksSuccess)
throw start_failed("k3lGetDeviceConfig(channel)");
}
/* adjust link count for device */
_link_count[dev] = _device_config[dev].LinkCount;
/* caches each link config */
_link_config[dev] = new link_conf_type[_link_count[dev]];
for (unsigned int obj = 0; obj < _link_count[dev]; obj++)
{
if (k3lGetDeviceConfig(dev, ksoLink + obj, &(_link_config[dev][obj]),
sizeof(_link_config[dev][obj])) != ksSuccess)
throw start_failed("k3lGetDeviceConfig(link)");
}
}
}
#include <string>
#include <k3l.h>
/* if using full k3l.h (for softpbx), version already defined. */
#ifndef k3lApiMajorVersion
# include <k3lVersion.h>
#endif
#ifdef __GNUC_PREREQ
#if __GNUC_PREREQ(4,3)
#include <cstring>
#endif
#endif
#ifndef INCLUDED_K3LAPI_HPP
#define INCLUDED_K3LAPI_HPP
struct K3LAPI
{
/* exceptions */
struct start_failed
{
start_failed(const char * _msg) : msg(_msg) {};
std::string msg;
};
struct failed_command
{
failed_command(int32 _code, unsigned short _dev, unsigned short _obj, int32 _rc)
: code(_code), dev(_dev), obj(_obj), rc(_rc) {};
int32 code;
unsigned short dev;
unsigned short obj;
int32 rc;
};
struct failed_raw_command
{
failed_raw_command(unsigned short _dev, unsigned short _dsp, int32 _rc)
: dev(_dev), dsp(_dsp), rc(_rc) {};
unsigned short dev;
unsigned short dsp;
int32 rc;
};
struct invalid_session
{
invalid_session(unsigned int _device, unsigned int _channel)
: device(_device), channel(_channel) {};
unsigned int device;
unsigned int channel;
};
struct invalid_device
{
invalid_device(int32 _device)
: device(_device) {};
int32 device;
};
struct invalid_channel
{
invalid_channel(int32 _device, int32 _channel)
: device(_device), channel(_channel) {};
int32 device, channel;
};
struct invalid_link
{
invalid_link(unsigned int _device, unsigned int _link)
: device(_device), link(_link) {};
int32 device, link;
};
struct get_param_failed
{
get_param_failed(std::string _name, int32 _rc)
: name(_name), rc((KLibraryStatus)_rc) {};
std::string name;
KLibraryStatus rc;
};
typedef K3L_DEVICE_CONFIG device_conf_type;
typedef K3L_CHANNEL_CONFIG channel_conf_type;
typedef K3L_CHANNEL_CONFIG * channel_ptr_conf_type;
typedef K3L_LINK_CONFIG link_conf_type;
typedef K3L_LINK_CONFIG * link_ptr_conf_type;
/* constructors/destructors */
K3LAPI();
~K3LAPI();
/* (init|final)ialize the whole thing! */
void start(void);
void stop(void);
/* verificacao de intervalos */
inline bool valid_device(int32 dev)
{
return (dev >= 0 && dev < ((int32)_device_count));
}
inline bool valid_channel(int32 dev, int32 obj)
{
return (valid_device(dev) && obj >= 0 && obj < ((int32)_channel_count[dev]));
}
inline bool valid_link(int32 dev, int32 obj)
{
return (valid_device(dev) && obj >= 0 && obj < ((int32)_link_count[dev]));
}
/* identificadores alto-nivel de objectos */
struct target
{
typedef enum { DEVICE, CHANNEL, MIXER, LINK } target_type;
target(K3LAPI & k3lapi, target_type type_init, int32 device_value, int32 object_value)
: type(type_init),
device((unsigned short)device_value),
object((unsigned short)object_value)
{
switch (type_init)
{
case DEVICE:
if (!k3lapi.valid_device(device))
throw invalid_device(device);
break;
case CHANNEL:
case MIXER:
if (!k3lapi.valid_channel(device, object))
throw invalid_channel(device, object);
break;
case LINK:
if (!k3lapi.valid_link(device, object))
throw invalid_link(device, object);
break;
}
};
target_type type;
unsigned short device;
unsigned short object;
};
/* envio de comandos para placa (geral) */
void raw_command(int32 dev, int32 dsp, std::string & str);
void raw_command(int32 dev, int32 dsp, const char * cmds, int32 size);
/* obter dados 'cacheados' (geral) */
inline unsigned int device_count(void)
{
return _device_count;
}
/* envio de comandos para placa (sem identificadores) */
void mixer(int32 dev, int32 obj, byte track, KMixerSource src, int32 index);
void mixerCTbus(int32 dev, int32 obj, byte track, KMixerSource src, int32 index);
void command (int32 dev, int32 obj, int32 code, std::string & str);
void command (int32 dev, int32 obj, int32 code, const char * parms = NULL);
/* obter dados 'cacheados' (sem identificadores) */
inline unsigned int channel_count(int32 dev)
{
if (!valid_device(dev))
throw invalid_device(dev);
return _channel_count[dev];
}
inline unsigned int link_count(int32 dev)
{
if (!valid_device(dev))
throw invalid_device(dev);
return _link_count[dev];
}
KDeviceType device_type(int32 dev)
{
if (!valid_device(dev))
throw invalid_device(dev);
return _device_type[dev];
}
K3L_DEVICE_CONFIG & device_config(int32 dev)
{
if (!valid_device(dev))
throw invalid_device(dev);
return _device_config[dev];
}
K3L_CHANNEL_CONFIG & channel_config(int32 dev, int32 obj)
{
if (!valid_channel(dev, obj))
throw invalid_channel(dev, obj);
return _channel_config[dev][obj];
}
K3L_LINK_CONFIG & link_config(int32 dev, int32 obj)
{
if (!valid_channel(dev, obj))
throw invalid_channel(dev, obj);
return _link_config[dev][obj];
}
/* envio de comandos para placa (com identificadores) */
void mixer(target & tgt, byte track, KMixerSource src, int32 index)
{
mixer((int32)tgt.device, (int32)tgt.object, track, src, index);
}
void mixerCTbus(target & tgt, byte track, KMixerSource src, int32 index)
{
mixerCTbus((int32)tgt.device, (int32)tgt.object, track, src, index);
}
void command (target & tgt, int32 code, std::string & str)
{
command((int32)tgt.device, (int32)tgt.object, code, str);
};
void command (target & tgt, int32 code, const char * parms = NULL)
{
command((int32)tgt.device, (int32)tgt.object, code, parms);
};
/* obter dados 'cacheados' (com indentificadores) */
inline unsigned int channel_count(target & tgt)
{
return _channel_count[tgt.device];
}
inline unsigned int link_count(target & tgt)
{
return _link_count[tgt.device];
}
KDeviceType device_type(target & tgt)
{
return _device_type[tgt.device];
}
K3L_DEVICE_CONFIG & device_config(target & tgt)
{
return _device_config[tgt.device];
}
K3L_CHANNEL_CONFIG & channel_config(target & tgt)
{
return _channel_config[tgt.device][tgt.object];
}
K3L_LINK_CONFIG & link_config(target & tgt)
{
return _link_config[tgt.device][tgt.object];
}
/* pega valores em strings de eventos */
KLibraryStatus get_param(K3L_EVENT *ev, const char *name, std::string &res);
std::string get_param(K3L_EVENT *ev, const char *name);
/* inicializa valores em cache */
void init(void);
protected:
unsigned int _device_count;
unsigned int * _channel_count;
unsigned int * _link_count;
device_conf_type * _device_config;
channel_ptr_conf_type * _channel_config;
link_ptr_conf_type * _link_config;
KDeviceType * _device_type;
};
#endif /* INCLUDED_K3LAPI_HPP */
#include <strings.hpp>
void Strings::Merger::add(std::string s)
{
_list.push_back(s);
};
std::string Strings::Merger::merge(const std::string & sep)
{
list_type::iterator i = _list.begin();
std::string res;
if (i != _list.end())
{
res += (*i);
++i;
};
while (i != _list.end())
{
res += sep;
res += (*i);
++i;
}
return res;
};
std::string Strings::Merger::merge(const char *sep)
{
std::string ssep(sep);
return merge(ssep);
}
void Strings::tokenize(const std::string & str, Strings::vector_type & tokens,
const std::string & delims, long int max_tokens)
{
std::string::size_type init = str.find_first_not_of(delims, 0);
std::string::size_type fini = str.find_first_of(delims, init);
long int cur_token = 1;
while (std::string::npos != init)
{
if (std::string::npos != fini && cur_token < max_tokens)
{
tokens.push_back(str.substr(init, fini - init));
++cur_token;
}
else
{
tokens.push_back(str.substr(init, str.size() - init));
break;
}
init = str.find_first_not_of(delims, fini);
fini = str.find_first_of(delims, init);
}
}
bool Strings::toboolean(std::string str)
{
std::string tmp(str);
Strings::lower(tmp);
if ((tmp == "true") || (tmp == "yes")) return true;
if ((tmp == "false") || (tmp == "no")) return false;
throw invalid_value(str);
}
long Strings::tolong(std::string str, int base)
{
char *str_end = 0;
unsigned long value = strtol(str.c_str(), &str_end, base);
if (str_end && *str_end == 0)
return value;
throw invalid_value(str);
}
unsigned long Strings::toulong(std::string str, int base)
{
char *str_end = 0;
unsigned long value = strtoul(str.c_str(), &str_end, base);
if (str_end && *str_end == 0)
return value;
throw invalid_value(str);
}
unsigned long long Strings::toulonglong(std::string str, int base)
{
#if defined(_WINDOWS) || defined(_Windows) || defined(_WIN32) || defined(WIN32)
throw not_implemented();
#else
char *str_end = 0;
unsigned long long value = strtoull(str.c_str(), &str_end, base);
if (str_end && *str_end == 0)
return value;
throw invalid_value(str);
#endif
}
std::string Strings::fromboolean(bool value)
{
if (value) return "true";
else return "false";
}
std::string Strings::lower(std::string str)
{
std::string res;
for (std::string::iterator i = str.begin(); i != str.end(); i++)
res += tolower((*i));
return res;
}
std::string Strings::hexadecimal(std::string value)
{
std::string result;
for (std::string::iterator i = value.begin(); i != value.end(); i++)
{
if (i != value.begin())
result += " ";
result += STG(FMT("%2x") % (unsigned int)(*i));
}
return result;
}
std::string Strings::trim(const std::string& str, const std::string& trim_chars)
{
std::string result(str);
result.erase( result.find_last_not_of( trim_chars ) + 1 );
result.erase( 0, result.find_first_not_of( trim_chars ) );
return result;
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论