PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. %module char_strings
  7. %warnfilter(SWIGWARN_TYPEMAP_VARIN_UNDEF) global_char_array1; // Unable to set variable of type char[]
  8. %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) global_const_char; // Setting a const char * variable may leak memory.
  9. #ifdef SWIG_ALLEGRO_CL
  10. %{
  11. #include <stdio.h>
  12. %}
  13. #endif
  14. %{
  15. #define OTHERLAND_MSG "Little message from the safe world."
  16. #define CPLUSPLUS_MSG "A message from the deep dark world of C++, where anything is possible."
  17. static char *global_str = NULL;
  18. const int UINT_DIGITS = 10; // max unsigned int is 4294967295
  19. bool check(const char *const str, unsigned int number) {
  20. static char expected[256];
  21. sprintf(expected, "%s%d", OTHERLAND_MSG, number);
  22. bool matches = (strcmp(str, expected) == 0);
  23. if (!matches) printf("Failed: [%s][%s]\n", str, expected);
  24. return matches;
  25. }
  26. %}
  27. %immutable global_const_char;
  28. %inline %{
  29. // get functions
  30. char *GetCharHeapString() {
  31. global_str = new char[sizeof(CPLUSPLUS_MSG)+1];
  32. strcpy(global_str, CPLUSPLUS_MSG);
  33. return global_str;
  34. }
  35. const char *GetConstCharProgramCodeString() {
  36. return CPLUSPLUS_MSG;
  37. }
  38. void DeleteCharHeapString() {
  39. delete[] global_str;
  40. global_str = NULL;
  41. }
  42. char *GetCharStaticString() {
  43. static char str[sizeof(CPLUSPLUS_MSG)+1];
  44. strcpy(str, CPLUSPLUS_MSG);
  45. return str;
  46. }
  47. char *GetCharStaticStringFixed() {
  48. static char str[] = CPLUSPLUS_MSG;
  49. return str;
  50. }
  51. const char *GetConstCharStaticStringFixed() {
  52. static const char str[] = CPLUSPLUS_MSG;
  53. return str;
  54. }
  55. // set functions
  56. bool SetCharHeapString(char *str, unsigned int number) {
  57. delete[] global_str;
  58. global_str = new char[strlen(str)+UINT_DIGITS+1];
  59. strcpy(global_str, str);
  60. return check(global_str, number);
  61. }
  62. bool SetCharStaticString(char *str, unsigned int number) {
  63. static char static_str[] = CPLUSPLUS_MSG;
  64. strcpy(static_str, str);
  65. return check(static_str, number);
  66. }
  67. bool SetCharArrayStaticString(char str[], unsigned int number) {
  68. static char static_str[] = CPLUSPLUS_MSG;
  69. strcpy(static_str, str);
  70. return check(static_str, number);
  71. }
  72. bool SetConstCharHeapString(const char *str, unsigned int number) {
  73. delete[] global_str;
  74. global_str = new char[strlen(str)+UINT_DIGITS+1];
  75. strcpy(global_str, str);
  76. return check(global_str, number);
  77. }
  78. bool SetConstCharStaticString(const char *str, unsigned int number) {
  79. static char static_str[] = CPLUSPLUS_MSG;
  80. strcpy(static_str, str);
  81. return check(static_str, number);
  82. }
  83. bool SetConstCharArrayStaticString(const 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. bool SetCharConstStaticString(char *const str, unsigned int number) {
  89. static char static_str[] = CPLUSPLUS_MSG;
  90. strcpy(static_str, str);
  91. return check(static_str, number);
  92. }
  93. bool SetConstCharConstStaticString(const char *const str, unsigned int number) {
  94. static char static_str[] = CPLUSPLUS_MSG;
  95. strcpy(static_str, str);
  96. return check(static_str, number);
  97. }
  98. // get set function
  99. char *CharPingPong(char *str) {
  100. return str;
  101. }
  102. char *CharArrayPingPong(char abcstr[]) {
  103. return abcstr;
  104. }
  105. char *CharArrayDimsPingPong(char abcstr[16]) {
  106. return abcstr;
  107. }
  108. // variables
  109. char *global_char = NULL;
  110. char global_char_array1[] = CPLUSPLUS_MSG;
  111. char global_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
  112. const char *global_const_char = CPLUSPLUS_MSG;
  113. const char global_const_char_array1[] = CPLUSPLUS_MSG;
  114. const char global_const_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
  115. %}
  116. %typemap(newfree) char *GetNewCharString() { /* hello */ delete[] $1; }
  117. %newobject GetNewCharString();
  118. %inline {
  119. char *GetNewCharString() {
  120. char *nstr = new char[sizeof(CPLUSPLUS_MSG)+1];
  121. strcpy(nstr, CPLUSPLUS_MSG);
  122. return nstr;
  123. }
  124. }
  125. %inline {
  126. struct Formatpos;
  127. struct OBFormat;
  128. static int GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat) {
  129. return 0;
  130. }
  131. }
  132. %inline %{
  133. // char *& tests
  134. char *&GetCharPointerRef() {
  135. static char str[] = CPLUSPLUS_MSG;
  136. static char *ptr = str;
  137. return ptr;
  138. }
  139. bool SetCharPointerRef(char *&str, unsigned int number) {
  140. static char static_str[] = CPLUSPLUS_MSG;
  141. strcpy(static_str, str);
  142. return check(static_str, number);
  143. }
  144. const char *&GetConstCharPointerRef() {
  145. static const char str[] = CPLUSPLUS_MSG;
  146. static const char *ptr = str;
  147. return ptr;
  148. }
  149. bool SetConstCharPointerRef(const char *&str, unsigned int number) {
  150. static char static_str[] = CPLUSPLUS_MSG;
  151. strcpy(static_str, str);
  152. return check(static_str, number);
  153. }
  154. %}