09-util-topic-tokenise.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mosquitto.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. bool match;
  22. if(mosquitto_sub_topic_tokenise("topic", &topics, &topic_count)){
  23. printf("Out of memory.\n");
  24. return 1;
  25. }
  26. if(topic_count != 1 || strcmp(topics[0], "topic")){
  27. print_error("topic", topics, topic_count);
  28. return 1;
  29. }
  30. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  31. if(mosquitto_sub_topic_tokenise("a/deep/topic/hierarchy", &topics, &topic_count)){
  32. printf("Out of memory.\n");
  33. return 1;
  34. }
  35. if(topic_count != 4
  36. || strcmp(topics[0], "a")
  37. || strcmp(topics[1], "deep")
  38. || strcmp(topics[2], "topic")
  39. || strcmp(topics[3], "hierarchy")){
  40. print_error("a/deep/topic/hierarchy", topics, topic_count);
  41. return 1;
  42. }
  43. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  44. if(mosquitto_sub_topic_tokenise("/a/deep/topic/hierarchy", &topics, &topic_count)){
  45. printf("Out of memory.\n");
  46. return 1;
  47. }
  48. if(topic_count != 5
  49. || topics[0]
  50. || strcmp(topics[1], "a")
  51. || strcmp(topics[2], "deep")
  52. || strcmp(topics[3], "topic")
  53. || strcmp(topics[4], "hierarchy")){
  54. print_error("/a/deep/topic/hierarchy", topics, topic_count);
  55. return 1;
  56. }
  57. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  58. if(mosquitto_sub_topic_tokenise("a/b/c", &topics, &topic_count)){
  59. printf("Out of memory.\n");
  60. return 1;
  61. }
  62. if(topic_count != 3
  63. || strcmp(topics[0], "a")
  64. || strcmp(topics[1], "b")
  65. || strcmp(topics[2], "c")){
  66. print_error("a/b/c", topics, topic_count);
  67. return 1;
  68. }
  69. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  70. if(mosquitto_sub_topic_tokenise("/a/b/c", &topics, &topic_count)){
  71. printf("Out of memory.\n");
  72. return 1;
  73. }
  74. if(topic_count != 4
  75. || topics[0]
  76. || strcmp(topics[1], "a")
  77. || strcmp(topics[2], "b")
  78. || strcmp(topics[3], "c")){
  79. print_error("/a/b/c", topics, topic_count);
  80. return 1;
  81. }
  82. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  83. if(mosquitto_sub_topic_tokenise("a///hierarchy", &topics, &topic_count)){
  84. printf("Out of memory.\n");
  85. return 1;
  86. }
  87. if(topic_count != 4
  88. || strcmp(topics[0], "a")
  89. || topics[1]
  90. || topics[2]
  91. || strcmp(topics[3], "hierarchy")){
  92. print_error("a///hierarchy", topics, topic_count);
  93. return 1;
  94. }
  95. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  96. if(mosquitto_sub_topic_tokenise("/a///hierarchy", &topics, &topic_count)){
  97. printf("Out of memory.\n");
  98. return 1;
  99. }
  100. if(topic_count != 5
  101. || topics[0]
  102. || strcmp(topics[1], "a")
  103. || topics[2]
  104. || topics[3]
  105. || strcmp(topics[4], "hierarchy")){
  106. print_error("/a///hierarchy", topics, topic_count);
  107. return 1;
  108. }
  109. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  110. if(mosquitto_sub_topic_tokenise("/a///hierarchy/", &topics, &topic_count)){
  111. printf("Out of memory.\n");
  112. return 1;
  113. }
  114. if(topic_count != 6
  115. || topics[0]
  116. || strcmp(topics[1], "a")
  117. || topics[2]
  118. || topics[3]
  119. || strcmp(topics[4], "hierarchy")
  120. || topics[3]){
  121. print_error("/a///hierarchy/", topics, topic_count);
  122. return 1;
  123. }
  124. mosquitto_sub_topic_tokens_free(&topics, topic_count);
  125. return 0;
  126. }