/tags/rel-1-3-26/SWIG/Examples/test-suite/char_strings.i
Swig | 115 lines | 88 code | 22 blank | 5 comment | 0 complexity | ffb5f1405747eb9cebc23d49cce5a7c8 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
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(462) global_char_array1; // Unable to set variable of type char[] 10 11%{ 12#define OTHERLAND_MSG "Little message from the the safe world." 13#define CPLUSPLUS_MSG "A message from the deep dark world of C++, where anything is possible." 14static char *global_str = NULL; 15const int UINT_DIGITS = 10; // max unsigned int is 4294967295 16 17bool check(const char *str, unsigned int number) { 18 static char expected[256]; 19 sprintf(expected, "%s%d", OTHERLAND_MSG, number); 20 bool matches = (strcmp(str, expected) == 0); 21 if (!matches) printf("Failed: [%s][%s]\n", str, expected); 22 return matches; 23} 24 25%} 26 27%immutable global_const_char; 28 29%inline %{ 30// get functions 31char *GetCharHeapString() { 32 global_str = new char[sizeof(CPLUSPLUS_MSG)+1]; 33 strcpy(global_str, CPLUSPLUS_MSG); 34 return global_str; 35} 36 37const char *GetConstCharProgramCodeString() { 38 return CPLUSPLUS_MSG; 39} 40 41void DeleteCharHeapString() { 42 delete[] global_str; 43 global_str = NULL; 44} 45 46char *GetCharStaticString() { 47 static char str[sizeof(CPLUSPLUS_MSG)+1]; 48 strcpy(str, CPLUSPLUS_MSG); 49 return str; 50} 51 52char *GetCharStaticStringFixed() { 53 static char str[] = CPLUSPLUS_MSG; 54 return str; 55} 56 57const char *GetConstCharStaticStringFixed() { 58 static const char str[] = CPLUSPLUS_MSG; 59 return str; 60} 61 62// set functions 63bool SetCharHeapString(char *str, unsigned int number) { 64 delete[] global_str; 65 global_str = new char[strlen(str)+UINT_DIGITS+1]; 66 strcpy(global_str, str); 67 return check(global_str, number); 68} 69 70bool SetCharStaticString(char *str, unsigned int number) { 71 static char static_str[] = CPLUSPLUS_MSG; 72 strcpy(static_str, str); 73 return check(static_str, number); 74} 75 76bool SetCharArrayStaticString(char str[], unsigned int number) { 77 static char static_str[] = CPLUSPLUS_MSG; 78 strcpy(static_str, str); 79 return check(static_str, number); 80} 81 82bool SetConstCharHeapString(const char *str, unsigned int number) { 83 delete[] global_str; 84 global_str = new char[strlen(str)+UINT_DIGITS+1]; 85 strcpy(global_str, str); 86 return check(global_str, number); 87} 88 89bool SetConstCharStaticString(const char *str, unsigned int number) { 90 static char static_str[] = CPLUSPLUS_MSG; 91 strcpy(static_str, str); 92 return check(static_str, number); 93} 94 95bool SetConstCharArrayStaticString(const char str[], unsigned int number) { 96 static char static_str[] = CPLUSPLUS_MSG; 97 strcpy(static_str, str); 98 return check(static_str, number); 99} 100 101// get set function 102char *CharPingPong(char *str) { 103 return str; 104} 105 106// variables 107char *global_char = NULL; 108char global_char_array1[] = CPLUSPLUS_MSG; 109char global_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG; 110 111const char *global_const_char = CPLUSPLUS_MSG; 112const char global_const_char_array1[] = CPLUSPLUS_MSG; 113const char global_const_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG; 114 115%}