summaryrefslogtreecommitdiff
path: root/Examples/test-suite/memberin_extend_c.i
blob: c7e0173056e3189d5e4d273dcb0230c82fff63a5 (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
%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) {
  p->name[0] = '\0';
  strncat(p->name, val, sizeof(p->name) - 1);
  make_upper(p->name);
}
%}