Commit 85664908 authored by Jeff Baumes's avatar Jeff Baumes
Browse files

Adding python support for string data with embedded nulls

Also adding some API to vtkDataReader/Writer to take advantage
of the new functionality.

Change-Id: Ice2d7ce8d772ac7a0409e021799263acfbe21204
parent 3c745388
......@@ -27,6 +27,7 @@
#define __vtkDataReader_h
#include "vtkAlgorithm.h"
#include "vtkStdString.h" // For API using strings
#define VTK_ASCII 1
#define VTK_BINARY 2
......@@ -78,6 +79,8 @@ public:
void SetInputString(const char *in, int len);
vtkGetMacro(InputStringLength, int);
void SetBinaryInputString(const char *, int len);
void SetInputString(const vtkStdString& input)
{ this->SetBinaryInputString(input.c_str(), input.length()); }
// Description:
// Specify the vtkCharArray to be used when reading from a string.
......
......@@ -1937,6 +1937,10 @@ char *vtkDataWriter::RegisterAndGetOutputString()
return tmp;
}
vtkStdString vtkDataWriter::GetOutputStdString()
{
return vtkStdString(this->OutputString, this->OutputStringLength);
}
void vtkDataWriter::PrintSelf(ostream& os, vtkIndent indent)
{
......
......@@ -68,6 +68,11 @@ public:
{
return reinterpret_cast<unsigned char *>(this->OutputString);
}
// Description:
// When WriteToOutputString is on, this method returns a copy of the
// output string in a vtkStdString.
vtkStdString GetOutputStdString();
// Description:
// This convenience method returns the string, sets the IVAR to NULL,
......
......@@ -179,6 +179,44 @@ bool vtkPythonGetStringValue(PyObject *o, T *&a, const char *exctext)
return false;
}
inline bool vtkPythonGetStdStringValue(PyObject *o, std::string &a, const char *exctext)
{
if (PyString_Check(o))
{
char* val;
Py_ssize_t len;
PyString_AsStringAndSize(o, &val, &len);
a = std::string(val, len);
return true;
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(o))
{
#ifdef _PyUnicode_AsDefaultEncodedString
PyObject *s = _PyUnicode_AsDefaultEncodedString(o, NULL);
#else
PyObject *s = PyUnicode_AsEncodedString(o, 0, NULL);
#endif
if (s)
{
char* val;
Py_ssize_t len;
PyString_AsStringAndSize(s, &val, &len);
a = std::string(val, len);
#ifndef _PyUnicode_AsDefaultEncodedString
Py_DECREF(s);
#endif
return true;
}
exctext = "(unicode conversion error)";
}
#endif
PyErr_SetString(PyExc_TypeError, exctext);
return false;
}
//--------------------------------------------------------------------
// Overloaded methods, mostly based on the above templates
......@@ -251,10 +289,8 @@ bool vtkPythonGetValue(PyObject *o, char *&a)
inline
bool vtkPythonGetValue(PyObject *o, std::string &a)
{
const char *b;
if (vtkPythonGetStringValue(o, b, "string is required"))
if (vtkPythonGetStdStringValue(o, a, "string is required"))
{
a = b;
return true;
}
return false;
......
......@@ -679,17 +679,18 @@ PyObject *vtkPythonArgs::BuildValue(const char *a)
inline
PyObject *vtkPythonArgs::BuildValue(const std::string &a)
{
return PyString_FromString(a.c_str());
return PyString_FromStringAndSize(a.c_str(), static_cast<Py_ssize_t>(a.size()));
}
inline
PyObject *vtkPythonArgs::BuildValue(const vtkUnicodeString &a)
{
const char *s = a.utf8_str();
std::string s;
a.utf8_str(s);
#ifdef Py_USING_UNICODE
return PyUnicode_DecodeUTF8(s, strlen(s), NULL);
return PyUnicode_DecodeUTF8(s.c_str(), static_cast<Py_ssize_t>(s.size()), NULL);
#else
return PyString_FromString(s);
return PyString_FromStringAndSize(s.c_str(), static_cast<Py_ssize_t>(s.size()));
#endif
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment