summaryrefslogtreecommitdiff
path: root/Examples/test-suite/memberin_extend_c.i
blob: 0599e65a089f1453818af6837f0c1f90a69baf5b (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
%module memberin_extend_c

/* Example from the Manual, section 5.5.6: "Adding member functions to C structures" */

%{
typedef struct {
  char name[50];
} Person;
%}

typedef struct {
  %extend {
    char name[50];
  }
} Person;

%{
#include <ctype.h>
#include <string.h>

void make_upper(char *name) {
  char *c;
  for (c = name; *c; ++c)
    *c = (char)toupper((int)*c);
}

/* Specific implementation of set/get functions forcing capitalization */

char *Person_name_get(Person *p) {
  make_upper(p->name);
  return p->name;
}

void Person_name_set(Person *p, char *val) {
  strncpy(p->name,val,50);
  make_upper(p->name);
}
%}