#include "menuitems.h"
#include "common.h"
#include <stdlib.h>
#include <string.h>

// --- cPerlMenuEditStrItem ---------------------------------------------------

const char *cPerlMenuEditStrItem::AllowedChars = "";

cPerlMenuEditStrItem::cPerlMenuEditStrItem(FILE *Pipe, const char *Name, 
                                           const char *Value, int Length, 
                                           const char *Allowed,
                                           const char *OName):
		cMenuEditStrItem(Name, mValue = strnadup(Value, Length), Length, 
                         Allowed ? Allowed : AllowedChars),
		cPerlCallbackBase(Pipe, OName),
		mLength(Length)
{
}

cPerlMenuEditStrItem::~cPerlMenuEditStrItem()
{
	SendAction("M %s=%p SetResult '%s'", Name(), this,
	           *strescape(mValue, "'\\"));
	ReadAction("m");
	free(mValue);
}

// --- cPerlMenuEditStraItem --------------------------------------------------

cPerlMenuEditStraItem::cPerlMenuEditStraItem(FILE *Pipe, const char *Name, 
                                             int Value, int NumStrings,
                                             char **Strings, const char *OName):
		cMenuEditStraItem(Name, (mValue = Value, &mValue), NumStrings, Strings),
		cPerlCallbackBase(Pipe, OName),
		mNumStrings(NumStrings),
		mStrings(Strings)
{
}

cPerlMenuEditStraItem::~cPerlMenuEditStraItem()
{
	SendAction("M %s=%p SetResult '%d'", Name(), this, mValue);
	ReadAction("m");

	for (int i = 0; i < mNumStrings; ++i)
		free(mStrings[i]);
	free(mStrings);
}

