tweaked BuildName

develop
doomchild 2010-04-26 10:31:43 -05:00
parent e43bc0dfc5
commit 3bf53e59f1
1 changed files with 38 additions and 49 deletions

@ -90,9 +90,19 @@ static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss)
static DFHack::t_matglossPair ReverseBuildMatglossPair(PyObject* mObj) static DFHack::t_matglossPair ReverseBuildMatglossPair(PyObject* mObj)
{ {
DFHack::t_matglossPair mPair; DFHack::t_matglossPair mPair;
PyObject* temp;
temp = PyTuple_GetItem(mObj, 0);
mPair.type = (int16_t)PyInt_AsLong(temp);
Py_DECREF(temp);
mPair.type = (int16_t)PyInt_AsLong(PyTuple_GetItem(mObj, 0)); temp = PyTuple_GetItem(mObj, 1);
mPair.index = (int32_t)PyInt_AsLong(PyTuple_GetItem(mObj, 1));
mPair.index = (int32_t)PyInt_AsLong(temp);
Py_DECREF(temp);
return mPair; return mPair;
} }
@ -172,46 +182,28 @@ static PyObject* BuildNote(DFHack::t_note& note)
return noteObj; return noteObj;
} }
static int NAME_WORD_COUNT = 7;
static PyObject* BuildName(DFHack::t_name& name) static PyObject* BuildName(DFHack::t_name& name)
{ {
PyObject* nameObj; PyObject* nameObj;
PyObject *wordList, *speechList; PyObject *wordList, *speechList, *args;
PyObject* temp;
int wordCount = 7;
nameObj = PyObject_CallObject(Name_type, NULL);
if(name.first_name[0])
temp = PyString_FromString(name.first_name);
else
temp = PyString_FromString("");
OBJSET(nameObj, "first_name", temp);
if(name.nickname[0])
temp = PyString_FromString(name.nickname);
else
temp = PyString_FromString("");
OBJSET(nameObj, "nickname", temp);
temp = PyInt_FromLong(name.language); wordList = PyList_New(NAME_WORD_COUNT);
OBJSET(nameObj, "language", temp); speechList = PyList_New(NAME_WORD_COUNT);
temp = PyBool_FromLong((int)name.has_name); for(int i = 0; i < NAME_WORD_COUNT; i++)
OBJSET(nameObj, "has_name", temp);
wordList = PyList_New(wordCount);
speechList = PyList_New(wordCount);
for(int i = 0; i < wordCount; i++)
{ {
PyList_SET_ITEM(wordList, i, PyInt_FromLong(name.words[i])); PyList_SET_ITEM(wordList, i, PyInt_FromLong(name.words[i]));
PyList_SET_ITEM(speechList, i, PyInt_FromLong(name.parts_of_speech[i])); PyList_SET_ITEM(speechList, i, PyInt_FromLong(name.parts_of_speech[i]));
} }
OBJSET(nameObj, "words", wordList); args = Py_BuildValue("ssiOOO", name.first_name, name.nickname, name.language, \
OBJSET(nameObj, "parts_of_speech", speechList); PyBool_FromLong((int)name.has_name), wordList, speechList);
nameObj = PyObject_CallObject(Name_type, args);
Py_DECREF(args);
return nameObj; return nameObj;
} }
@ -219,8 +211,8 @@ static PyObject* BuildName(DFHack::t_name& name)
static DFHack::t_name ReverseBuildName(PyObject* nameObj) static DFHack::t_name ReverseBuildName(PyObject* nameObj)
{ {
PyObject *temp, *listTemp; PyObject *temp, *listTemp;
int boolTemp, arrLength; int boolTemp;
Py_ssize_t listLength, strLength; Py_ssize_t strLength;
char* strTemp; char* strTemp;
DFHack::t_name name; DFHack::t_name name;
@ -231,38 +223,33 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj)
boolTemp = (int)PyInt_AsLong(temp); boolTemp = (int)PyInt_AsLong(temp);
Py_DECREF(temp);
if(boolTemp != 0) if(boolTemp != 0)
name.has_name = true; name.has_name = true;
else else
name.has_name = false; name.has_name = false;
//I seriously doubt the name arrays will change length, but why take chances?
listTemp = PyObject_GetAttrString(nameObj, "words"); listTemp = PyObject_GetAttrString(nameObj, "words");
arrLength = sizeof(name.words) / sizeof(uint32_t); for(int i = 0; i < NAME_WORD_COUNT; i++)
listLength = PyList_Size(listTemp);
if(listLength < arrLength)
arrLength = listLength;
for(int i = 0; i < arrLength; i++)
name.words[i] = (uint32_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); name.words[i] = (uint32_t)PyInt_AsLong(PyList_GetItem(listTemp, i));
listTemp = PyObject_GetAttrString(nameObj, "parts_of_speech"); Py_DECREF(listTemp);
arrLength = sizeof(name.parts_of_speech) / sizeof(uint16_t);
listLength = PyList_Size(listTemp);
if(listLength < arrLength) listTemp = PyObject_GetAttrString(nameObj, "parts_of_speech");
arrLength = listLength;
for(int i = 0; i < arrLength; i++) for(int i = 0; i < NAME_WORD_COUNT; i++)
name.parts_of_speech[i] = (uint16_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); name.parts_of_speech[i] = (uint16_t)PyInt_AsLong(PyList_GetItem(listTemp, i));
Py_DECREF(listTemp);
temp = PyObject_GetAttrString(nameObj, "first_name"); temp = PyObject_GetAttrString(nameObj, "first_name");
strLength = PyString_Size(temp); strLength = PyString_Size(temp);
strTemp = PyString_AsString(temp); strTemp = PyString_AsString(temp);
Py_DECREF(temp);
if(strLength > 128) if(strLength > 128)
{ {
strncpy(name.first_name, strTemp, 127); strncpy(name.first_name, strTemp, 127);
@ -278,6 +265,8 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj)
strLength = PyString_Size(temp); strLength = PyString_Size(temp);
strTemp = PyString_AsString(temp); strTemp = PyString_AsString(temp);
Py_DECREF(temp);
if(strLength > 128) if(strLength > 128)
{ {
strncpy(name.nickname, strTemp, 127); strncpy(name.nickname, strTemp, 127);