examples_dstr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * File : examples_dstr.c
  3. * This file is the example code of dstr(dynamic string) package
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-06-07 never the first version
  23. * 2018-07-25 never add sample API
  24. * 2018-08-30 never add sample API
  25. */
  26. #include <rtthread.h>
  27. #include <finsh.h>
  28. #include "dstr.h"
  29. #include <dfs_posix.h>
  30. void rt_dstr_print(rt_dstr_t *thiz)
  31. {
  32. if (thiz == NULL)
  33. return;
  34. printf("%s\n", thiz->str);
  35. }
  36. void dstr_test_new(void)
  37. {
  38. const char *str = "new dstr";
  39. rt_dstr_t *p = NULL;
  40. printf("\n");
  41. printf("sample of new:\n");
  42. p = rt_dstr_new(str);
  43. rt_dstr_print(p);
  44. rt_dstr_del(p);
  45. printf("\n");
  46. }
  47. void dstr_test_cat(void)
  48. {
  49. rt_dstr_t *p = NULL;
  50. const char *str = "cat ";
  51. printf("sample of cat:\n");
  52. p = rt_dstr_new(str);
  53. rt_dstr_cat(p, "sample");
  54. rt_dstr_print(p);
  55. rt_dstr_del(p);
  56. printf("\n");
  57. }
  58. void dstr_test_ncat(void)
  59. {
  60. rt_dstr_t *p = NULL;
  61. const char *str = "ncat ";
  62. printf("sample of ncat:\n");
  63. p = rt_dstr_new(str);
  64. rt_dstr_ncat(p, "samplexxxxxx", 6);
  65. rt_dstr_print(p);
  66. rt_dstr_del(p);
  67. printf("\n");
  68. }
  69. void dstr_test_precat(void)
  70. {
  71. rt_dstr_t *p = NULL;
  72. printf("sample of precat:\n");
  73. p = rt_dstr_precat(NULL, "456");
  74. rt_dstr_precat(p, "123");
  75. rt_dstr_precat(p, "pre sample: ");
  76. rt_dstr_print(p);
  77. rt_dstr_del(p);
  78. }
  79. void dstr_test_cat_dstr(void)
  80. {
  81. rt_dstr_t *p1 = NULL, *p2 = NULL;
  82. printf("\n");
  83. printf("sample of cat_dstr:\n");
  84. p1 = rt_dstr_new("dstr1");
  85. p2 = rt_dstr_new("dstr2");
  86. p1 = rt_dstr_cat_dstr(p1, p2);
  87. rt_dstr_print(p1);
  88. rt_dstr_print(p2);
  89. rt_dstr_del(p1);
  90. rt_dstr_del(p2);
  91. }
  92. void dstr_test_precat_dstr(void)
  93. {
  94. rt_dstr_t *p1 = NULL, *p2 = NULL;
  95. printf("\n");
  96. printf("sample of precat_dstr:\n");
  97. p1 = rt_dstr_new("dstr1");
  98. p2 = rt_dstr_new("dstr2");
  99. p1 = rt_dstr_precat_dstr(p1, p2);
  100. rt_dstr_print(p1);
  101. rt_dstr_print(p2);
  102. rt_dstr_del(p1);
  103. rt_dstr_del(p2);
  104. }
  105. void dstr_test_cmp(void)
  106. {
  107. rt_dstr_t *p1 = NULL;
  108. rt_dstr_t *p2 = NULL;
  109. int res = 0;
  110. printf("\n");
  111. printf("sample of cmp:\n");
  112. p1 = rt_dstr_new("helle");
  113. p2 = rt_dstr_new("hellc");
  114. res = rt_dstr_cmp(p1, p2);
  115. printf("cmp result: %d\n", res);
  116. rt_dstr_del(p1);
  117. rt_dstr_del(p2);
  118. /* one of them is NULL */
  119. p1 = rt_dstr_new("abc");
  120. res = rt_dstr_cmp(p1, NULL);
  121. printf("s2:NULL result: %d\n", res);
  122. rt_dstr_del(p1);
  123. p1 = rt_dstr_new("efg");
  124. res = rt_dstr_cmp(NULL, p1);
  125. printf("s1:NULL result: %d\n", res);
  126. rt_dstr_del(p1);
  127. res = rt_dstr_cmp(NULL, NULL);
  128. printf("two NULL result: %d\n\n", res);
  129. }
  130. void dstr_test_ncmp(void)
  131. {
  132. rt_dstr_t *p1 = NULL;
  133. rt_dstr_t *p2 = NULL;
  134. int res = 0;
  135. printf("sample of ncmp:\n");
  136. p1 = rt_dstr_new("hello");
  137. p2 = rt_dstr_new("hella");
  138. res = rt_dstr_ncmp(p1, p2, 5);
  139. printf("ncmp result: %d\n", res);
  140. rt_dstr_del(p1);
  141. rt_dstr_del(p2);
  142. /* one of them is NULL */
  143. p1 = rt_dstr_new("abc");
  144. res = rt_dstr_ncmp(p1, NULL, 2);
  145. printf("s2:NULL ncmp result: %d\n", res);
  146. rt_dstr_del(p1);
  147. p1 = rt_dstr_new("efg");
  148. res = rt_dstr_ncmp(NULL, p1, 3);
  149. printf("s1:NULL ncmp result: %d\n", res);
  150. rt_dstr_del(p1);
  151. res = rt_dstr_ncmp(NULL, NULL, 4);
  152. printf("two NULL ncmp result: %d\n\n", res);
  153. }
  154. void dstr_test_casecmp(void)
  155. {
  156. rt_dstr_t *p1 = NULL;
  157. rt_dstr_t *p2 = NULL;
  158. int res = 0;
  159. printf("sample of casecmp:\n");
  160. p1 = rt_dstr_new("hello");
  161. p2 = rt_dstr_new("HELLO");
  162. res = rt_dstr_casecmp(p1, p2);
  163. printf("casecmp result: %d\n", res);
  164. rt_dstr_del(p1);
  165. rt_dstr_del(p2);
  166. /* one of them is NULL */
  167. p1 = rt_dstr_new("abc");
  168. res = rt_dstr_casecmp(p1, NULL);
  169. printf("s2:NULL casecmp result: %d\n", res);
  170. rt_dstr_del(p1);
  171. p1 = rt_dstr_new("efg");
  172. res = rt_dstr_casecmp(NULL, p1);
  173. printf("s1:NULL casecmp result: %d\n", res);
  174. rt_dstr_del(p1);
  175. res = rt_dstr_casecmp(NULL, NULL);
  176. printf("two NULL casecmp result: %d\n\n", res);
  177. }
  178. void dstr_test_strlen(void)
  179. {
  180. rt_dstr_t *p1 = NULL;
  181. int res = 0;
  182. printf("sample of strlen:\n");
  183. p1 = rt_dstr_new("hello strlen");
  184. res = rt_dstr_strlen(p1);
  185. if (res == -1)
  186. return;
  187. printf("length: %d\n", res);
  188. rt_dstr_del(p1);
  189. printf("\n");
  190. }
  191. void dstr_test_sprintf(void)
  192. {
  193. const char *src = "sprintf";
  194. rt_dstr_t *p1 = NULL;
  195. rt_dstr_t *p2 = NULL;
  196. printf("sample of sprintf:\n");
  197. p1 = rt_dstr_new("test");
  198. rt_dstr_sprintf(p1, "%s", src);
  199. rt_dstr_print(p1);
  200. rt_dstr_del(p1);
  201. p2 = rt_dstr_new("");
  202. rt_dstr_sprintf(p2, "%08x", 0x20180604);
  203. rt_dstr_print(p2);
  204. rt_dstr_del(p2);
  205. printf("\n");
  206. }
  207. static rt_dstr_t *path_cat(const char *path, const char *filename)
  208. {
  209. rt_dstr_t *p = RT_NULL;
  210. p = rt_dstr_sprintf(p, "%s/%s", path, filename);
  211. return p;
  212. }
  213. static rt_dstr_t *header_info_cat(char *send_buffer)
  214. {
  215. rt_dstr_t *p = RT_NULL;
  216. const char *key = "header-key";
  217. /* build header for upload */
  218. p = rt_dstr_append_printf(p, "api-key: %s\r\n", key);
  219. rt_dstr_append_printf(p, "Content-Length: %d\r\n", strlen(send_buffer));
  220. rt_dstr_cat(p, "Content-Type: application/octet-stream\r\n");
  221. return p;
  222. }
  223. void dstr_test_append(void)
  224. {
  225. char *buffer = "test of header";
  226. rt_dstr_t *p = NULL;
  227. printf("sample of append:\n");
  228. p = path_cat("/home", "bsp/thread.c");
  229. rt_dstr_print(p);
  230. rt_dstr_del(p);
  231. printf("\n");
  232. p = header_info_cat(buffer);
  233. rt_dstr_print(p);
  234. rt_dstr_del(p);
  235. }
  236. void dstr_test(void)
  237. {
  238. dstr_test_new();
  239. dstr_test_cat();
  240. dstr_test_ncat();
  241. dstr_test_precat();
  242. dstr_test_cat_dstr();
  243. dstr_test_precat_dstr();
  244. dstr_test_cmp();
  245. dstr_test_ncmp();
  246. dstr_test_casecmp();
  247. dstr_test_strlen();
  248. dstr_test_sprintf();
  249. dstr_test_append();
  250. }
  251. MSH_CMD_EXPORT(dstr_test, dstr example);