PageRenderTime 16ms CodeModel.GetById 10ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/test-suite/char_strings.i

#
Swig | 195 lines | 149 code | 41 blank | 5 comment | 0 complexity | 30702a04ffc9839a785b9e9d5a99ff57 MD5 | raw file
  1/*
  2A test case for testing char based strings. Useful for runtime testing,
  3there were some runtime crashes and leaks in the C# module in some of the scenarios
  4below.
  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."
 21static char *global_str = NULL;
 22const int UINT_DIGITS = 10; // max unsigned int is 4294967295
 23
 24bool check(const char *const 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
 38char *GetCharHeapString() {
 39  global_str = new char[sizeof(CPLUSPLUS_MSG)+1];
 40  strcpy(global_str, CPLUSPLUS_MSG);
 41  return global_str;
 42}
 43
 44const char *GetConstCharProgramCodeString() {
 45  return CPLUSPLUS_MSG;
 46}
 47
 48void DeleteCharHeapString() {
 49  delete[] global_str;
 50  global_str = NULL;
 51}
 52
 53char *GetCharStaticString() {
 54  static char str[sizeof(CPLUSPLUS_MSG)+1];
 55  strcpy(str, CPLUSPLUS_MSG);
 56  return str;
 57}
 58
 59char *GetCharStaticStringFixed() {
 60  static char str[] = CPLUSPLUS_MSG;
 61  return str;
 62}
 63
 64const char *GetConstCharStaticStringFixed() {
 65  static const char str[] = CPLUSPLUS_MSG;
 66  return str;
 67}
 68
 69// set functions
 70bool 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
 77bool 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
 83bool 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
 89bool 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
 96bool 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
102bool 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
108bool SetCharConstStaticString(char *const str, unsigned int number) {
109  static char static_str[] = CPLUSPLUS_MSG;
110  strcpy(static_str, str);
111  return check(static_str, number);
112}
113
114bool SetConstCharConstStaticString(const char *const str, unsigned int number) {
115  static char static_str[] = CPLUSPLUS_MSG;
116  strcpy(static_str, str);
117  return check(static_str, number);
118}
119
120// get set function
121char *CharPingPong(char *str) {
122  return str;
123}
124char *CharArrayPingPong(char abcstr[]) {
125  return abcstr;
126}
127char *CharArrayDimsPingPong(char abcstr[16]) {
128  return abcstr;
129}
130
131// variables
132char *global_char = NULL;
133char global_char_array1[] = CPLUSPLUS_MSG;
134char global_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
135
136const char *global_const_char = CPLUSPLUS_MSG;
137const char global_const_char_array1[] = CPLUSPLUS_MSG;
138const char global_const_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
139
140%}
141
142
143%typemap(newfree) char *GetNewCharString() { /* hello */ delete[] $1; }
144%newobject GetNewCharString();
145
146%inline {
147  char *GetNewCharString() {
148    char *nstr = new char[sizeof(CPLUSPLUS_MSG)+1];
149    strcpy(nstr, CPLUSPLUS_MSG);
150    return nstr;
151  }
152}
153
154%inline {
155  struct Formatpos;
156  struct OBFormat;
157  
158  static int GetNextFormat(Formatpos& itr, const  char*& str,OBFormat*& pFormat) {
159    return 0;
160  }
161  
162
163
164}
165
166
167%inline %{
168
169// char *& tests
170char *&GetCharPointerRef() {
171  static char str[] = CPLUSPLUS_MSG;
172  static char *ptr = str;
173  return ptr;
174}
175
176bool SetCharPointerRef(char *&str, unsigned int number) {
177  static char static_str[] = CPLUSPLUS_MSG;
178  strcpy(static_str, str);
179  return check(static_str, number);
180}
181
182const char *&GetConstCharPointerRef() {
183  static const char str[] = CPLUSPLUS_MSG;
184  static const char *ptr = str;
185  return ptr;
186}
187
188bool SetConstCharPointerRef(const char *&str, unsigned int number) {
189  static char static_str[] = CPLUSPLUS_MSG;
190  strcpy(static_str, str);
191  return check(static_str, number);
192}
193
194%}
195