summaryrefslogtreecommitdiff
path: root/Lib/c/std_string.i
blob: c8e2dff9d706fd3d10c4e197885851984ce642fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
%{
#include <string>
%}

namespace std {

// use "const string &" typemaps for wrapping member strings
%naturalvar string;

class string;

%typemap(cmodtype) string "char *"
%typemap(cmodtype) string * "char *"
%typemap(cmodtype) string & "char *"
%typemap(cmodtype) const string & "char *"
%typemap(ctype) string "char *"
%typemap(ctype) string * "char *"
%typemap(ctype) string & "char *"
%typemap(ctype) const string & "char *"
%typemap(couttype) string  "char *"
%typemap(couttype) string * "char *"
%typemap(couttype) string & "char *"
%typemap(couttype) const string & "char *"
%typemap(cppouttype, retobj="1") string "std::string*"
%typemap(cppouttype) const string &, string *, string & "std::string*"

%typemap(in) string {
  if ($input) {
    $1.assign($input);
  }
  else {
    $1.resize(0);
  }
}

%typemap(in) const string &, string *, string & {
  if ($input) {
    $1 = new std::string($input);
  }
  else {
    $1 = new std::string();
    $1->resize(0);
  }
}

%typemap(freearg) const string &, string *, string & {
  if ($1)
    delete $1;
}

%typemap(out) string, const string &, string *, string & {
  const char *str = cppresult->c_str();
  size_t len = strlen(str);
  $result = (char *) malloc(len + 1);
  memcpy($result, str, len);
  $result[len] = '\0';
}

}