public int Run(string str)
{
lock (this)
{
int exit_code;
// check if the string we are trying to run doesn't exceed max length for the 'run_string' function
if (str.Length < RUN_STRING_MAX_LENGTH)
{
// GSAPI: run the string
int rc_run = _gs.gsapi_run_string(_gs_instance, str, 0, out exit_code);
if (ierrors.IsFatalIgnoreNeedInput(rc_run))
{
throw new GhostscriptAPICallException("gsapi_run_string", rc_run);
}
return rc_run;
}
else // we need to split a string into chunks
{
// GSAPI: prepare a Ghostscript for running string in chunks
int rc_run_beg = _gs.gsapi_run_string_begin(_gs_instance, 0, out exit_code);
if (ierrors.IsFatalIgnoreNeedInput(rc_run_beg))
{
throw new GhostscriptAPICallException("gsapi_run_string_begin", rc_run_beg);
}
int chunkStart = 0;
// start splitting a string into chunks
for (int size = str.Length; size > 0; size -= RUN_STRING_MAX_LENGTH)
{
int chunkSize = (size < RUN_STRING_MAX_LENGTH) ? size : RUN_STRING_MAX_LENGTH;
string chunk = str.Substring(chunkStart, chunkSize);
// GSAPI: run a chunk
int rc_run_con = _gs.gsapi_run_string_continue(_gs_instance, chunk, (uint)chunkSize, 0, out exit_code);
if (ierrors.IsFatalIgnoreNeedInput(rc_run_con))
{
throw new GhostscriptAPICallException("gsapi_run_string_continue", rc_run_con);
}
chunkStart += chunkSize;
}
// GSAPI: notify Ghostscript we are done with running chunked string
int rc_run_end = _gs.gsapi_run_string_end(_gs_instance, 0, out exit_code);
if (ierrors.IsFatalIgnoreNeedInput(rc_run_end))
{
throw new GhostscriptAPICallException("gsapi_run_string_end", rc_run_end);
}
return rc_run_end;
}
}
}