]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* python subdir moved to gdcmPython (preparation of distutils packaging).
[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 %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;
31
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);
40                 }
41                 PyDict_SetItem( NewDict, NewKey, NewList);
42         }
43         $result = NewDict;
44 }
45
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
53
54         for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
55
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
60                 // TagKey as key.
61                 if (RawName == "Unknown")
62                         RawName = tag->second->GetKey();
63                 NewKey = PyString_FromString(RawName.c_str());
64
65                 // Element values are striped from leading/trailing spaces
66                 RawValue = tag->second->GetValue();
67                 EatLeadingAndTrailingSpaces(RawValue);
68                 NewVal = PyString_FromString(RawValue.c_str());
69
70                 PyDict_SetItem( NewDict, NewKey, NewVal);
71     }
72         $result = NewDict;
73 }
74
75 %include gdcm.h