PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/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. /*
  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(462) global_char_array1; // Unable to set variable of type char[]
  8. %{
  9. #define OTHERLAND_MSG "Little message from the the safe world."
  10. #define CPLUSPLUS_MSG "A message from the deep dark world of C++, where anything is possible."
  11. static char *global_str = NULL;
  12. const int UINT_DIGITS = 10; // max unsigned int is 4294967295
  13. bool check(const char *str, unsigned int number) {
  14. static char expected[256];
  15. sprintf(expected, "%s%d", OTHERLAND_MSG, number);
  16. bool matches = (strcmp(str, expected) == 0);
  17. if (!matches) printf("Failed: [%s][%s]\n", str, expected);
  18. return matches;
  19. }
  20. %}
  21. %immutable global_const_char;
  22. %inline %{
  23. // get functions
  24. char *GetCharHeapString() {
  25. global_str = new char[sizeof(CPLUSPLUS_MSG)+1];
  26. strcpy(global_str, CPLUSPLUS_MSG);
  27. return global_str;
  28. }
  29. const char *GetConstCharProgramCodeString() {
  30. return CPLUSPLUS_MSG;
  31. }
  32. void DeleteCharHeapString() {
  33. delete[] global_str;
  34. global_str = NULL;
  35. }
  36. char *GetCharStaticString() {
  37. static char str[sizeof(CPLUSPLUS_MSG)+1];
  38. strcpy(str, CPLUSPLUS_MSG);
  39. return str;
  40. }
  41. char *GetCharStaticStringFixed() {
  42. static char str[] = CPLUSPLUS_MSG;
  43. return str;
  44. }
  45. const char *GetConstCharStaticStringFixed() {
  46. static const char str[] = CPLUSPLUS_MSG;
  47. return str;
  48. }
  49. // set functions
  50. bool SetCharHeapString(char *str, unsigned int number) {
  51. delete[] global_str;
  52. global_str = new char[strlen(str)+UINT_DIGITS+1];
  53. strcpy(global_str, str);
  54. return check(global_str, number);
  55. }
  56. bool SetCharStaticString(char *str, unsigned int number) {
  57. static char static_str[] = CPLUSPLUS_MSG;
  58. strcpy(static_str, str);
  59. return check(static_str, number);
  60. }
  61. bool SetCharArrayStaticString(char str[], unsigned int number) {
  62. static char static_str[] = CPLUSPLUS_MSG;
  63. strcpy(static_str, str);
  64. return check(static_str, number);
  65. }
  66. bool SetConstCharHeapString(const char *str, unsigned int number) {
  67. delete[] global_str;
  68. global_str = new char[strlen(str)+UINT_DIGITS+1];
  69. strcpy(global_str, str);
  70. return check(global_str, number);
  71. }
  72. bool SetConstCharStaticString(const char *str, unsigned int number) {
  73. static char static_str[] = CPLUSPLUS_MSG;
  74. strcpy(static_str, str);
  75. return check(static_str, number);
  76. }
  77. bool SetConstCharArrayStaticString(const 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. // get set function
  83. char *CharPingPong(char *str) {
  84. return str;
  85. }
  86. // variables
  87. char *global_char = NULL;
  88. char global_char_array1[] = CPLUSPLUS_MSG;
  89. char global_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
  90. const char *global_const_char = CPLUSPLUS_MSG;
  91. const char global_const_char_array1[] = CPLUSPLUS_MSG;
  92. const char global_const_char_array2[sizeof(CPLUSPLUS_MSG)+1] = CPLUSPLUS_MSG;
  93. %}