09-util-topic-tokenise.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <mosquittopp.h>
  4. void print_error(const char *topic, char **topics, int topic_count)
  5. {
  6. int i;
  7. printf("TOPIC: %s\n", topic);
  8. printf("TOKENS: ");
  9. for(i=0; i<topic_count; i++){
  10. printf("%s", topics[i]);
  11. if(i+1<topic_count){
  12. printf("/");
  13. }
  14. }
  15. printf("\n");
  16. }
  17. int main(int argc, char *argv[])
  18. {
  19. char **topics;
  20. int topic_count;
  21. if(mosqpp::sub_topic_tokenise("topic", &topics, &topic_count)){
  22. printf("Out of memory.\n");
  23. return 1;
  24. }
  25. if(topic_count != 1 || strcmp(topics[0], "topic")){
  26. print_error("topic", topics, topic_count);
  27. return 1;
  28. }
  29. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  30. if(mosqpp::sub_topic_tokenise("a/deep/topic/hierarchy", &topics, &topic_count)){
  31. printf("Out of memory.\n");
  32. return 1;
  33. }
  34. if(topic_count != 4
  35. || strcmp(topics[0], "a")
  36. || strcmp(topics[1], "deep")
  37. || strcmp(topics[2], "topic")
  38. || strcmp(topics[3], "hierarchy")){
  39. print_error("a/deep/topic/hierarchy", topics, topic_count);
  40. return 1;
  41. }
  42. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  43. if(mosqpp::sub_topic_tokenise("/a/deep/topic/hierarchy", &topics, &topic_count)){
  44. printf("Out of memory.\n");
  45. return 1;
  46. }
  47. if(topic_count != 5
  48. || topics[0]
  49. || strcmp(topics[1], "a")
  50. || strcmp(topics[2], "deep")
  51. || strcmp(topics[3], "topic")
  52. || strcmp(topics[4], "hierarchy")){
  53. print_error("/a/deep/topic/hierarchy", topics, topic_count);
  54. return 1;
  55. }
  56. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  57. if(mosqpp::sub_topic_tokenise("a/b/c", &topics, &topic_count)){
  58. printf("Out of memory.\n");
  59. return 1;
  60. }
  61. if(topic_count != 3
  62. || strcmp(topics[0], "a")
  63. || strcmp(topics[1], "b")
  64. || strcmp(topics[2], "c")){
  65. print_error("a/b/c", topics, topic_count);
  66. return 1;
  67. }
  68. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  69. if(mosqpp::sub_topic_tokenise("/a/b/c", &topics, &topic_count)){
  70. printf("Out of memory.\n");
  71. return 1;
  72. }
  73. if(topic_count != 4
  74. || topics[0]
  75. || strcmp(topics[1], "a")
  76. || strcmp(topics[2], "b")
  77. || strcmp(topics[3], "c")){
  78. print_error("/a/b/c", topics, topic_count);
  79. return 1;
  80. }
  81. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  82. if(mosqpp::sub_topic_tokenise("a///hierarchy", &topics, &topic_count)){
  83. printf("Out of memory.\n");
  84. return 1;
  85. }
  86. if(topic_count != 4
  87. || strcmp(topics[0], "a")
  88. || topics[1]
  89. || topics[2]
  90. || strcmp(topics[3], "hierarchy")){
  91. print_error("a///hierarchy", topics, topic_count);
  92. return 1;
  93. }
  94. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  95. if(mosqpp::sub_topic_tokenise("/a///hierarchy", &topics, &topic_count)){
  96. printf("Out of memory.\n");
  97. return 1;
  98. }
  99. if(topic_count != 5
  100. || topics[0]
  101. || strcmp(topics[1], "a")
  102. || topics[2]
  103. || topics[3]
  104. || strcmp(topics[4], "hierarchy")){
  105. print_error("/a///hierarchy", topics, topic_count);
  106. return 1;
  107. }
  108. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  109. if(mosqpp::sub_topic_tokenise("/a///hierarchy/", &topics, &topic_count)){
  110. printf("Out of memory.\n");
  111. return 1;
  112. }
  113. if(topic_count != 6
  114. || topics[0]
  115. || strcmp(topics[1], "a")
  116. || topics[2]
  117. || topics[3]
  118. || strcmp(topics[4], "hierarchy")
  119. || topics[3]){
  120. print_error("/a///hierarchy/", topics, topic_count);
  121. return 1;
  122. }
  123. mosqpp::sub_topic_tokens_free(&topics, topic_count);
  124. return 0;
  125. }