#include "normPostProcess.h"
#include "protoDebug.h"
#include <ctype.h>
#include <string.h>
NormPostProcessor::NormPostProcessor()
: process_argv(NULL), process_argc(0)
{
}
NormPostProcessor::~NormPostProcessor()
{
SetCommand(NULL);
}
bool NormPostProcessor::SetCommand(const char* cmd)
{
if (process_argv)
{
char** arg = process_argv;
while (*arg)
{
delete *arg;
arg++;
}
delete process_argv;
process_argv = NULL;
process_argc = 0;
}
if (NULL == cmd) return true; if (!strcmp(cmd, "none")) return SetCommand(NULL);
const char* ptr = cmd;
unsigned int argCount = 0;
while (isspace(*ptr) && ('\0' != *ptr)) ptr++;
while ('\0' != *ptr)
{
argCount++;
while (!isspace(*ptr) && ('\0' != *ptr)) ptr++;
while (isspace(*ptr) && ('\0' != *ptr)) ptr++;
}
if (!argCount) return true;
if (!(process_argv = new char*[argCount+2]))
{
PLOG(PL_FATAL, "NormPostProcessor::SetCommand new(process_argv) error: %s\n",
GetErrorString());
return false;
}
memset((void*)process_argv, 0, (argCount+2)*sizeof(char**));
process_argc = argCount;
ptr = cmd;
while (isspace(*ptr) && ('\0' != *ptr)) ptr++;
unsigned int index = 0;
while ('\0' != *ptr)
{
const char* argStart = ptr;
while (!isspace(*ptr) && ('\0' != *ptr)) ptr++;
unsigned int argLength = (unsigned int)(ptr - argStart);
char* arg;
if (!(arg = new char[argLength+1]))
{
SetCommand(NULL);
PLOG(PL_FATAL, "NormPostProcessor::SetCommand new(process_arg) error: %s\n",
GetErrorString());
return false;
}
strncpy(arg, argStart, argLength);
arg[argLength] = '\0';
process_argv[index++] = arg;
while (isspace(*ptr) && ('\0' != *ptr)) ptr++;
}
return true;
}