5 // Utility functions on strings for removing leading and trailing spaces
6 void EatLeadingAndTrailingSpaces(string & s) {
7 while ( s.length() && (s[0] == ' ') )
9 while ( s.length() && (s[s.length()-1] == ' ') )
10 s.erase(s.length()-1, 1);
13 typedef unsigned short guint16;
14 typedef unsigned int guint32;
16 %typemap(out) list<string> * {
17 PyObject* NewItem = (PyObject*)0;
18 PyObject* NewList = PyList_New(0); // The result of this typemap
19 for (list<string>::iterator NewString = ($1)->begin();
20 NewString != ($1)->end(); ++NewString) {
21 NewItem = PyString_FromString(NewString->c_str());
22 PyList_Append( NewList, NewItem);
27 %typemap(out) map<string, list<string> > * {
28 PyObject* NewDict = PyDict_New(); // The result of this typemap
29 PyObject* NewKey = (PyObject*)0;
30 PyObject* NewVal = (PyObject*)0;
32 for (map<string, list<string> >::iterator tag = ($1)->begin();
33 tag != ($1)->end(); ++tag) {
34 NewKey = PyString_FromString(tag->first.c_str());
35 PyObject* NewList = PyList_New(0);
36 for (list<string>::iterator Item = tag->second.begin();
37 Item != tag->second.end(); ++Item) {
38 NewVal = PyString_FromString(Item->c_str());
39 PyList_Append( NewList, NewVal);
41 PyDict_SetItem( NewDict, NewKey, NewList);
46 // Return an hash table as a python native dictionary
47 %typemap(out) TagElValueHT & {
48 PyObject* NewDict = PyDict_New(); // The result of this typemap
49 string RawName; // Element name as gotten from gdcm
50 PyObject* NewKey = (PyObject*)0; // Associated name as python object
51 string RawValue; // Element value as gotten from gdcm
52 PyObject* NewVal = (PyObject*)0; // Associated value as python object
54 for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
56 // The element name shall be the key:
57 RawName = tag->second->GetName();
58 // gdcm unrecognized (including not loaded because their size exceeds
59 // the user specified treshold) elements are exported with their
61 if (RawName == "Unknown")
62 RawName = tag->second->GetKey();
63 NewKey = PyString_FromString(RawName.c_str());
65 // Element values are striped from leading/trailing spaces
66 RawValue = tag->second->GetValue();
67 EatLeadingAndTrailingSpaces(RawValue);
68 NewVal = PyString_FromString(RawValue.c_str());
70 PyDict_SetItem( NewDict, NewKey, NewVal);