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 // Convert a c++ hash table in a python native dictionary
28 %typemap(out) map<string, list<string> > * {
29 PyObject* NewDict = PyDict_New(); // The result of this typemap
30 PyObject* NewKey = (PyObject*)0;
31 PyObject* NewVal = (PyObject*)0;
33 for (map<string, list<string> >::iterator tag = ($1)->begin();
34 tag != ($1)->end(); ++tag) {
35 string first = tag->first;
36 // Do not publish entries whose keys is made of spaces
37 if (first.length() == 0)
39 NewKey = PyString_FromString(first.c_str());
40 PyObject* NewList = PyList_New(0);
41 for (list<string>::iterator Item = tag->second.begin();
42 Item != tag->second.end(); ++Item) {
43 NewVal = PyString_FromString(Item->c_str());
44 PyList_Append( NewList, NewVal);
46 PyDict_SetItem( NewDict, NewKey, NewList);
51 // Convert a c++ hash table in a python native dictionary
52 %typemap(out) TagElValueHT & {
53 PyObject* NewDict = PyDict_New(); // The result of this typemap
54 string RawName; // Element name as gotten from gdcm
55 PyObject* NewKey = (PyObject*)0; // Associated name as python object
56 string RawValue; // Element value as gotten from gdcm
57 PyObject* NewVal = (PyObject*)0; // Associated value as python object
59 for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
61 // The element name shall be the key:
62 RawName = tag->second->GetName();
63 // gdcm unrecognized (including not loaded because their size exceeds
64 // the user specified treshold) elements are exported with their
66 if (RawName == "Unknown")
67 RawName = tag->second->GetKey();
68 NewKey = PyString_FromString(RawName.c_str());
70 // Element values are striped from leading/trailing spaces
71 RawValue = tag->second->GetValue();
72 EatLeadingAndTrailingSpaces(RawValue);
73 NewVal = PyString_FromString(RawValue.c_str());
75 PyDict_SetItem( NewDict, NewKey, NewVal);