import source from 1.3.40
[external/swig.git] / Examples / test-suite / char_strings.i
1 /*
2 A test case for testing char based strings. Useful for runtime testing,
3 there were some runtime crashes and leaks in the C# module in some of the scenarios
4 below.
5 */
6
7 %module char_strings
8
9 %warnfilter(SWIGWARN_TYPEMAP_VARIN_UNDEF) global_char_array1;  // Unable to set variable of type char[]
10 %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) global_const_char;  // Setting a const char * variable may leak memory.
11
12 #ifdef SWIG_ALLEGRO_CL
13 %{
14 #include <stdio.h>
15 %}
16 #endif
17
18 %{
19 #define OTHERLAND_MSG "Little message from the safe world."
20 #define CPLUSPLUS_MSG "A message from the deep dark world of C++, where anything is possible."
21 static char *global_str = NULL;
22 const int UINT_DIGITS = 10; // max unsigned int is 4294967295
23
24 bool check(const char *str, unsigned int number) {
25   static char expected[256];
26   sprintf(expected, "%s%d", OTHERLAND_MSG, number);
27   bool matches = (strcmp(str, expected) == 0);
28   if (!matches) printf("Failed: [%s][%s]\n", str, expected);
29   return matches;
30 }
31
32 %}
33
34 %immutable global_const_char;
35
36 %inline %{
37 // get functions
38 char *GetCharHeapString() {
39   global_str = new char[sizeof(CPLUSPLUS_MSG)+1];
40   strcpy(global_str, CPLUSPLUS_MSG);
41   return global_str;
42 }
43
44 const char *GetConstCharProgramCodeString() {
45   return CPLUSPLUS_MSG;
46 }
47
48 void DeleteCharHeapString() {
49   delete[] global_str;
50   global_str = NULL;
51 }
52
53 char *GetCharStaticString() {
54   static char str[sizeof(CPLUSPLUS_MSG)+1];
55   strcpy(str, CPLUSPLUS_MSG);
56   return str;
57 }
58
59 char *GetCharStaticStringFixed() {
60   static char str[] = CPLUSPLUS_MSG;
61   return str;
62 }
63
64 const char *GetConstCharStaticStringFixed() {
65   static const char str[] = CPLUSPLUS_MSG;
66   return str;
67 }
68
69 // set functions
70 bool SetCharHeapString(char *str, unsigned int number) {
71   delete[] global_str;
72   global_str = new char[strlen(str)+UINT_DIGITS+1];
73   strcpy(global_str, str);
74   return check(global_str, number);
75 }
76
77 bool SetCharStaticString(char *str, unsigned int number) {
78   static char static_str[] = CPLUSPLUS_MSG;
79   strcpy(static_str, str);
80   return check(static_str, number);
81 }
82
83 bool SetCharArrayStaticString(char str[], unsigned int number) {
84   static char static_str[] = CPLUSPLUS_MSG;
85   strcpy(static_str, str);
86   return check(static_str, number);
87 }
88
89 bool SetConstCharHeapString(const char *str, unsigned int number) {
90   delete[] global_str;
91   global_str = new char[strlen(str)+UINT_DIGITS+1];
92   strcpy(global_str, str);
93   return check(global_str, number);
94 }
95
96 bool SetConstCharStaticString(const char *str, unsigned int number) {
97   static char static_str[] = CPLUSPLUS_MSG;
98   strcpy(static_str, str);
99   return check(static_str, number);
100 }
101
102 bool SetConstCharArrayStaticString(const char str[], unsigned int number) {
103   static char static_str[] = CPLUSPLUS_MSG;
104   strcpy(static_str, str);
105   return check(static_str, number);
106 }
107
108 // get set function
109 char *CharPingPong(char *str) {
110   return str;
111 }
112
113 // variables
114 char *global_char = NULL;
115 char global_char_array1[] = CPLUSPLUS_MSG;
116 char global_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
117
118 const char *global_const_char = CPLUSPLUS_MSG;
119 const char global_const_char_array1[] = CPLUSPLUS_MSG;
120 const char global_const_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
121
122 %}
123
124
125 %typemap(newfree) char *GetNewCharString() { /* hello */ delete[] $1; }
126 %newobject GetNewCharString();
127
128 %inline {
129   char *GetNewCharString() {
130     char *nstr = new char[sizeof(CPLUSPLUS_MSG)+1];
131     strcpy(nstr, CPLUSPLUS_MSG);
132     return nstr;
133   }
134 }
135
136 %inline {
137   struct Formatpos;
138   struct OBFormat;
139   
140   static int GetNextFormat(Formatpos& itr, const  char*& str,OBFormat*& pFormat) {
141     return 0;
142   }
143   
144
145
146 }
147
148
149 %inline %{
150
151 // char *& tests
152 char *&GetCharPointerRef() {
153   static char str[] = CPLUSPLUS_MSG;
154   static char *ptr = str;
155   return ptr;
156 }
157
158 bool SetCharPointerRef(char *&str, unsigned int number) {
159   static char static_str[] = CPLUSPLUS_MSG;
160   strcpy(static_str, str);
161   return check(static_str, number);
162 }
163
164 const char *&GetConstCharPointerRef() {
165   static const char str[] = CPLUSPLUS_MSG;
166   static const char *ptr = str;
167   return ptr;
168 }
169
170 bool SetConstCharPointerRef(const char *&str, unsigned int number) {
171   static char static_str[] = CPLUSPLUS_MSG;
172   strcpy(static_str, str);
173   return check(static_str, number);
174 }
175
176 %}
177