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