]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* Eventually, python/gdcmPython was renamed to gdcmPython. This
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 %{
3 #include "gdcm.h"
4
5 // Utility functions on strings for removing leading and trailing spaces
6 void EatLeadingAndTrailingSpaces(string & s) {
7         while ( s.length() && (s[0] == ' ') )
8                 s.erase(0,1);
9         while ( s.length() && (s[s.length()-1] == ' ') )
10                 s.erase(s.length()-1, 1);
11 }
12 %}
13 typedef  unsigned short guint16;
14 typedef  unsigned int guint32;
15
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);
23         }
24         $result = NewList;
25 }
26
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;
32
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)
38          continue;
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);
45                 }
46                 PyDict_SetItem( NewDict, NewKey, NewList);
47         }
48         $result = NewDict;
49 }
50
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
58
59         for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
60
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
65                 // TagKey as key.
66                 if (RawName == "Unknown")
67                         RawName = tag->second->GetKey();
68                 NewKey = PyString_FromString(RawName.c_str());
69
70                 // Element values are striped from leading/trailing spaces
71                 RawValue = tag->second->GetValue();
72                 EatLeadingAndTrailingSpaces(RawValue);
73                 NewVal = PyString_FromString(RawValue.c_str());
74
75                 PyDict_SetItem( NewDict, NewKey, NewVal);
76     }
77         $result = NewDict;
78 }
79
80 %include gdcm.h