]> Creatis software - gdcm.git/blob - gdcmPython/gdcm.i
* src/*.[h] all occurences of stl classes are now prefixed with
[gdcm.git] / gdcmPython / gdcm.i
1 %module gdcm
2 %{
3 #include "gdcmCommon.h"
4 #include "gdcmDictEntry.h"
5 #include "gdcmDict.h"
6 #include "gdcmDictSet.h"
7 #include "gdcmElValue.h"
8 #include "gdcmElValSet.h"
9 #include "gdcmHeader.h"
10 #include "gdcmFile.h"
11 using namespace std;
12
13 // Utility functions on strings for removing leading and trailing spaces
14 void EatLeadingAndTrailingSpaces(string & s) {
15         while ( s.length() && (s[0] == ' ') )
16                 s.erase(0,1);
17         while ( s.length() && (s[s.length()-1] == ' ') )
18                 s.erase(s.length()-1, 1);
19 }
20 %}
21 typedef  unsigned short guint16;
22 typedef  unsigned int guint32;
23
24 ////////////////////////////////////////////////////////////////////////////
25 %typemap(out) list<string> * {
26         PyObject* NewItem = (PyObject*)0;
27         PyObject* NewList = PyList_New(0); // The result of this typemap
28         for (list<string>::iterator NewString = ($1)->begin();
29              NewString != ($1)->end(); ++NewString) {
30                 NewItem = PyString_FromString(NewString->c_str());
31                 PyList_Append( NewList, NewItem);
32         }
33         $result = NewList;
34 }
35
36 ////////////////////////////////////////////////////////////////////////////
37 // Convert a c++ hash table in a python native dictionary
38 %typemap(out) map<string, list<string> > * {
39         PyObject* NewDict = PyDict_New(); // The result of this typemap
40         PyObject* NewKey = (PyObject*)0;
41         PyObject* NewVal = (PyObject*)0;
42
43         for (map<string, list<string> >::iterator tag = ($1)->begin();
44              tag != ($1)->end(); ++tag) {
45       string first = tag->first;
46       // Do not publish entries whose keys is made of spaces
47       if (first.length() == 0)
48          continue;
49                 NewKey = PyString_FromString(first.c_str());
50                 PyObject* NewList = PyList_New(0);
51                 for (list<string>::iterator Item = tag->second.begin();
52                      Item != tag->second.end(); ++Item) {
53                         NewVal = PyString_FromString(Item->c_str());
54                         PyList_Append( NewList, NewVal);
55                 }
56                 PyDict_SetItem( NewDict, NewKey, NewList);
57         }
58         $result = NewDict;
59 }
60
61 ////////////////////////////////////////////////////////////////////////////
62 // Convert a c++ hash table in a python native dictionary
63 %typemap(out) TagElValueHT & {
64         PyObject* NewDict = PyDict_New(); // The result of this typemap
65         string RawName;                   // Element name as gotten from gdcm
66         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
67         string RawValue;                  // Element value as gotten from gdcm
68         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
69
70         for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
71
72                 // The element name shall be the key:
73                 RawName = tag->second->GetName();
74                 // gdcm unrecognized (including not loaded because their size exceeds
75                 // the user specified treshold) elements are exported with their
76                 // TagKey as key.
77                 if (RawName == "Unknown")
78                         RawName = tag->second->GetKey();
79                 NewKey = PyString_FromString(RawName.c_str());
80
81                 // Element values are striped from leading/trailing spaces
82                 RawValue = tag->second->GetValue();
83                 EatLeadingAndTrailingSpaces(RawValue);
84                 NewVal = PyString_FromString(RawValue.c_str());
85
86                 PyDict_SetItem( NewDict, NewKey, NewVal);
87     }
88         $result = NewDict;
89 }
90
91 ////////////////////////////////////////////////////////////////////////////
92 // Deals with function returning a C++ string.
93 %typemap(out) string  {
94     $result = PyString_FromString(($1).c_str());
95 }
96
97 ////////////////////////////////////////////////////////////////////////////
98 %include "gdcmCommon.h"
99 %include "gdcmDictEntry.h"
100 %include "gdcmDict.h"
101 %include "gdcmDictSet.h"
102 %include "gdcmElValue.h"
103 %include "gdcmElValSet.h"
104 %include "gdcmHeader.h"
105 %include "gdcmFile.h"
106