]> 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 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 %typemap(out) list<string> * {
28         PyObject* NewItem = (PyObject*)0;
29         PyObject* NewList = PyList_New(0); // The result of this typemap
30         for (list<string>::iterator NewString = ($1)->begin();
31              NewString != ($1)->end(); ++NewString) {
32                 NewItem = PyString_FromString(NewString->c_str());
33                 PyList_Append( NewList, NewItem);
34         }
35         $result = NewList;
36 }
37
38 ////////////////////////////////////////////////////////////////////////////
39 // Convert a c++ hash table in a python native dictionary
40 %typemap(out) map<string, list<string> > * {
41         PyObject* NewDict = PyDict_New(); // The result of this typemap
42         PyObject* NewKey = (PyObject*)0;
43         PyObject* NewVal = (PyObject*)0;
44
45         for (map<string, list<string> >::iterator tag = ($1)->begin();
46              tag != ($1)->end(); ++tag) {
47       string first = tag->first;
48       // Do not publish entries whose keys is made of spaces
49       if (first.length() == 0)
50          continue;
51                 NewKey = PyString_FromString(first.c_str());
52                 PyObject* NewList = PyList_New(0);
53                 for (list<string>::iterator Item = tag->second.begin();
54                      Item != tag->second.end(); ++Item) {
55                         NewVal = PyString_FromString(Item->c_str());
56                         PyList_Append( NewList, NewVal);
57                 }
58                 PyDict_SetItem( NewDict, NewKey, NewList);
59         }
60         $result = NewDict;
61 }
62
63 ////////////////////////////////////////////////////////////////////////////
64 // Convert a c++ hash table in a python native dictionary
65 %typemap(out) TagElValueHT & {
66         PyObject* NewDict = PyDict_New(); // The result of this typemap
67         string RawName;                   // Element name as gotten from gdcm
68         PyObject* NewKey = (PyObject*)0;  // Associated name as python object
69         string RawValue;                  // Element value as gotten from gdcm
70         PyObject* NewVal = (PyObject*)0;  // Associated value as python object
71
72         for (TagElValueHT::iterator tag = $1->begin(); tag != $1->end(); ++tag) {
73
74                 // The element name shall be the key:
75                 RawName = tag->second->GetName();
76                 // gdcm unrecognized (including not loaded because their size exceeds
77                 // the user specified treshold) elements are exported with their
78                 // TagKey as key.
79                 if (RawName == "Unknown")
80                         RawName = tag->second->GetKey();
81                 NewKey = PyString_FromString(RawName.c_str());
82
83                 // Element values are striped from leading/trailing spaces
84                 RawValue = tag->second->GetValue();
85                 EatLeadingAndTrailingSpaces(RawValue);
86                 NewVal = PyString_FromString(RawValue.c_str());
87
88                 PyDict_SetItem( NewDict, NewKey, NewVal);
89     }
90         $result = NewDict;
91 }
92
93 ////////////////////////////////////////////////////////////////////////////
94 // Deals with function returning a C++ string.
95 %typemap(out) string  {
96     $result = PyString_FromString(($1).c_str());
97 }
98 %typemap(out) std::string  {
99     $result = PyString_FromString(($1).c_str());
100 }
101 //////%apply int { std::int };    
102
103 ////////////////////////////////////////////////////////////////////////////
104 %include "gdcmCommon.h"
105 %include "gdcmDictEntry.h"
106 %include "gdcmDict.h"
107 %include "gdcmDictSet.h"
108 %include "gdcmElValue.h"
109 %include "gdcmElValSet.h"
110 %include "gdcmHeader.h"
111 %include "gdcmFile.h"
112