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