#ifndef VDR_PERL_COMMON_H
#define VDR_PERL_COMMON_H

#define strlenc(x) (sizeof(x)-1)
#define strn2cmp(a,b) strncmp((a), (b), strlenc(b))

char *strnadup(const char *str, int len);
char **SplitParameters(const char *String, int Count, bool ForceCount = true);
void FreeParameters(char **Array, int Count);

#define SET_PARAM_STRING(name,var,def) \
	if (strcmp(params[0], name) == 0) { \
		if (var != NULL) free(var); \
		if (params[1][0] != '\0') var = strdup(params[1]); \
		else var = def ? strdup(def) : NULL; \
	}

#define BEGIN_METHOD_MAP0(opts, x...) \
	char **_params = SplitParameters(opts, 1); \
	do { x } while (0); \
	FreeParameters(_params, 1);

#define BEGIN_METHOD_MAP1(parent, opts, x...) \
	char **_params = SplitParameters(opts, 1); \
	do { x parent::Method(opts); } while (0); \
	FreeParameters(_params, 1);

#define MAP_METHOD(name) \
	if (strcmp(_params[0], #name) == 0) { \
		name(_params[1]); \
		break; \
	}

#define BEGIN_OBJECT_MAP(opts, x...) \
	char **_params = SplitParameters(opts, 1); \
	if (_params == NULL) return; \
	do { x } while (0); \
	FreeParameters(_params, 1);

#define MAP_OBJECT(name) \
	if (strn2cmp(_params[0], #name "=") == 0) { \
		cPerl##name *obj = (cPerl##name*)strtoul(_params[0] + \
		                   strlenc(#name "="), NULL, 0); \
		obj->Method(_params[1]); \
		break; \
	}


#endif // VDR_PERL_COMMON_H
